3 namespace Drupal\Tests\datetime\Kernel;
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\Tests\field\Kernel\FieldKernelTestBase;
11 use Drupal\field\Entity\FieldStorageConfig;
14 * Tests the new entity API for the date field type.
18 class DateTimeItemTest extends FieldKernelTestBase {
21 * A field storage to use in this test class.
23 * @var \Drupal\field\Entity\FieldStorageConfig
25 protected $fieldStorage;
28 * The field used in this test class.
30 * @var \Drupal\field\Entity\FieldConfig
39 public static $modules = ['datetime'];
41 protected function setUp() {
44 // Create a field with settings to validate.
45 $this->fieldStorage = FieldStorageConfig::create([
46 'field_name' => 'field_datetime',
48 'entity_type' => 'entity_test',
49 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
51 $this->fieldStorage->save();
52 $this->field = FieldConfig::create([
53 'field_storage' => $this->fieldStorage,
54 'bundle' => 'entity_test',
56 'default_value' => 'blank',
63 * Tests using entity fields of the datetime field type.
65 public function testDateTime() {
66 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
67 $this->fieldStorage->save();
69 // Verify entity creation.
70 $entity = EntityTest::create();
71 $value = '2014-01-01T20:00:00';
72 $entity->field_datetime = $value;
73 $entity->name->value = $this->randomMachineName();
74 $this->entityValidateAndSave($entity);
76 // Verify entity has been created properly.
78 $entity = EntityTest::load($id);
79 $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
80 $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
81 $this->assertEqual($entity->field_datetime->value, $value);
82 $this->assertEqual($entity->field_datetime[0]->value, $value);
83 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
85 // Verify changing the date value.
86 $new_value = '2016-11-04T00:21:00';
87 $entity->field_datetime->value = $new_value;
88 $this->assertEqual($entity->field_datetime->value, $new_value);
89 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
91 // Read changed entity and assert changed values.
92 $this->entityValidateAndSave($entity);
93 $entity = EntityTest::load($id);
94 $this->assertEqual($entity->field_datetime->value, $new_value);
95 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
97 // Test the generateSampleValue() method.
98 $entity = EntityTest::create();
99 $entity->field_datetime->generateSampleItems();
100 $this->entityValidateAndSave($entity);
104 * Tests using entity fields of the date field type.
106 public function testDateOnly() {
107 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
108 $this->fieldStorage->save();
110 // Verify entity creation.
111 $entity = EntityTest::create();
112 $value = '2014-01-01';
113 $entity->field_datetime = $value;
114 $entity->name->value = $this->randomMachineName();
115 $this->entityValidateAndSave($entity);
117 // Verify entity has been created properly.
119 $entity = EntityTest::load($id);
120 $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
121 $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
122 $this->assertEqual($entity->field_datetime->value, $value);
123 $this->assertEqual($entity->field_datetime[0]->value, $value);
124 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
126 // Verify changing the date value.
127 $new_value = '2016-11-04';
128 $entity->field_datetime->value = $new_value;
129 $this->assertEqual($entity->field_datetime->value, $new_value);
130 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
132 // Read changed entity and assert changed values.
133 $this->entityValidateAndSave($entity);
134 $entity = EntityTest::load($id);
135 $this->assertEqual($entity->field_datetime->value, $new_value);
136 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
138 // Test the generateSampleValue() method.
139 $entity = EntityTest::create();
140 $entity->field_datetime->generateSampleItems();
141 $this->entityValidateAndSave($entity);
145 * Tests DateTimeItem::setValue().
147 public function testSetValue() {
148 // Test a date+time field.
149 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
150 $this->fieldStorage->save();
152 // Test DateTimeItem::setValue() using string.
153 $entity = EntityTest::create();
154 $value = '2014-01-01T20:00:00';
155 $entity->get('field_datetime')->set(0, $value);
156 $this->entityValidateAndSave($entity);
157 // Load the entity and ensure the field was saved correctly.
159 $entity = EntityTest::load($id);
160 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
161 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
163 // Test DateTimeItem::setValue() using property array.
164 $entity = EntityTest::create();
165 $value = '2014-01-01T20:00:00';
166 $entity->set('field_datetime', $value);
167 $this->entityValidateAndSave($entity);
168 // Load the entity and ensure the field was saved correctly.
170 $entity = EntityTest::load($id);
171 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
172 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
174 // Test a date-only field.
175 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
176 $this->fieldStorage->save();
178 // Test DateTimeItem::setValue() using string.
179 $entity = EntityTest::create();
180 $value = '2014-01-01';
181 $entity->get('field_datetime')->set(0, $value);
182 $this->entityValidateAndSave($entity);
183 // Load the entity and ensure the field was saved correctly.
185 $entity = EntityTest::load($id);
186 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
187 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
189 // Test DateTimeItem::setValue() using property array.
190 $entity = EntityTest::create();
191 $value = '2014-01-01';
192 $entity->set('field_datetime', $value);
193 $this->entityValidateAndSave($entity);
194 // Load the entity and ensure the field was saved correctly.
196 $entity = EntityTest::load($id);
197 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
198 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
202 * Tests setting the value of the DateTimeItem directly.
204 public function testSetValueProperty() {
205 // Test Date::setValue() with a date+time field.
206 // Test a date+time field.
207 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
208 $this->fieldStorage->save();
209 $entity = EntityTest::create();
210 $value = '2014-01-01T20:00:00';
212 $entity->set('field_datetime', $value);
213 $this->entityValidateAndSave($entity);
214 // Load the entity and ensure the field was saved correctly.
216 $entity = EntityTest::load($id);
217 $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
218 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
220 // Test Date::setValue() with a date-only field.
221 // Test a date+time field.
222 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
223 $this->fieldStorage->save();
224 $entity = EntityTest::create();
225 $value = '2014-01-01';
227 $entity->set('field_datetime', $value);
228 $this->entityValidateAndSave($entity);
229 // Load the entity and ensure the field was saved correctly.
231 $entity = EntityTest::load($id);
232 $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
233 $this->assertEquals(DATETIME_STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
237 * Tests the constraint validations for fields with date and time.
239 * @dataProvider datetimeValidationProvider
241 public function testDatetimeValidation($value) {
242 $this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
244 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
245 $this->fieldStorage->save();
246 $entity = EntityTest::create();
248 $entity->set('field_datetime', $value);
249 $this->entityValidateAndSave($entity);
253 * Provider for testDatetimeValidation().
255 public function datetimeValidationProvider() {
257 // Valid ISO 8601 dates, but unsupported by DateTimeItem.
258 ['2014-01-01T20:00:00Z'],
259 ['2014-01-01T20:00:00+04:00'],
260 ['2014-01-01T20:00:00+0400'],
261 ['2014-01-01T20:00:00+04'],
262 ['2014-01-01T20:00:00.123'],
263 ['2014-01-01T200000'],
266 ['20140101T20:00:00'],
267 ['2014-01T20:00:00'],
268 ['2014-001T20:00:00'],
269 ['2014001T20:00:00'],
270 // Valid date strings, but unsupported by DateTimeItem.
271 ['2016-11-03 20:52:00'],
272 ['Thu, 03 Nov 2014 20:52:00 -0400'],
273 ['Thursday, November 3, 2016 - 20:52'],
274 ['Thu, 11/03/2016 - 20:52'],
275 ['11/03/2016 - 20:52'],
276 // Invalid date strings.
277 ['YYYY-01-01T20:00:00'],
278 ['2014-MM-01T20:00:00'],
279 ['2014-01-DDT20:00:00'],
280 ['2014-01-01Thh:00:00'],
281 ['2014-01-01T20:mm:00'],
282 ['2014-01-01T20:00:ss'],
284 ['2014-13-13T20:00:00'],
285 ['2014-01-55T20:00:00'],
286 ['2014-01-01T25:00:00'],
287 ['2014-01-01T00:70:00'],
288 ['2014-01-01T00:00:70'],
289 // Proper format for different field setting.
292 [['2014', '01', '01', '00', '00', '00']],
297 * Tests the constraint validations for fields with date only.
299 * @dataProvider dateonlyValidationProvider
301 public function testDateonlyValidation($value) {
302 $this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
304 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
305 $this->fieldStorage->save();
306 $entity = EntityTest::create();
308 $entity->set('field_datetime', $value);
309 $this->entityValidateAndSave($entity);
313 * Provider for testDatetimeValidation().
315 public function dateonlyValidationProvider() {
317 // Valid date strings, but unsupported by DateTimeItem.
318 ['Thu, 03 Nov 2014'],
319 ['Thursday, November 3, 2016'],
322 // Invalid date strings.
329 // Proper format for different field setting.
330 ['2014-01-01T20:00:00'],
332 [['2014', '01', '01']],