1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\Form; |
9
|
|
|
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator; |
10
|
|
|
use SilverStripe\Forms\TextField; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package framework |
14
|
|
|
* @subpackage tests |
15
|
|
|
*/ |
16
|
|
|
class ValidatorTest extends SapphireTest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Common method for setting up form, since that will always be a dependency for the validator. |
21
|
|
|
* |
22
|
|
|
* @param array $fieldNames |
23
|
|
|
* @return Form |
24
|
|
|
*/ |
25
|
|
|
protected function getForm(array $fieldNames = array()) |
26
|
|
|
{ |
27
|
|
|
// Setup field list now. We're only worried about names right now. |
28
|
|
|
$fieldList = new FieldList(); |
29
|
|
|
foreach ($fieldNames as $name) { |
30
|
|
|
$fieldList->add(new TextField($name)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return new Form(Controller::curr(), "testForm", $fieldList, new FieldList([/* no actions */])); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
public function testRemoveValidation() |
38
|
|
|
{ |
39
|
|
|
$validator = new TestValidator(); |
40
|
|
|
|
41
|
|
|
// Setup a form with the fields/data we're testing (a form is a dependency for validation right now). |
42
|
|
|
$data = array("foobar" => ""); |
43
|
|
|
// We only care right now about the fields we've got setup in this array. |
44
|
|
|
$form = $this->getForm(array_keys($data)); |
45
|
|
|
$form->disableSecurityToken(); |
46
|
|
|
// Setup validator now that we've got our form. |
47
|
|
|
$form->setValidator($validator); |
48
|
|
|
// Put data into the form so the validator can pull it back out again. |
49
|
|
|
$form->loadDataFrom($data); |
50
|
|
|
|
51
|
|
|
$result = $form->validationResult(); |
52
|
|
|
$this->assertFalse($result->isValid()); |
53
|
|
|
$this->assertCount(1, $result->getMessages()); |
54
|
|
|
|
55
|
|
|
// Make sure it doesn't fail after removing validation AND has no errors |
56
|
|
|
// (since calling validate should reset errors). |
57
|
|
|
$validator->removeValidation(); |
58
|
|
|
$result = $form->validationResult(); |
59
|
|
|
$this->assertTrue($result->isValid()); |
60
|
|
|
$this->assertEmpty($result->getMessages()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|