|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use IntlDateFormatter; |
|
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
7
|
|
|
use SilverStripe\Forms\DateField; |
|
8
|
|
|
use SilverStripe\Forms\RequiredFields; |
|
9
|
|
|
use SilverStripe\i18n\i18n; |
|
10
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
11
|
|
|
|
|
12
|
|
|
class DateFieldTest extends SapphireTest |
|
13
|
|
|
{ |
|
14
|
|
|
protected function setUp() |
|
15
|
|
|
{ |
|
16
|
|
|
parent::setUp(); |
|
17
|
|
|
i18n::set_locale('en_NZ'); |
|
18
|
|
|
DBDatetime::set_mock_now('2011-02-01 8:34:00'); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testValidateMinDate() |
|
22
|
|
|
{ |
|
23
|
|
|
$dateField = new DateField('Date'); |
|
24
|
|
|
$dateField->setMinDate('2009-03-31'); |
|
25
|
|
|
$dateField->setValue('2010-03-31'); |
|
26
|
|
|
$this->assertTrue($dateField->validate(new RequiredFields()), 'Date above min date'); |
|
27
|
|
|
|
|
28
|
|
|
$dateField = new DateField('Date'); |
|
29
|
|
|
$dateField->setMinDate('2009-03-31'); |
|
30
|
|
|
$dateField->setValue('1999-03-31'); |
|
31
|
|
|
$this->assertFalse($dateField->validate(new RequiredFields()), 'Date below min date'); |
|
32
|
|
|
|
|
33
|
|
|
$dateField = new DateField('Date'); |
|
34
|
|
|
$dateField->setMinDate('2009-03-31'); |
|
35
|
|
|
$dateField->setValue('2009-03-31'); |
|
36
|
|
|
$this->assertTrue($dateField->validate(new RequiredFields()), 'Date matching min date'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testValidateMinDateStrtotime() |
|
40
|
|
|
{ |
|
41
|
|
|
$f = new DateField('Date'); |
|
42
|
|
|
$f->setMinDate('-7 days'); |
|
43
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('-8 days', DBDatetime::now()->getTimestamp()))); |
|
44
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime'); |
|
45
|
|
|
|
|
46
|
|
|
$f = new DateField('Date'); |
|
47
|
|
|
$f->setMinDate('-7 days'); |
|
48
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('-7 days', DBDatetime::now()->getTimestamp()))); |
|
49
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testValidateMaxDateStrtotime() |
|
53
|
|
|
{ |
|
54
|
|
|
$f = new DateField('Date'); |
|
55
|
|
|
$f->setMaxDate('7 days'); |
|
56
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('8 days', DBDatetime::now()->getTimestamp()))); |
|
57
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime'); |
|
58
|
|
|
|
|
59
|
|
|
$f = new DateField('Date'); |
|
60
|
|
|
$f->setMaxDate('7 days'); |
|
61
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('7 days', DBDatetime::now()->getTimestamp()))); |
|
62
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testValidateMaxDate() |
|
66
|
|
|
{ |
|
67
|
|
|
$f = new DateField('Date'); |
|
68
|
|
|
$f->setMaxDate('2009-03-31'); |
|
69
|
|
|
$f->setValue('1999-03-31'); |
|
70
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date'); |
|
71
|
|
|
|
|
72
|
|
|
$f = new DateField('Date'); |
|
73
|
|
|
$f->setMaxDate('2009-03-31'); |
|
74
|
|
|
$f->setValue('2010-03-31'); |
|
75
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date'); |
|
76
|
|
|
|
|
77
|
|
|
$f = new DateField('Date'); |
|
78
|
|
|
$f->setMaxDate('2009-03-31'); |
|
79
|
|
|
$f->setValue('2009-03-31'); |
|
80
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testConstructorWithoutArgs() |
|
84
|
|
|
{ |
|
85
|
|
|
$f = new DateField('Date'); |
|
86
|
|
|
$this->assertEquals($f->dataValue(), null); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testConstructorWithDateString() |
|
90
|
|
|
{ |
|
91
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
92
|
|
|
$this->assertEquals(null, $f->dataValue()); |
|
93
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29 12:23:00'); |
|
94
|
|
|
$this->assertEquals('2003-03-29', $f->dataValue()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function testTidyISO8601() |
|
98
|
|
|
{ |
|
99
|
|
|
$f = new DateField('Date', 'Date'); |
|
100
|
|
|
$this->assertEquals(null, $f->tidyISO8601('notadate')); |
|
101
|
|
|
$this->assertEquals('2011-01-31', $f->tidyISO8601('-1 day')); |
|
102
|
|
|
$this->assertEquals(null, $f->tidyISO8601('29/03/2003')); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function testSetValueWithDateString() |
|
106
|
|
|
{ |
|
107
|
|
|
$f = new DateField('Date', 'Date'); |
|
108
|
|
|
$f->setHTML5(false); |
|
109
|
|
|
$f->setSubmittedValue('29/03/2003'); |
|
110
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testConstructorWithIsoDate() |
|
114
|
|
|
{ |
|
115
|
|
|
// used by Form->loadDataFrom() |
|
116
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29'); |
|
117
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function testValidateDMY() |
|
121
|
|
|
{ |
|
122
|
|
|
// Constructor only accepts iso8601 |
|
123
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
124
|
|
|
$this->assertFalse($f->validate(new RequiredFields())); |
|
125
|
|
|
|
|
126
|
|
|
// Set via submitted value (localised) accepts this, however |
|
127
|
|
|
$f = new DateField('Date', 'Date'); |
|
128
|
|
|
$f->setSubmittedValue('29/03/2003'); |
|
129
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
130
|
|
|
|
|
131
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29'); |
|
132
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
133
|
|
|
|
|
134
|
|
|
$f = new DateField('Date', 'Date', 'wrong'); |
|
135
|
|
|
$this->assertFalse($f->validate(new RequiredFields())); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testFormatEnNz() |
|
139
|
|
|
{ |
|
140
|
|
|
/* We get YYYY-MM-DD format as the data value for DD/MM/YYYY input value */ |
|
141
|
|
|
$f = new DateField('Date', 'Date'); |
|
142
|
|
|
$f->setHTML5(false); |
|
143
|
|
|
$f->setSubmittedValue('29/03/2003'); |
|
144
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function testSetLocale() |
|
148
|
|
|
{ |
|
149
|
|
|
// should get en_NZ by default through setUp() |
|
150
|
|
|
i18n::set_locale('de_DE'); |
|
151
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
152
|
|
|
$f->setHTML5(false); |
|
153
|
|
|
$f->setValue('29.06.2006'); |
|
154
|
|
|
$this->assertEquals($f->dataValue(), '2006-06-29'); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Note: This is mostly tested for legacy reasons |
|
159
|
|
|
*/ |
|
160
|
|
|
public function testMDYFormat() |
|
161
|
|
|
{ |
|
162
|
|
|
$dateField = new DateField('Date', 'Date'); |
|
163
|
|
|
$dateField->setHTML5(false); |
|
164
|
|
|
$dateField->setDateFormat('d/M/y'); |
|
165
|
|
|
$dateField->setSubmittedValue('31/03/2003'); |
|
166
|
|
|
$this->assertEquals( |
|
167
|
|
|
'2003-03-31', |
|
168
|
|
|
$dateField->dataValue(), |
|
169
|
|
|
"We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value" |
|
170
|
|
|
); |
|
171
|
|
|
|
|
172
|
|
|
$dateField2 = new DateField('Date', 'Date'); |
|
173
|
|
|
$dateField2->setHTML5(false); |
|
174
|
|
|
$dateField2->setDateFormat('d/M/y'); |
|
175
|
|
|
$dateField2->setSubmittedValue('04/3/03'); |
|
176
|
|
|
$this->assertEquals( |
|
177
|
|
|
$dateField2->dataValue(), |
|
178
|
|
|
'2003-03-04', |
|
179
|
|
|
"Even if input value hasn't got leading 0's in it we still get the correct data value" |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @expectedException \LogicException |
|
185
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setDateFormat/ |
|
186
|
|
|
*/ |
|
187
|
|
|
public function testHtml5WithCustomFormatThrowsException() |
|
188
|
|
|
{ |
|
189
|
|
|
$dateField = new DateField('Date', 'Date'); |
|
190
|
|
|
$dateField->setValue('2010-03-31'); |
|
191
|
|
|
$dateField->setDateFormat('d/M/y'); |
|
192
|
|
|
$dateField->Value(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @expectedException \LogicException |
|
197
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setDateLength/ |
|
198
|
|
|
*/ |
|
199
|
|
|
public function testHtml5WithCustomDateLengthThrowsException() |
|
200
|
|
|
{ |
|
201
|
|
|
$dateField = new DateField('Date', 'Date'); |
|
202
|
|
|
$dateField->setValue('2010-03-31'); |
|
203
|
|
|
$dateField->setDateLength(IntlDateFormatter::MEDIUM); |
|
204
|
|
|
$dateField->Value(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @expectedException \LogicException |
|
209
|
|
|
* @expectedExceptionMessageRegExp /Please opt-out .* if using setLocale/ |
|
210
|
|
|
*/ |
|
211
|
|
|
public function testHtml5WithCustomLocaleThrowsException() |
|
212
|
|
|
{ |
|
213
|
|
|
$dateField = new DateField('Date', 'Date'); |
|
214
|
|
|
$dateField->setValue('2010-03-31'); |
|
215
|
|
|
$dateField->setLocale('de_DE'); |
|
216
|
|
|
$dateField->Value(); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|