-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX for #4417: Ensuring ->removeValidation() is defined on instances …
…of Validator. Setup new API for enabling/disabling validation. Documentation and better type handling.
- Loading branch information
1 parent
958f98b
commit 9400ec9
Showing
2 changed files
with
101 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/** | ||
* @package framework | ||
* @subpackage tests | ||
*/ | ||
|
||
class ValidatorTest extends SapphireTest { | ||
|
||
/** | ||
* Common method for setting up form, since that will always be a dependency for the validator. | ||
* | ||
* @param array $fieldNames | ||
* @return Form | ||
*/ | ||
protected function getForm(array $fieldNames = array()) { | ||
// Setup field list now. We're only worried about names right now. | ||
$fieldList = new FieldList(); | ||
foreach($fieldNames as $name) { | ||
$fieldList->add(new TextField($name)); | ||
} | ||
|
||
return new Form(new Controller(), "testForm", $fieldList, new FieldList(array(/* no actions */))); | ||
} | ||
|
||
|
||
public function testRemoveValidation() { | ||
$validator = new ValidatorTestExample(); | ||
|
||
// Setup a form with the fields/data we're testing (a form is a dependency for validation right now). | ||
$data = array("foobar" => ""); | ||
$form = $this->getForm(array_keys($data)); // We only care right now about the fields we've got setup in this array. | ||
$form->setValidator($validator); // Setup validator now that we've got our form. | ||
$form->loadDataFrom($data); // Put data into the form so the validator can pull it back out again. | ||
|
||
// Make sure it fails and that error results match ->getErrors(). | ||
$errors = $validator->validate(); | ||
$this->assertCount(1, $errors); | ||
$this->assertEquals($validator->getErrors(), $errors); | ||
|
||
// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should reset errors). | ||
$validator->removeValidation(); | ||
$errors = $validator->validate(); | ||
$this->assertEquals(array(), $errors); | ||
} | ||
|
||
} | ||
|
||
|
||
class ValidatorTestExample extends Validator implements TestOnly { | ||
|
||
/** | ||
* Requires a specific field for test purposes. | ||
* | ||
* @param array $data | ||
* @return null | ||
*/ | ||
public function php($data) { | ||
$this->requireField("foobar", $data); | ||
} | ||
} |