Security update for Core, with self-updated composer
[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\Tests\UnitTestCase;
6 use Drupal\Core\Config\Entity\ConfigEntityType;
7 use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Config\Entity\ConfigEntityType
11  * @group Config
12  */
13 class ConfigEntityTypeTest extends UnitTestCase {
14
15   /**
16    * Sets up a ConfigEntityType object for a given set of values.
17    *
18    * @param array $definition
19    *   An array of values to use for the ConfigEntityType.
20    *
21    * @return \Drupal\Core\Config\Entity\ConfigEntityTypeInterface
22    */
23   protected function setUpConfigEntityType($definition) {
24     if (!isset($definition['id'])) {
25       $definition += [
26         'id' => 'example_config_entity_type',
27       ];
28     }
29     return new ConfigEntityType($definition);
30   }
31
32   /**
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.
35    *
36    * @covers ::getConfigPrefix
37    */
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.
41     $definition = [
42       'provider' => $this->randomMachineName(24),
43       'config_prefix' => $this->randomMachineName(59),
44     ];
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
49     );
50     $this->assertEmpty($config_entity->getConfigPrefix());
51   }
52
53   /**
54    * Tests that a valid config prefix returned by getConfigPrefix()
55    * does not throw an exception and is formatted as expected.
56    *
57    * @covers ::getConfigPrefix
58    */
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.
62     $definition = [
63       'provider' => $this->randomMachineName(24),
64       'config_prefix' => $this->randomMachineName(58),
65     ];
66     $config_entity = $this->setUpConfigEntityType($definition);
67     $expected_prefix = $definition['provider'] . '.' . $definition['config_prefix'];
68     $this->assertEquals($expected_prefix, $config_entity->getConfigPrefix());
69   }
70
71   /**
72    * @covers ::__construct
73    */
74   public function testConstruct() {
75     $config_entity = new ConfigEntityType([
76       'id' => 'example_config_entity_type',
77     ]);
78     $this->assertEquals('Drupal\Core\Config\Entity\ConfigEntityStorage', $config_entity->getStorageClass());
79   }
80
81   /**
82    * @covers ::__construct
83    */
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']
89     ]);
90   }
91
92   /**
93    * @covers ::setStorageClass
94    */
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');
99   }
100
101   /**
102    * Tests the getConfigPrefix() method.
103    *
104    * @dataProvider providerTestGetConfigPrefix
105    *
106    * @covers ::getConfigPrefix
107    */
108   public function testGetConfigPrefix($definition, $expected) {
109     $entity_type = $this->setUpConfigEntityType($definition);
110     $this->assertSame($expected, $entity_type->getConfigPrefix());
111   }
112
113   /**
114    * Provides test data.
115    */
116   public function providerTestGetConfigPrefix() {
117     return [
118       [['provider' => 'node', 'id' => 'node_type', 'config_prefix' => 'type'], 'node.type'],
119       [['provider' => 'views', 'id' => 'view'], 'views.view'],
120     ];
121   }
122
123   /**
124    * @covers ::getPropertiesToExport
125    *
126    * @dataProvider providerGetPropertiesToExport
127    */
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);
132
133     // Ensure the method is idempotent.
134     $properties_to_export = $entity_type->getPropertiesToExport();
135     $this->assertSame($expected, $properties_to_export);
136   }
137
138   public function providerGetPropertiesToExport() {
139     $data = [];
140     $data[] = [
141       [],
142       NULL,
143     ];
144
145     $data[] = [
146       [
147         'config_export' => [
148           'id',
149           'custom_property' => 'customProperty',
150         ],
151       ],
152       [
153         'uuid' => 'uuid',
154         'langcode' => 'langcode',
155         'status' => 'status',
156         'dependencies' => 'dependencies',
157         'third_party_settings' => 'third_party_settings',
158         '_core' => '_core',
159         'id' => 'id',
160         'custom_property' => 'customProperty',
161       ],
162     ];
163
164     $data[] = [
165       [
166         'config_export' => [
167           'id',
168         ],
169         'mergedConfigExport' => [
170           'random_key' => 'random_key',
171         ],
172       ],
173       [
174         'random_key' => 'random_key',
175       ],
176     ];
177     return $data;
178   }
179
180 }