e28667f0725f86bb947228371d5f5d7be410404a
[yaffs-website] / web / core / modules / datetime / tests / src / Kernel / DateTimeItemTest.php
1 <?php
2
3 namespace Drupal\Tests\datetime\Kernel;
4
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;
12
13 /**
14  * Tests the new entity API for the date field type.
15  *
16  * @group datetime
17  */
18 class DateTimeItemTest extends FieldKernelTestBase {
19
20   /**
21    * A field storage to use in this test class.
22    *
23    * @var \Drupal\field\Entity\FieldStorageConfig
24    */
25   protected $fieldStorage;
26
27   /**
28    * The field used in this test class.
29    *
30    * @var \Drupal\field\Entity\FieldConfig
31    */
32   protected $field;
33
34   /**
35    * Modules to enable.
36    *
37    * @var array
38    */
39   public static $modules = ['datetime'];
40
41   protected function setUp() {
42     parent::setUp();
43
44     // Create a field with settings to validate.
45     $this->fieldStorage = FieldStorageConfig::create([
46       'field_name' => 'field_datetime',
47       'type' => 'datetime',
48       'entity_type' => 'entity_test',
49       'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
50     ]);
51     $this->fieldStorage->save();
52     $this->field = FieldConfig::create([
53       'field_storage' => $this->fieldStorage,
54       'bundle' => 'entity_test',
55       'settings' => [
56         'default_value' => 'blank',
57       ],
58     ]);
59     $this->field->save();
60   }
61
62   /**
63    * Tests using entity fields of the datetime field type.
64    */
65   public function testDateTime() {
66     $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
67     $this->fieldStorage->save();
68
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);
75
76     // Verify entity has been created properly.
77     $id = $entity->id();
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
84     // Verify changing the date value.
85     $new_value = '2016-11-04T00:21:00';
86     $entity->field_datetime->value = $new_value;
87     $this->assertEqual($entity->field_datetime->value, $new_value);
88
89     // Read changed entity and assert changed values.
90     $this->entityValidateAndSave($entity);
91     $entity = EntityTest::load($id);
92     $this->assertEqual($entity->field_datetime->value, $new_value);
93
94     // Test the generateSampleValue() method.
95     $entity = EntityTest::create();
96     $entity->field_datetime->generateSampleItems();
97     $this->entityValidateAndSave($entity);
98   }
99
100   /**
101    * Tests using entity fields of the date field type.
102    */
103   public function testDateOnly() {
104     $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
105     $this->fieldStorage->save();
106
107     // Verify entity creation.
108     $entity = EntityTest::create();
109     $value = '2014-01-01';
110     $entity->field_datetime = $value;
111     $entity->name->value = $this->randomMachineName();
112     $this->entityValidateAndSave($entity);
113
114     // Verify entity has been created properly.
115     $id = $entity->id();
116     $entity = EntityTest::load($id);
117     $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');
118     $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
119     $this->assertEqual($entity->field_datetime->value, $value);
120     $this->assertEqual($entity->field_datetime[0]->value, $value);
121
122     // Verify changing the date value.
123     $new_value = '2016-11-04';
124     $entity->field_datetime->value = $new_value;
125     $this->assertEqual($entity->field_datetime->value, $new_value);
126
127     // Read changed entity and assert changed values.
128     $this->entityValidateAndSave($entity);
129     $entity = EntityTest::load($id);
130     $this->assertEqual($entity->field_datetime->value, $new_value);
131
132     // Test the generateSampleValue() method.
133     $entity = EntityTest::create();
134     $entity->field_datetime->generateSampleItems();
135     $this->entityValidateAndSave($entity);
136   }
137
138   /**
139    * Tests DateTimeItem::setValue().
140    */
141   public function testSetValue() {
142     // Test a date+time field.
143     $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
144     $this->fieldStorage->save();
145
146     // Test DateTimeItem::setValue() using string.
147     $entity = EntityTest::create();
148     $value = '2014-01-01T20:00:00';
149     $entity->get('field_datetime')->set(0, $value);
150     $this->entityValidateAndSave($entity);
151     // Load the entity and ensure the field was saved correctly.
152     $id = $entity->id();
153     $entity = EntityTest::load($id);
154     $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
155
156     // Test DateTimeItem::setValue() using property array.
157     $entity = EntityTest::create();
158     $value = '2014-01-01T20:00:00';
159     $entity->set('field_datetime', $value);
160     $this->entityValidateAndSave($entity);
161     // Load the entity and ensure the field was saved correctly.
162     $id = $entity->id();
163     $entity = EntityTest::load($id);
164     $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
165
166     // Test a date-only field.
167     $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
168     $this->fieldStorage->save();
169
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
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   }
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
202     $entity->set('field_datetime', $value);
203     $this->entityValidateAndSave($entity);
204     // Load the entity and ensure the field was saved correctly.
205     $id = $entity->id();
206     $entity = EntityTest::load($id);
207     $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
208
209     // Test Date::setValue() with a date-only field.
210     // Test a date+time field.
211     $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
212     $this->fieldStorage->save();
213     $entity = EntityTest::create();
214     $value = '2014-01-01';
215
216     $entity->set('field_datetime', $value);
217     $this->entityValidateAndSave($entity);
218     // Load the entity and ensure the field was saved correctly.
219     $id = $entity->id();
220     $entity = EntityTest::load($id);
221     $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
222   }
223
224 }