1<?php
2
3namespace Drupal\Tests\system\Functional\System;
4
5use Drupal\Tests\BrowserTestBase;
6
7/**
8 * Tests validity of date format machine names.
9 *
10 * @group system
11 */
12class DateFormatsMachineNameTest extends BrowserTestBase {
13
14  /**
15   * {@inheritdoc}
16   */
17  protected $defaultTheme = 'stark';
18
19  /**
20   * {@inheritdoc}
21   */
22  protected function setUp(): void {
23    parent::setUp();
24    // Create a new administrator user for the test.
25    $admin = $this->drupalCreateUser(['administer site configuration']);
26    $this->drupalLogin($admin);
27  }
28
29  /**
30   * Tests that date formats cannot be created with invalid machine names.
31   */
32  public function testDateFormatsMachineNameAllowedValues() {
33    // Try to create a date format with a not allowed character to test the date
34    // format specific machine name replace pattern.
35    $edit = [
36      'label' => 'Something Not Allowed',
37      'id' => 'something.bad',
38      'date_format_pattern' => 'Y-m-d',
39    ];
40    $this->drupalGet('admin/config/regional/date-time/formats/add');
41    $this->submitForm($edit, 'Add format');
42    $this->assertSession()->pageTextContains('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".');
43
44    // Try to create a date format with the reserved machine name "custom".
45    $edit = [
46      'label' => 'Custom',
47      'id' => 'custom',
48      'date_format_pattern' => 'Y-m-d',
49    ];
50    $this->drupalGet('admin/config/regional/date-time/formats/add');
51    $this->submitForm($edit, 'Add format');
52    $this->assertSession()->pageTextContains('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".');
53
54    // Try to create a date format with a machine name, "fallback", that
55    // already exists.
56    $edit = [
57      'label' => 'Fallback',
58      'id' => 'fallback',
59      'date_format_pattern' => 'j/m/Y',
60    ];
61    $this->drupalGet('admin/config/regional/date-time/formats/add');
62    $this->submitForm($edit, 'Add format');
63    $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');
64
65    // Create a date format with a machine name distinct from the previous two.
66    $id = mb_strtolower($this->randomMachineName(16));
67    $edit = [
68      'label' => $this->randomMachineName(16),
69      'id' => $id,
70      'date_format_pattern' => 'd/m/Y',
71    ];
72    $this->drupalGet('admin/config/regional/date-time/formats/add');
73    $this->submitForm($edit, 'Add format');
74    $this->assertSession()->pageTextContains('Custom date format added.');
75
76    // Try to create a date format with same machine name as the previous one.
77    $edit = [
78      'label' => $this->randomMachineName(16),
79      'id' => $id,
80      'date_format_pattern' => 'd-m-Y',
81    ];
82    $this->drupalGet('admin/config/regional/date-time/formats/add');
83    $this->submitForm($edit, 'Add format');
84    $this->assertSession()->pageTextContains('The machine-readable name is already in use. It must be unique.');
85  }
86
87}
88