diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php index 2205942..e9561e1 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php @@ -112,6 +112,27 @@ public static function dateTimeLookup(array $form, array $form_state) { } /** + * Callback to check if a machine name cannot be used for a new date format. + * + * @param string $machine_name + * The machine name. + * @param array $element + * The form element. + * @param array $form_state + * The form state. + * + * @return bool + * TRUE if this format already exists, or is equal to "custom" or + * "fallback". FALSE otherwise. + */ + public function allowedMachineName($machine_name, array $element, array $form_state) { + if ($machine_name == 'custom' || $machine_name == 'fallback') { + return TRUE; + } + return $this->exists($machine_name, $element, $form_state); + } + + /** * {@inheritdoc} */ public function form(array $form, array &$form_state) { @@ -129,7 +150,7 @@ public function form(array $form, array &$form_state) { '#disabled' => !$this->entity->isNew(), '#default_value' => $this->entity->id(), '#machine_name' => array( - 'exists' => array($this, 'exists'), + 'exists' => array($this, 'allowedMachineName'), ), ); diff --git a/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsMachineNameTest.php b/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsMachineNameTest.php new file mode 100644 index 0000000..6338aa9 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsMachineNameTest.php @@ -0,0 +1,85 @@ + 'Date Formats machine names', + 'description' => 'Test DateTime date formats machine names.', + 'group' => 'System', + ); + } + + public function setUp() { + parent::setUp(); + // Create a new administrator user for the test. + $admin = $this->drupalCreateUser(array('administer site configuration')); + $this->drupalLogin($admin); + } + + /** + * Tests that machine names cannot be created with not allowed values. + */ + public function testDateFormatsMachineNameAllowedValues() { + // Try to create a date format with machine name "custom". + $edit = array( + 'label' => 'Custom', + 'id' => 'custom', + 'date_format_pattern' => 'Y-m-d', + ); + $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format')); + $this->assertText('The machine-readable name is already in use. It must be unique.'); + + // Try to create a date format with machine name "fallback". + $edit = array( + 'label' => 'Fallback', + 'id' => 'fallback', + 'date_format_pattern' => 'j/m/Y', + ); + $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format')); + $this->assertText('The machine-readable name is already in use. It must be unique.'); + + // Create a date format with a machine name distinct from the previous two. + do { + $id = Unicode::strtolower($this->randomName(16)); + } while ($id == 'custom' || $id == 'fallback'); + $edit = array( + 'label' => $this->randomName(16), + 'id' => $id, + 'date_format_pattern' => 'd/m/Y', + ); + $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format')); + $this->assertText('Custom date format added.'); + + // Try to create a date format with same machine name as the previous one. + $edit = array( + 'label' => $this->randomName(16), + 'id' => $id, + 'date_format_pattern' => 'd-m-Y', + ); + $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format')); + $this->assertText('The machine-readable name is already in use. It must be unique.'); + } + +}