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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use PHPUnit\Framework\AssertionFailedError;
/**
* Tests the new entity API for the date field type.
*
* @group datetime
*/
class DateTimeItemTest extends FieldKernelTestBase {
/**
* A field storage to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The field used in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* {@inheritdoc}
*/
protected static $modules = ['datetime'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_datetime',
'type' => 'datetime',
'entity_type' => 'entity_test',
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
]);
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'default_value' => 'blank',
],
]);
$this->field->save();
}
/**
* Tests using entity fields of the datetime field type.
*/
public function testDateTime(): void {
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
// Verify entity creation.
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->field_datetime = $value;
$entity->name->value = $this->randomMachineName();
$this->entityValidateAndSave($entity);
// Verify entity has been created properly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->field_datetime);
$this->assertInstanceOf(FieldItemInterface::class, $entity->field_datetime[0]);
$this->assertEquals($value, $entity->field_datetime->value);
$this->assertEquals($value, $entity->field_datetime[0]->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Verify changing the date value.
$new_value = '2016-11-04T00:21:00';
$entity->field_datetime->value = $new_value;
$this->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Read changed entity and assert changed values.
$this->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_datetime->generateSampleItems();
$this->entityValidateAndSave($entity);
}
/**
* Tests using entity fields of the date field type.
*/
public function testDateOnly(): void {
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
// Verify entity creation.
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->field_datetime = $value;
$entity->name->value = $this->randomMachineName();
$this->entityValidateAndSave($entity);
// Verify entity has been created properly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->field_datetime);
$this->assertInstanceOf(FieldItemInterface::class, $entity->field_datetime[0]);
$this->assertEquals($value, $entity->field_datetime->value);
$this->assertEquals($value, $entity->field_datetime[0]->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Verify changing the date value.
$new_value = '2016-11-04';
$entity->field_datetime->value = $new_value;
$this->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Read changed entity and assert changed values.
$this->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_datetime->generateSampleItems();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$this->entityValidateAndSave($entity);
}
/**
* Tests DateTimeItem::setValue().
*/
public function testSetValue(): void {
// Test a date+time field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
// Test DateTimeItem::setValue() using string.
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->get('field_datetime')->set(0, $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, 'DateTimeItem::setValue() works with string value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test DateTimeItem::setValue() using property array.
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, 'DateTimeItem::setValue() works with array value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test a date-only field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
// Test DateTimeItem::setValue() using string.
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->get('field_datetime')->set(0, $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, 'DateTimeItem::setValue() works with string value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test DateTimeItem::setValue() using property array.
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, 'DateTimeItem::setValue() works with array value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
}
/**
* Tests setting the value of the DateTimeItem directly.
*/
public function testSetValueProperty(): void {
// Test Date::setValue() with a date+time field.
// Test a date+time field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, '"Value" property can be set directly.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test Date::setValue() with a date-only field.
// Test a date+time field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, '"Value" property can be set directly.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
}
/**
* Tests the constraint validations for fields with date and time.
*
* @dataProvider datetimeValidationProvider
*/
public function testDatetimeValidation($value): void {
$this->expectException(AssertionFailedError::class);
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
$entity = EntityTest::create();
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
}
/**
* Provider for testDatetimeValidation().
*/
public static function datetimeValidationProvider() {
return [
// Valid ISO 8601 dates, but unsupported by DateTimeItem.
['2014-01-01T20:00:00Z'],
['2014-01-01T20:00:00+04:00'],
['2014-01-01T20:00:00+0400'],
['2014-01-01T20:00:00+04'],
['2014-01-01T20:00:00.123'],
['2014-01-01T200000'],
['2014-01-01T2000'],
['2014-01-01T20'],
['20140101T20:00:00'],
['2014-01T20:00:00'],
['2014-001T20:00:00'],
['2014001T20:00:00'],
// Valid date strings, but unsupported by DateTimeItem.
['2016-11-03 20:52:00'],
['Thu, 03 Nov 2014 20:52:00 -0400'],
['Thursday, November 3, 2016 - 20:52'],
['Thu, 11/03/2016 - 20:52'],
['11/03/2016 - 20:52'],
// Invalid date strings.
['YYYY-01-01T20:00:00'],
['2014-MM-01T20:00:00'],
['2014-01-DDT20:00:00'],
['2014-01-01Thh:00:00'],
['2014-01-01T20:mm:00'],
['2014-01-01T20:00:ss'],
// Invalid dates.
['2014-13-13T20:00:00'],
['2014-01-55T20:00:00'],
['2014-01-01T25:00:00'],
['2014-01-01T00:70:00'],
['2014-01-01T00:00:70'],
// Proper format for different field setting.
['2014-01-01'],
// Wrong input type.
[['2014', '01', '01', '00', '00', '00']],
];
}
/**
* Tests the constraint validations for fields with date only.
*
* @dataProvider dateOnlyValidationProvider
*/
public function testDateOnlyValidation($value): void {
$this->expectException(AssertionFailedError::class);
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
$entity = EntityTest::create();
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
}
/**
* Provider for testDatetimeValidation().
*/
public static function dateOnlyValidationProvider() {
return [
// Valid date strings, but unsupported by DateTimeItem.
['Thu, 03 Nov 2014'],
['Thursday, November 3, 2016'],
['Thu, 11/03/2016'],
['11/03/2016'],
// Invalid date strings.
['YYYY-01-01'],
['2014-MM-01'],
['2014-01-DD'],
// Invalid dates.
['2014-13-01'],
['2014-01-55'],
// Proper format for different field setting.
['2014-01-01T20:00:00'],
// Wrong input type.
[['2014', '01', '01']],
];
}
}
|