3 namespace Drupal\Tests\system\Functional\System;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests validity of date format machine names.
12 class DateFormatsMachineNameTest extends BrowserTestBase {
17 protected function setUp() {
19 // Create a new administrator user for the test.
20 $admin = $this->drupalCreateUser(['administer site configuration']);
21 $this->drupalLogin($admin);
25 * Tests that date formats cannot be created with invalid machine names.
27 public function testDateFormatsMachineNameAllowedValues() {
28 // Try to create a date format with a not allowed character to test the date
29 // format specific machine name replace pattern.
31 'label' => 'Something Not Allowed',
32 'id' => 'something.bad',
33 'date_format_pattern' => 'Y-m-d',
35 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
36 $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.');
38 // Try to create a date format with the reserved machine name "custom".
42 'date_format_pattern' => 'Y-m-d',
44 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
45 $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".');
47 // Try to create a date format with a machine name, "fallback", that
50 'label' => 'Fallback',
52 'date_format_pattern' => 'j/m/Y',
54 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
55 $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.');
57 // Create a date format with a machine name distinct from the previous two.
58 $id = mb_strtolower($this->randomMachineName(16));
60 'label' => $this->randomMachineName(16),
62 'date_format_pattern' => 'd/m/Y',
64 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
65 $this->assertText(t('Custom date format added.'), 'It is possible to create a date format with a new machine name.');
67 // Try to create a date format with same machine name as the previous one.
69 'label' => $this->randomMachineName(16),
71 'date_format_pattern' => 'd-m-Y',
73 $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
74 $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.');