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