3 namespace Drupal\Tests\rest\Kernel\Entity;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\rest\Entity\ConfigDependencies;
7 use Drupal\rest\Entity\RestResourceConfig;
8 use Drupal\rest\RestResourceConfigInterface;
11 * @coversDefaultClass \Drupal\rest\Entity\ConfigDependencies
15 class ConfigDependenciesTest extends KernelTestBase {
20 public static $modules = ['rest', 'entity_test', 'serialization'];
23 * @covers ::calculateDependencies
25 * @dataProvider providerBasicDependencies
27 public function testCalculateDependencies(array $configuration) {
28 $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
30 $rest_config = RestResourceConfig::create($configuration);
32 $result = $config_dependencies->calculateDependencies($rest_config);
34 'module' => ['basic_auth', 'serialization', 'hal'],
39 * @covers ::onDependencyRemoval
40 * @covers ::onDependencyRemovalForMethodGranularity
41 * @covers ::onDependencyRemovalForResourceGranularity
43 * @dataProvider providerBasicDependencies
45 public function testOnDependencyRemovalRemoveUnrelatedDependency(array $configuration) {
46 $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
48 $rest_config = RestResourceConfig::create($configuration);
50 $this->assertFalse($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['node']]));
51 $this->assertEquals($configuration['configuration'], $rest_config->get('configuration'));
56 * An array with numerical keys:
57 * 0. The original REST resource configuration.
59 public function providerBasicDependencies() {
63 'plugin_id' => 'entity:entity_test',
64 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
67 'supported_auth' => ['basic_auth'],
68 'supported_formats' => ['json'],
71 'supported_auth' => ['cookie'],
72 'supported_formats' => ['hal_json'],
79 'plugin_id' => 'entity:entity_test',
80 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
82 'methods' => ['GET', 'POST'],
83 'formats' => ['json', 'hal_json'],
84 'authentication' => ['cookie', 'basic_auth'],
92 * @covers ::onDependencyRemoval
93 * @covers ::onDependencyRemovalForMethodGranularity
95 public function testOnDependencyRemovalRemoveFormatForMethodGranularity() {
96 $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
98 $rest_config = RestResourceConfig::create([
99 'plugin_id' => 'entity:entity_test',
100 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
103 'supported_auth' => ['cookie'],
104 'supported_formats' => ['json'],
107 'supported_auth' => ['basic_auth'],
108 'supported_formats' => ['hal_json'],
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([
118 'supported_auth' => ['cookie'],
119 'supported_formats' => ['json'],
122 'supported_auth' => ['basic_auth'],
124 ], $rest_config->get('configuration'));
128 * @covers ::onDependencyRemoval
129 * @covers ::onDependencyRemovalForMethodGranularity
131 public function testOnDependencyRemovalRemoveAuth() {
132 $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
134 $rest_config = RestResourceConfig::create([
135 'plugin_id' => 'entity:entity_test',
136 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
139 'supported_auth' => ['cookie'],
140 'supported_formats' => ['json'],
143 'supported_auth' => ['basic_auth'],
144 'supported_formats' => ['hal_json'],
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([
154 'supported_auth' => ['cookie'],
155 'supported_formats' => ['json'],
158 'supported_formats' => ['hal_json'],
160 ], $rest_config->get('configuration'));
164 * @covers ::onDependencyRemoval
165 * @covers ::onDependencyRemovalForMethodGranularity
167 public function testOnDependencyRemovalRemoveAuthAndFormats() {
168 $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
170 $rest_config = RestResourceConfig::create([
171 'plugin_id' => 'entity:entity_test',
172 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
175 'supported_auth' => ['cookie'],
176 'supported_formats' => ['json'],
179 'supported_auth' => ['basic_auth'],
180 'supported_formats' => ['hal_json'],
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([
192 'supported_auth' => ['cookie'],
193 'supported_formats' => ['json'],
195 ], $rest_config->get('configuration'));
199 * @covers ::onDependencyRemoval
200 * @covers ::onDependencyRemovalForResourceGranularity
202 * @dataProvider providerOnDependencyRemovalForResourceGranularity
204 public function testOnDependencyRemovalForResourceGranularity(array $configuration, $module, $expected_configuration) {
205 assert(is_string($module));
206 assert($expected_configuration === FALSE || is_array($expected_configuration));
208 $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']);
210 $rest_config = RestResourceConfig::create($configuration);
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'));
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.
225 public function providerOnDependencyRemovalForResourceGranularity() {
227 'resource with multiple formats' => [
229 'plugin_id' => 'entity:entity_test',
230 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
232 'methods' => ['GET', 'POST'],
233 'formats' => ['json', 'hal_json'],
234 'authentication' => ['cookie', 'basic_auth'],
239 'methods' => ['GET', 'POST'],
240 'formats' => ['json'],
241 'authentication' => ['cookie', 'basic_auth'],
244 'resource with only HAL+JSON format' => [
246 'plugin_id' => 'entity:entity_test',
247 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
249 'methods' => ['GET', 'POST'],
250 'formats' => ['hal_json'],
251 'authentication' => ['cookie', 'basic_auth'],
257 'resource with multiple authentication providers' => [
259 'plugin_id' => 'entity:entity_test',
260 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
262 'methods' => ['GET', 'POST'],
263 'formats' => ['json', 'hal_json'],
264 'authentication' => ['cookie', 'basic_auth'],
269 'methods' => ['GET', 'POST'],
270 'formats' => ['json', 'hal_json'],
271 'authentication' => ['cookie'],
274 'resource with only basic_auth authentication' => [
276 'plugin_id' => 'entity:entity_test',
277 'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
279 'methods' => ['GET', 'POST'],
280 'formats' => ['json', 'hal_json'],
281 'authentication' => ['basic_auth'],