/**
  * 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 the Form::loadFieldType method.
  *
  * @return void
  */
 public function testLoadFieldType()
 {
     $this->assertThat(FormHelper::loadFieldType('bogus'), $this->isFalse(), 'Line:' . __LINE__ . ' loadFieldType should return false if class not found.');
     $this->assertThat(FormHelper::loadFieldType('list') instanceof \Joomla\Form\Field_List, $this->isTrue(), 'Line:' . __LINE__ . ' loadFieldType should return the correct class.');
     // Add custom path.
     FormHelper::addFieldPath(__DIR__ . '/_testfields');
     include_once '_testfields/test.php';
     $this->assertThat(FormHelper::loadFieldType('test') instanceof \Joomla\Form\Field_Test, $this->isTrue(), 'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.');
     include_once '_testfields/bar.php';
     $this->assertThat(FormHelper::loadFieldType('foo.bar') instanceof \Foo\Form\Field_Bar, $this->isTrue(), 'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.');
     include_once '_testfields/modal/foo.php';
     $this->assertThat(FormHelper::loadFieldType('modal_foo') instanceof \Joomla\Form\Field_Modal_Foo, $this->isTrue(), 'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.');
     include_once '_testfields/modal/bar.php';
     $this->assertThat(FormHelper::loadFieldType('foo.modal_bar') instanceof \Foo\Form\Field_Modal_Bar, $this->isTrue(), 'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.');
 }
예제 #3
0
 /**
  * Method to load, setup and return a FormField object based on field data.
  *
  * @param   string  $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.
  *
  * @return  mixed  The FormField object for the field or boolean false on error.
  *
  * @since   1.0
  */
 protected function loadField($element, $group = null, $value = null)
 {
     // Make sure there is a valid SimpleXMLElement.
     if (!$element instanceof \SimpleXMLElement) {
         return false;
     }
     // Get the field type.
     $type = $element['type'] ? (string) $element['type'] : 'text';
     // Load the Field object for the field.
     $field = FormHelper::loadFieldType($type);
     // If the object could not be loaded, get a text field object.
     if ($field === false) {
         $field = FormHelper::loadFieldType('text');
     }
     /*
      * Get the value for the form field if not set.
      * Default to the translated version of the 'default' attribute
      * if 'translate_default' attribute if set to 'true' or '1'
      * else the value of the 'default' attribute for the field.
      */
     if ($value === null) {
         $default = (string) $element['default'];
         if (($translate = $element['translate_default']) && ((string) $translate == 'true' || (string) $translate == '1')) {
             $lang = Language::getInstance();
             if ($lang->hasKey($default)) {
                 $debug = $lang->setDebug(false);
                 $default = Text::_($default);
                 $lang->setDebug($debug);
             } else {
                 $default = Text::_($default);
             }
         }
         $value = $this->getValue((string) $element['name'], $group, $default);
     }
     // Setup the Field object.
     $field->setForm($this);
     if ($field->setup($element, $value, $group)) {
         return $field;
     } else {
         return false;
     }
 }