diff --git a/core/lib/Drupal/Core/Config/ConfigPrefixLengthException.php b/core/lib/Drupal/Core/Config/ConfigPrefixLengthException.php new file mode 100644 index 0000000..d915238 --- /dev/null +++ b/core/lib/Drupal/Core/Config/ConfigPrefixLengthException.php @@ -0,0 +1,13 @@ +config_prefix)) { - $config_prefix = $this->config_prefix; - } - else { - $config_prefix = $this->id(); - } // Ensure that all configuration entities are prefixed by the module that // provides the configuration entity type. This ensures that default // configuration will be created as expected during module install and // dependencies can be calculated without the modules that provide the // entity types being installed. - return $this->provider . '.' . $config_prefix; + if (isset($this->config_prefix)) { + $config_prefix = $this->provider . '.' . $this->config_prefix; + } + else { + $config_prefix = $this->provider . '.' . $this->id(); + } + + if (strlen($config_prefix) > 83) { + throw new ConfigPrefixLengthException(String::format('The @config_prefix config_prefix length is larger than the maximum limit of @char_count characters', array( + '@config_prefix' => $config_prefix, + '@char_count' => 83, + ))); + } + return $config_prefix; } /** diff --git a/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php new file mode 100644 index 0000000..2366a86 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Config/Entity/ConfigEntityTypeTest.php @@ -0,0 +1,100 @@ + '', + 'name' => '\Drupal\Core\Config\Entity\ConfigEntityType unit test', + 'group' => 'Entity', + ); + } + + /** + * Tests that we get an exception when the config prefix is too long. + * + * @expectedException \Drupal\Core\Config\ConfigPrefixLengthException + * @expectedExceptionMessage The extra_long_provider_name.long_random_configuration_prefix_so_that_go_over_the_limit12 config_prefix length is larger than the maximum limit of 83 characters + * @covers ::getConfigPrefix() + */ + public function testConfigPrefixLengthWithPrefixExceeds() { + // A config entity with a provider length of 24 and config_prefix length of 60 + // (+1 for the .) results in a config length of 85, which is too long. + $config_entity = new ConfigEntityType(array( + 'provider' => 'extra_long_provider_name', + 'config_prefix' => 'long_random_configuration_prefix_so_that_go_over_the_limit12', + )); + $this->assertEmpty($config_entity->getConfigPrefix()); + } + + /** + * Tests that we get an exception when the config prefix is too long. + * + * @expectedException \Drupal\Core\Config\ConfigPrefixLengthException + * @expectedExceptionMessage The extra_long_provider_name.long_random_entity_id_so_that_we_will_go_over_the_limit12345 config_prefix length is larger than the maximum limit of 83 characters + * @covers ::getConfigPrefix() + */ + public function testConfigPrefixLengthWithIdExceeds() { + // A config entity with an provider length of 24 and id length of 60 + // (+1 for the .) results in a config length of 85, which is too long. + $config_entity = new ConfigEntityType(array( + 'provider' => 'extra_long_provider_name', + 'id' => 'long_random_entity_id_so_that_we_will_go_over_the_limit12345', + )); + $this->assertEmpty($config_entity->getConfigPrefix()); + } + + /** + * Test that we do not get a any exceptions when the config prefix is not too long. + * + * @covers ::getConfigPrefix() + */ + public function testConfigPrefixLengthWithPrefixNormal() { + // A config entity with a provider length of 24 and config_prefix length of 58 + // (+1 for the .) results in a config length of 83, which is right at the limit. + $entity_data = array( + 'provider' => 'extra_long_provider_name', + 'config_prefix' => 'random_configiguration_prefix_that_exactly_at_limit_123456', + ); + $config_entity = new ConfigEntityType($entity_data); + $expected_prefix = $entity_data['provider'] . '.' . $entity_data['config_prefix']; + $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix()); + } + + /** + * Test that we do not get a any exceptionswhen the config prefix is not too long. + * + * @covers ::getConfigPrefix() + */ + public function testConfigPrefixLengthWithIdNormal() { + // A config entity with an provider length of 24 and id length of 58 + // (+1 for the .) results in a config length of 83, which is right at the limit. + $entity_data = array( + 'provider' => 'extra_long_provider_name', + 'id' => 'random_entity_id_that_we_will_be_exactly_at_limit_12345678', + ); + $config_entity = new ConfigEntityType($entity_data); + $expected_prefix = $entity_data['provider'] . '.' . $entity_data['id']; + $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix()); + } + +}