/** * 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.'); }
/** * 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.'); }
/** * Method to synchronize any field, form or rule paths contained in the XML document. * * @return boolean True on success. * * @since 1.0 * @todo Maybe we should receive all addXXXpaths attributes at once? */ protected function syncPaths() { // Make sure there is a valid Form XML document. if (!$this->xml instanceof \SimpleXMLElement) { return false; } // Get any addfieldpath attributes from the form definition. $paths = $this->xml->xpath('//*[@addfieldpath]/@addfieldpath'); $paths = array_map('strval', $paths ? $paths : array()); // Add the field paths. foreach ($paths as $path) { $path = JPATH_ROOT . '/' . ltrim($path, '/\\'); FormHelper::addFieldPath($path); } // Get any addformpath attributes from the form definition. $paths = $this->xml->xpath('//*[@addformpath]/@addformpath'); $paths = array_map('strval', $paths ? $paths : array()); // Add the form paths. foreach ($paths as $path) { $path = JPATH_ROOT . '/' . ltrim($path, '/\\'); FormHelper::addFormPath($path); } // Get any addrulepath attributes from the form definition. $paths = $this->xml->xpath('//*[@addrulepath]/@addrulepath'); $paths = array_map('strval', $paths ? $paths : array()); // Add the rule paths. foreach ($paths as $path) { $path = JPATH_ROOT . '/' . ltrim($path, '/\\'); FormHelper::addRulePath($path); } return true; }