3 namespace Drupal\Tests\system\Functional\System;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Tests\BrowserTestBase;
9 * Tests validity of date format machine names.
13 class DateFormatsMachineNameTest extends BrowserTestBase {
18 protected function setUp() {
20 // Create a new administrator user for the test.
21 $admin = $this->drupalCreateUser(['administer site configuration']);
22 $this->drupalLogin($admin);
26 * Tests that date formats cannot be created with invalid machine names.
28 public function testDateFormatsMachineNameAllowedValues() {
29 // Try to create a date format with a not allowed character to test the date
30 // format specific machine name replace pattern.
32 'label' => 'Something Not Allowed',
33 'id' => 'something.bad',
34 'date_format_pattern' => 'Y-m-d',
36 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
37 $this->assertText(t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'), 'It is not possible to create a date format with the machine name that has any character other than lowercase letters, digits or underscore.');
39 // Try to create a date format with the reserved machine name "custom".
43 'date_format_pattern' => 'Y-m-d',
45 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
46 $this->assertText(t('The machine-readable name must be unique, and can only contain lowercase letters, numbers, and underscores. Additionally, it can not be the reserved word "custom".'), 'It is not possible to create a date format with the machine name "custom".');
48 // Try to create a date format with a machine name, "fallback", that
51 'label' => 'Fallback',
53 'date_format_pattern' => 'j/m/Y',
55 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
56 $this->assertText(t('The machine-readable name is already in use. It must be unique.'), 'It is not possible to create a date format with the machine name "fallback". It is a built-in format that already exists.');
58 // Create a date format with a machine name distinct from the previous two.
59 $id = Unicode::strtolower($this->randomMachineName(16));
61 'label' => $this->randomMachineName(16),
63 'date_format_pattern' => 'd/m/Y',
65 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
66 $this->assertText(t('Custom date format added.'), 'It is possible to create a date format with a new machine name.');
68 // Try to create a date format with same machine name as the previous one.
70 'label' => $this->randomMachineName(16),
72 'date_format_pattern' => 'd-m-Y',
74 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
75 $this->assertText(t('The machine-readable name is already in use. It must be unique.'), 'It is not possible to create a new date format with an existing machine name.');