3 namespace Drupal\Tests\Core\Config\Entity;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\Config\Entity\ConfigEntityType;
7 use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException;
10 * @coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityType
13 class ConfigEntityTypeTest extends UnitTestCase {
16 * Sets up a ConfigEntityType object for a given set of values.
18 * @param array $definition
19 * An array of values to use for the ConfigEntityType.
21 * @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
23 protected function setUpConfigEntityType($definition) {
24 if (!isset($definition['id'])) {
26 'id' => 'example_config_entity_type',
29 return new ConfigEntityType($definition);
33 * Tests that we get an exception when the length of the config prefix that is
34 * returned by getConfigPrefix() exceeds the maximum defined prefix length.
36 * @covers ::getConfigPrefix
38 public function testConfigPrefixLengthExceeds() {
39 // A provider length of 24 and config_prefix length of 59 (+1 for the .)
40 // results in a config length of 84, which is too long.
42 'provider' => $this->randomMachineName(24),
43 'config_prefix' => $this->randomMachineName(59),
45 $config_entity = $this->setUpConfigEntityType($definition);
46 $this->setExpectedException(
47 '\Drupal\Core\Config\ConfigPrefixLengthException',
48 "The configuration file name prefix {$definition['provider']}.{$definition['config_prefix']} exceeds the maximum character limit of " . ConfigEntityType::PREFIX_LENGTH
50 $this->assertEmpty($config_entity->getConfigPrefix());
54 * Tests that a valid config prefix returned by getConfigPrefix()
55 * does not throw an exception and is formatted as expected.
57 * @covers ::getConfigPrefix
59 public function testConfigPrefixLengthValid() {
60 // A provider length of 24 and config_prefix length of 58 (+1 for the .)
61 // results in a config length of 83, which is right at the limit.
63 'provider' => $this->randomMachineName(24),
64 'config_prefix' => $this->randomMachineName(58),
66 $config_entity = $this->setUpConfigEntityType($definition);
67 $expected_prefix = $definition['provider'] . '.' . $definition['config_prefix'];
68 $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix());
72 * @covers ::__construct
74 public function testConstruct() {
75 $config_entity = new ConfigEntityType([
76 'id' => 'example_config_entity_type',
78 $this->assertEquals('Drupal\Core\Config\Entity\ConfigEntityStorage', $config_entity->getStorageClass());
82 * @covers ::__construct
84 public function testConstructBadStorage() {
85 $this->setExpectedException(ConfigEntityStorageClassException::class, '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it');
86 new ConfigEntityType([
87 'id' => 'example_config_entity_type',
88 'handlers' => ['storage' => '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage']
93 * @covers ::setStorageClass
95 public function testSetStorageClass() {
96 $config_entity = $this->setUpConfigEntityType([]);
97 $this->setExpectedException(ConfigEntityStorageClassException::class, '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it');
98 $config_entity->setStorageClass('\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage');
102 * Tests the getConfigPrefix() method.
104 * @dataProvider providerTestGetConfigPrefix
106 * @covers ::getConfigPrefix
108 public function testGetConfigPrefix($definition, $expected) {
109 $entity_type = $this->setUpConfigEntityType($definition);
110 $this->assertSame($expected, $entity_type->getConfigPrefix());
114 * Provides test data.
116 public function providerTestGetConfigPrefix() {
118 [['provider' => 'node', 'id' => 'node_type', 'config_prefix' => 'type'], 'node.type'],
119 [['provider' => 'views', 'id' => 'view'], 'views.view'],
124 * @covers ::getPropertiesToExport
126 * @dataProvider providerGetPropertiesToExport
128 public function testGetPropertiesToExport($definition, $expected) {
129 $entity_type = $this->setUpConfigEntityType($definition);
130 $properties_to_export = $entity_type->getPropertiesToExport();
131 $this->assertSame($expected, $properties_to_export);
133 // Ensure the method is idempotent.
134 $properties_to_export = $entity_type->getPropertiesToExport();
135 $this->assertSame($expected, $properties_to_export);
138 public function providerGetPropertiesToExport() {
149 'custom_property' => 'customProperty',
154 'langcode' => 'langcode',
155 'status' => 'status',
156 'dependencies' => 'dependencies',
157 'third_party_settings' => 'third_party_settings',
160 'custom_property' => 'customProperty',
169 'mergedConfigExport' => [
170 'random_key' => 'random_key',
174 'random_key' => 'random_key',