Completed
Pull Request — master (#6766)
by Ingo
08:55
created

DatetimeFieldTest::testFormSaveIntoLocalised()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 26
rs 8.8571
1
<?php
2
3
namespace SilverStripe\Forms\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\DatetimeField;
8
use SilverStripe\Forms\RequiredFields;
9
use SilverStripe\Forms\DateField;
10
use SilverStripe\Forms\Tests\DatetimeFieldTest\Model;
11
use SilverStripe\Forms\TimeField;
12
use SilverStripe\Forms\FieldList;
13
use SilverStripe\Forms\FormAction;
14
use SilverStripe\Forms\Form;
15
use SilverStripe\i18n\i18n;
16
17
class DatetimeFieldTest extends SapphireTest
18
{
19
    protected $timezone = null;
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
        i18n::set_locale('en_NZ');
25
        $this->timezone = date_default_timezone_get();
26
    }
27
28
    protected function tearDown()
29
    {
30
        date_default_timezone_set($this->timezone);
31
        parent::tearDown(); // TODO: Change the autogenerated stub
32
    }
33
34
    public function testFormSaveInto()
35
    {
36
        $dateTimeField = new DatetimeField('MyDatetime');
37
        $form = $this->getMockForm();
38
        $form->Fields()->push($dateTimeField);
39
40
        $dateTimeField->setSubmittedValue([
41
            'date' => '2003-03-29',
42
            'time' => '23:59:38'
43
        ]);
44
        $validator = new RequiredFields();
45
        $this->assertTrue($dateTimeField->validate($validator));
46
        $m = new Model();
47
        $form->saveInto($m);
48
        $this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
49
    }
50
51
    public function testFormSaveIntoLocalised()
52
    {
53
        $dateTimeField = new DatetimeField('MyDatetime');
54
55
        $dateTimeField->getDateField()
56
            ->setHTML5(false)
57
            ->setLocale('en_NZ');
58
59
        $dateTimeField->getTimeField()
60
            ->setHTML5(false)
61
            ->setLocale('en_NZ');
62
63
        $form = $this->getMockForm();
64
        $form->Fields()->push($dateTimeField);
65
66
        // en_NZ standard format
67
        $dateTimeField->setSubmittedValue([
68
            'date' => '29/03/2003',
69
            'time' => '11:59:38 pm'
70
        ]);
71
        $validator = new RequiredFields();
72
        $this->assertTrue($dateTimeField->validate($validator));
73
        $m = new Model();
74
        $form->saveInto($m);
75
        $this->assertEquals('2003-03-29 23:59:38', $m->MyDatetime);
76
    }
77
78
    public function testDataValue()
79
    {
80
        $f = new DatetimeField('Datetime');
81
        $this->assertEquals(null, $f->dataValue(), 'Empty field');
82
83
        $f = new DatetimeField('Datetime', null, '2003-03-29 23:59:38');
84
        $this->assertEquals('2003-03-29 23:59:38', $f->dataValue(), 'From date/time string');
85
    }
86
87
    public function testConstructorWithoutArgs()
88
    {
89
        $f = new DatetimeField('Datetime');
90
        $this->assertEquals($f->dataValue(), null);
91
    }
92
93
    // /**
94
    //  * @expectedException InvalidArgumentException
95
    //  */
96
    // public function testConstructorWithLocalizedDateString() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
    // 	$f = new DatetimeField('Datetime', 'Datetime', '29/03/2003 23:59:38');
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
    // }
99
100
    public function testConstructorWithIsoDate()
101
    {
102
        // used by Form->loadDataFrom()
103
        $f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
104
        $this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
105
    }
106
107
    // /**
108
    //  * @expectedException InvalidArgumentException
109
    //  */
110
    // public function testSetValueWithDateString() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
    // 	$f = new DatetimeField('Datetime', 'Datetime');
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
112
    // 	$f->setValue('29/03/2003');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
113
    // }
114
115
    public function testSetValueWithDateTimeString()
116
    {
117
        $f = new DatetimeField('Datetime', 'Datetime');
118
        $f->setValue('2003-03-29 23:59:38');
119
        $this->assertEquals($f->dataValue(), '2003-03-29 23:59:38');
120
    }
121
122
    public function testSetValueWithArray()
123
    {
124
        $datetimeField = new DatetimeField('Datetime', 'Datetime');
125
        $datetimeField->setSubmittedValue([
126
            'date' => '2003-03-29',
127
            'time' => '23:00:00'
128
        ]);
129
        $this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
130
    }
131
132
    public function testSetValueWithArrayLocalised()
133
    {
134
        $datetimeField = new DatetimeField('Datetime', 'Datetime');
135
136
        $datetimeField->getDateField()
137
            ->setHTML5(false)
138
            ->setLocale('en_NZ');
139
140
        $datetimeField->getTimeField()
141
            ->setHTML5(false)
142
            ->setLocale('en_NZ');
143
144
        // Values can only be localized (= non-ISO) in array notation
145
        $datetimeField->setSubmittedValue([
146
            'date' => '29/03/2003',
147
            'time' => '11:00:00 pm'
148
        ]);
149
        $this->assertEquals($datetimeField->dataValue(), '2003-03-29 23:00:00');
150
    }
151
152
    public function testValidate()
153
    {
154
        $f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 23:59:38');
155
        $this->assertTrue($f->validate(new RequiredFields()));
156
157
        $f = new DatetimeField('Datetime', 'Datetime', '2003-03-29 00:00:00');
158
        $this->assertTrue($f->validate(new RequiredFields()));
159
160
        $f = new DatetimeField('Datetime', 'Datetime', 'wrong');
161
        $this->assertFalse($f->validate(new RequiredFields()));
162
    }
163
164
    public function testTimezoneSetLocalised()
165
    {
166
        date_default_timezone_set('Europe/Berlin');
167
        // Berlin and Auckland have 12h time difference in northern hemisphere winter
168
        $datetimeField = new DatetimeField('Datetime', 'Datetime');
169
170
        $datetimeField->getDateField()
171
            ->setHTML5(false)
172
            ->setLocale('en_NZ');
173
174
        $datetimeField->getTimeField()
175
            ->setHTML5(false)
176
            ->setLocale('en_NZ');
177
178
        $datetimeField->setTimezone('Pacific/Auckland');
179
        $datetimeField->setValue('2003-12-24 23:59:59');
180
        $this->assertEquals(
181
            '25/12/2003 11:59:59 AM',
182
            $datetimeField->Value(),
183
            'User value is formatted, and in user timezone'
184
        );
185
        $this->assertEquals('25/12/2003', $datetimeField->getDateField()->Value());
186
        $this->assertEquals('11:59:59 AM', $datetimeField->getTimeField()->Value());
187
        $this->assertEquals(
188
            '2003-12-24 23:59:59',
189
            $datetimeField->dataValue(),
190
            'Data value is unformatted, and in server timezone'
191
        );
192
    }
193
194
    public function testTimezoneFromConfigLocalised()
195
    {
196
        date_default_timezone_set('Europe/Berlin');
197
        // Berlin and Auckland have 12h time difference in northern hemisphere summer, but Berlin and Moscow only 2h.
198
        $datetimeField = new DatetimeField('Datetime', 'Datetime');
199
200
        $datetimeField->getDateField()
201
            ->setHTML5(false)
202
            ->setLocale('en_NZ');
203
204
        $datetimeField->getTimeField()
205
            ->setHTML5(false)
206
            ->setLocale('en_NZ');
207
208
        $datetimeField->setTimezone('Europe/Moscow');
209
        $datetimeField->setSubmittedValue([
210
            // pass in default format, at user time (Moscow)
211
            'date' => '24/06/2003',
212
            'time' => '11:59:59 pm',
213
        ]);
214
        $this->assertTrue($datetimeField->validate(new RequiredFields()));
215
        $this->assertEquals('2003-06-24 21:59:59', $datetimeField->dataValue(), 'Data value matches server timezone');
216
    }
217
218
    public function testSetDateField()
219
    {
220
        $form = $this->getMockForm();
221
        $field = new DatetimeField('Datetime', 'Datetime');
222
        $field->setForm($form);
223
        $field->setSubmittedValue([
224
            'date' => '2003-06-24',
225
            'time' => '23:59:59',
226
        ]);
227
        $dateField = new DateField('Datetime[date]');
228
        $field->setDateField($dateField);
229
230
        $this->assertEquals(
231
            $dateField->getForm(),
232
            $form,
233
            'Sets form on new field'
234
        );
235
236
        $this->assertEquals(
237
            '2003-06-24',
238
            $dateField->dataValue(),
239
            'Sets existing value on new field'
240
        );
241
    }
242
243
    public function testSetTimeField()
244
    {
245
        $form = $this->getMockForm();
246
        $field = new DatetimeField('Datetime', 'Datetime');
247
        $field->setForm($form);
248
        $field->setSubmittedValue([
249
            'date' => '2003-06-24',
250
            'time' => '23:59:59',
251
        ]);
252
        $timeField = new TimeField('Datetime[time]');
253
        $field->setTimeField($timeField);
254
255
        $this->assertEquals(
256
            $timeField->getForm(),
257
            $form,
258
            'Sets form on new field'
259
        );
260
261
        $this->assertEquals(
262
            '23:59:59',
263
            $timeField->dataValue(),
264
            'Sets existing value on new field'
265
        );
266
    }
267
268
    public function testGetName()
269
    {
270
        $field = new DatetimeField('Datetime');
271
272
        $this->assertEquals('Datetime', $field->getName());
273
        $this->assertEquals('Datetime[date]', $field->getDateField()->getName());
274
        $this->assertEquals('Datetime[time]', $field->getTimeField()->getName());
275
    }
276
277
    public function testSetName()
278
    {
279
        $field = new DatetimeField('Datetime', 'Datetime');
280
        $field->setName('CustomDatetime');
281
        $this->assertEquals('CustomDatetime', $field->getName());
282
        $this->assertEquals('CustomDatetime[date]', $field->getDateField()->getName());
283
        $this->assertEquals('CustomDatetime[time]', $field->getTimeField()->getName());
284
    }
285
286
    protected function getMockForm()
287
    {
288
        /** @skipUpgrade */
289
        return new Form(
290
            new Controller(),
291
            'Form',
292
            new FieldList(),
293
            new FieldList(
294
                new FormAction('doSubmit')
295
            )
296
        );
297
    }
298
}
299