diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php index ccfc043..d45caca 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php @@ -11,10 +11,10 @@ use Drupal\Core\Ajax\ReplaceCommand; use Drupal\Core\Config\Entity\ConfigStorageController; use Drupal\Core\Datetime\Date; -use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Datetime\DrupalDateTime; -use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Entity\EntityFormController; +use Drupal\Core\Entity\Query\QueryFactory; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a base form controller for date formats. @@ -124,6 +124,24 @@ public static function dateTimeLookup(array $form, array $form_state) { } /** + * Checks 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", FALSE + * otherwise. + */ + public function isInvalidMachineName($machine_name, array $element, array $form_state) { + return ($machine_name == 'custom') || $this->exists($machine_name, $element, $form_state); + } + + /** * {@inheritdoc} */ public function form(array $form, array &$form_state) { @@ -141,7 +159,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, 'isInvalidMachineName'), ), ); 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..0f24773 --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/System/DateFormatsMachineNameTest.php @@ -0,0 +1,76 @@ + 'Date format ids', + 'description' => 'Tests validity of date format 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 date formats cannot be created with invalid machine names. + */ + public function testDateFormatsMachineNameAllowedValues() { + // Try to create a date format with the reserved 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(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 "custom".'); + + // Try to create a date format with the reserved 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(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".'); + + // Create a date format with a machine name distinct from the previous two. + $id = Unicode::strtolower($this->randomName(16)); + $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(t('Custom date format added.'), 'It is possible to create a date format with a new machine name.'); + + // 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(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.'); + } + +}