|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package framework |
|
4
|
|
|
* @subpackage tests |
|
5
|
|
|
*/ |
|
6
|
|
|
class DateFieldTest extends SapphireTest { |
|
7
|
|
|
|
|
8
|
|
|
public function setUp() { |
|
9
|
|
|
parent::setUp(); |
|
10
|
|
|
|
|
11
|
|
|
$this->originalLocale = i18n::get_locale(); |
|
|
|
|
|
|
12
|
|
|
i18n::set_locale('en_NZ'); |
|
13
|
|
|
$this->origConfig = Config::inst()->get('DateField', 'default_config'); |
|
|
|
|
|
|
14
|
|
|
Config::inst()->update('DateField', 'default_config', array('dateformat' => 'dd/MM/yyyy')); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function tearDown() { |
|
18
|
|
|
parent::tearDown(); |
|
19
|
|
|
|
|
20
|
|
|
i18n::set_locale($this->originalLocale); |
|
|
|
|
|
|
21
|
|
|
Config::inst()->remove('DateField', 'default_config'); |
|
22
|
|
|
Config::inst()->update('DateField', 'default_config', $this->origConfig); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testValidateMinDate() { |
|
26
|
|
|
$f = new DateField('Date'); |
|
27
|
|
|
$f->setConfig('min', '2009-03-31'); |
|
28
|
|
|
$f->setValue('2010-03-31'); |
|
29
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date'); |
|
30
|
|
|
|
|
31
|
|
|
$f = new DateField('Date'); |
|
32
|
|
|
$f->setConfig('min', '2009-03-31'); |
|
33
|
|
|
$f->setValue('1999-03-31'); |
|
34
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date'); |
|
35
|
|
|
|
|
36
|
|
|
$f = new DateField('Date'); |
|
37
|
|
|
$f->setConfig('min', '2009-03-31'); |
|
38
|
|
|
$f->setValue('2009-03-31'); |
|
39
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testValidateMinDateStrtotime() { |
|
43
|
|
|
$f = new DateField('Date'); |
|
44
|
|
|
$f->setConfig('min', '-7 days'); |
|
45
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('-8 days'))); |
|
46
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime'); |
|
47
|
|
|
|
|
48
|
|
|
$f = new DateField('Date'); |
|
49
|
|
|
$f->setConfig('min', '-7 days'); |
|
50
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('-7 days'))); |
|
51
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testValidateMaxDateStrtotime() { |
|
55
|
|
|
$f = new DateField('Date'); |
|
56
|
|
|
$f->setConfig('max', '7 days'); |
|
57
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('8 days'))); |
|
58
|
|
|
$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime'); |
|
59
|
|
|
|
|
60
|
|
|
$f = new DateField('Date'); |
|
61
|
|
|
$f->setConfig('max', '7 days'); |
|
62
|
|
|
$f->setValue(strftime('%Y-%m-%d', strtotime('7 days'))); |
|
63
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testValidateMaxDate() { |
|
67
|
|
|
$f = new DateField('Date'); |
|
68
|
|
|
$f->setConfig('max', '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->setConfig('max', '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->setConfig('max', '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
|
|
|
$f = new DateField('Date'); |
|
85
|
|
|
$this->assertEquals($f->dataValue(), null); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testConstructorWithDateString() { |
|
89
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
90
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function testSetValueWithDateString() { |
|
94
|
|
|
$f = new DateField('Date', 'Date'); |
|
95
|
|
|
$f->setValue('29/03/2003'); |
|
96
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testSetValueWithDateArray() { |
|
100
|
|
|
$f = new DateField('Date', 'Date'); |
|
101
|
|
|
$f->setConfig('dmyfields', true); |
|
102
|
|
|
$f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003)); |
|
103
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testConstructorWithIsoDate() { |
|
107
|
|
|
// used by Form->loadDataFrom() |
|
108
|
|
|
$f = new DateField('Date', 'Date', '2003-03-29'); |
|
109
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testValidateDMY() { |
|
113
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
114
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
115
|
|
|
|
|
116
|
|
|
$f = new DateField('Date', 'Date', 'wrong'); |
|
117
|
|
|
$this->assertFalse($f->validate(new RequiredFields())); |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testEmptyValueValidation() { |
|
122
|
|
|
$field = new DateField('Date'); |
|
123
|
|
|
$validator = new RequiredFields(); |
|
124
|
|
|
$this->assertTrue($field->validate($validator)); |
|
125
|
|
|
$field->setConfig('dmyfields', true); |
|
126
|
|
|
$this->assertTrue($field->validate($validator)); |
|
127
|
|
|
$field->setValue(array( |
|
128
|
|
|
'day' => '', |
|
129
|
|
|
'month' => '', |
|
130
|
|
|
'year' => '', |
|
131
|
|
|
)); |
|
132
|
|
|
$this->assertTrue($field->validate($validator)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testValidateArray() { |
|
136
|
|
|
$f = new DateField('Date', 'Date'); |
|
137
|
|
|
$f->setConfig('dmyfields', true); |
|
138
|
|
|
$f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003)); |
|
139
|
|
|
$this->assertTrue($f->validate(new RequiredFields())); |
|
140
|
|
|
|
|
141
|
|
|
$f->setValue(null); |
|
142
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'NULL values are validating TRUE'); |
|
143
|
|
|
|
|
144
|
|
|
$f->setValue(array()); |
|
145
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Empty array values are validating TRUE'); |
|
146
|
|
|
|
|
147
|
|
|
$f->setValue(array('day' => null, 'month' => null, 'year' => null)); |
|
148
|
|
|
$this->assertTrue($f->validate(new RequiredFields()), 'Empty array values with keys are validating TRUE'); |
|
149
|
|
|
|
|
150
|
|
|
// TODO Fix array validation |
|
151
|
|
|
// $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999)); |
|
|
|
|
|
|
152
|
|
|
// $this->assertFalse($f->validate(new RequiredFields())); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function testValidateEmptyArrayValuesSetsNullForValueObject() { |
|
156
|
|
|
$f = new DateField('Date', 'Date'); |
|
157
|
|
|
$f->setConfig('dmyfields', true); |
|
158
|
|
|
|
|
159
|
|
|
$f->setValue(array('day' => '', 'month' => '', 'year' => '')); |
|
160
|
|
|
$this->assertNull($f->dataValue()); |
|
161
|
|
|
|
|
162
|
|
|
$f->setValue(array('day' => null, 'month' => null, 'year' => null)); |
|
163
|
|
|
$this->assertNull($f->dataValue()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function testValidateArrayValue() { |
|
167
|
|
|
$f = new DateField('Date', 'Date'); |
|
168
|
|
|
$this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003))); |
|
169
|
|
|
$this->assertFalse($f->validateArrayValue(array('month' => 03, 'year' => 2003))); |
|
170
|
|
|
$this->assertFalse($f->validateArrayValue(array('day' => 99, 'month' => 99, 'year' => 2003))); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function testFormatEnNz() { |
|
174
|
|
|
/* We get YYYY-MM-DD format as the data value for DD/MM/YYYY input value */ |
|
175
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
176
|
|
|
$this->assertEquals($f->dataValue(), '2003-03-29'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function testSetLocale() { |
|
180
|
|
|
// should get en_NZ by default through setUp() |
|
181
|
|
|
$f = new DateField('Date', 'Date', '29/03/2003'); |
|
182
|
|
|
$f->setLocale('de_DE'); |
|
183
|
|
|
$f->setValue('29.06.2006'); |
|
184
|
|
|
$this->assertEquals($f->dataValue(), '2006-06-29'); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Note: This is mostly tested for legacy reasons |
|
189
|
|
|
*/ |
|
190
|
|
|
public function testMDYFormat() { |
|
191
|
|
|
$dateField = new DateField('Date', 'Date'); |
|
192
|
|
|
$dateField->setConfig('dateformat', 'd/M/Y'); |
|
193
|
|
|
$dateField->setValue('31/03/2003'); |
|
194
|
|
|
$this->assertEquals( |
|
195
|
|
|
$dateField->dataValue(), |
|
196
|
|
|
'2003-03-31', |
|
197
|
|
|
"We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value" |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
$dateField2 = new DateField('Date', 'Date'); |
|
201
|
|
|
$dateField2->setConfig('dateformat', 'd/M/Y'); |
|
202
|
|
|
$dateField2->setValue('04/3/03'); |
|
203
|
|
|
$this->assertEquals( |
|
204
|
|
|
$dateField2->dataValue(), |
|
205
|
|
|
'2003-03-04', |
|
206
|
|
|
"Even if input value hasn't got leading 0's in it we still get the correct data value" |
|
207
|
|
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.