You are here

public function TimestampTest::testWidget in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php \Drupal\FunctionalTests\Datetime\TimestampTest::testWidget()
  2. 9 core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php \Drupal\FunctionalTests\Datetime\TimestampTest::testWidget()

Tests the "datetime_timestamp" widget.

File

core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php, line 107

Class

TimestampTest
Tests the functionality of Timestamp core field UI.

Namespace

Drupal\FunctionalTests\Datetime

Code

public function testWidget() {

  // Build up a date in the UTC timezone.
  $value = '2012-12-31 00:00:00';
  $date = new DrupalDateTime($value, 'UTC');

  // Update the timezone to the system default.
  $date
    ->setTimezone(timezone_open(date_default_timezone_get()));

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');

  // Make sure the field description is properly displayed.
  $this
    ->assertSession()
    ->pageTextContains('Description for timestamp field.');

  // Make sure the "datetime_timestamp" widget is on the page.
  $this
    ->assertSession()
    ->elementsCount('xpath', '//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]', 1);

  // Look for the widget elements and make sure they are empty.
  $this
    ->assertSession()
    ->fieldExists('field_timestamp[0][value][date]');
  $this
    ->assertSession()
    ->fieldValueEquals('field_timestamp[0][value][date]', '');
  $this
    ->assertSession()
    ->fieldExists('field_timestamp[0][value][time]');
  $this
    ->assertSession()
    ->fieldValueEquals('field_timestamp[0][value][time]', '');

  // Submit the date.
  $date_format = DateFormat::load('html_date')
    ->getPattern();
  $time_format = DateFormat::load('html_time')
    ->getPattern();
  $edit = [
    'field_timestamp[0][value][date]' => $date
      ->format($date_format),
    'field_timestamp[0][value][time]' => $date
      ->format($time_format),
  ];
  $this
    ->submitForm($edit, 'Save');

  // Make sure the submitted date is set as the default in the widget.
  $this
    ->assertSession()
    ->fieldExists('field_timestamp[0][value][date]');
  $this
    ->assertSession()
    ->fieldValueEquals('field_timestamp[0][value][date]', $date
    ->format($date_format));
  $this
    ->assertSession()
    ->fieldExists('field_timestamp[0][value][time]');
  $this
    ->assertSession()
    ->fieldValueEquals('field_timestamp[0][value][time]', $date
    ->format($time_format));

  // Make sure the entity was saved.
  preg_match('|entity_test/manage/(\\d+)|', $this
    ->getSession()
    ->getCurrentUrl(), $match);
  $id = $match[1];
  $this
    ->assertSession()
    ->pageTextContains(sprintf('entity_test %s has been created.', $id));

  // Make sure the timestamp is output properly with the default formatter.
  $medium = DateFormat::load('medium')
    ->getPattern();
  $this
    ->drupalGet('entity_test/' . $id);
  $this
    ->assertSession()
    ->pageTextContains($date
    ->format($medium));
}