DatetimeFieldTest.php
7.3 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* @package framework
* @subpackage tests
*/
class DatetimeFieldTest extends SapphireTest {
public function setUp() {
parent::setUp();
$this->originalLocale = i18n::get_locale();
i18n::set_locale('en_NZ');
$this->origDateConfig = Config::inst()->get('DateField', 'default_config');
$this->origTimeConfig = Config::inst()->get('TimeField', 'default_config');
Config::inst()->update('DateField', 'default_config', array('dateformat' => 'dd/MM/yyyy'));
Config::inst()->update('TimeField', 'default_config', array('timeformat' => 'HH:mm:ss'));
}
public function tearDown() {
parent::tearDown();
i18n::set_locale($this->originalLocale);
Config::inst()->remove('DateField', 'default_config');
Config::inst()->update('DateField', 'default_config', $this->origDateConfig);
Config::inst()->remove('TimeField', 'default_config');
Config::inst()->update('TimeField', 'default_config', $this->origTimeConfig);
}
public function testFormSaveInto() {
$f = new DatetimeField('MyDatetime', null);
$form = $this->getMockForm();
$form->Fields()->push($f);
$f->setValue(array(
'date' => '29/03/2003',
'time' => '23:59:38'
));
$m = new DatetimeFieldTest_Model();
$form->saveInto($m);
$this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
}
public function testDataValue() {
$f = new DatetimeField('Datetime');
$this->assertEquals(null, $f->dataValue(), 'Empty field');
$f = new DatetimeField('Datetime', null, '2003-03-29 23:59:38');
$this->assertEquals('2003-03-29 23:59:38', $f->dataValue(), 'From date/time string');
}
public function testConstructorWithoutArgs() {
$f = new DatetimeField('Datetime');
$this->assertEquals($f->dataValue(), null);
}
// /**
// * @expectedException InvalidArgumentException
// */
// public function testConstructorWithLocalizedDateString() {
// $f = new DatetimeField('Datetime', 'Datetime', '29/03/2003 23:59:38');
// }
public function testConstructorWithIsoDate() {
// used by Form->loadDataFrom()
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
$this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
}
// /**
// * @expectedException InvalidArgumentException
// */
// public function testSetValueWithDateString() {
// $f = new DatetimeField('Datetime', 'Datetime');
// $f->setValue('29/03/2003');
// }
public function testSetValueWithDateTimeString() {
$f = new DatetimeField('Datetime', 'Datetime');
$f->setValue('2003-03-29 23:59:38');
$this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
}
public function testSetValueWithArray() {
$f = new DatetimeField('Datetime', 'Datetime');
// Values can only be localized (= non-ISO) in array notation
$f->setValue(array(
'date' => '29/03/2003',
'time' => '11pm'
));
$this->assertEquals($f->dataValue(), '2003-03-29 23:00:00');
}
public function testSetValueWithDmyArray() {
$f = new DatetimeField('Datetime', 'Datetime');
$f->getDateField()->setConfig('dmyfields', true);
$f->setValue(array(
'date' => array('day' => 29, 'month' => 03, 'year' => 2003),
'time' => '11pm'
));
$this->assertEquals($f->dataValue(), '2003-03-29 23:00:00');
}
public function testValidate() {
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
$this->assertTrue($f->validate(new RequiredFields()));
$f = new DatetimeField('Datetime', 'Datetime', '2003-03-29');
$this->assertTrue($f->validate(new RequiredFields()));
$f = new DatetimeField('Datetime', 'Datetime', 'wrong');
$this->assertFalse($f->validate(new RequiredFields()));
}
public function testTimezone() {
$oldTz = date_default_timezone_get();
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere winter
$f = new DatetimeField('Datetime', 'Datetime', '2003-12-24 23:59:59');
$f->setConfig('usertimezone', 'Pacific/Auckland');
$this->assertEquals('25/12/2003 11:59:59', $f->Value(),
'User value is formatted, and in user timezone');
$this->assertEquals('25/12/2003', $f->getDateField()->Value());
$this->assertEquals('11:59:59', $f->getTimeField()->Value());
$this->assertEquals('2003-12-24 23:59:59', $f->dataValue(),
'Data value is unformatted, and in server timezone');
date_default_timezone_set($oldTz);
}
public function testTimezoneFromFormSubmission() {
$oldTz = date_default_timezone_get();
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
$f = new DatetimeField('Datetime', 'Datetime');
$f->setConfig('usertimezone', 'Pacific/Auckland'); // should be overridden by form submission
$f->setValue(array(
// pass in default format, at user time (Moscow)
'date' => '24/06/2003',
'time' => '23:59:59',
'timezone' => 'Europe/Moscow'
));
$this->assertEquals('24/06/2003 23:59:59', $f->Value(), 'View composite value matches user timezone');
$this->assertEquals('24/06/2003', $f->getDateField()->Value(), 'View date part matches user timezone');
$this->assertEquals('23:59:59', $f->getTimeField()->Value(), 'View time part matches user timezone');
// 2h difference to Moscow
$this->assertEquals('2003-06-24 21:59:59', $f->dataValue(), 'Data value matches server timezone');
date_default_timezone_set($oldTz);
}
public function testTimezoneFromConfig() {
$oldTz = date_default_timezone_get();
date_default_timezone_set('Europe/Berlin');
// Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
$f = new DatetimeField('Datetime', 'Datetime');
$f->setConfig('usertimezone', 'Europe/Moscow');
$f->setValue(array(
// pass in default format, at user time (Moscow)
'date' => '24/06/2003',
'time' => '23:59:59',
));
$this->assertEquals('2003-06-24 21:59:59', $f->dataValue(), 'Data value matches server timezone');
date_default_timezone_set($oldTz);
}
public function testSetDateField() {
$form = $this->getMockForm();
$field = new DatetimeField('Datetime', 'Datetime');
$field->setForm($form);
$field->setValue(array(
'date' => '24/06/2003',
'time' => '23:59:59',
));
$dateField = new DateField('Datetime[date]');
$field->setDateField($dateField);
$this->assertEquals(
$dateField->getForm(),
$form,
'Sets form on new field'
);
$this->assertEquals(
'2003-06-24',
$dateField->dataValue(),
'Sets existing value on new field'
);
}
public function testSetTimeField() {
$form = $this->getMockForm();
$field = new DatetimeField('Datetime', 'Datetime');
$field->setForm($form);
$field->setValue(array(
'date' => '24/06/2003',
'time' => '23:59:59',
));
$timeField = new TimeField('Datetime[time]');
$field->setTimeField($timeField);
$this->assertEquals(
$timeField->getForm(),
$form,
'Sets form on new field'
);
$this->assertEquals(
'23:59:59',
$timeField->dataValue(),
'Sets existing value on new field'
);
}
protected function getMockForm() {
return new Form(
new Controller(),
'Form',
new FieldList(),
new FieldList(
new FormAction('doSubmit')
)
);
}
}
/**
* @package framework
* @subpackage tests
*/
class DatetimeFieldTest_Model extends DataObject implements TestOnly {
private static $db = array(
'MyDatetime' => 'SS_Datetime'
);
}