/**
  * Test the getOptions method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testGetOptions()
 {
     $form = new JFormInspector('form1');
     $this->assertThat($form->load('<form><field name="radio" type="radio"><option value="0">No</option><item value="1">Yes</item></field></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Radio($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat(strlen($field->input), $this->logicalNot($this->StringContains('Yes')), 'Line:' . __LINE__ . ' The field should not contain a Yes option.');
 }
 /**
  * Test the getInput method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testGetInput()
 {
     $form = new JFormInspector('form1');
     $this->assertThat($form->load('<form><field name="folderlist" type="folderlist" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_FolderList($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat(strlen($field->input), $this->greaterThan(0), 'Line:' . __LINE__ . ' The getInput method should return something without error.');
     // TODO: Should check all the attributes have come in properly.
 }
 /**
  * Test the getInput method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testGetInput()
 {
     $form = new JFormInspector('form1');
     $this->assertThat($form->load('<form><field name="databaseconnection" type="databaseconnection" supported="mysqli" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_DatabaseConnection($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat(strlen($field->input), $this->greaterThan(0), 'Line:' . __LINE__ . ' The getInput method should return something without error; in this case, a "Mysqli" option.');
     $this->assertThat($form->load('<form><field name="databaseconnection" type="databaseconnection" supported="non-existing" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_DatabaseConnection($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat(strlen($field->input), $this->greaterThan(0), 'Line:' . __LINE__ . ' The getInput method should return something without error; in this case, a "None" option.');
     // TODO: Should check all the attributes have come in properly.
 }
 /**
  * Test the getOptions method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testGetOptions()
 {
     $form = new JFormInspector('form1');
     $this->assertThat($form->load('<form><field name="integer" type="integer" first="1" last="-5" step="1"/></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Integer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat($field->input, $this->logicalNot($this->StringContains('<option')), 'Line:' . __LINE__ . ' The field should not contain any options.');
     $this->assertThat($form->load('<form><field name="integer" type="integer" first="-7" last="-5" step="1"/></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Integer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat($field->input, $this->StringContains('<option value="-7">-7</option>'), 'Line:' . __LINE__ . ' The field should contain -7 through -5 as options.');
     $this->assertThat($form->load('<form><field name="integer" type="integer" first="-7" last="-5" step="-1"/></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Integer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat($field->input, $this->logicalNot($this->StringContains('<option')), 'Line:' . __LINE__ . ' The field should not contain any options.');
     $this->assertThat($form->load('<form><field name="integer" type="integer" first="-5" last="-7" step="-1"/></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Integer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $this->assertThat($field->input, $this->StringContains('<option value="-7">-7</option>'), 'Line:' . __LINE__ . ' The field should contain -5 through -7 as options.');
 }
 /**
  * Test the getInput method.
  *
  * @return void
  */
 public function testGetInput()
 {
     $form = new JFormInspector('form1');
     // Test a traditional hidden field type.
     $this->assertThat($form->load('<form><field name="hidden" type="hidden" label="foo" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Hidden($form);
     $this->assertThat($form->getLabel('hidden'), $this->equalTo(''), 'Line:' . __LINE__ . ' The label of a hidden element should be nothing.');
     // Test a field with attribute hidden = true.
     $this->assertThat($form->load('<form><field name="hidden" type="text" label="foo" hidden="true" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Hidden($form);
     $this->assertThat($form->getLabel('hidden'), $this->equalTo(''), 'Line:' . __LINE__ . ' The label of a hidden element should be nothing.');
     // Test a field with attribute hidden = false.
     $this->assertThat($form->load('<form><field name="hidden" type="text" label="foo" hidden="false" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Hidden($form);
     $this->assertThat($form->getLabel('hidden'), $this->equalTo('<label id="hidden-lbl" for="hidden" class="">foo</label>'), 'Line:' . __LINE__ . ' The label of a non-hidden element should be some HTML.');
 }
 /**
  * Test the getLabel method.
  *
  * @return void
  */
 public function testGetLabel()
 {
     $form = new JFormInspector('form1');
     $this->assertThat($form->load('<form><field name="spacer" type="spacer" description="spacer" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Spacer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $equals = '<span class="spacer"><span class="before"></span><span class="">' . '<label id="spacer-lbl" class="hasTip" title="spacer::spacer">spacer</label></span>' . '<span class="after"></span></span>';
     $this->assertEquals($field->label, $equals, 'Line:' . __LINE__ . ' The getLabel method should return something without error.');
     $this->assertThat($form->load('<form><field name="spacer" type="spacer" class="text" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Spacer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $equals = '<span class="spacer"><span class="before"></span><span class="text">' . '<label id="spacer-lbl" class="">spacer</label></span><span class="after"></span></span>';
     $this->assertEquals($field->label, $equals, 'Line:' . __LINE__ . ' The getLabel method should return something without error.');
     $this->assertThat($form->load('<form><field name="spacer" type="spacer" class="text" label="MyLabel" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Spacer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $equals = '<span class="spacer"><span class="before"></span><span class="text">' . '<label id="spacer-lbl" class="">MyLabel</label></span><span class="after"></span></span>';
     $this->assertEquals($field->label, $equals, 'Line:' . __LINE__ . ' The getLabel method should return something without error.');
     $this->assertThat($form->load('<form><field name="spacer" type="spacer" hr="true" /></form>'), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new Field_Spacer($form);
     $this->assertThat($field->setup($form->getXml()->field, 'value'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true.');
     $expected = '<span class="spacer"><span class="before"></span><span class=""><hr class="" /></span>' . '<span class="after"></span></span>';
     $this->assertEquals($field->label, $expected, 'Line:' . __LINE__ . ' The getLabel method should return something without error.');
 }
 /**
  * Tests the Joomla\Form\Field::setup method
  *
  * @covers Joomla\Form\Field::setup
  * @covers Joomla\Form\Field::__get
  *
  * @return void
  */
 public function testSetup()
 {
     $form = new JFormInspector('form1');
     $this->assertThat($form->load(JFormDataHelper::$loadFieldDocument), $this->isTrue(), 'Line:' . __LINE__ . ' XML string should load successfully.');
     $field = new JFormFieldInspector($form);
     // Standard usage.
     $xml = $form->getXML();
     $title = array_pop($xml->xpath('fields/field[@name="title"]'));
     $this->assertThat($field->setup($title, 'The title'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true if successful.');
     $this->assertThat($field->name, $this->equalTo('title'), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->value, $this->equalTo('The title'), 'Line:' . __LINE__ . ' The value should be set from the setup method argument.');
     $this->assertThat($field->id, $this->equalTo('title_id'), 'Line:' . __LINE__ . ' The property should be set from the XML (non-alpha transposed to underscore).');
     $this->assertThat((string) $title['class'], $this->equalTo('inputbox required'), 'Line:' . __LINE__ . ' The property should be set from the XML.');
     $this->assertThat($field->validate, $this->equalTo('none'), 'Line:' . __LINE__ . ' The property should be set from the XML.');
     $this->assertThat($field->multiple, $this->isFalse(), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->required, $this->isTrue(), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->input, $this->equalTo(''), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $equals = '<label id="title_id-lbl" for="title_id" class="hasTip required" title="Title::The title.">' . 'Title<span class="star">&#160;*</span></label>';
     $this->assertThat($field->label, $this->equalTo($equals), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->title, $this->equalTo('Title'), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->unexisting, $this->equalTo(null), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     // Test multiple attribute and form group name.
     $colours = array_pop($xml->xpath('fields/fields[@name="params"]/field[@name="colours"]'));
     $this->assertThat($field->setup($colours, 'green', 'params'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true if successful.');
     $this->assertThat($field->id, $this->equalTo('params_colours'), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->name, $this->equalTo('params[colours][]'), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->multiple, $this->isTrue(), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertEquals($field->group, 'params', 'Line:' . __LINE__ . ' The property should be set to the the group name.');
     // Test hidden field type.
     $id = array_pop($xml->xpath('fields/field[@name="id"]'));
     $this->assertThat($field->setup($id, 42), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true if successful.');
     $this->assertThat($field->hidden, $this->isTrue(), 'Line:' . __LINE__ . ' The hidden property should be set from the field type.');
     // Test hidden attribute.
     $createdDate = array_pop($xml->xpath('fields/field[@name="created_date"]'));
     $this->assertThat($field->setup($createdDate, '0000-00-00 00:00:00'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true if successful.');
     $this->assertThat($field->hidden, $this->isTrue(), 'Line:' . __LINE__ . ' The hidden property should be set from the hidden attribute.');
     // Test automatic generated name.
     $spacer = array_pop($xml->xpath('fields/field[@type="spacer"]'));
     $this->assertThat($field->setup($spacer, ''), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true if successful.');
     $this->assertThat($field->name, $this->equalTo('__field1'), 'Line:' . __LINE__ . ' The spacer name should be set using an automatic generated name.');
     // Test nested groups and forced multiple.
     $comment = array_pop($xml->xpath('fields/fields[@name="params"]/fields[@name="subparams"]/field[@name="comment"]'));
     $field->forceMultiple = true;
     $this->assertThat($field->setup($comment, 'My comment', 'params.subparams'), $this->isTrue(), 'Line:' . __LINE__ . ' The setup method should return true if successful.');
     $this->assertThat($field->id, $this->equalTo('params_subparams_comment'), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertThat($field->name, $this->equalTo('params[subparams][comment][]'), 'Line:' . __LINE__ . ' The property should be computed from the XML.');
     $this->assertEquals($field->group, 'params.subparams', 'Line:' . __LINE__ . ' The property should be set to the the group name.');
     $this->assertEquals($field->element['class'], 'required', 'Line:' . __LINE__ . ' The property should be computed from the XML.');
 }
 /**
  * Test for Form::validateField method for missing rule exception.
  *
  * return   void
  *
  * @covers  Joomla\Form\Form::validateField
  * @since   1.0
  *
  * @expectedException  \UnexpectedValueException
  *
  * @return void
  */
 public function testValidateField_missingRule()
 {
     $form = new JFormInspector('form1');
     $form->load(JFormDataHelper::$validateFieldDocument);
     $xml = $form->getXML();
     $field = array_pop($xml->xpath('fields/field[@name="missingrule"]'));
     $result = $form->validateField($field, null, 'value');
 }