|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
6
|
|
|
use SilverStripe\Forms\TextField; |
|
7
|
|
|
use SilverStripe\Forms\RequiredFields; |
|
8
|
|
|
use SilverStripe\Forms\Tip; |
|
9
|
|
|
|
|
10
|
|
|
class TextFieldTest extends SapphireTest |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tests the TextField Max Length Validation Failure |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testMaxLengthValidationFail() |
|
17
|
|
|
{ |
|
18
|
|
|
$textField = new TextField('TestField'); |
|
19
|
|
|
$textField->setMaxLength(5); |
|
20
|
|
|
$textField->setValue("John Doe"); // 8 characters, so should fail |
|
21
|
|
|
$result = $textField->validate(new RequiredFields()); |
|
22
|
|
|
$this->assertFalse($result); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Tests the TextField Max Length Validation Success |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testMaxLengthValidationSuccess() |
|
29
|
|
|
{ |
|
30
|
|
|
$textField = new TextField('TestField'); |
|
31
|
|
|
$textField->setMaxLength(5); |
|
32
|
|
|
$textField->setValue("John"); // 4 characters, so should pass |
|
33
|
|
|
$result = $textField->validate(new RequiredFields()); |
|
34
|
|
|
$this->assertTrue($result); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Ensures that when a Tip is applied to the field, it outputs it in the schema |
|
39
|
|
|
*/ |
|
40
|
|
|
public function testTipIsIncludedInSchema() |
|
41
|
|
|
{ |
|
42
|
|
|
$textField = new TextField('TestField'); |
|
43
|
|
|
$this->assertArrayNotHasKey('tip', $textField->getSchemaDataDefaults()); |
|
44
|
|
|
|
|
45
|
|
|
$textField->setTip(new Tip('TestTip')); |
|
46
|
|
|
$this->assertArrayHasKey('tip', $textField->getSchemaDataDefaults()); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|