You are here

public function StoreDateTimeTest::testFormatter in Commerce Core 8.2

Tests the formatter.

File

modules/store/tests/src/Functional/StoreDateTimeTest.php, line 147

Class

StoreDateTimeTest
Tests the commerce_store_datetime formatter and widget.

Namespace

Drupal\Tests\commerce_store\Functional

Code

public function testFormatter() {
  $storage_format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
  $date = new DrupalDateTime('2019-10-31 12:15:30', 'UTC');
  $entity = $this
    ->createEntity('entity_test', [
    'name' => 'Test',
    'test_date' => $date
      ->format($storage_format),
  ]);

  // Confirm that the value is rendered using the "medium" date format.
  $date_format = DateFormat::load('medium');
  $date_pattern = $date_format
    ->getPattern();
  $this
    ->drupalGet($entity
    ->toUrl('canonical'));
  $this
    ->assertSession()
    ->pageTextContains('Test date!');
  $this
    ->assertSession()
    ->pageTextContains($date
    ->format($date_pattern));

  // Confirm that a custom date format can be used.
  $date_format = DateFormat::create([
    'id' => 'test_format',
    'name' => 'Test Format',
    'pattern' => 'Y-m-d H:i:s T',
  ]);
  $date_format
    ->save();
  $view_display = commerce_get_entity_display('entity_test', 'entity_test', 'view');
  $view_display
    ->setComponent('test_date', [
    'type' => 'commerce_store_datetime',
    'settings' => [
      'date_format' => 'test_format',
    ],
  ])
    ->save();
  $this
    ->drupalGet($entity
    ->toUrl('canonical'));
  $this
    ->assertSession()
    ->pageTextContains('2019-10-31 12:15:30 AEDT');

  // Confirm that changing the timezone does not change the value.
  $this->store
    ->setTimezone('America/Chicago');
  $this->store
    ->save();
  $this
    ->drupalGet($entity
    ->toUrl('canonical'));
  $this
    ->assertSession()
    ->pageTextContains('2019-10-31 12:15:30 CDT');

  // Confirm that the site timezone is used if there is no store.
  $this->store
    ->delete();
  $this
    ->drupalGet($entity
    ->toUrl('canonical'));
  $this
    ->assertSession()
    ->pageTextContains('2019-10-31 12:15:30 AEDT');

  // Confirm that the "fallback" date format is used if the specified one
  // is missing.
  $date_format
    ->delete();
  $this
    ->drupalGet($entity
    ->toUrl('canonical'));
  $this
    ->assertSession()
    ->pageTextContains('10/31/2019 - 12:15');
}