/**
  * Tests the Joomla\Form\Field::__construct method
  *
  * @covers Joomla\Form\Field::__construct
  *
  * @return void
  */
 public function testConstruct()
 {
     $form = new Form('form1');
     $this->assertThat($form->load(JFormDataHelper::$loadFieldDocument), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new JFormFieldInspector($form);
     $this->assertThat($field instanceof \Joomla\Form\Field, $this->isTrue(), 'Line:' . __LINE__ . ' The JFormField constuctor should return a JFormField object.');
     $this->assertThat($field->getForm(), $this->identicalTo($form), 'Line:' . __LINE__ . ' The internal form should be identical to the variable passed in the contructor.');
     // Add custom path.
     FormHelper::addFieldPath(__DIR__ . '/_testfields');
     FormHelper::loadFieldType('foo.bar');
     $field = new \Foo\Form\Field_Bar($form);
     $this->assertEquals($field->type, 'Foo\\Field_Bar', 'Line:' . __LINE__ . ' The field type should have been guessed by the constructor.');
     FormHelper::loadFieldType('foo');
     $field = new \Joomla\Form\Field_Foo($form);
     $this->assertEquals($field->type, 'Joomla\\Field_Foo', 'Line:' . __LINE__ . ' The field type should have been guessed by the constructor.');
     FormHelper::loadFieldType('modal_foo');
     $field = new \Joomla\Form\Field_Modal_Foo($form);
     $this->assertEquals($field->type, 'Joomla\\Field_Modal_Foo', 'Line:' . __LINE__ . ' The field type should have been guessed by the constructor.');
 }
 /**
  * Test for Form::loadRuleType method.
  *
  * @return void
  */
 public function testLoadRuleType()
 {
     // Test error handling.
     $this->assertThat(FormHelper::loadRuleType('bogus'), $this->isFalse(), 'Line:' . __LINE__ . ' Loading an unknown rule should return false.');
     // Test loading a custom rule.
     FormHelper::addRulePath(__DIR__ . '/_testrules');
     $this->assertThat(FormHelper::loadRuleType('custom') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading a known rule should return a rule object.');
     // Test all the stock rules load.
     $this->assertThat(FormHelper::loadRuleType('boolean') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the boolean rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('email') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the email rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('equals') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the equals rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('options') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the options rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('color') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the color rule should return a rule object.');
     $this->assertThat(FormHelper::loadRuleType('tel') instanceof Rule, $this->isTrue(), 'Line:' . __LINE__ . ' Loading the tel rule should return a rule object.');
 }
Exemplo n.º 3
0
 /**
  * Test for Form::syncPaths method.
  *
  * @return void
  */
 public function testSyncPaths()
 {
     $form = new JFormInspector('testSyncPaths');
     $this->assertThat($form->load(JFormDataHelper::$syncPathsDocument), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $fieldPaths = FormHelper::addFieldPath();
     $formPaths = FormHelper::addFormPath();
     $rulePaths = FormHelper::addRulePath();
     $this->assertThat(in_array(JPATH_ROOT . '/field1', $fieldPaths), $this->isTrue(), 'Line:' . __LINE__ . ' The field path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/field2', $fieldPaths), $this->isTrue(), 'Line:' . __LINE__ . ' The field path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/field3', $fieldPaths), $this->isTrue(), 'Line:' . __LINE__ . ' The field path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/form1', $formPaths), $this->isTrue(), 'Line:' . __LINE__ . ' The form path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/form2', $formPaths), $this->isTrue(), 'Line:' . __LINE__ . ' The form path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/form3', $formPaths), $this->isTrue(), 'Line:' . __LINE__ . ' The form path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/rule1', $rulePaths), $this->isTrue(), 'Line:' . __LINE__ . ' The rule path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/rule2', $rulePaths), $this->isTrue(), 'Line:' . __LINE__ . ' The rule path from the XML file should be present.');
     $this->assertThat(in_array(JPATH_ROOT . '/rule3', $rulePaths), $this->isTrue(), 'Line:' . __LINE__ . ' The rule path from the XML file should be present.');
 }
Exemplo n.º 4
0
 /**
  * Method to validate a Field object based on field data.
  *
  * @param   \SimpleXMLElement  $element  The XML element object representation of the form field.
  * @param   string             $group    The optional dot-separated form group path on which to find the field.
  * @param   mixed              $value    The optional value to use as the default for the field.
  * @param   Registry           $input    An optional Registry object with the entire data set to validate
  *                                       against the entire form.
  *
  * @return  mixed  Boolean true if field value is valid, Exception on failure.
  *
  * @since   1.0
  * @throws  \InvalidArgumentException
  * @throws  \UnexpectedValueException
  */
 protected function validateField(\SimpleXMLElement $element, $group = null, $value = null, Registry $input = null)
 {
     $valid = true;
     // Check if the field is required.
     $required = (string) $element['required'] == 'true' || (string) $element['required'] == 'required';
     if ($required) {
         // If the field is required and the value is empty return an error message.
         if ($value === '' || $value === null) {
             if ($element['label']) {
                 $message = Text::_($element['label']);
             } else {
                 $message = Text::_($element['name']);
             }
             $message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_REQUIRED', $message);
             return new \RuntimeException($message);
         }
     }
     // Get the field validation rule.
     if ($type = (string) $element['validate']) {
         // Load the Rule object for the field.
         $rule = FormHelper::loadRuleType($type);
         // If the object could not be loaded return an error message.
         if ($rule === false) {
             throw new \UnexpectedValueException(sprintf('%s::validateField() rule `%s` missing.', get_class($this), $type));
         }
         // Run the field validation rule test.
         $valid = $rule->test($element, $value, $group, $input, $this);
         // Check for an error in the validation test.
         if ($valid instanceof \Exception) {
             return $valid;
         }
     }
     // Check if the field is valid.
     if ($valid === false) {
         // Does the field have a defined error message?
         $message = (string) $element['message'];
         if ($message) {
             $message = Text::_($element['message']);
             return new \UnexpectedValueException($message);
         } else {
             $message = Text::_($element['label']);
             $message = Text::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', $message);
             return new \UnexpectedValueException($message);
         }
     }
     return true;
 }