You are here

public function DateTimeTest::testDateFormatConfiguration in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/System/DateTimeTest.php \Drupal\Tests\system\Functional\System\DateTimeTest::testDateFormatConfiguration()

Tests date format configuration.

File

core/modules/system/tests/src/Functional/System/DateTimeTest.php, line 96

Class

DateTimeTest
Configure date and time settings. Test date formatting and time zone handling, including daylight saving time.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testDateFormatConfiguration() {

  // Confirm 'no custom date formats available' message appears.
  $this
    ->drupalGet('admin/config/regional/date-time');

  // Add custom date format.
  $this
    ->clickLink('Add format');
  $date_format_id = strtolower($this
    ->randomMachineName(8));
  $name = ucwords($date_format_id);
  $date_format = 'd.m.Y - H:i';
  $edit = [
    'id' => $date_format_id,
    'label' => $name,
    'date_format_pattern' => $date_format,
  ];
  $this
    ->drupalGet('admin/config/regional/date-time/formats/add');
  $this
    ->submitForm($edit, 'Add format');

  // Verify that the user is redirected to the correct page.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.date_format.collection'));

  // Check that date format added confirmation message appears.
  $this
    ->assertSession()
    ->pageTextContains('Custom date format added.');

  // Check that custom date format appears in the date format list.
  $this
    ->assertSession()
    ->pageTextContains($name);

  // Check that the delete link for custom date format appears.
  $this
    ->assertSession()
    ->pageTextContains('Delete');

  // Edit the custom date format and re-save without editing the format.
  $this
    ->drupalGet('admin/config/regional/date-time');
  $this
    ->clickLink('Edit');
  $this
    ->submitForm([], 'Save format');

  // Verify that the user is redirected to the correct page.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.date_format.collection'));
  $this
    ->assertSession()
    ->pageTextContains('Custom date format updated.');

  // Edit custom date format.
  $this
    ->drupalGet('admin/config/regional/date-time');
  $this
    ->clickLink('Edit');
  $edit = [
    'date_format_pattern' => 'Y m',
  ];
  $this
    ->drupalGet($this
    ->getUrl());
  $this
    ->submitForm($edit, 'Save format');

  // Verify that the user is redirected to the correct page.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.date_format.collection'));
  $this
    ->assertSession()
    ->pageTextContains('Custom date format updated.');

  // Delete custom date format.
  $this
    ->clickLink('Delete');
  $this
    ->drupalGet('admin/config/regional/date-time/formats/manage/' . $date_format_id . '/delete');
  $this
    ->submitForm([], 'Delete');

  // Verify that the user is redirected to the correct page.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.date_format.collection'));
  $this
    ->assertSession()
    ->pageTextContains("The date format {$name} has been deleted.");

  // Make sure the date does not exist in config.
  $date_format = DateFormat::load($date_format_id);
  $this
    ->assertNull($date_format);

  // Add a new date format with an existing format.
  $date_format_id = strtolower($this
    ->randomMachineName(8));
  $name = ucwords($date_format_id);
  $date_format = 'Y';
  $edit = [
    'id' => $date_format_id,
    'label' => $name,
    'date_format_pattern' => $date_format,
  ];
  $this
    ->drupalGet('admin/config/regional/date-time/formats/add');
  $this
    ->submitForm($edit, 'Add format');

  // Verify that the user is redirected to the correct page.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.date_format.collection'));
  $this
    ->assertSession()
    ->pageTextContains('Custom date format added.');

  // Check that the custom date format appears in the date format list.
  $this
    ->assertSession()
    ->pageTextContains($name);

  // Check that the delete link for custom date format appears.
  $this
    ->assertSession()
    ->pageTextContains('Delete');
  $date_format = DateFormat::create([
    'id' => 'xss_short',
    'label' => 'XSS format',
    'pattern' => '\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\'\\X\\S\\S\'\\)\\;\\<\\/\\s\\c\\r\\i\\p\\t\\>',
  ]);
  $date_format
    ->save();
  $this
    ->drupalGet(Url::fromRoute('entity.date_format.collection'));

  // Ensure that the date format is properly escaped.
  $this
    ->assertSession()
    ->assertEscaped("<script>alert('XSS');</script>");

  // Add a new date format with HTML in it.
  $date_format_id = strtolower($this
    ->randomMachineName(8));
  $name = ucwords($date_format_id);
  $date_format = '& \\<\\e\\m\\>Y\\<\\/\\e\\m\\>';
  $edit = [
    'id' => $date_format_id,
    'label' => $name,
    'date_format_pattern' => $date_format,
  ];
  $this
    ->drupalGet('admin/config/regional/date-time/formats/add');
  $this
    ->submitForm($edit, 'Add format');

  // Verify that the user is redirected to the correct page.
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.date_format.collection'));
  $this
    ->assertSession()
    ->pageTextContains('Custom date format added.');

  // Check that the custom date format appears in the date format list.
  $this
    ->assertSession()
    ->pageTextContains($name);
  $this
    ->assertSession()
    ->assertEscaped('<em>' . date("Y") . '</em>');
}