51ad3e977f088067988445f71990f37353ea5679
[yaffs-website] / web / core / modules / rest / tests / src / Kernel / Entity / ConfigDependenciesTest.php
1 <?php
2
3 namespace Drupal\Tests\rest\Kernel\Entity;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\rest\Entity\ConfigDependencies;
7 use Drupal\rest\Entity\RestResourceConfig;
8 use Drupal\rest\RestResourceConfigInterface;
9
10 /**
11  * @coversDefaultClass \Drupal\rest\Entity\ConfigDependencies
12  *
13  * @group rest
14  */
15 class ConfigDependenciesTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['rest', 'entity_test', 'serialization'];
21
22   /**
23    * @covers ::calculateDependencies
24    *
25    * @dataProvider providerBasicDependencies
26    */
27   public function testCalculateDependencies(array $configuration) {
28     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
29
30     $rest_config = RestResourceConfig::create($configuration);
31
32     $result = $config_dependencies->calculateDependencies($rest_config);
33     $this->assertEquals([
34       'module' => ['basic_auth', 'serialization', 'hal'],
35     ], $result);
36   }
37
38   /**
39    * @covers ::onDependencyRemoval
40    * @covers ::onDependencyRemovalForMethodGranularity
41    * @covers ::onDependencyRemovalForResourceGranularity
42    *
43    * @dataProvider providerBasicDependencies
44    */
45   public function testOnDependencyRemovalRemoveUnrelatedDependency(array $configuration) {
46     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
47
48     $rest_config = RestResourceConfig::create($configuration);
49
50     $this->assertFalse($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['node']]));
51     $this->assertEquals($configuration['configuration'], $rest_config->get('configuration'));
52   }
53
54   /**
55    * @return array
56    *   An array with numerical keys:
57    *   0. The original REST resource configuration.
58    */
59   public function providerBasicDependencies() {
60     return [
61       'method' => [
62         [
63           'plugin_id' => 'entity:entity_test',
64           'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
65           'configuration' => [
66             'GET' => [
67               'supported_auth' => ['basic_auth'],
68               'supported_formats' => ['json'],
69             ],
70             'POST' => [
71               'supported_auth' => ['cookie'],
72               'supported_formats' => ['hal_json'],
73             ],
74           ],
75         ],
76       ],
77       'resource' => [
78         [
79           'plugin_id' => 'entity:entity_test',
80           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
81           'configuration' => [
82             'methods' => ['GET', 'POST'],
83             'formats' => ['json', 'hal_json'],
84             'authentication' => ['cookie', 'basic_auth'],
85           ],
86         ],
87       ],
88     ];
89   }
90
91   /**
92    * @covers ::onDependencyRemoval
93    * @covers ::onDependencyRemovalForMethodGranularity
94    */
95   public function testOnDependencyRemovalRemoveFormatForMethodGranularity() {
96     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
97
98     $rest_config = RestResourceConfig::create([
99       'plugin_id' => 'entity:entity_test',
100       'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
101       'configuration' => [
102         'GET' => [
103           'supported_auth' => ['cookie'],
104           'supported_formats' => ['json'],
105         ],
106         'POST' => [
107           'supported_auth' => ['basic_auth'],
108           'supported_formats' => ['hal_json'],
109         ],
110       ],
111     ]);
112
113     $this->assertTrue($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['hal']]));
114     $this->assertEquals(['json'], $rest_config->getFormats('GET'));
115     $this->assertEquals([], $rest_config->getFormats('POST'));
116     $this->assertEquals([
117       'GET' => [
118         'supported_auth' => ['cookie'],
119         'supported_formats' => ['json'],
120       ],
121       'POST' => [
122         'supported_auth' => ['basic_auth'],
123       ],
124     ], $rest_config->get('configuration'));
125   }
126
127   /**
128    * @covers ::onDependencyRemoval
129    * @covers ::onDependencyRemovalForMethodGranularity
130    */
131   public function testOnDependencyRemovalRemoveAuth() {
132     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
133
134     $rest_config = RestResourceConfig::create([
135       'plugin_id' => 'entity:entity_test',
136       'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
137       'configuration' => [
138         'GET' => [
139           'supported_auth' => ['cookie'],
140           'supported_formats' => ['json'],
141         ],
142         'POST' => [
143           'supported_auth' => ['basic_auth'],
144           'supported_formats' => ['hal_json'],
145         ],
146       ],
147     ]);
148
149     $this->assertTrue($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['basic_auth']]));
150     $this->assertEquals(['cookie'], $rest_config->getAuthenticationProviders('GET'));
151     $this->assertEquals([], $rest_config->getAuthenticationProviders('POST'));
152     $this->assertEquals([
153       'GET' => [
154         'supported_auth' => ['cookie'],
155         'supported_formats' => ['json'],
156       ],
157       'POST' => [
158         'supported_formats' => ['hal_json'],
159       ],
160     ], $rest_config->get('configuration'));
161   }
162
163   /**
164    * @covers ::onDependencyRemoval
165    * @covers ::onDependencyRemovalForMethodGranularity
166    */
167   public function testOnDependencyRemovalRemoveAuthAndFormats() {
168     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
169
170     $rest_config = RestResourceConfig::create([
171       'plugin_id' => 'entity:entity_test',
172       'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
173       'configuration' => [
174         'GET' => [
175           'supported_auth' => ['cookie'],
176           'supported_formats' => ['json'],
177         ],
178         'POST' => [
179           'supported_auth' => ['basic_auth'],
180           'supported_formats' => ['hal_json'],
181         ],
182       ],
183     ]);
184
185     $this->assertTrue($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['basic_auth', 'hal']]));
186     $this->assertEquals(['json'], $rest_config->getFormats('GET'));
187     $this->assertEquals(['cookie'], $rest_config->getAuthenticationProviders('GET'));
188     $this->assertEquals([], $rest_config->getFormats('POST'));
189     $this->assertEquals([], $rest_config->getAuthenticationProviders('POST'));
190     $this->assertEquals([
191       'GET' => [
192         'supported_auth' => ['cookie'],
193         'supported_formats' => ['json'],
194       ],
195     ], $rest_config->get('configuration'));
196   }
197
198   /**
199    * @covers ::onDependencyRemoval
200    * @covers ::onDependencyRemovalForResourceGranularity
201    *
202    * @dataProvider providerOnDependencyRemovalForResourceGranularity
203    */
204   public function testOnDependencyRemovalForResourceGranularity(array $configuration, $module, $expected_configuration) {
205     assert('is_string($module)');
206     assert('$expected_configuration === FALSE || is_array($expected_configuration)');
207     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
208
209     $rest_config = RestResourceConfig::create($configuration);
210
211     $this->assertSame(!empty($expected_configuration), $config_dependencies->onDependencyRemoval($rest_config, ['module' => [$module]]));
212     if (!empty($expected_configuration)) {
213       $this->assertEquals($expected_configuration, $rest_config->get('configuration'));
214     }
215   }
216
217   /**
218    * @return array
219    *   An array with numerical keys:
220    *   0. The original REST resource configuration.
221    *   1. The module to uninstall (the dependency that is about to be removed).
222    *   2. The expected configuration after uninstalling this module.
223    */
224   public function providerOnDependencyRemovalForResourceGranularity() {
225     return [
226       'resource with multiple formats' => [
227         [
228           'plugin_id' => 'entity:entity_test',
229           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
230           'configuration' => [
231             'methods' => ['GET', 'POST'],
232             'formats' => ['json', 'hal_json'],
233             'authentication' => ['cookie', 'basic_auth'],
234           ],
235         ],
236         'hal',
237         [
238           'methods' => ['GET', 'POST'],
239           'formats' => ['json'],
240           'authentication' => ['cookie', 'basic_auth'],
241         ]
242       ],
243       'resource with only HAL+JSON format' => [
244         [
245           'plugin_id' => 'entity:entity_test',
246           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
247           'configuration' => [
248             'methods' => ['GET', 'POST'],
249             'formats' => ['hal_json'],
250             'authentication' => ['cookie', 'basic_auth'],
251           ],
252         ],
253         'hal',
254         FALSE
255       ],
256       'resource with multiple authentication providers' => [
257         [
258           'plugin_id' => 'entity:entity_test',
259           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
260           'configuration' => [
261             'methods' => ['GET', 'POST'],
262             'formats' => ['json', 'hal_json'],
263             'authentication' => ['cookie', 'basic_auth'],
264           ],
265         ],
266         'basic_auth',
267         [
268           'methods' => ['GET', 'POST'],
269           'formats' => ['json', 'hal_json'],
270           'authentication' => ['cookie'],
271         ]
272       ],
273       'resource with only basic_auth authentication' => [
274         [
275           'plugin_id' => 'entity:entity_test',
276           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
277           'configuration' => [
278             'methods' => ['GET', 'POST'],
279             'formats' => ['json', 'hal_json'],
280             'authentication' => ['basic_auth'],
281           ],
282         ],
283         'basic_auth',
284         FALSE,
285       ],
286     ];
287   }
288
289 }