Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Config / Entity / ConfigEntityTypeTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Config\Entity;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityType
13  * @group Config
14  */
15 class ConfigEntityTypeTest extends UnitTestCase {
16
17   /**
18    * The mocked typed config manager.
19    *
20    * @var \Drupal\Core\Config\TypedConfigManagerInterface|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $typedConfigManager;
23
24   /**
25    * {@inheritdoc}
26    */
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);
32   }
33
34   /**
35    * Sets up a ConfigEntityType object for a given set of values.
36    *
37    * @param array $definition
38    *   An array of values to use for the ConfigEntityType.
39    *
40    * @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
41    */
42   protected function setUpConfigEntityType($definition) {
43     if (!isset($definition['id'])) {
44       $definition += [
45         'id' => 'example_config_entity_type',
46       ];
47     }
48     return new ConfigEntityType($definition);
49   }
50
51   /**
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.
54    *
55    * @covers ::getConfigPrefix
56    */
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.
60     $definition = [
61       'provider' => $this->randomMachineName(24),
62       'config_prefix' => $this->randomMachineName(59),
63     ];
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
68     );
69     $this->assertEmpty($config_entity->getConfigPrefix());
70   }
71
72   /**
73    * Tests that a valid config prefix returned by getConfigPrefix()
74    * does not throw an exception and is formatted as expected.
75    *
76    * @covers ::getConfigPrefix
77    */
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.
81     $definition = [
82       'provider' => $this->randomMachineName(24),
83       'config_prefix' => $this->randomMachineName(58),
84     ];
85     $config_entity = $this->setUpConfigEntityType($definition);
86     $expected_prefix = $definition['provider'] . '.' . $definition['config_prefix'];
87     $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix());
88   }
89
90   /**
91    * @covers ::__construct
92    */
93   public function testConstruct() {
94     $config_entity = new ConfigEntityType([
95       'id' => 'example_config_entity_type',
96     ]);
97     $this->assertEquals('Drupal\Core\Config\Entity\ConfigEntityStorage', $config_entity->getStorageClass());
98   }
99
100   /**
101    * @covers ::__construct
102    */
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'],
108     ]);
109   }
110
111   /**
112    * @covers ::setStorageClass
113    */
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');
118   }
119
120   /**
121    * Tests the getConfigPrefix() method.
122    *
123    * @dataProvider providerTestGetConfigPrefix
124    *
125    * @covers ::getConfigPrefix
126    */
127   public function testGetConfigPrefix($definition, $expected) {
128     $entity_type = $this->setUpConfigEntityType($definition);
129     $this->assertSame($expected, $entity_type->getConfigPrefix());
130   }
131
132   /**
133    * Provides test data.
134    */
135   public function providerTestGetConfigPrefix() {
136     return [
137       [['provider' => 'node', 'id' => 'node_type', 'config_prefix' => 'type'], 'node.type'],
138       [['provider' => 'views', 'id' => 'view'], 'views.view'],
139     ];
140   }
141
142   /**
143    * @covers ::getPropertiesToExport
144    *
145    * @dataProvider providerGetPropertiesToExport
146    */
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);
151
152     // Ensure the method is idempotent.
153     $properties_to_export = $entity_type->getPropertiesToExport();
154     $this->assertSame($expected, $properties_to_export);
155   }
156
157   public function providerGetPropertiesToExport() {
158     $data = [];
159     $data[] = [
160       [
161         'config_export' => [
162           'id',
163           'custom_property' => 'customProperty',
164         ],
165       ],
166       [
167         'uuid' => 'uuid',
168         'langcode' => 'langcode',
169         'status' => 'status',
170         'dependencies' => 'dependencies',
171         'third_party_settings' => 'third_party_settings',
172         '_core' => '_core',
173         'id' => 'id',
174         'custom_property' => 'customProperty',
175       ],
176     ];
177
178     $data[] = [
179       [
180         'config_export' => [
181           'id',
182         ],
183         'mergedConfigExport' => [
184           'random_key' => 'random_key',
185         ],
186       ],
187       [
188         'random_key' => 'random_key',
189       ],
190     ];
191     return $data;
192   }
193
194   /**
195    * @covers ::getPropertiesToExport
196    */
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',
203     ]);
204     $this->assertEquals(['id' => 'id', 'dependencies' => 'dependencies'], $config_entity_type->getPropertiesToExport('test'));
205   }
206
207   /**
208    * @covers ::getPropertiesToExport
209    */
210   public function testGetPropertiesToExportNoFallback() {
211     $config_entity_type = new ConfigEntityType([
212       'id' => 'example_config_entity_type',
213     ]);
214     $this->assertNull($config_entity_type->getPropertiesToExport());
215   }
216
217 }