Version 1
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / DateFormatsMachineNameTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Tests validity of date format machine names.
10  *
11  * @group system
12  */
13 class DateFormatsMachineNameTest extends BrowserTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function setUp() {
19     parent::setUp();
20     // Create a new administrator user for the test.
21     $admin = $this->drupalCreateUser(['administer site configuration']);
22     $this->drupalLogin($admin);
23   }
24
25   /**
26    * Tests that date formats cannot be created with invalid machine names.
27    */
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.
31     $edit = [
32       'label' => 'Something Not Allowed',
33       'id' => 'something.bad',
34       'date_format_pattern' => 'Y-m-d',
35     ];
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.');
38
39     // Try to create a date format with the reserved machine name "custom".
40     $edit = [
41       'label' => 'Custom',
42       'id' => 'custom',
43       'date_format_pattern' => 'Y-m-d',
44     ];
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".');
47
48     // Try to create a date format with a machine name, "fallback", that
49     // already exists.
50     $edit = [
51       'label' => 'Fallback',
52       'id' => 'fallback',
53       'date_format_pattern' => 'j/m/Y',
54     ];
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.');
57
58     // Create a date format with a machine name distinct from the previous two.
59     $id = Unicode::strtolower($this->randomMachineName(16));
60     $edit = [
61       'label' => $this->randomMachineName(16),
62       'id' => $id,
63       'date_format_pattern' => 'd/m/Y',
64     ];
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.');
67
68     // Try to create a date format with same machine name as the previous one.
69     $edit = [
70       'label' => $this->randomMachineName(16),
71       'id' => $id,
72       'date_format_pattern' => 'd-m-Y',
73     ];
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.');
76   }
77
78 }