|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use IntlDateFormatter; |
|
6
|
|
|
use SilverStripe\Core\Config\Config; |
|
7
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
8
|
|
|
use SilverStripe\Forms\TimeField; |
|
9
|
|
|
use SilverStripe\Forms\RequiredFields; |
|
10
|
|
|
use SilverStripe\i18n\i18n; |
|
11
|
|
|
|
|
12
|
|
|
class TimeFieldTest extends SapphireTest |
|
13
|
|
|
{ |
|
14
|
|
|
protected function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
i18n::set_locale('en_NZ'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function testConstructorWithoutArgs() |
|
21
|
|
|
{ |
|
22
|
|
|
$f = new TimeField('Time'); |
|
23
|
|
|
$this->assertEquals($f->dataValue(), null); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testConstructorWithString() |
|
27
|
|
|
{ |
|
28
|
|
|
$f = new TimeField('Time', 'Time', '23:00:00'); |
|
29
|
|
|
$this->assertEquals($f->dataValue(), '23:00:00'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testValidate() |
|
33
|
|
|
{ |
|
34
|
|
|
$f = new TimeField('Time', 'Time', '11pm'); |
|
35
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
36
|
|
|
|
|
37
|
|
|
$f = new TimeField('Time', 'Time', '23:59'); |
|
38
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
39
|
|
|
|
|
40
|
|
|
$f = new TimeField('Time', 'Time', 'wrong'); |
|
41
|
|
|
$this->assertFalse($f->validate(new RequiredFields())); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testValidateLenientWithHtml5() |
|
45
|
|
|
{ |
|
46
|
|
|
$f = new TimeField('Time', 'Time', '23:59:59'); |
|
47
|
|
|
$f->setHTML5(true); |
|
48
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
49
|
|
|
|
|
50
|
|
|
$f = new TimeField('Time', 'Time', '23:59'); // leave out seconds |
|
51
|
|
|
$f->setHTML5(true); |
|
52
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function testSetLocale() |
|
56
|
|
|
{ |
|
57
|
|
|
// should get en_NZ by default through setUp() |
|
58
|
|
|
$f = new TimeField('Time', 'Time'); |
|
59
|
|
|
$f->setHTML5(false); |
|
60
|
|
|
$f->setLocale('fr_FR'); |
|
61
|
|
|
// TODO Find a hour format thats actually different |
|
62
|
|
|
$f->setValue('23:59'); |
|
63
|
|
|
$this->assertEquals($f->dataValue(), '23:59:00'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testSetValueWithUseStrToTime() |
|
67
|
|
|
{ |
|
68
|
|
|
$f = new TimeField('Time', 'Time'); |
|
69
|
|
|
$f->setValue('11pm'); |
|
70
|
|
|
$this->assertEquals( |
|
71
|
|
|
$f->dataValue(), |
|
72
|
|
|
'23:00:00', |
|
73
|
|
|
'Setting value to "11pm" parses with strtotime enabled' |
|
74
|
|
|
); |
|
75
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
76
|
|
|
|
|
77
|
|
|
$f = new TimeField('Time', 'Time'); |
|
78
|
|
|
$f->setValue('11:59pm'); |
|
79
|
|
|
$this->assertEquals('23:59:00', $f->dataValue()); |
|
80
|
|
|
|
|
81
|
|
|
$f = new TimeField('Time', 'Time'); |
|
82
|
|
|
$f->setValue('11:59 pm'); |
|
83
|
|
|
$this->assertEquals('23:59:00', $f->dataValue()); |
|
84
|
|
|
|
|
85
|
|
|
$f = new TimeField('Time', 'Time'); |
|
86
|
|
|
$f->setValue('23:59'); |
|
87
|
|
|
$this->assertEquals('23:59:00', $f->dataValue()); |
|
88
|
|
|
|
|
89
|
|
|
$f = new TimeField('Time', 'Time'); |
|
90
|
|
|
$f->setValue('23:59:38'); |
|
91
|
|
|
$this->assertEquals('23:59:38', $f->dataValue()); |
|
92
|
|
|
|
|
93
|
|
|
$f = new TimeField('Time', 'Time'); |
|
94
|
|
|
$f->setValue('12:00 am'); |
|
95
|
|
|
$this->assertEquals($f->dataValue(), '00:00:00'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testOverrideWithNull() |
|
99
|
|
|
{ |
|
100
|
|
|
$field = new TimeField('Time', 'Time'); |
|
101
|
|
|
$field->setValue('11:00:00'); |
|
102
|
|
|
$field->setValue(''); |
|
103
|
|
|
$this->assertEquals($field->dataValue(), ''); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Test that AM/PM is preserved correctly in various situations |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testSetTimeFormat() |
|
110
|
|
|
{ |
|
111
|
|
|
|
|
112
|
|
|
// Test with timeformat that includes hour |
|
113
|
|
|
|
|
114
|
|
|
// Check pm |
|
115
|
|
|
$f = new TimeField('Time', 'Time'); |
|
116
|
|
|
$f->setHTML5(false); |
|
117
|
|
|
$f->setTimeFormat('h:mm:ss a'); |
|
118
|
|
|
$f->setValue('3:59 pm'); |
|
119
|
|
|
$this->assertEquals($f->dataValue(), '15:59:00'); |
|
120
|
|
|
|
|
121
|
|
|
// Check am |
|
122
|
|
|
$f = new TimeField('Time', 'Time'); |
|
123
|
|
|
$f->setHTML5(false); |
|
124
|
|
|
$f->setTimeFormat('h:mm:ss a'); |
|
125
|
|
|
$f->setValue('3:59 am'); |
|
126
|
|
|
$this->assertEquals($f->dataValue(), '03:59:00'); |
|
127
|
|
|
|
|
128
|
|
|
// Check with ISO date/time |
|
129
|
|
|
$f = new TimeField('Time', 'Time'); |
|
130
|
|
|
$f->setHTML5(false); |
|
131
|
|
|
$f->setTimeFormat('h:mm:ss a'); |
|
132
|
|
|
$f->setValue('15:59:00'); |
|
133
|
|
|
$this->assertEquals($f->dataValue(), '15:59:00'); |
|
134
|
|
|
|
|
135
|
|
|
// ISO am |
|
136
|
|
|
$f = new TimeField('Time', 'Time'); |
|
137
|
|
|
$f->setHTML5(false); |
|
138
|
|
|
$f->setTimeFormat('h:mm:ss a'); |
|
139
|
|
|
$f->setValue('03:59:00'); |
|
140
|
|
|
$this->assertEquals($f->dataValue(), '03:59:00'); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testLenientSubmissionParseWithoutSecondsOnHtml5() |
|
144
|
|
|
{ |
|
145
|
|
|
$f = new TimeField('Time', 'Time'); |
|
146
|
|
|
$f->setSubmittedValue('23:59'); |
|
147
|
|
|
$this->assertEquals($f->Value(), '23:59:00'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @expectedException \LogicException |
|
152
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setTimeFormat/ |
|
153
|
|
|
*/ |
|
154
|
|
|
public function testHtml5WithCustomFormatThrowsException() |
|
155
|
|
|
{ |
|
156
|
|
|
$f = new TimeField('Time', 'Time'); |
|
157
|
|
|
$f->setValue('15:59:00'); |
|
158
|
|
|
$f->setTimeFormat('mm:HH'); |
|
159
|
|
|
$f->Value(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @expectedException \LogicException |
|
164
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setTimeLength/ |
|
165
|
|
|
*/ |
|
166
|
|
|
public function testHtml5WithCustomDateLengthThrowsException() |
|
167
|
|
|
{ |
|
168
|
|
|
$f = new TimeField('Time', 'Time'); |
|
169
|
|
|
$f->setValue('15:59:00'); |
|
170
|
|
|
$f->setTimeLength(IntlDateFormatter::MEDIUM); |
|
171
|
|
|
$f->Value(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @expectedException \LogicException |
|
176
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setLocale/ |
|
177
|
|
|
*/ |
|
178
|
|
|
public function testHtml5WithCustomLocaleThrowsException() |
|
179
|
|
|
{ |
|
180
|
|
|
$f = new TimeField('Time', 'Time'); |
|
181
|
|
|
$f->setValue('15:59:00'); |
|
182
|
|
|
$f->setLocale('de_DE'); |
|
183
|
|
|
$f->Value(); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|