Пример #1
0
 function testValidateFields_valid()
 {
     stub($this->field1)->isValid()->returns(true);
     stub($this->field1)->userCanSubmit()->returns(true);
     stub($this->field2)->isValid('*', '123')->returns(true);
     stub($this->field2)->isValid('*', '456')->returns(false);
     stub($this->field2)->userCanSubmit()->returns(true);
     stub($this->field3)->isValid()->returns(true);
     stub($this->field3)->userCanSubmit()->returns(true);
     stub($this->workflow)->validate()->returns(true);
     $fields_data = array(102 => '123');
     $this->assertTrue($this->initial_changeset_fields_validator->validate($this->artifact, $fields_data));
     $this->assertFalse(isset($fields_data[101]));
     $this->assertFalse(isset($fields_data[103]));
     $fields_data = array(102 => '456');
     $this->assertFalse($this->initial_changeset_fields_validator->validate($this->artifact, $fields_data));
     $this->assertFalse(isset($fields_data[101]));
     $this->assertFalse(isset($fields_data[103]));
 }
Пример #2
0
 /**
  * Check if CSV file contains unknown fields
  *
  * @param Array $lines, the CSV file lines
  *
  * @return bool true if has unknown fields, false otherwise
  */
 public function hasError($header_line, $lines)
 {
     $has_error = false;
     $fef = $this->getFormElementFactory();
     $aid_key = array_search('aid', $header_line);
     $af = $this->getTrackerArtifactFactory();
     $artifact = null;
     $hp = Codendi_HTMLPurifier::instance();
     foreach ($lines as $cpt_line => $line) {
         $data = array();
         foreach ($header_line as $idx => $field_name) {
             //Fields other than aid
             if ($field_name != 'aid') {
                 $field = $fef->getUsedFieldByName($this->getId(), $field_name);
                 if (!$field) {
                     // a field is unknown
                     $GLOBALS['Response']->addFeedback('error', $GLOBALS['Language']->getText('plugin_tracker_admin_import', 'unknown_field', array($field_name)));
                     $has_error = true;
                 } else {
                     //check if value is ok
                     if ($aid_key !== false && $this->aidExists($line[$aid_key])) {
                         $artifact_id = $line[$aid_key];
                     } else {
                         $artifact_id = 0;
                     }
                     $artifact = $af->getInstanceFromRow(array('id' => $artifact_id, 'tracker_id' => $this->id, 'submitted_by' => $this->getUserManager()->getCurrentuser()->getId(), 'submitted_on' => $_SERVER['REQUEST_TIME'], 'use_artifact_permissions' => 0));
                     if ($line[$idx] != '') {
                         $data[$field->getId()] = $field->getFieldDataFromCSVValue($line[$idx]);
                         if ($data[$field->getId()] === null) {
                             $GLOBALS['Response']->addFeedback('error', $GLOBALS['Language']->getText('plugin_tracker_admin_import', 'unknown_value', array($line[$idx], $field_name)));
                             $has_error = true;
                         }
                     }
                 }
             } else {
                 //Field is aid : we check if the artifact id exists
                 if ($this->hasUnknownAid($header_line, $lines)) {
                     $has_error = true;
                 }
             }
         }
         if ($artifact) {
             $is_new_artifact = $artifact->getId() == 0;
             if ($is_new_artifact) {
                 $fields_validator = new Tracker_Artifact_Changeset_InitialChangesetFieldsValidator($this->getFormElementFactory());
             } else {
                 $fields_validator = new Tracker_Artifact_Changeset_NewChangesetFieldsValidator($this->getFormElementFactory());
             }
             if (!$fields_validator->validate($artifact, $data)) {
                 $has_error = true;
             }
         }
     }
     return $has_error;
 }