Example #1
0
 public function testVisitField()
 {
     $writer = new HTMLWriter();
     /* A literal field */
     $field = new Field(["field-class"], [], "literal", "A literal field", "initial", true, [], 200);
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitField($field));
     // Assert that the field contains the given elements
     $this->assertContains("initial", $writer->visitField($field));
     $this->assertContains("field-class", $writer->visitField($field));
     /* A section-label field */
     $field = new Field([], [], "section-label", "A section-label field", "initial");
     $this->assertContains("A section-label field", $writer->visitField($field));
     $this->assertNotContains("initial", $writer->visitField($field));
     /* An html field */
     $field = new Field([], [], FieldBuilder::TYPE_HTML, "An HTML Field", "initial");
     $this->assertContains('textarea class=html', $this->stripQuotes($writer->visitField($field)));
     /* A choice field */
     $aliases = ["alias1", "alias2"];
     $values = ["value1", "value2"];
     $choices = [ChoiceBuilder::begin()->setAlias($aliases[0])->setValue($values[0])->build(), ChoiceBuilder::begin()->setAlias($aliases[1])->setValue($values[1])->build()];
     $field = new Field([], [], "choice", "A literal field", $values[0], true, $choices, 200);
     $keys = array_keys($field->getChoices());
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitField($field));
     // Assert that the field contains our choices
     $this->assertContains($values[0], $result);
     $this->assertContains($values[1], $result);
     $this->assertContains("value=" . $keys[0], $result);
     $this->assertContains("value=" . $keys[1], $result);
     // Assert that the "initial" choice is selected
     $this->assertContains("value={$keys[0]} checked", $result);
     /* A multiple choice field */
     $field = new Field([], [], "multiple-choice", "A multiple-choice field", [$values[1]], true, $choices, 200);
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitField($field));
     // Assert that the field contains our choices
     $this->assertContains($values[0], $result);
     $this->assertContains($values[1], $result);
     $this->assertContains("value={$keys[0]}", $result);
     $this->assertContains("value={$keys[1]}", $result);
     // Assert that the "initial" choice is selected
     $this->assertContains("value={$keys[1]}  selected", $result);
     /* A text field */
     $field = new Field([], [], "text", "A text field", "5", true, [], 200, "helptext", "placeholder for a text field");
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitField($field));
     $this->assertContains('value=5', $result);
     $this->assertContains('<input type=text', $result);
     $this->assertContains('placeholder for a text field', $result);
     /* A textarea field */
     $field = new Field([], [], "textarea", "A textarea field", "initial value", true, [], 1000, "helptext", "placeholder for a textarea field");
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitField($field));
     // By our current method of calculation, should have size of 100 means 10 rows
     // If change calculation, change this test
     $this->assertContains('rows=10', $result);
     $this->assertContains('<textarea', $result);
     $this->assertContains('initial value', $result);
     $this->assertContains('placeholder for a textarea field', $result);
     /* A textarea field without an initial value*/
     $field = new Field([], [], "textarea", "A textarea field", "", true, [], 1000);
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitField($field));
     // Assert that the text area contains no initial text
     $this->assertContains('></textarea>', $result);
 }