comparison core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\datetime\Functional; 3 namespace Drupal\Tests\datetime\Functional;
4 4
5 use Drupal\Component\Render\FormattableMarkup; 5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Datetime\DrupalDateTime; 6 use Drupal\Core\Datetime\DrupalDateTime;
9 use Drupal\Core\Datetime\Entity\DateFormat; 7 use Drupal\Core\Datetime\Entity\DateFormat;
10 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; 8 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
11 use Drupal\entity_test\Entity\EntityTest; 9 use Drupal\entity_test\Entity\EntityTest;
12 use Drupal\field\Entity\FieldConfig; 10 use Drupal\field\Entity\FieldConfig;
171 169
172 // Verify that the 'datetime_time_ago' formatter works for intervals in the 170 // Verify that the 'datetime_time_ago' formatter works for intervals in the
173 // past. First update the test entity so that the date difference always 171 // past. First update the test entity so that the date difference always
174 // has the same interval. Since the database always stores UTC, and the 172 // has the same interval. Since the database always stores UTC, and the
175 // interval will use this, force the test date to use UTC and not the local 173 // interval will use this, force the test date to use UTC and not the local
176 // or user timezome. 174 // or user timezone.
177 $timestamp = REQUEST_TIME - 87654321; 175 $timestamp = REQUEST_TIME - 87654321;
178 $entity = EntityTest::load($id); 176 $entity = EntityTest::load($id);
179 $field_name = $this->fieldStorage->getName(); 177 $field_name = $this->fieldStorage->getName();
180 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); 178 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
181 $entity->{$field_name}->value = $date->format($date_format); 179 $entity->{$field_name}->value = $date->format($date_format);
188 'granularity' => 3, 186 'granularity' => 3,
189 ]; 187 ];
190 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 188 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
191 ->setComponent($field_name, $this->displayOptions) 189 ->setComponent($field_name, $this->displayOptions)
192 ->save(); 190 ->save();
193 $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ 191 $expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
194 '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) 192 '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
195 ]); 193 ]);
196 $output = $this->renderTestEntity($id); 194 $output = $this->renderTestEntity($id);
197 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [ 195 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
198 '%expected' => $expected, 196 '%expected' => $expected,
199 '%timezone' => $timezone, 197 '%timezone' => $timezone,
201 199
202 // Verify that the 'datetime_time_ago' formatter works for intervals in the 200 // Verify that the 'datetime_time_ago' formatter works for intervals in the
203 // future. First update the test entity so that the date difference always 201 // future. First update the test entity so that the date difference always
204 // has the same interval. Since the database always stores UTC, and the 202 // has the same interval. Since the database always stores UTC, and the
205 // interval will use this, force the test date to use UTC and not the local 203 // interval will use this, force the test date to use UTC and not the local
206 // or user timezome. 204 // or user timezone.
207 $timestamp = REQUEST_TIME + 87654321; 205 $timestamp = REQUEST_TIME + 87654321;
208 $entity = EntityTest::load($id); 206 $entity = EntityTest::load($id);
209 $field_name = $this->fieldStorage->getName(); 207 $field_name = $this->fieldStorage->getName();
210 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); 208 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
211 $entity->{$field_name}->value = $date->format($date_format); 209 $entity->{$field_name}->value = $date->format($date_format);
212 $entity->save(); 210 $entity->save();
213 211
214 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 212 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
215 ->setComponent($field_name, $this->displayOptions) 213 ->setComponent($field_name, $this->displayOptions)
216 ->save(); 214 ->save();
217 $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ 215 $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
218 '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) 216 '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
219 ]); 217 ]);
220 $output = $this->renderTestEntity($id); 218 $output = $this->renderTestEntity($id);
221 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [ 219 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
222 '%expected' => $expected, 220 '%expected' => $expected,
223 '%timezone' => $timezone, 221 '%timezone' => $timezone,
283 // Verify that a date is displayed. 281 // Verify that a date is displayed.
284 $expected = format_date($date->getTimestamp(), $new_value); 282 $expected = format_date($date->getTimestamp(), $new_value);
285 $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); 283 $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
286 $output = $this->renderTestEntity($id); 284 $output = $this->renderTestEntity($id);
287 $expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>'; 285 $expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
288 $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])); 286 $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]));
289 break; 287 break;
290 } 288 }
291 } 289 }
292 } 290 }
293 291
297 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 295 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
298 ->setComponent($field_name, $this->displayOptions) 296 ->setComponent($field_name, $this->displayOptions)
299 ->save(); 297 ->save();
300 $expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT); 298 $expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
301 $output = $this->renderTestEntity($id); 299 $output = $this->renderTestEntity($id);
302 $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); 300 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
303 301
304 // Verify that the 'datetime_custom' formatter works. 302 // Verify that the 'datetime_custom' formatter works.
305 $this->displayOptions['type'] = 'datetime_custom'; 303 $this->displayOptions['type'] = 'datetime_custom';
306 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings; 304 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings;
307 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 305 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
308 ->setComponent($field_name, $this->displayOptions) 306 ->setComponent($field_name, $this->displayOptions)
309 ->save(); 307 ->save();
310 $expected = $date->format($this->displayOptions['settings']['date_format']); 308 $expected = $date->format($this->displayOptions['settings']['date_format']);
311 $output = $this->renderTestEntity($id); 309 $output = $this->renderTestEntity($id);
312 $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); 310 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
313 311
314 // Verify that the 'timezone_override' setting works. 312 // Verify that the 'timezone_override' setting works.
315 $this->displayOptions['type'] = 'datetime_custom'; 313 $this->displayOptions['type'] = 'datetime_custom';
316 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings; 314 $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings;
317 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 315 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
318 ->setComponent($field_name, $this->displayOptions) 316 ->setComponent($field_name, $this->displayOptions)
319 ->save(); 317 ->save();
320 $expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); 318 $expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
321 $output = $this->renderTestEntity($id); 319 $output = $this->renderTestEntity($id);
322 $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); 320 $this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
323 321
324 // Verify that the 'datetime_time_ago' formatter works for intervals in the 322 // Verify that the 'datetime_time_ago' formatter works for intervals in the
325 // past. First update the test entity so that the date difference always 323 // past. First update the test entity so that the date difference always
326 // has the same interval. Since the database always stores UTC, and the 324 // has the same interval. Since the database always stores UTC, and the
327 // interval will use this, force the test date to use UTC and not the local 325 // interval will use this, force the test date to use UTC and not the local
328 // or user timezome. 326 // or user timezone.
329 $timestamp = REQUEST_TIME - 87654321; 327 $timestamp = REQUEST_TIME - 87654321;
330 $entity = EntityTest::load($id); 328 $entity = EntityTest::load($id);
331 $field_name = $this->fieldStorage->getName(); 329 $field_name = $this->fieldStorage->getName();
332 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); 330 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
333 $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT); 331 $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
340 'granularity' => 3, 338 'granularity' => 3,
341 ]; 339 ];
342 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 340 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
343 ->setComponent($field_name, $this->displayOptions) 341 ->setComponent($field_name, $this->displayOptions)
344 ->save(); 342 ->save();
345 $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ 343 $expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
346 '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) 344 '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
347 ]); 345 ]);
348 $output = $this->renderTestEntity($id); 346 $output = $this->renderTestEntity($id);
349 $this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); 347 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
350 348
351 // Verify that the 'datetime_time_ago' formatter works for intervals in the 349 // Verify that the 'datetime_time_ago' formatter works for intervals in the
352 // future. First update the test entity so that the date difference always 350 // future. First update the test entity so that the date difference always
353 // has the same interval. Since the database always stores UTC, and the 351 // has the same interval. Since the database always stores UTC, and the
354 // interval will use this, force the test date to use UTC and not the local 352 // interval will use this, force the test date to use UTC and not the local
355 // or user timezome. 353 // or user timezone.
356 $timestamp = REQUEST_TIME + 87654321; 354 $timestamp = REQUEST_TIME + 87654321;
357 $entity = EntityTest::load($id); 355 $entity = EntityTest::load($id);
358 $field_name = $this->fieldStorage->getName(); 356 $field_name = $this->fieldStorage->getName();
359 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC'); 357 $date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
360 $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT); 358 $entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
361 $entity->save(); 359 $entity->save();
362 360
363 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') 361 entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
364 ->setComponent($field_name, $this->displayOptions) 362 ->setComponent($field_name, $this->displayOptions)
365 ->save(); 363 ->save();
366 $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ 364 $expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
367 '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) 365 '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
368 ]); 366 ]);
369 $output = $this->renderTestEntity($id); 367 $output = $this->renderTestEntity($id);
370 $this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); 368 $this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
371 } 369 }
372 370
373 /** 371 /**
374 * Tests Date List Widget functionality. 372 * Tests Date List Widget functionality.
375 */ 373 */
646 public function testDefaultValue() { 644 public function testDefaultValue() {
647 // Create a test content type. 645 // Create a test content type.
648 $this->drupalCreateContentType(['type' => 'date_content']); 646 $this->drupalCreateContentType(['type' => 'date_content']);
649 647
650 // Create a field storage with settings to validate. 648 // Create a field storage with settings to validate.
651 $field_name = Unicode::strtolower($this->randomMachineName()); 649 $field_name = mb_strtolower($this->randomMachineName());
652 $field_storage = FieldStorageConfig::create([ 650 $field_storage = FieldStorageConfig::create([
653 'field_name' => $field_name, 651 'field_name' => $field_name,
654 'entity_type' => 'node', 652 'entity_type' => 'node',
655 'type' => 'datetime', 653 'type' => 'datetime',
656 'settings' => ['datetime_type' => 'date'], 654 'settings' => ['datetime_type' => 'date'],
853 public function testDateStorageSettings() { 851 public function testDateStorageSettings() {
854 // Create a test content type. 852 // Create a test content type.
855 $this->drupalCreateContentType(['type' => 'date_content']); 853 $this->drupalCreateContentType(['type' => 'date_content']);
856 854
857 // Create a field storage with settings to validate. 855 // Create a field storage with settings to validate.
858 $field_name = Unicode::strtolower($this->randomMachineName()); 856 $field_name = mb_strtolower($this->randomMachineName());
859 $field_storage = FieldStorageConfig::create([ 857 $field_storage = FieldStorageConfig::create([
860 'field_name' => $field_name, 858 'field_name' => $field_name,
861 'entity_type' => 'node', 859 'entity_type' => 'node',
862 'type' => 'datetime', 860 'type' => 'datetime',
863 'settings' => [ 861 'settings' => [