function DateTimeTest::testDateFormatConfiguration
Same name in other branches
- 9 core/modules/system/tests/src/Functional/System/DateTimeTest.php \Drupal\Tests\system\Functional\System\DateTimeTest::testDateFormatConfiguration()
- 10 core/modules/system/tests/src/Functional/System/DateTimeTest.php \Drupal\Tests\system\Functional\System\DateTimeTest::testDateFormatConfiguration()
- 11.x core/modules/system/tests/src/Functional/System/DateTimeTest.php \Drupal\Tests\system\Functional\System\DateTimeTest::testDateFormatConfiguration()
Test date format configuration.
File
-
core/
modules/ system/ tests/ src/ Functional/ System/ DateTimeTest.php, line 92
Class
- DateTimeTest
- Configure date and time settings. Test date formatting and time zone handling, including daylight saving time.
Namespace
Drupal\Tests\system\Functional\SystemCode
public function testDateFormatConfiguration() {
// Confirm 'no custom date formats available' message appears.
$this->drupalGet('admin/config/regional/date-time');
// Add custom date format.
$this->clickLink(t('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->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
$this->assertUrl(Url::fromRoute('entity.date_format.collection', [], [
'absolute' => TRUE,
])->toString(), [], 'Correct page redirection.');
$this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
$this->assertText($name, 'Custom date format appears in the date format list.');
$this->assertText(t('Delete'), 'Delete link for custom date format appears.');
// Edit the custom date format and re-save without editing the format.
$this->drupalGet('admin/config/regional/date-time');
$this->clickLink(t('Edit'));
$this->drupalPostForm(NULL, NULL, t('Save format'));
$this->assertUrl('admin/config/regional/date-time', [
'absolute' => TRUE,
], 'Correct page redirection.');
$this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
// Edit custom date format.
$this->drupalGet('admin/config/regional/date-time');
$this->clickLink(t('Edit'));
$edit = [
'date_format_pattern' => 'Y m',
];
$this->drupalPostForm($this->getUrl(), $edit, t('Save format'));
$this->assertUrl(Url::fromRoute('entity.date_format.collection', [], [
'absolute' => TRUE,
])->toString(), [], 'Correct page redirection.');
$this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
// Delete custom date format.
$this->clickLink(t('Delete'));
$this->drupalPostForm('admin/config/regional/date-time/formats/manage/' . $date_format_id . '/delete', [], t('Delete'));
$this->assertUrl(Url::fromRoute('entity.date_format.collection', [], [
'absolute' => TRUE,
])->toString(), [], 'Correct page redirection.');
$this->assertRaw(t('The date format %format has been deleted.', [
'%format' => $name,
]), 'Custom date format removed.');
// 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->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
$this->assertUrl(Url::fromRoute('entity.date_format.collection', [], [
'absolute' => TRUE,
])->toString(), [], 'Correct page redirection.');
$this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
$this->assertText($name, 'Custom date format appears in the date format list.');
$this->assertText(t('Delete'), 'Delete link for custom date format appears.');
$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->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->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
$this->assertUrl(Url::fromRoute('entity.date_format.collection', [], [
'absolute' => TRUE,
])->toString(), [], 'Correct page redirection.');
$this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
$this->assertText($name, 'Custom date format appears in the date format list.');
$this->assertEscaped('<em>' . date("Y") . '</em>');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.