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