You are here

public function StoreDateTimeTest::testWidget in Commerce Core 8.2

Tests the widget.

File

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

Class

StoreDateTimeTest
Tests the commerce_store_datetime formatter and widget.

Namespace

Drupal\Tests\commerce_store\Functional

Code

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

  // Confirm that a date/time value can be added.
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->pageTextContains('Test date!');
  $this
    ->assertSession()
    ->fieldExists('test_date[0][value][date]');
  $this
    ->assertSession()
    ->fieldExists('test_date[0][value][time]');
  $this
    ->submitForm([
    'test_date[0][value][date]' => $date
      ->format('Y-m-d'),
    'test_date[0][value][time]' => $date
      ->format('H:i:s'),
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('entity_test 1 has been updated.');
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEquals($date
    ->format($storage_format), $entity
    ->get('test_date')->value);

  // Confirm that a date/time value can be edited.
  $new_date = new DrupalDateTime('2019-11-15 11:10:15', 'UTC');
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->pageTextContains('Test date!');
  $this
    ->assertSession()
    ->fieldValueEquals('test_date[0][value][date]', $date
    ->format('Y-m-d'));
  $this
    ->assertSession()
    ->fieldValueEquals('test_date[0][value][time]', $date
    ->format('H:i:s'));
  $this
    ->submitForm([
    'test_date[0][value][date]' => $new_date
      ->format('Y-m-d'),
    'test_date[0][value][time]' => $new_date
      ->format('H:i:s'),
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('entity_test 1 has been updated.');
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEquals($new_date
    ->format($storage_format), $entity
    ->get('test_date')->value);

  // Confirm that changing the store timezone does not change the value.
  $this->store
    ->setTimezone('America/Chicago');
  $this->store
    ->save();
  $new_date = new DrupalDateTime('2019-11-15 11:10:15', 'UTC');
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->fieldValueEquals('test_date[0][value][date]', $new_date
    ->format('Y-m-d'));
  $this
    ->assertSession()
    ->fieldValueEquals('test_date[0][value][time]', $new_date
    ->format('H:i:s'));

  // Confirm that it is possible to enter just a date.
  $storage_format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
  $field_storage = FieldStorageConfig::load('entity_test.test_date');
  $field_storage
    ->setSetting('datetime_type', 'date');
  $field_storage
    ->save();
  $date = new DrupalDateTime('2019-10-31', 'UTC');
  $new_date = new DrupalDateTime('2019-11-15', 'UTC');
  $entity = $this
    ->reloadEntity($entity);
  $entity
    ->set('test_date', $date
    ->format($storage_format));
  $entity
    ->save();
  $this
    ->drupalGet($entity
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->pageTextContains('Test date!');
  $this
    ->assertSession()
    ->fieldValueEquals('test_date[0][value][date]', $date
    ->format('Y-m-d'));
  $this
    ->assertSession()
    ->fieldNotExists('test_date[0][value][time]');
  $this
    ->submitForm([
    'test_date[0][value][date]' => $new_date
      ->format('Y-m-d'),
  ], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('entity_test 1 has been updated.');
  $entity = $this
    ->reloadEntity($entity);
  $this
    ->assertEquals($new_date
    ->format($storage_format), $entity
    ->get('test_date')->value);
}