diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php index 4656897..e54bd50 100644 --- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php +++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php @@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityStorageBase; use Drupal\Core\Config\Config; use Drupal\Core\Config\StorageInterface; +use Drupal\Core\Config\ConfigNameException; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\Query\QueryFactory; @@ -38,6 +39,13 @@ class ConfigEntityStorage extends EntityStorageBase implements ConfigEntityStorageInterface, ImportableEntityStorageInterface { /** + * The maximum length of a config entity ID. + * + * @see \Drupal\Core\ConfigBase::MAX_NAME_LENGTH + */ + const MAX_ID_LENGTH = 166; + + /** * The UUID service. * * @var \Drupal\Component\Uuid\UuidInterface @@ -315,6 +323,15 @@ public function save(EntityInterface $entity) { $entity->{$this->idKey} = $entity->id(); } + // Check config entity ID length. + // @see \Drupal\Core\Config\Entity\ConfigEntityStorage::MAX_ID_LENGTH + if (strlen($entity->{$this->idKey}) > self::MAX_ID_LENGTH) { + throw new ConfigNameException(String::format('Config entity ID @id exceeds maximum allowed length of @len characters.', array( + '@id' => $entity->{$this->idKey}, + '@len' => self::MAX_ID_LENGTH, + ))); + } + $entity->preSave($this); $this->invokeHook('presave', $entity); diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php index 19ebaff..adacde2 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigEntityTest.php @@ -7,8 +7,11 @@ namespace Drupal\config\Tests; +use Drupal\Component\Utility\String; use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityStorageException; +use Drupal\Core\Config\Entity\ConfigEntityStorage; +use Drupal\Core\Config\ConfigNameException; use Drupal\Core\Language\Language; use Drupal\simpletest\WebTestBase; @@ -142,6 +145,56 @@ function testCRUD() { $this->assertIdentical($config_test->isNew(), FALSE); $this->assertIdentical($config_test->getOriginalId(), $expected['id']); + // Verify that a configuration entity can be saved with + // an ID of maximum allowed length, but not longer. + + // Test with a short ID + // By default randomName() generates a 8 character long name + $config_test_idlen = entity_create('config_test', array( + 'id' => $this->randomName(), + )); + try { + $config_test_idlen->save(); + $this->pass(String::format("config_test entity with ID length @len was saved.", array( + '@len' => strlen($config_test_idlen->id)) + )); + } + catch (ConfigNameException $e) { + $this->fail($e->getMessage()); + } + + // Test with an ID of maximum allowed length + $config_test_idlen = entity_create('config_test', array( + 'id' => $this->randomName(ConfigEntityStorage::MAX_ID_LENGTH), + )); + try { + $config_test_idlen->save(); + $this->pass(String::format("config_test entity with ID length @len was saved.", array( + '@len' => strlen($config_test_idlen->id), + ))); + } + catch (ConfigNameException $e) { + $this->fail($e->getMessage()); + } + + // Test with an ID exeeding the maximum allowed length + $config_test_idlen = entity_create('config_test', array( + 'id' => $this->randomName(ConfigEntityStorage::MAX_ID_LENGTH+1), + )); + try { + $status = $config_test_idlen->save(); + $this->fail(String::format("config_test entity with ID length @len, exceeding the maximum allowed length of @max saved successfully", array( + '@len' => strlen($config_test_idlen->id), + '@max' => ConfigEntityStorage::MAX_ID_LENGTH, + ))); + } + catch (ConfigNameException $e) { + $this->pass(String::format("config_test entity with ID length @len, exceeding the maximum allowed length of @max failed to save", array( + '@len' => strlen($config_test_idlen->id), + '@max' => ConfigEntityStorage::MAX_ID_LENGTH, + ))); + } + // Ensure that creating an entity with the same id as an existing one is not // possible. $same_id = entity_create('config_test', array(