Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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
208     $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
209
210     $rest_config = RestResourceConfig::create($configuration);
211
212     $this->assertSame(!empty($expected_configuration), $config_dependencies->onDependencyRemoval($rest_config, ['module' => [$module]]));
213     if (!empty($expected_configuration)) {
214       $this->assertEquals($expected_configuration, $rest_config->get('configuration'));
215     }
216   }
217
218   /**
219    * @return array
220    *   An array with numerical keys:
221    *   0. The original REST resource configuration.
222    *   1. The module to uninstall (the dependency that is about to be removed).
223    *   2. The expected configuration after uninstalling this module.
224    */
225   public function providerOnDependencyRemovalForResourceGranularity() {
226     return [
227       'resource with multiple formats' => [
228         [
229           'plugin_id' => 'entity:entity_test',
230           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
231           'configuration' => [
232             'methods' => ['GET', 'POST'],
233             'formats' => ['json', 'hal_json'],
234             'authentication' => ['cookie', 'basic_auth'],
235           ],
236         ],
237         'hal',
238         [
239           'methods' => ['GET', 'POST'],
240           'formats' => ['json'],
241           'authentication' => ['cookie', 'basic_auth'],
242         ],
243       ],
244       'resource with only HAL+JSON format' => [
245         [
246           'plugin_id' => 'entity:entity_test',
247           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
248           'configuration' => [
249             'methods' => ['GET', 'POST'],
250             'formats' => ['hal_json'],
251             'authentication' => ['cookie', 'basic_auth'],
252           ],
253         ],
254         'hal',
255         FALSE,
256       ],
257       'resource with multiple authentication providers' => [
258         [
259           'plugin_id' => 'entity:entity_test',
260           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
261           'configuration' => [
262             'methods' => ['GET', 'POST'],
263             'formats' => ['json', 'hal_json'],
264             'authentication' => ['cookie', 'basic_auth'],
265           ],
266         ],
267         'basic_auth',
268         [
269           'methods' => ['GET', 'POST'],
270           'formats' => ['json', 'hal_json'],
271           'authentication' => ['cookie'],
272         ],
273       ],
274       'resource with only basic_auth authentication' => [
275         [
276           'plugin_id' => 'entity:entity_test',
277           'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
278           'configuration' => [
279             'methods' => ['GET', 'POST'],
280             'formats' => ['json', 'hal_json'],
281             'authentication' => ['basic_auth'],
282           ],
283         ],
284         'basic_auth',
285         FALSE,
286       ],
287     ];
288   }
289
290 }