Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\datetime\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Render\FormattableMarkup;
|
Chris@0
|
6 use Drupal\Core\Datetime\DrupalDateTime;
|
Chris@0
|
7 use Drupal\Core\Datetime\Entity\DateFormat;
|
Chris@14
|
8 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
Chris@0
|
9 use Drupal\entity_test\Entity\EntityTest;
|
Chris@0
|
10 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
11 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
12 use Drupal\node\Entity\Node;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Tests Datetime field functionality.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @group datetime
|
Chris@0
|
18 */
|
Chris@0
|
19 class DateTimeFieldTest extends DateTestBase {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The default display settings to use for the formatters.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $defaultSettings = ['timezone_override' => ''];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * {@inheritdoc}
|
Chris@0
|
30 */
|
Chris@0
|
31 protected function getTestFieldType() {
|
Chris@0
|
32 return 'datetime';
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Tests date field functionality.
|
Chris@0
|
37 */
|
Chris@0
|
38 public function testDateField() {
|
Chris@0
|
39 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
40
|
Chris@0
|
41 // Loop through defined timezones to test that date-only fields work at the
|
Chris@0
|
42 // extremes.
|
Chris@0
|
43 foreach (static::$timezones as $timezone) {
|
Chris@0
|
44
|
Chris@0
|
45 $this->setSiteTimezone($timezone);
|
Chris@0
|
46 $this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
|
Chris@0
|
47
|
Chris@0
|
48 // Display creation form.
|
Chris@0
|
49 $this->drupalGet('entity_test/add');
|
Chris@0
|
50 $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
|
Chris@0
|
51 $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class,"js-form-required")]', TRUE, 'Required markup found');
|
Chris@0
|
52 $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Time element not found.');
|
Chris@0
|
53 $this->assertFieldByXPath('//input[@aria-describedby="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA described-by found');
|
Chris@0
|
54 $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0-value--description"]', NULL, 'ARIA description found');
|
Chris@0
|
55
|
Chris@0
|
56 // Build up a date in the UTC timezone. Note that using this will also
|
Chris@0
|
57 // mimic the user in a different timezone simply entering '2012-12-31' via
|
Chris@0
|
58 // the UI.
|
Chris@0
|
59 $value = '2012-12-31 00:00:00';
|
Chris@14
|
60 $date = new DrupalDateTime($value, DateTimeItemInterface::STORAGE_TIMEZONE);
|
Chris@0
|
61
|
Chris@0
|
62 // Submit a valid date and ensure it is accepted.
|
Chris@0
|
63 $date_format = DateFormat::load('html_date')->getPattern();
|
Chris@0
|
64 $time_format = DateFormat::load('html_time')->getPattern();
|
Chris@0
|
65
|
Chris@0
|
66 $edit = [
|
Chris@0
|
67 "{$field_name}[0][value][date]" => $date->format($date_format),
|
Chris@0
|
68 ];
|
Chris@0
|
69 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
70 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
Chris@0
|
71 $id = $match[1];
|
Chris@0
|
72 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
Chris@0
|
73 $this->assertRaw($date->format($date_format));
|
Chris@0
|
74 $this->assertNoRaw($date->format($time_format));
|
Chris@0
|
75
|
Chris@0
|
76 // Verify the date doesn't change if using a timezone that is UTC+12 when
|
Chris@0
|
77 // the entity is edited through the form.
|
Chris@0
|
78 $entity = EntityTest::load($id);
|
Chris@0
|
79 $this->assertEqual('2012-12-31', $entity->{$field_name}->value);
|
Chris@0
|
80 $this->drupalGet('entity_test/manage/' . $id . '/edit');
|
Chris@0
|
81 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@0
|
82 $this->drupalGet('entity_test/manage/' . $id . '/edit');
|
Chris@0
|
83 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@0
|
84 $this->drupalGet('entity_test/manage/' . $id . '/edit');
|
Chris@0
|
85 $this->drupalPostForm(NULL, [], t('Save'));
|
Chris@0
|
86 $entity = EntityTest::load($id);
|
Chris@0
|
87 $this->assertEqual('2012-12-31', $entity->{$field_name}->value);
|
Chris@0
|
88
|
Chris@0
|
89 // Reset display options since these get changed below.
|
Chris@0
|
90 $this->displayOptions = [
|
Chris@0
|
91 'type' => 'datetime_default',
|
Chris@0
|
92 'label' => 'hidden',
|
Chris@0
|
93 'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
|
Chris@0
|
94 ];
|
Chris@0
|
95 // Verify that the date is output according to the formatter settings.
|
Chris@0
|
96 $options = [
|
Chris@0
|
97 'format_type' => ['short', 'medium', 'long'],
|
Chris@0
|
98 ];
|
Chris@0
|
99 // Formats that display a time component for date-only fields will display
|
Chris@0
|
100 // the default time, so that is applied before calculating the expected
|
Chris@0
|
101 // value.
|
Chris@14
|
102 $this->massageTestDate($date);
|
Chris@0
|
103 foreach ($options as $setting => $values) {
|
Chris@0
|
104 foreach ($values as $new_value) {
|
Chris@0
|
105 // Update the entity display settings.
|
Chris@0
|
106 $this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
|
Chris@0
|
107 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
108 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
109 ->save();
|
Chris@0
|
110
|
Chris@0
|
111 $this->renderTestEntity($id);
|
Chris@0
|
112 switch ($setting) {
|
Chris@0
|
113 case 'format_type':
|
Chris@0
|
114 // Verify that a date is displayed. Since this is a date-only
|
Chris@0
|
115 // field, it is expected to display the time as 00:00:00.
|
Chris@18
|
116 /** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
|
Chris@18
|
117 $date_formatter = $this->container->get('date.formatter');
|
Chris@18
|
118 $expected = $date_formatter->format($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
|
Chris@18
|
119 $expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
|
Chris@0
|
120 $output = $this->renderTestEntity($id);
|
Chris@0
|
121 $expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
|
Chris@0
|
122 $this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
|
Chris@0
|
123 '%value' => $new_value,
|
Chris@0
|
124 '%expected' => $expected,
|
Chris@0
|
125 '%expected_iso' => $expected_iso,
|
Chris@0
|
126 '%timezone' => $timezone,
|
Chris@0
|
127 ]));
|
Chris@0
|
128 break;
|
Chris@0
|
129 }
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 // Verify that the plain formatter works.
|
Chris@0
|
134 $this->displayOptions['type'] = 'datetime_plain';
|
Chris@0
|
135 $this->displayOptions['settings'] = $this->defaultSettings;
|
Chris@0
|
136 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
137 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
138 ->save();
|
Chris@14
|
139 $expected = $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
|
Chris@0
|
140 $output = $this->renderTestEntity($id);
|
Chris@0
|
141 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected in %timezone.', [
|
Chris@0
|
142 '%expected' => $expected,
|
Chris@0
|
143 '%timezone' => $timezone,
|
Chris@0
|
144 ]));
|
Chris@0
|
145
|
Chris@0
|
146 // Verify that the 'datetime_custom' formatter works.
|
Chris@0
|
147 $this->displayOptions['type'] = 'datetime_custom';
|
Chris@0
|
148 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings;
|
Chris@0
|
149 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
150 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
151 ->save();
|
Chris@0
|
152 $expected = $date->format($this->displayOptions['settings']['date_format']);
|
Chris@0
|
153 $output = $this->renderTestEntity($id);
|
Chris@0
|
154 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected in %timezone.', [
|
Chris@0
|
155 '%expected' => $expected,
|
Chris@0
|
156 '%timezone' => $timezone,
|
Chris@0
|
157 ]));
|
Chris@0
|
158
|
Chris@0
|
159 // Test that allowed markup in custom format is preserved and XSS is
|
Chris@0
|
160 // removed.
|
Chris@0
|
161 $this->displayOptions['settings']['date_format'] = '\\<\\s\\t\\r\\o\\n\\g\\>m/d/Y\\<\\/\\s\\t\\r\\o\\n\\g\\>\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\\S\\t\\r\\i\\n\\g\\.\\f\\r\\o\\m\\C\\h\\a\\r\\C\\o\\d\\e\\(\\8\\8\\,\\8\\3\\,\\8\\3\\)\\)\\<\\/\\s\\c\\r\\i\\p\\t\\>';
|
Chris@0
|
162 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
163 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
164 ->save();
|
Chris@0
|
165 $expected = '<strong>' . $date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
|
Chris@0
|
166 $output = $this->renderTestEntity($id);
|
Chris@0
|
167 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
|
Chris@0
|
168 '%expected' => $expected,
|
Chris@0
|
169 '%timezone' => $timezone,
|
Chris@0
|
170 ]));
|
Chris@0
|
171
|
Chris@0
|
172 // Verify that the 'datetime_time_ago' formatter works for intervals in the
|
Chris@0
|
173 // past. First update the test entity so that the date difference always
|
Chris@0
|
174 // has the same interval. Since the database always stores UTC, and the
|
Chris@0
|
175 // interval will use this, force the test date to use UTC and not the local
|
Chris@17
|
176 // or user timezone.
|
Chris@0
|
177 $timestamp = REQUEST_TIME - 87654321;
|
Chris@0
|
178 $entity = EntityTest::load($id);
|
Chris@0
|
179 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
180 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
|
Chris@0
|
181 $entity->{$field_name}->value = $date->format($date_format);
|
Chris@0
|
182 $entity->save();
|
Chris@0
|
183
|
Chris@0
|
184 $this->displayOptions['type'] = 'datetime_time_ago';
|
Chris@0
|
185 $this->displayOptions['settings'] = [
|
Chris@0
|
186 'future_format' => '@interval in the future',
|
Chris@0
|
187 'past_format' => '@interval in the past',
|
Chris@0
|
188 'granularity' => 3,
|
Chris@0
|
189 ];
|
Chris@0
|
190 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
191 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
192 ->save();
|
Chris@17
|
193 $expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
|
Chris@17
|
194 '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
Chris@0
|
195 ]);
|
Chris@0
|
196 $output = $this->renderTestEntity($id);
|
Chris@0
|
197 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
|
Chris@0
|
198 '%expected' => $expected,
|
Chris@0
|
199 '%timezone' => $timezone,
|
Chris@0
|
200 ]));
|
Chris@0
|
201
|
Chris@0
|
202 // Verify that the 'datetime_time_ago' formatter works for intervals in the
|
Chris@0
|
203 // future. First update the test entity so that the date difference always
|
Chris@0
|
204 // has the same interval. Since the database always stores UTC, and the
|
Chris@0
|
205 // interval will use this, force the test date to use UTC and not the local
|
Chris@17
|
206 // or user timezone.
|
Chris@0
|
207 $timestamp = REQUEST_TIME + 87654321;
|
Chris@0
|
208 $entity = EntityTest::load($id);
|
Chris@0
|
209 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
210 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
|
Chris@0
|
211 $entity->{$field_name}->value = $date->format($date_format);
|
Chris@0
|
212 $entity->save();
|
Chris@0
|
213
|
Chris@0
|
214 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
215 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
216 ->save();
|
Chris@17
|
217 $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
|
Chris@17
|
218 '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
Chris@0
|
219 ]);
|
Chris@0
|
220 $output = $this->renderTestEntity($id);
|
Chris@0
|
221 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
|
Chris@0
|
222 '%expected' => $expected,
|
Chris@0
|
223 '%timezone' => $timezone,
|
Chris@0
|
224 ]));
|
Chris@0
|
225 }
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * Tests date and time field.
|
Chris@0
|
230 */
|
Chris@0
|
231 public function testDatetimeField() {
|
Chris@0
|
232 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
233 $field_label = $this->field->label();
|
Chris@0
|
234 // Change the field to a datetime field.
|
Chris@0
|
235 $this->fieldStorage->setSetting('datetime_type', 'datetime');
|
Chris@0
|
236 $this->fieldStorage->save();
|
Chris@0
|
237
|
Chris@0
|
238 // Display creation form.
|
Chris@0
|
239 $this->drupalGet('entity_test/add');
|
Chris@0
|
240 $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
|
Chris@0
|
241 $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
|
Chris@0
|
242 $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
|
Chris@0
|
243 $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
|
Chris@0
|
244 $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
|
Chris@0
|
245
|
Chris@0
|
246 // Build up a date in the UTC timezone.
|
Chris@0
|
247 $value = '2012-12-31 00:00:00';
|
Chris@0
|
248 $date = new DrupalDateTime($value, 'UTC');
|
Chris@0
|
249
|
Chris@0
|
250 // Update the timezone to the system default.
|
Chris@0
|
251 $date->setTimezone(timezone_open(drupal_get_user_timezone()));
|
Chris@0
|
252
|
Chris@0
|
253 // Submit a valid date and ensure it is accepted.
|
Chris@0
|
254 $date_format = DateFormat::load('html_date')->getPattern();
|
Chris@0
|
255 $time_format = DateFormat::load('html_time')->getPattern();
|
Chris@0
|
256
|
Chris@0
|
257 $edit = [
|
Chris@0
|
258 "{$field_name}[0][value][date]" => $date->format($date_format),
|
Chris@0
|
259 "{$field_name}[0][value][time]" => $date->format($time_format),
|
Chris@0
|
260 ];
|
Chris@0
|
261 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
262 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
Chris@0
|
263 $id = $match[1];
|
Chris@0
|
264 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
Chris@0
|
265 $this->assertRaw($date->format($date_format));
|
Chris@0
|
266 $this->assertRaw($date->format($time_format));
|
Chris@0
|
267
|
Chris@0
|
268 // Verify that the date is output according to the formatter settings.
|
Chris@0
|
269 $options = [
|
Chris@0
|
270 'format_type' => ['short', 'medium', 'long'],
|
Chris@0
|
271 ];
|
Chris@0
|
272 foreach ($options as $setting => $values) {
|
Chris@0
|
273 foreach ($values as $new_value) {
|
Chris@0
|
274 // Update the entity display settings.
|
Chris@0
|
275 $this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
|
Chris@0
|
276 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
277 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
278 ->save();
|
Chris@0
|
279
|
Chris@0
|
280 $this->renderTestEntity($id);
|
Chris@0
|
281 switch ($setting) {
|
Chris@0
|
282 case 'format_type':
|
Chris@0
|
283 // Verify that a date is displayed.
|
Chris@18
|
284 $date_formatter = $this->container->get('date.formatter');
|
Chris@18
|
285 $expected = $date_formatter->format($date->getTimestamp(), $new_value);
|
Chris@18
|
286 $expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
|
Chris@0
|
287 $output = $this->renderTestEntity($id);
|
Chris@0
|
288 $expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
|
Chris@17
|
289 $this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
|
Chris@0
|
290 break;
|
Chris@0
|
291 }
|
Chris@0
|
292 }
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 // Verify that the plain formatter works.
|
Chris@0
|
296 $this->displayOptions['type'] = 'datetime_plain';
|
Chris@0
|
297 $this->displayOptions['settings'] = $this->defaultSettings;
|
Chris@0
|
298 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
299 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
300 ->save();
|
Chris@14
|
301 $expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
|
Chris@0
|
302 $output = $this->renderTestEntity($id);
|
Chris@17
|
303 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
|
Chris@0
|
304
|
Chris@0
|
305 // Verify that the 'datetime_custom' formatter works.
|
Chris@0
|
306 $this->displayOptions['type'] = 'datetime_custom';
|
Chris@0
|
307 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings;
|
Chris@0
|
308 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
309 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
310 ->save();
|
Chris@0
|
311 $expected = $date->format($this->displayOptions['settings']['date_format']);
|
Chris@0
|
312 $output = $this->renderTestEntity($id);
|
Chris@17
|
313 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
Chris@0
|
314
|
Chris@0
|
315 // Verify that the 'timezone_override' setting works.
|
Chris@0
|
316 $this->displayOptions['type'] = 'datetime_custom';
|
Chris@0
|
317 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings;
|
Chris@0
|
318 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
319 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
320 ->save();
|
Chris@0
|
321 $expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
|
Chris@0
|
322 $output = $this->renderTestEntity($id);
|
Chris@17
|
323 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
Chris@0
|
324
|
Chris@0
|
325 // Verify that the 'datetime_time_ago' formatter works for intervals in the
|
Chris@0
|
326 // past. First update the test entity so that the date difference always
|
Chris@0
|
327 // has the same interval. Since the database always stores UTC, and the
|
Chris@0
|
328 // interval will use this, force the test date to use UTC and not the local
|
Chris@17
|
329 // or user timezone.
|
Chris@0
|
330 $timestamp = REQUEST_TIME - 87654321;
|
Chris@0
|
331 $entity = EntityTest::load($id);
|
Chris@0
|
332 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
333 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
|
Chris@14
|
334 $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
|
Chris@0
|
335 $entity->save();
|
Chris@0
|
336
|
Chris@0
|
337 $this->displayOptions['type'] = 'datetime_time_ago';
|
Chris@0
|
338 $this->displayOptions['settings'] = [
|
Chris@0
|
339 'future_format' => '@interval from now',
|
Chris@0
|
340 'past_format' => '@interval earlier',
|
Chris@0
|
341 'granularity' => 3,
|
Chris@0
|
342 ];
|
Chris@0
|
343 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
344 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
345 ->save();
|
Chris@17
|
346 $expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
|
Chris@17
|
347 '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
Chris@0
|
348 ]);
|
Chris@0
|
349 $output = $this->renderTestEntity($id);
|
Chris@17
|
350 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
Chris@0
|
351
|
Chris@0
|
352 // Verify that the 'datetime_time_ago' formatter works for intervals in the
|
Chris@0
|
353 // future. First update the test entity so that the date difference always
|
Chris@0
|
354 // has the same interval. Since the database always stores UTC, and the
|
Chris@0
|
355 // interval will use this, force the test date to use UTC and not the local
|
Chris@17
|
356 // or user timezone.
|
Chris@0
|
357 $timestamp = REQUEST_TIME + 87654321;
|
Chris@0
|
358 $entity = EntityTest::load($id);
|
Chris@0
|
359 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
360 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
|
Chris@14
|
361 $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
|
Chris@0
|
362 $entity->save();
|
Chris@0
|
363
|
Chris@0
|
364 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
Chris@0
|
365 ->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
366 ->save();
|
Chris@17
|
367 $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
|
Chris@17
|
368 '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
Chris@0
|
369 ]);
|
Chris@0
|
370 $output = $this->renderTestEntity($id);
|
Chris@17
|
371 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 /**
|
Chris@0
|
375 * Tests Date List Widget functionality.
|
Chris@0
|
376 */
|
Chris@0
|
377 public function testDatelistWidget() {
|
Chris@0
|
378 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
379 $field_label = $this->field->label();
|
Chris@0
|
380
|
Chris@0
|
381 // Ensure field is set to a date only field.
|
Chris@0
|
382 $this->fieldStorage->setSetting('datetime_type', 'date');
|
Chris@0
|
383 $this->fieldStorage->save();
|
Chris@0
|
384
|
Chris@0
|
385 // Change the widget to a datelist widget.
|
Chris@0
|
386 entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
Chris@0
|
387 ->setComponent($field_name, [
|
Chris@0
|
388 'type' => 'datetime_datelist',
|
Chris@0
|
389 'settings' => [
|
Chris@0
|
390 'date_order' => 'YMD',
|
Chris@0
|
391 ],
|
Chris@0
|
392 ])
|
Chris@0
|
393 ->save();
|
Chris@0
|
394 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
395
|
Chris@0
|
396 // Display creation form.
|
Chris@0
|
397 $this->drupalGet('entity_test/add');
|
Chris@0
|
398 $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
|
Chris@0
|
399 $this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
|
Chris@0
|
400 $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
|
Chris@0
|
401
|
Chris@0
|
402 // Assert that Hour and Minute Elements do not appear on Date Only
|
Chris@0
|
403 $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element not found on Date Only.');
|
Chris@0
|
404 $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-minute\"]", NULL, 'Minute element not found on Date Only.');
|
Chris@0
|
405
|
Chris@0
|
406 // Go to the form display page to assert that increment option does not appear on Date Only
|
Chris@0
|
407 $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
|
Chris@0
|
408 $this->drupalGet($fieldEditUrl);
|
Chris@0
|
409
|
Chris@0
|
410 // Click on the widget settings button to open the widget settings form.
|
Chris@0
|
411 $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
|
Chris@0
|
412 $xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
|
Chris@0
|
413 $this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.');
|
Chris@0
|
414
|
Chris@0
|
415 // Change the field to a datetime field.
|
Chris@0
|
416 $this->fieldStorage->setSetting('datetime_type', 'datetime');
|
Chris@0
|
417 $this->fieldStorage->save();
|
Chris@0
|
418
|
Chris@0
|
419 // Change the widget to a datelist widget.
|
Chris@0
|
420 entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
Chris@0
|
421 ->setComponent($field_name, [
|
Chris@0
|
422 'type' => 'datetime_datelist',
|
Chris@0
|
423 'settings' => [
|
Chris@0
|
424 'increment' => 1,
|
Chris@0
|
425 'date_order' => 'YMD',
|
Chris@0
|
426 'time_type' => '12',
|
Chris@0
|
427 ],
|
Chris@0
|
428 ])
|
Chris@0
|
429 ->save();
|
Chris@0
|
430 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
431
|
Chris@0
|
432 // Go to the form display page to assert that increment option does appear on Date Time
|
Chris@0
|
433 $fieldEditUrl = 'entity_test/structure/entity_test/form-display';
|
Chris@0
|
434 $this->drupalGet($fieldEditUrl);
|
Chris@0
|
435
|
Chris@0
|
436 // Click on the widget settings button to open the widget settings form.
|
Chris@0
|
437 $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
|
Chris@0
|
438 $this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.');
|
Chris@0
|
439
|
Chris@0
|
440 // Display creation form.
|
Chris@0
|
441 $this->drupalGet('entity_test/add');
|
Chris@0
|
442
|
Chris@0
|
443 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-year\"]", NULL, 'Year element found.');
|
Chris@0
|
444 $this->assertOptionSelected("edit-$field_name-0-value-year", '', 'No year selected.');
|
Chris@0
|
445 $this->assertOptionByText("edit-$field_name-0-value-year", t('Year'));
|
Chris@0
|
446 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-month\"]", NULL, 'Month element found.');
|
Chris@0
|
447 $this->assertOptionSelected("edit-$field_name-0-value-month", '', 'No month selected.');
|
Chris@0
|
448 $this->assertOptionByText("edit-$field_name-0-value-month", t('Month'));
|
Chris@0
|
449 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-day\"]", NULL, 'Day element found.');
|
Chris@0
|
450 $this->assertOptionSelected("edit-$field_name-0-value-day", '', 'No day selected.');
|
Chris@0
|
451 $this->assertOptionByText("edit-$field_name-0-value-day", t('Day'));
|
Chris@0
|
452 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element found.');
|
Chris@0
|
453 $this->assertOptionSelected("edit-$field_name-0-value-hour", '', 'No hour selected.');
|
Chris@0
|
454 $this->assertOptionByText("edit-$field_name-0-value-hour", t('Hour'));
|
Chris@0
|
455 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-minute\"]", NULL, 'Minute element found.');
|
Chris@0
|
456 $this->assertOptionSelected("edit-$field_name-0-value-minute", '', 'No minute selected.');
|
Chris@0
|
457 $this->assertOptionByText("edit-$field_name-0-value-minute", t('Minute'));
|
Chris@0
|
458 $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-second\"]", NULL, 'Second element not found.');
|
Chris@0
|
459 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-ampm\"]", NULL, 'AMPM element found.');
|
Chris@0
|
460 $this->assertOptionSelected("edit-$field_name-0-value-ampm", '', 'No ampm selected.');
|
Chris@0
|
461 $this->assertOptionByText("edit-$field_name-0-value-ampm", t('AM/PM'));
|
Chris@0
|
462
|
Chris@0
|
463 // Submit a valid date and ensure it is accepted.
|
Chris@0
|
464 $date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15];
|
Chris@0
|
465
|
Chris@0
|
466 $edit = [];
|
Chris@0
|
467 // Add the ampm indicator since we are testing 12 hour time.
|
Chris@0
|
468 $date_value['ampm'] = 'am';
|
Chris@0
|
469 foreach ($date_value as $part => $value) {
|
Chris@0
|
470 $edit["{$field_name}[0][value][$part]"] = $value;
|
Chris@0
|
471 }
|
Chris@0
|
472
|
Chris@0
|
473 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
474 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
Chris@0
|
475 $id = $match[1];
|
Chris@0
|
476 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
Chris@0
|
477
|
Chris@0
|
478 $this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.');
|
Chris@0
|
479 $this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.');
|
Chris@0
|
480 $this->assertOptionSelected("edit-$field_name-0-value-day", '31', 'Correct day selected.');
|
Chris@0
|
481 $this->assertOptionSelected("edit-$field_name-0-value-hour", '5', 'Correct hour selected.');
|
Chris@0
|
482 $this->assertOptionSelected("edit-$field_name-0-value-minute", '15', 'Correct minute selected.');
|
Chris@0
|
483 $this->assertOptionSelected("edit-$field_name-0-value-ampm", 'am', 'Correct ampm selected.');
|
Chris@0
|
484
|
Chris@0
|
485 // Test the widget using increment other than 1 and 24 hour mode.
|
Chris@0
|
486 entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
Chris@0
|
487 ->setComponent($field_name, [
|
Chris@0
|
488 'type' => 'datetime_datelist',
|
Chris@0
|
489 'settings' => [
|
Chris@0
|
490 'increment' => 15,
|
Chris@0
|
491 'date_order' => 'YMD',
|
Chris@0
|
492 'time_type' => '24',
|
Chris@0
|
493 ],
|
Chris@0
|
494 ])
|
Chris@0
|
495 ->save();
|
Chris@0
|
496 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
497
|
Chris@0
|
498 // Display creation form.
|
Chris@0
|
499 $this->drupalGet('entity_test/add');
|
Chris@0
|
500
|
Chris@0
|
501 // Other elements are unaffected by the changed settings.
|
Chris@0
|
502 $this->assertFieldByXPath("//*[@id=\"edit-$field_name-0-value-hour\"]", NULL, 'Hour element found.');
|
Chris@0
|
503 $this->assertOptionSelected("edit-$field_name-0-value-hour", '', 'No hour selected.');
|
Chris@0
|
504 $this->assertNoFieldByXPath("//*[@id=\"edit-$field_name-0-value-ampm\"]", NULL, 'AMPM element not found.');
|
Chris@0
|
505
|
Chris@0
|
506 // Submit a valid date and ensure it is accepted.
|
Chris@0
|
507 $date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15];
|
Chris@0
|
508
|
Chris@0
|
509 $edit = [];
|
Chris@0
|
510 foreach ($date_value as $part => $value) {
|
Chris@0
|
511 $edit["{$field_name}[0][value][$part]"] = $value;
|
Chris@0
|
512 }
|
Chris@0
|
513
|
Chris@0
|
514 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
515 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
Chris@0
|
516 $id = $match[1];
|
Chris@0
|
517 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
Chris@0
|
518
|
Chris@0
|
519 $this->assertOptionSelected("edit-$field_name-0-value-year", '2012', 'Correct year selected.');
|
Chris@0
|
520 $this->assertOptionSelected("edit-$field_name-0-value-month", '12', 'Correct month selected.');
|
Chris@0
|
521 $this->assertOptionSelected("edit-$field_name-0-value-day", '31', 'Correct day selected.');
|
Chris@0
|
522 $this->assertOptionSelected("edit-$field_name-0-value-hour", '17', 'Correct hour selected.');
|
Chris@0
|
523 $this->assertOptionSelected("edit-$field_name-0-value-minute", '15', 'Correct minute selected.');
|
Chris@0
|
524
|
Chris@0
|
525 // Test the widget for partial completion of fields.
|
Chris@0
|
526 entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default')
|
Chris@0
|
527 ->setComponent($field_name, [
|
Chris@0
|
528 'type' => 'datetime_datelist',
|
Chris@0
|
529 'settings' => [
|
Chris@0
|
530 'increment' => 1,
|
Chris@0
|
531 'date_order' => 'YMD',
|
Chris@0
|
532 'time_type' => '24',
|
Chris@0
|
533 ],
|
Chris@0
|
534 ])
|
Chris@0
|
535 ->save();
|
Chris@0
|
536 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
537
|
Chris@0
|
538 // Test the widget for validation notifications.
|
Chris@0
|
539 foreach ($this->datelistDataProvider($field_label) as $data) {
|
Chris@0
|
540 list($date_value, $expected) = $data;
|
Chris@0
|
541
|
Chris@0
|
542 // Display creation form.
|
Chris@0
|
543 $this->drupalGet('entity_test/add');
|
Chris@0
|
544
|
Chris@0
|
545 // Submit a partial date and ensure and error message is provided.
|
Chris@0
|
546 $edit = [];
|
Chris@0
|
547 foreach ($date_value as $part => $value) {
|
Chris@0
|
548 $edit["{$field_name}[0][value][$part]"] = $value;
|
Chris@0
|
549 }
|
Chris@0
|
550
|
Chris@0
|
551 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
552 $this->assertResponse(200);
|
Chris@0
|
553 foreach ($expected as $expected_text) {
|
Chris@0
|
554 $this->assertText(t($expected_text));
|
Chris@0
|
555 }
|
Chris@0
|
556 }
|
Chris@0
|
557
|
Chris@0
|
558 // Test the widget for complete input with zeros as part of selections.
|
Chris@0
|
559 $this->drupalGet('entity_test/add');
|
Chris@0
|
560
|
Chris@0
|
561 $date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0'];
|
Chris@0
|
562 $edit = [];
|
Chris@0
|
563 foreach ($date_value as $part => $value) {
|
Chris@0
|
564 $edit["{$field_name}[0][value][$part]"] = $value;
|
Chris@0
|
565 }
|
Chris@0
|
566
|
Chris@0
|
567 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
568 $this->assertResponse(200);
|
Chris@0
|
569 preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
|
Chris@0
|
570 $id = $match[1];
|
Chris@0
|
571 $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
|
Chris@0
|
572
|
Chris@0
|
573 // Test the widget to ensure zeros are not deselected on validation.
|
Chris@0
|
574 $this->drupalGet('entity_test/add');
|
Chris@0
|
575
|
Chris@0
|
576 $date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0'];
|
Chris@0
|
577 $edit = [];
|
Chris@0
|
578 foreach ($date_value as $part => $value) {
|
Chris@0
|
579 $edit["{$field_name}[0][value][$part]"] = $value;
|
Chris@0
|
580 }
|
Chris@0
|
581
|
Chris@0
|
582 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
583 $this->assertResponse(200);
|
Chris@0
|
584 $this->assertOptionSelected("edit-$field_name-0-value-minute", '0', 'Correct minute selected.');
|
Chris@0
|
585 }
|
Chris@0
|
586
|
Chris@0
|
587 /**
|
Chris@0
|
588 * The data provider for testing the validation of the datelist widget.
|
Chris@0
|
589 *
|
Chris@0
|
590 * @param string $field_label
|
Chris@0
|
591 * The label of the field being tested.
|
Chris@0
|
592 *
|
Chris@0
|
593 * @return array
|
Chris@0
|
594 * An array of datelist input permutations to test.
|
Chris@0
|
595 */
|
Chris@0
|
596 protected function datelistDataProvider($field_label) {
|
Chris@0
|
597 return [
|
Chris@0
|
598 // Nothing selected.
|
Chris@0
|
599 [
|
Chris@0
|
600 ['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
|
Chris@0
|
601 ["The $field_label date is required."],
|
Chris@0
|
602 ],
|
Chris@0
|
603 // Year only selected, validation error on Month, Day, Hour, Minute.
|
Chris@0
|
604 [
|
Chris@0
|
605 ['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
|
Chris@0
|
606 [
|
Chris@0
|
607 "The $field_label date is incomplete.",
|
Chris@0
|
608 'A value must be selected for month.',
|
Chris@0
|
609 'A value must be selected for day.',
|
Chris@0
|
610 'A value must be selected for hour.',
|
Chris@0
|
611 'A value must be selected for minute.',
|
Chris@0
|
612 ],
|
Chris@0
|
613 ],
|
Chris@0
|
614 // Year and Month selected, validation error on Day, Hour, Minute.
|
Chris@0
|
615 [
|
Chris@0
|
616 ['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''],
|
Chris@0
|
617 [
|
Chris@0
|
618 "The $field_label date is incomplete.",
|
Chris@0
|
619 'A value must be selected for day.',
|
Chris@0
|
620 'A value must be selected for hour.',
|
Chris@0
|
621 'A value must be selected for minute.',
|
Chris@0
|
622 ],
|
Chris@0
|
623 ],
|
Chris@0
|
624 // Year, Month and Day selected, validation error on Hour, Minute.
|
Chris@0
|
625 [
|
Chris@0
|
626 ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''],
|
Chris@0
|
627 [
|
Chris@0
|
628 "The $field_label date is incomplete.",
|
Chris@0
|
629 'A value must be selected for hour.',
|
Chris@0
|
630 'A value must be selected for minute.',
|
Chris@0
|
631 ],
|
Chris@0
|
632 ],
|
Chris@0
|
633 // Year, Month, Day and Hour selected, validation error on Minute only.
|
Chris@0
|
634 [
|
Chris@0
|
635 ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''],
|
Chris@0
|
636 [
|
Chris@0
|
637 "The $field_label date is incomplete.",
|
Chris@0
|
638 'A value must be selected for minute.',
|
Chris@0
|
639 ],
|
Chris@0
|
640 ],
|
Chris@0
|
641 ];
|
Chris@0
|
642 }
|
Chris@0
|
643
|
Chris@0
|
644 /**
|
Chris@0
|
645 * Test default value functionality.
|
Chris@0
|
646 */
|
Chris@0
|
647 public function testDefaultValue() {
|
Chris@0
|
648 // Create a test content type.
|
Chris@0
|
649 $this->drupalCreateContentType(['type' => 'date_content']);
|
Chris@0
|
650
|
Chris@0
|
651 // Create a field storage with settings to validate.
|
Chris@17
|
652 $field_name = mb_strtolower($this->randomMachineName());
|
Chris@0
|
653 $field_storage = FieldStorageConfig::create([
|
Chris@0
|
654 'field_name' => $field_name,
|
Chris@0
|
655 'entity_type' => 'node',
|
Chris@0
|
656 'type' => 'datetime',
|
Chris@0
|
657 'settings' => ['datetime_type' => 'date'],
|
Chris@0
|
658 ]);
|
Chris@0
|
659 $field_storage->save();
|
Chris@0
|
660
|
Chris@0
|
661 $field = FieldConfig::create([
|
Chris@0
|
662 'field_storage' => $field_storage,
|
Chris@0
|
663 'bundle' => 'date_content',
|
Chris@0
|
664 ]);
|
Chris@0
|
665 $field->save();
|
Chris@0
|
666
|
Chris@0
|
667 // Loop through defined timezones to test that date-only defaults work at
|
Chris@0
|
668 // the extremes.
|
Chris@0
|
669 foreach (static::$timezones as $timezone) {
|
Chris@0
|
670
|
Chris@0
|
671 $this->setSiteTimezone($timezone);
|
Chris@0
|
672 $this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
|
Chris@0
|
673
|
Chris@0
|
674 // Set now as default_value.
|
Chris@0
|
675 $field_edit = [
|
Chris@0
|
676 'default_value_input[default_date_type]' => 'now',
|
Chris@0
|
677 ];
|
Chris@0
|
678 $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
Chris@0
|
679
|
Chris@0
|
680 // Check that default value is selected in default value form.
|
Chris@0
|
681 $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
|
Chris@0
|
682 $this->assertOptionSelected('edit-default-value-input-default-date-type', 'now', 'The default value is selected in instance settings page');
|
Chris@0
|
683 $this->assertFieldByName('default_value_input[default_date]', '', 'The relative default value is empty in instance settings page');
|
Chris@0
|
684
|
Chris@0
|
685 // Check if default_date has been stored successfully.
|
Chris@0
|
686 $config_entity = $this->config('field.field.node.date_content.' . $field_name)
|
Chris@0
|
687 ->get();
|
Chris@0
|
688 $this->assertEqual($config_entity['default_value'][0], [
|
Chris@0
|
689 'default_date_type' => 'now',
|
Chris@0
|
690 'default_date' => 'now',
|
Chris@0
|
691 ], 'Default value has been stored successfully');
|
Chris@0
|
692
|
Chris@0
|
693 // Clear field cache in order to avoid stale cache values.
|
Chris@0
|
694 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
695
|
Chris@0
|
696 // Create a new node to check that datetime field default value is today.
|
Chris@0
|
697 $new_node = Node::create(['type' => 'date_content']);
|
Chris@0
|
698 $expected_date = new DrupalDateTime('now', drupal_get_user_timezone());
|
Chris@0
|
699 $this->assertEqual($new_node->get($field_name)
|
Chris@14
|
700 ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
|
Chris@0
|
701
|
Chris@0
|
702 // Set an invalid relative default_value to test validation.
|
Chris@0
|
703 $field_edit = [
|
Chris@0
|
704 'default_value_input[default_date_type]' => 'relative',
|
Chris@0
|
705 'default_value_input[default_date]' => 'invalid date',
|
Chris@0
|
706 ];
|
Chris@0
|
707 $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
Chris@0
|
708
|
Chris@0
|
709 $this->assertText('The relative date value entered is invalid.');
|
Chris@0
|
710
|
Chris@0
|
711 // Set a relative default_value.
|
Chris@0
|
712 $field_edit = [
|
Chris@0
|
713 'default_value_input[default_date_type]' => 'relative',
|
Chris@0
|
714 'default_value_input[default_date]' => '+90 days',
|
Chris@0
|
715 ];
|
Chris@0
|
716 $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
Chris@0
|
717
|
Chris@0
|
718 // Check that default value is selected in default value form.
|
Chris@0
|
719 $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
|
Chris@0
|
720 $this->assertOptionSelected('edit-default-value-input-default-date-type', 'relative', 'The default value is selected in instance settings page');
|
Chris@0
|
721 $this->assertFieldByName('default_value_input[default_date]', '+90 days', 'The relative default value is displayed in instance settings page');
|
Chris@0
|
722
|
Chris@0
|
723 // Check if default_date has been stored successfully.
|
Chris@0
|
724 $config_entity = $this->config('field.field.node.date_content.' . $field_name)
|
Chris@0
|
725 ->get();
|
Chris@0
|
726 $this->assertEqual($config_entity['default_value'][0], [
|
Chris@0
|
727 'default_date_type' => 'relative',
|
Chris@0
|
728 'default_date' => '+90 days',
|
Chris@0
|
729 ], 'Default value has been stored successfully');
|
Chris@0
|
730
|
Chris@0
|
731 // Clear field cache in order to avoid stale cache values.
|
Chris@0
|
732 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
733
|
Chris@0
|
734 // Create a new node to check that datetime field default value is +90
|
Chris@0
|
735 // days.
|
Chris@0
|
736 $new_node = Node::create(['type' => 'date_content']);
|
Chris@0
|
737 $expected_date = new DrupalDateTime('+90 days', drupal_get_user_timezone());
|
Chris@0
|
738 $this->assertEqual($new_node->get($field_name)
|
Chris@14
|
739 ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
|
Chris@0
|
740
|
Chris@0
|
741 // Remove default value.
|
Chris@0
|
742 $field_edit = [
|
Chris@0
|
743 'default_value_input[default_date_type]' => '',
|
Chris@0
|
744 ];
|
Chris@0
|
745 $this->drupalPostForm('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name, $field_edit, t('Save settings'));
|
Chris@0
|
746
|
Chris@0
|
747 // Check that default value is selected in default value form.
|
Chris@0
|
748 $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
|
Chris@0
|
749 $this->assertOptionSelected('edit-default-value-input-default-date-type', '', 'The default value is selected in instance settings page');
|
Chris@0
|
750 $this->assertFieldByName('default_value_input[default_date]', '', 'The relative default value is empty in instance settings page');
|
Chris@0
|
751
|
Chris@0
|
752 // Check if default_date has been stored successfully.
|
Chris@0
|
753 $config_entity = $this->config('field.field.node.date_content.' . $field_name)
|
Chris@0
|
754 ->get();
|
Chris@0
|
755 $this->assertTrue(empty($config_entity['default_value']), 'Empty default value has been stored successfully');
|
Chris@0
|
756
|
Chris@0
|
757 // Clear field cache in order to avoid stale cache values.
|
Chris@0
|
758 \Drupal::entityManager()->clearCachedFieldDefinitions();
|
Chris@0
|
759
|
Chris@0
|
760 // Create a new node to check that datetime field default value is not
|
Chris@0
|
761 // set.
|
Chris@0
|
762 $new_node = Node::create(['type' => 'date_content']);
|
Chris@0
|
763 $this->assertNull($new_node->get($field_name)->value, 'Default value is not set');
|
Chris@0
|
764 }
|
Chris@0
|
765 }
|
Chris@0
|
766
|
Chris@0
|
767 /**
|
Chris@0
|
768 * Test that invalid values are caught and marked as invalid.
|
Chris@0
|
769 */
|
Chris@0
|
770 public function testInvalidField() {
|
Chris@0
|
771 // Change the field to a datetime field.
|
Chris@0
|
772 $this->fieldStorage->setSetting('datetime_type', 'datetime');
|
Chris@0
|
773 $this->fieldStorage->save();
|
Chris@0
|
774 $field_name = $this->fieldStorage->getName();
|
Chris@0
|
775
|
Chris@0
|
776 // Display creation form.
|
Chris@0
|
777 $this->drupalGet('entity_test/add');
|
Chris@0
|
778 $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
|
Chris@0
|
779 $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
|
Chris@0
|
780
|
Chris@0
|
781 // Submit invalid dates and ensure they is not accepted.
|
Chris@0
|
782 $date_value = '';
|
Chris@0
|
783 $edit = [
|
Chris@0
|
784 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
785 "{$field_name}[0][value][time]" => '12:00:00',
|
Chris@0
|
786 ];
|
Chris@0
|
787 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
788 $this->assertText('date is invalid', 'Empty date value has been caught.');
|
Chris@0
|
789
|
Chris@0
|
790 $date_value = 'aaaa-12-01';
|
Chris@0
|
791 $edit = [
|
Chris@0
|
792 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
793 "{$field_name}[0][value][time]" => '00:00:00',
|
Chris@0
|
794 ];
|
Chris@0
|
795 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
796 $this->assertText('date is invalid', format_string('Invalid year value %date has been caught.', ['%date' => $date_value]));
|
Chris@0
|
797
|
Chris@0
|
798 $date_value = '2012-75-01';
|
Chris@0
|
799 $edit = [
|
Chris@0
|
800 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
801 "{$field_name}[0][value][time]" => '00:00:00',
|
Chris@0
|
802 ];
|
Chris@0
|
803 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
804 $this->assertText('date is invalid', format_string('Invalid month value %date has been caught.', ['%date' => $date_value]));
|
Chris@0
|
805
|
Chris@0
|
806 $date_value = '2012-12-99';
|
Chris@0
|
807 $edit = [
|
Chris@0
|
808 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
809 "{$field_name}[0][value][time]" => '00:00:00',
|
Chris@0
|
810 ];
|
Chris@0
|
811 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
812 $this->assertText('date is invalid', format_string('Invalid day value %date has been caught.', ['%date' => $date_value]));
|
Chris@0
|
813
|
Chris@0
|
814 $date_value = '2012-12-01';
|
Chris@0
|
815 $time_value = '';
|
Chris@0
|
816 $edit = [
|
Chris@0
|
817 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
818 "{$field_name}[0][value][time]" => $time_value,
|
Chris@0
|
819 ];
|
Chris@0
|
820 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
821 $this->assertText('date is invalid', 'Empty time value has been caught.');
|
Chris@0
|
822
|
Chris@0
|
823 $date_value = '2012-12-01';
|
Chris@0
|
824 $time_value = '49:00:00';
|
Chris@0
|
825 $edit = [
|
Chris@0
|
826 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
827 "{$field_name}[0][value][time]" => $time_value,
|
Chris@0
|
828 ];
|
Chris@0
|
829 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
830 $this->assertText('date is invalid', format_string('Invalid hour value %time has been caught.', ['%time' => $time_value]));
|
Chris@0
|
831
|
Chris@0
|
832 $date_value = '2012-12-01';
|
Chris@0
|
833 $time_value = '12:99:00';
|
Chris@0
|
834 $edit = [
|
Chris@0
|
835 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
836 "{$field_name}[0][value][time]" => $time_value,
|
Chris@0
|
837 ];
|
Chris@0
|
838 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
839 $this->assertText('date is invalid', format_string('Invalid minute value %time has been caught.', ['%time' => $time_value]));
|
Chris@0
|
840
|
Chris@0
|
841 $date_value = '2012-12-01';
|
Chris@0
|
842 $time_value = '12:15:99';
|
Chris@0
|
843 $edit = [
|
Chris@0
|
844 "{$field_name}[0][value][date]" => $date_value,
|
Chris@0
|
845 "{$field_name}[0][value][time]" => $time_value,
|
Chris@0
|
846 ];
|
Chris@0
|
847 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
848 $this->assertText('date is invalid', format_string('Invalid second value %time has been caught.', ['%time' => $time_value]));
|
Chris@0
|
849 }
|
Chris@0
|
850
|
Chris@0
|
851 /**
|
Chris@0
|
852 * Tests that 'Date' field storage setting form is disabled if field has data.
|
Chris@0
|
853 */
|
Chris@0
|
854 public function testDateStorageSettings() {
|
Chris@0
|
855 // Create a test content type.
|
Chris@0
|
856 $this->drupalCreateContentType(['type' => 'date_content']);
|
Chris@0
|
857
|
Chris@0
|
858 // Create a field storage with settings to validate.
|
Chris@17
|
859 $field_name = mb_strtolower($this->randomMachineName());
|
Chris@0
|
860 $field_storage = FieldStorageConfig::create([
|
Chris@0
|
861 'field_name' => $field_name,
|
Chris@0
|
862 'entity_type' => 'node',
|
Chris@0
|
863 'type' => 'datetime',
|
Chris@0
|
864 'settings' => [
|
Chris@0
|
865 'datetime_type' => 'date',
|
Chris@0
|
866 ],
|
Chris@0
|
867 ]);
|
Chris@0
|
868 $field_storage->save();
|
Chris@0
|
869 $field = FieldConfig::create([
|
Chris@0
|
870 'field_storage' => $field_storage,
|
Chris@0
|
871 'field_name' => $field_name,
|
Chris@0
|
872 'bundle' => 'date_content',
|
Chris@0
|
873 ]);
|
Chris@0
|
874 $field->save();
|
Chris@0
|
875
|
Chris@0
|
876 entity_get_form_display('node', 'date_content', 'default')
|
Chris@0
|
877 ->setComponent($field_name, [
|
Chris@0
|
878 'type' => 'datetime_default',
|
Chris@0
|
879 ])
|
Chris@0
|
880 ->save();
|
Chris@0
|
881 $edit = [
|
Chris@0
|
882 'title[0][value]' => $this->randomString(),
|
Chris@0
|
883 'body[0][value]' => $this->randomString(),
|
Chris@0
|
884 $field_name . '[0][value][date]' => '2016-04-01',
|
Chris@0
|
885 ];
|
Chris@0
|
886 $this->drupalPostForm('node/add/date_content', $edit, t('Save'));
|
Chris@0
|
887 $this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name . '/storage');
|
Chris@0
|
888 $result = $this->xpath("//*[@id='edit-settings-datetime-type' and contains(@disabled, 'disabled')]");
|
Chris@0
|
889 $this->assertEqual(count($result), 1, "Changing datetime setting is disabled.");
|
Chris@0
|
890 $this->assertText('There is data for this field in the database. The field settings can no longer be changed.');
|
Chris@0
|
891 }
|
Chris@0
|
892
|
Chris@0
|
893 }
|