Skip to content

Commit

Permalink
ID#347: added unit tests for validation listener tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Achatz committed Mar 22, 2019
1 parent 689cad4 commit 4d068c9
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 3 deletions.
114 changes: 114 additions & 0 deletions tests/suites/tools/form/taglib/HtmlFormTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

class HtmlFormTagTest extends TestCase {

const LISTENER_ERROR_MESSAGE = 'Fields must be filled with 3 to 16 characters!';

/**
* Tests form which is not sent.
*/
Expand Down Expand Up @@ -1080,4 +1082,116 @@ public function testGetTagClass() {
$form->getTagClass('not-existing:tag');
}

/**
* ID#347: test simple single-field listener for single control.
*/
public function testValidationListener1() {

$_POST = ['submit' => 'submit'];
$_REQUEST = [];

$form = new HtmlFormTag();

// inject parent object to make recursive selection work
$doc = new Document();
$form->setParent($doc);

$form->setAttribute('name', 'foo');
$form->setContent('<form:listener control="first-name">' . self::LISTENER_ERROR_MESSAGE . '</form:listener>
<form:text name="first-name" />
<form:button name="submit" value="submit"/>
<form:addvalidator class="APF\tools\form\validator\TextLengthValidator" button="submit" control="first-name"/>');
$form->onParseTime();
$form->onAfterAppend();

$this->assertFalse($form->isValid());

$html = $form->transformForm();

$this->assertContains(self::LISTENER_ERROR_MESSAGE, $html);
$this->assertEquals(1, substr_count($html, self::LISTENER_ERROR_MESSAGE));
}

/**
* ID#347: test single-field listener setup using different messages per validator.
*/
public function testValidationListener2() {

$_POST = ['submit' => 'submit'];
$_REQUEST = [];

$form = new HtmlFormTag();

// inject parent object to make recursive selection work
$doc = new Document();
$form->setParent($doc);

$form->setAttribute('name', 'foo');
$form->setContent('<form:listener control="e-mail" validator="APF\tools\form\validator\TextLengthValidator">' . self::LISTENER_ERROR_MESSAGE . '</form:listener>
<form:listener control="e-mail" validator="APF\tools\form\validator\EMailValidator">' . self::LISTENER_ERROR_MESSAGE . '</form:listener>
<form:text name="e-mail" />
<form:button name="submit" value="submit"/>
<form:addvalidator class="APF\tools\form\validator\EMailValidator" button="submit" control="e-mail" type="special"/>');
$form->onParseTime();
$form->onAfterAppend();

$this->assertFalse($form->isValid());

$html = $form->transformForm();

$this->assertContains(self::LISTENER_ERROR_MESSAGE, $html);
$this->assertEquals(1, substr_count($html, self::LISTENER_ERROR_MESSAGE));
}

/**
* ID#347: test multi-field listener setup with simple and specific validator setup. At the same time, test
* usage of listeners within &lt;form:error /&gt; tags.
*/
public function testValidationListener3() {

$_POST = ['submit' => 'submit'];
$_REQUEST = [];

$form = new HtmlFormTag();

// inject parent object to make recursive selection work
$doc = new Document();
$form->setParent($doc);

$genericErrorMessage = 'Invalid e-mail!';
$syntaxInvalidErrorMessage = 'Syntax of e-mail invalid!';

$form->setAttribute('name', 'foo');
$form->setContent('<form:error>
<form:listener validator="APF\tools\form\validator\TextLengthValidator">' . self::LISTENER_ERROR_MESSAGE . '</form:listener>
<form:listener control="e-mail">' . $genericErrorMessage . '</form:listener>
<form:listener control="e-mail" validator="APF\tools\form\validator\EMailValidator">' . $syntaxInvalidErrorMessage . '</form:listener>
</form:error>
<form:text name="first-name" />
<form:text name="last-name" />
<form:text name="e-mail" />
<form:button name="submit" value="submit"/>
<form:addvalidator class="APF\tools\form\validator\TextLengthValidator" button="submit" control="first-name|last-name" type="generic"/>
<form:addvalidator class="APF\tools\form\validator\TextLengthValidator" button="submit" control="e-mail" />
<form:addvalidator class="APF\tools\form\validator\EMailValidator" button="submit" control="e-mail" type="special"/>');
$form->onParseTime();
$form->onAfterAppend();

$this->assertFalse($form->isValid());

$html = $form->transformForm();

// generic text length validator should trigger listener bound to the TextLengthValidator
$this->assertContains(self::LISTENER_ERROR_MESSAGE, $html);
$this->assertEquals(1, substr_count($html, self::LISTENER_ERROR_MESSAGE));

// "normal" text length validator on e-mail field should trigger standard field-based listener
$this->assertContains($genericErrorMessage, $html);
$this->assertEquals(1, substr_count($html, $genericErrorMessage));

// "special" e-mail validator on e-mail field should trigger special validator-based listener
$this->assertContains($syntaxInvalidErrorMessage, $html);
$this->assertEquals(1, substr_count($html, $syntaxInvalidErrorMessage));
}

}
4 changes: 1 addition & 3 deletions tools/form/validator/EMailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
class EMailValidator extends TextFieldValidator {

public function validate($input) {
$validator = new EMailValidatorImpl();

return $validator->isValid($input);
return (new EMailValidatorImpl())->isValid($input);
}

}

1 comment on commit 4d068c9

@GeneralCrime
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hallo,
also da ich im Tracker schlecht ein Feedback zu einem abgeschlossenen Ticket schreiben kann nur durch wiedereröffnen, mach ich das mal hier. Mein Anwendungsfall ist damit geklärt und es läuft auch alles wie erwartet.

Please sign in to comment.