3 namespace Drupal\Tests\Core\Config\Entity;
5 use Drupal\Core\Config\TypedConfigManagerInterface;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\Core\Config\Entity\ConfigEntityType;
9 use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException;
12 * @coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityType
15 class ConfigEntityTypeTest extends UnitTestCase {
18 * The mocked typed config manager.
20 * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
22 protected $typedConfigManager;
27 protected function setUp() {
28 $this->typedConfigManager = $this->getMock(TypedConfigManagerInterface::class);
29 $container = new ContainerBuilder();
30 $container->set('config.typed', $this->typedConfigManager);
31 \Drupal::setContainer($container);
35 * Sets up a ConfigEntityType object for a given set of values.
37 * @param array $definition
38 * An array of values to use for the ConfigEntityType.
40 * @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
42 protected function setUpConfigEntityType($definition) {
43 if (!isset($definition['id'])) {
45 'id' => 'example_config_entity_type',
48 return new ConfigEntityType($definition);
52 * Tests that we get an exception when the length of the config prefix that is
53 * returned by getConfigPrefix() exceeds the maximum defined prefix length.
55 * @covers ::getConfigPrefix
57 public function testConfigPrefixLengthExceeds() {
58 // A provider length of 24 and config_prefix length of 59 (+1 for the .)
59 // results in a config length of 84, which is too long.
61 'provider' => $this->randomMachineName(24),
62 'config_prefix' => $this->randomMachineName(59),
64 $config_entity = $this->setUpConfigEntityType($definition);
65 $this->setExpectedException(
66 '\Drupal\Core\Config\ConfigPrefixLengthException',
67 "The configuration file name prefix {$definition['provider']}.{$definition['config_prefix']} exceeds the maximum character limit of " . ConfigEntityType::PREFIX_LENGTH
69 $this->assertEmpty($config_entity->getConfigPrefix());
73 * Tests that a valid config prefix returned by getConfigPrefix()
74 * does not throw an exception and is formatted as expected.
76 * @covers ::getConfigPrefix
78 public function testConfigPrefixLengthValid() {
79 // A provider length of 24 and config_prefix length of 58 (+1 for the .)
80 // results in a config length of 83, which is right at the limit.
82 'provider' => $this->randomMachineName(24),
83 'config_prefix' => $this->randomMachineName(58),
85 $config_entity = $this->setUpConfigEntityType($definition);
86 $expected_prefix = $definition['provider'] . '.' . $definition['config_prefix'];
87 $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix());
91 * @covers ::__construct
93 public function testConstruct() {
94 $config_entity = new ConfigEntityType([
95 'id' => 'example_config_entity_type',
97 $this->assertEquals('Drupal\Core\Config\Entity\ConfigEntityStorage', $config_entity->getStorageClass());
101 * @covers ::__construct
103 public function testConstructBadStorage() {
104 $this->setExpectedException(ConfigEntityStorageClassException::class, '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it');
105 new ConfigEntityType([
106 'id' => 'example_config_entity_type',
107 'handlers' => ['storage' => '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage'],
112 * @covers ::setStorageClass
114 public function testSetStorageClass() {
115 $config_entity = $this->setUpConfigEntityType([]);
116 $this->setExpectedException(ConfigEntityStorageClassException::class, '\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage is not \Drupal\Core\Config\Entity\ConfigEntityStorage or it does not extend it');
117 $config_entity->setStorageClass('\Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage');
121 * Tests the getConfigPrefix() method.
123 * @dataProvider providerTestGetConfigPrefix
125 * @covers ::getConfigPrefix
127 public function testGetConfigPrefix($definition, $expected) {
128 $entity_type = $this->setUpConfigEntityType($definition);
129 $this->assertSame($expected, $entity_type->getConfigPrefix());
133 * Provides test data.
135 public function providerTestGetConfigPrefix() {
137 [['provider' => 'node', 'id' => 'node_type', 'config_prefix' => 'type'], 'node.type'],
138 [['provider' => 'views', 'id' => 'view'], 'views.view'],
143 * @covers ::getPropertiesToExport
145 * @dataProvider providerGetPropertiesToExport
147 public function testGetPropertiesToExport($definition, $expected) {
148 $entity_type = $this->setUpConfigEntityType($definition);
149 $properties_to_export = $entity_type->getPropertiesToExport();
150 $this->assertSame($expected, $properties_to_export);
152 // Ensure the method is idempotent.
153 $properties_to_export = $entity_type->getPropertiesToExport();
154 $this->assertSame($expected, $properties_to_export);
157 public function providerGetPropertiesToExport() {
163 'custom_property' => 'customProperty',
168 'langcode' => 'langcode',
169 'status' => 'status',
170 'dependencies' => 'dependencies',
171 'third_party_settings' => 'third_party_settings',
174 'custom_property' => 'customProperty',
183 'mergedConfigExport' => [
184 'random_key' => 'random_key',
188 'random_key' => 'random_key',
195 * @covers ::getPropertiesToExport
197 public function testGetPropertiesToExportSchemaFallback() {
198 $this->typedConfigManager->expects($this->once())
199 ->method('getDefinition')
200 ->will($this->returnValue(['mapping' => ['id' => '', 'dependencies' => '']]));
201 $config_entity_type = new ConfigEntityType([
202 'id' => 'example_config_entity_type',
204 $this->assertEquals(['id' => 'id', 'dependencies' => 'dependencies'], $config_entity_type->getPropertiesToExport('test'));
208 * @covers ::getPropertiesToExport
210 public function testGetPropertiesToExportNoFallback() {
211 $config_entity_type = new ConfigEntityType([
212 'id' => 'example_config_entity_type',
214 $this->assertNull($config_entity_type->getPropertiesToExport());