PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/datetime/tests/src/Kernel/DateTimeItemTest.php

http://github.com/drupal/drupal
PHP | 353 lines | 228 code | 39 blank | 86 comment | 0 complexity | af05d96f0dc9c8df640ec49006de782c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Tests\datetime\Kernel;
  3. use Drupal\Core\Field\FieldItemListInterface;
  4. use Drupal\Core\Field\FieldItemInterface;
  5. use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
  6. use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
  7. use Drupal\entity_test\Entity\EntityTest;
  8. use Drupal\field\Entity\FieldConfig;
  9. use Drupal\Tests\field\Kernel\FieldKernelTestBase;
  10. use Drupal\field\Entity\FieldStorageConfig;
  11. use PHPUnit\Framework\AssertionFailedError;
  12. /**
  13. * Tests the new entity API for the date field type.
  14. *
  15. * @group datetime
  16. */
  17. class DateTimeItemTest extends FieldKernelTestBase {
  18. /**
  19. * A field storage to use in this test class.
  20. *
  21. * @var \Drupal\field\Entity\FieldStorageConfig
  22. */
  23. protected $fieldStorage;
  24. /**
  25. * The field used in this test class.
  26. *
  27. * @var \Drupal\field\Entity\FieldConfig
  28. */
  29. protected $field;
  30. /**
  31. * Modules to enable.
  32. *
  33. * @var array
  34. */
  35. public static $modules = ['datetime'];
  36. protected function setUp() {
  37. parent::setUp();
  38. // Create a field with settings to validate.
  39. $this->fieldStorage = FieldStorageConfig::create([
  40. 'field_name' => 'field_datetime',
  41. 'type' => 'datetime',
  42. 'entity_type' => 'entity_test',
  43. 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
  44. ]);
  45. $this->fieldStorage->save();
  46. $this->field = FieldConfig::create([
  47. 'field_storage' => $this->fieldStorage,
  48. 'bundle' => 'entity_test',
  49. 'settings' => [
  50. 'default_value' => 'blank',
  51. ],
  52. ]);
  53. $this->field->save();
  54. }
  55. /**
  56. * Tests using entity fields of the datetime field type.
  57. */
  58. public function testDateTime() {
  59. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
  60. $this->fieldStorage->save();
  61. // Verify entity creation.
  62. $entity = EntityTest::create();
  63. $value = '2014-01-01T20:00:00';
  64. $entity->field_datetime = $value;
  65. $entity->name->value = $this->randomMachineName();
  66. $this->entityValidateAndSave($entity);
  67. // Verify entity has been created properly.
  68. $id = $entity->id();
  69. $entity = EntityTest::load($id);
  70. $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
  71. $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
  72. $this->assertEqual($entity->field_datetime->value, $value);
  73. $this->assertEqual($entity->field_datetime[0]->value, $value);
  74. $this->assertEqual(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
  75. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  76. // Verify changing the date value.
  77. $new_value = '2016-11-04T00:21:00';
  78. $entity->field_datetime->value = $new_value;
  79. $this->assertEqual($entity->field_datetime->value, $new_value);
  80. $this->assertEqual(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
  81. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  82. // Read changed entity and assert changed values.
  83. $this->entityValidateAndSave($entity);
  84. $entity = EntityTest::load($id);
  85. $this->assertEqual($entity->field_datetime->value, $new_value);
  86. $this->assertEqual(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
  87. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  88. // Test the generateSampleValue() method.
  89. $entity = EntityTest::create();
  90. $entity->field_datetime->generateSampleItems();
  91. $this->entityValidateAndSave($entity);
  92. }
  93. /**
  94. * Tests using entity fields of the date field type.
  95. */
  96. public function testDateOnly() {
  97. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  98. $this->fieldStorage->save();
  99. // Verify entity creation.
  100. $entity = EntityTest::create();
  101. $value = '2014-01-01';
  102. $entity->field_datetime = $value;
  103. $entity->name->value = $this->randomMachineName();
  104. $this->entityValidateAndSave($entity);
  105. // Verify entity has been created properly.
  106. $id = $entity->id();
  107. $entity = EntityTest::load($id);
  108. $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
  109. $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
  110. $this->assertEqual($entity->field_datetime->value, $value);
  111. $this->assertEqual($entity->field_datetime[0]->value, $value);
  112. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  113. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  114. $entity->field_datetime->date->setDefaultDateTime();
  115. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  116. // Verify changing the date value.
  117. $new_value = '2016-11-04';
  118. $entity->field_datetime->value = $new_value;
  119. $this->assertEqual($entity->field_datetime->value, $new_value);
  120. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  121. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  122. $entity->field_datetime->date->setDefaultDateTime();
  123. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  124. // Read changed entity and assert changed values.
  125. $this->entityValidateAndSave($entity);
  126. $entity = EntityTest::load($id);
  127. $this->assertEqual($entity->field_datetime->value, $new_value);
  128. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  129. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  130. $entity->field_datetime->date->setDefaultDateTime();
  131. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  132. // Test the generateSampleValue() method.
  133. $entity = EntityTest::create();
  134. $entity->field_datetime->generateSampleItems();
  135. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  136. $entity->field_datetime->date->setDefaultDateTime();
  137. $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
  138. $this->entityValidateAndSave($entity);
  139. }
  140. /**
  141. * Tests DateTimeItem::setValue().
  142. */
  143. public function testSetValue() {
  144. // Test a date+time field.
  145. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
  146. $this->fieldStorage->save();
  147. // Test DateTimeItem::setValue() using string.
  148. $entity = EntityTest::create();
  149. $value = '2014-01-01T20:00:00';
  150. $entity->get('field_datetime')->set(0, $value);
  151. $this->entityValidateAndSave($entity);
  152. // Load the entity and ensure the field was saved correctly.
  153. $id = $entity->id();
  154. $entity = EntityTest::load($id);
  155. $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
  156. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  157. // Test DateTimeItem::setValue() using property array.
  158. $entity = EntityTest::create();
  159. $value = '2014-01-01T20:00:00';
  160. $entity->set('field_datetime', $value);
  161. $this->entityValidateAndSave($entity);
  162. // Load the entity and ensure the field was saved correctly.
  163. $id = $entity->id();
  164. $entity = EntityTest::load($id);
  165. $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
  166. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  167. // Test a date-only field.
  168. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  169. $this->fieldStorage->save();
  170. // Test DateTimeItem::setValue() using string.
  171. $entity = EntityTest::create();
  172. $value = '2014-01-01';
  173. $entity->get('field_datetime')->set(0, $value);
  174. $this->entityValidateAndSave($entity);
  175. // Load the entity and ensure the field was saved correctly.
  176. $id = $entity->id();
  177. $entity = EntityTest::load($id);
  178. $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
  179. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  180. // Test DateTimeItem::setValue() using property array.
  181. $entity = EntityTest::create();
  182. $value = '2014-01-01';
  183. $entity->set('field_datetime', $value);
  184. $this->entityValidateAndSave($entity);
  185. // Load the entity and ensure the field was saved correctly.
  186. $id = $entity->id();
  187. $entity = EntityTest::load($id);
  188. $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
  189. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  190. }
  191. /**
  192. * Tests setting the value of the DateTimeItem directly.
  193. */
  194. public function testSetValueProperty() {
  195. // Test Date::setValue() with a date+time field.
  196. // Test a date+time field.
  197. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
  198. $this->fieldStorage->save();
  199. $entity = EntityTest::create();
  200. $value = '2014-01-01T20:00:00';
  201. $entity->set('field_datetime', $value);
  202. $this->entityValidateAndSave($entity);
  203. // Load the entity and ensure the field was saved correctly.
  204. $id = $entity->id();
  205. $entity = EntityTest::load($id);
  206. $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
  207. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  208. // Test Date::setValue() with a date-only field.
  209. // Test a date+time field.
  210. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  211. $this->fieldStorage->save();
  212. $entity = EntityTest::create();
  213. $value = '2014-01-01';
  214. $entity->set('field_datetime', $value);
  215. $this->entityValidateAndSave($entity);
  216. // Load the entity and ensure the field was saved correctly.
  217. $id = $entity->id();
  218. $entity = EntityTest::load($id);
  219. $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
  220. $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
  221. }
  222. /**
  223. * Tests the constraint validations for fields with date and time.
  224. *
  225. * @dataProvider datetimeValidationProvider
  226. */
  227. public function testDatetimeValidation($value) {
  228. $this->expectException(AssertionFailedError::class);
  229. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
  230. $this->fieldStorage->save();
  231. $entity = EntityTest::create();
  232. $entity->set('field_datetime', $value);
  233. $this->entityValidateAndSave($entity);
  234. }
  235. /**
  236. * Provider for testDatetimeValidation().
  237. */
  238. public function datetimeValidationProvider() {
  239. return [
  240. // Valid ISO 8601 dates, but unsupported by DateTimeItem.
  241. ['2014-01-01T20:00:00Z'],
  242. ['2014-01-01T20:00:00+04:00'],
  243. ['2014-01-01T20:00:00+0400'],
  244. ['2014-01-01T20:00:00+04'],
  245. ['2014-01-01T20:00:00.123'],
  246. ['2014-01-01T200000'],
  247. ['2014-01-01T2000'],
  248. ['2014-01-01T20'],
  249. ['20140101T20:00:00'],
  250. ['2014-01T20:00:00'],
  251. ['2014-001T20:00:00'],
  252. ['2014001T20:00:00'],
  253. // Valid date strings, but unsupported by DateTimeItem.
  254. ['2016-11-03 20:52:00'],
  255. ['Thu, 03 Nov 2014 20:52:00 -0400'],
  256. ['Thursday, November 3, 2016 - 20:52'],
  257. ['Thu, 11/03/2016 - 20:52'],
  258. ['11/03/2016 - 20:52'],
  259. // Invalid date strings.
  260. ['YYYY-01-01T20:00:00'],
  261. ['2014-MM-01T20:00:00'],
  262. ['2014-01-DDT20:00:00'],
  263. ['2014-01-01Thh:00:00'],
  264. ['2014-01-01T20:mm:00'],
  265. ['2014-01-01T20:00:ss'],
  266. // Invalid dates.
  267. ['2014-13-13T20:00:00'],
  268. ['2014-01-55T20:00:00'],
  269. ['2014-01-01T25:00:00'],
  270. ['2014-01-01T00:70:00'],
  271. ['2014-01-01T00:00:70'],
  272. // Proper format for different field setting.
  273. ['2014-01-01'],
  274. // Wrong input type.
  275. [['2014', '01', '01', '00', '00', '00']],
  276. ];
  277. }
  278. /**
  279. * Tests the constraint validations for fields with date only.
  280. *
  281. * @dataProvider dateonlyValidationProvider
  282. */
  283. public function testDateonlyValidation($value) {
  284. $this->expectException(AssertionFailedError::class);
  285. $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
  286. $this->fieldStorage->save();
  287. $entity = EntityTest::create();
  288. $entity->set('field_datetime', $value);
  289. $this->entityValidateAndSave($entity);
  290. }
  291. /**
  292. * Provider for testDatetimeValidation().
  293. */
  294. public function dateonlyValidationProvider() {
  295. return [
  296. // Valid date strings, but unsupported by DateTimeItem.
  297. ['Thu, 03 Nov 2014'],
  298. ['Thursday, November 3, 2016'],
  299. ['Thu, 11/03/2016'],
  300. ['11/03/2016'],
  301. // Invalid date strings.
  302. ['YYYY-01-01'],
  303. ['2014-MM-01'],
  304. ['2014-01-DD'],
  305. // Invalid dates.
  306. ['2014-13-01'],
  307. ['2014-01-55'],
  308. // Proper format for different field setting.
  309. ['2014-01-01T20:00:00'],
  310. // Wrong input type.
  311. [['2014', '01', '01']],
  312. ];
  313. }
  314. }