diff --git a/core/modules/rest/config/optional/rest.resource.entity__node.yml b/core/modules/rest/config/optional/rest.resource.entity.node.yml similarity index 96% rename from core/modules/rest/config/optional/rest.resource.entity__node.yml rename to core/modules/rest/config/optional/rest.resource.entity.node.yml index c7da93f..0cf4d78 100644 --- a/core/modules/rest/config/optional/rest.resource.entity__node.yml +++ b/core/modules/rest/config/optional/rest.resource.entity.node.yml @@ -1,4 +1,4 @@ -id: entity__node +id: entity.node plugin_id: 'entity:node' granularity: method configuration: diff --git a/core/modules/rest/rest.install b/core/modules/rest/rest.install index c40d8cf..25199f6 100644 --- a/core/modules/rest/rest.install +++ b/core/modules/rest/rest.install @@ -5,7 +5,7 @@ * Install, update and uninstall functions for the rest module. */ -use Drupal\rest\Entity\RestResourceConfig; +use Drupal\rest\Entity\RestResourceConfiguration; /** * Implements hook_requirements(). @@ -36,10 +36,10 @@ function rest_requirements($phase) { * @todo Automatically upgrade those REST resource config entities that have the same formats/auth mechanisms for all methods to "granular: resource" in https://www.drupal.org/node/2721595. */ function rest_update_8201() { - \Drupal::entityDefinitionUpdateManager()->installEntityType(\Drupal::entityTypeManager()->getDefinition('rest_resource_config')); + \Drupal::entityDefinitionUpdateManager()->installEntityType(\Drupal::entityTypeManager()->getDefinition('rest_resource_configuration')); foreach (\Drupal::config('rest.settings')->get('resources') as $key => $resource) { - $resource = RestResourceConfig::create([ - 'id' => str_replace(':', '__', $key), + $resource = RestResourceConfiguration::create([ + 'id' => str_replace(':', '.', $key), 'granularity' => 'method', 'configuration' => $resource, ]); diff --git a/core/modules/rest/src/Entity/ConfigDependencies.php b/core/modules/rest/src/Entity/ConfigDependencies.php index 12cc380..f6e7853 100644 --- a/core/modules/rest/src/Entity/ConfigDependencies.php +++ b/core/modules/rest/src/Entity/ConfigDependencies.php @@ -3,7 +3,8 @@ namespace Drupal\rest\Entity; use Drupal\Component\Plugin\DependentPluginInterface; -use Drupal\rest\RestResourceConfigInterface; +use Drupal\Component\Utility\NestedArray; +use Drupal\rest\RestResourceConfigurationInterface; /** * Calculates rest resource config dependencies. @@ -40,7 +41,7 @@ public function __construct(array $format_providers, array $auth_providers) { /** * Calculates dependencies of a specific rest resource configuration. * - * @param \Drupal\rest\RestResourceConfigInterface $rest_config + * @param \Drupal\rest\RestResourceConfigurationInterface $rest_config * The rest configuration. * * @return string[][] @@ -48,7 +49,7 @@ public function __construct(array $format_providers, array $auth_providers) { * * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies() */ - public function calculateDependencies(RestResourceConfigInterface $rest_config) { + public function calculateDependencies(RestResourceConfigurationInterface $rest_config) { $granularity = $rest_config->get('granularity'); if ($granularity === 'method') { return $this->calculateDependenciesForMethodGranularity($rest_config); @@ -61,7 +62,7 @@ public function calculateDependencies(RestResourceConfigInterface $rest_config) /** * Calculates dependencies of a specific rest resource configuration. * - * @param \Drupal\rest\RestResourceConfigInterface $rest_config + * @param \Drupal\rest\RestResourceConfigurationInterface $rest_config * The rest configuration. * * @return string[][] @@ -69,16 +70,11 @@ public function calculateDependencies(RestResourceConfigInterface $rest_config) * * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies() */ - protected function calculateDependenciesForMethodGranularity(RestResourceConfigInterface $rest_config) { + protected function calculateDependenciesForMethodGranularity(RestResourceConfigurationInterface $rest_config) { // The dependency lists for authentication providers and formats // generated on container build. $dependencies = []; foreach (array_keys($rest_config->get('configuration')) as $request_method) { - // 'granularity' is not a request method - if ($request_method === 'granularity') { - continue; - } - // Add dependencies based on the supported authentication providers. foreach ($rest_config->getAuthenticationProviders($request_method) as $auth) { if (isset($this->authProviders[$auth])) { @@ -96,7 +92,7 @@ protected function calculateDependenciesForMethodGranularity(RestResourceConfigI } if (($resource_plugin = $rest_config->getResourcePlugin()) && $resource_plugin instanceof DependentPluginInterface) { - $dependencies = array_merge($dependencies, $resource_plugin->calculateDependencies()); + $dependencies = NestedArray::mergeDeep($dependencies, $resource_plugin->calculateDependencies()); } return $dependencies; @@ -105,7 +101,7 @@ protected function calculateDependenciesForMethodGranularity(RestResourceConfigI /** * Informs the entity that entities it depends on will be deleted. * - * @param \Drupal\rest\RestResourceConfigInterface $rest_config + * @param \Drupal\rest\RestResourceConfigurationInterface $rest_config * The rest configuration. * @param array $dependencies * An array of dependencies that will be deleted keyed by dependency type. @@ -116,7 +112,7 @@ protected function calculateDependenciesForMethodGranularity(RestResourceConfigI * * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::onDependencyRemoval() */ - public function onDependencyRemoval(RestResourceConfigInterface $rest_config, array $dependencies) { + public function onDependencyRemoval(RestResourceConfigurationInterface $rest_config, array $dependencies) { $granularity = $rest_config->get('granularity'); if ($granularity === 'method') { return $this->onDependencyRemovalForMethodGranularity($rest_config, $dependencies); @@ -129,7 +125,7 @@ public function onDependencyRemoval(RestResourceConfigInterface $rest_config, ar /** * Informs the entity that entities it depends on will be deleted. * - * @param \Drupal\rest\RestResourceConfigInterface $rest_config + * @param \Drupal\rest\RestResourceConfigurationInterface $rest_config * The rest configuration. * @param array $dependencies * An array of dependencies that will be deleted keyed by dependency type. @@ -138,57 +134,36 @@ public function onDependencyRemoval(RestResourceConfigInterface $rest_config, ar * @return bool * TRUE if the entity has been changed as a result, FALSE if not. */ - public function onDependencyRemovalForMethodGranularity(RestResourceConfigInterface $rest_config, array $dependencies) { + protected function onDependencyRemovalForMethodGranularity(RestResourceConfigurationInterface $rest_config, array $dependencies) { $changed = FALSE; // Only module-related dependencies can be fixed. All other types of // dependencies cannot, because they were not generated based on supported // authentication providers or formats. if (isset($dependencies['module'])) { // Try to fix dependencies. - $removed_auth = []; - $removed_formats = []; - foreach ($dependencies['module'] as $dep_module) { - // Check if the removed dependency module contained an authentication - // provider. - foreach ($this->authProviders as $auth => $auth_module) { - if ($dep_module != $auth_module) { - continue; - } - $removed_auth[] = $auth; - } - // Check if the removed dependency module contained a format. - foreach ($this->formatProviders as $format => $format_module) { - if ($dep_module != $format_module) { - continue; - } - $removed_formats[] = $format; - } - } - $configuration = $rest_config->get('configuration'); + $removed_auth = array_keys(array_intersect($this->authProviders, $dependencies['module'])); + $removed_formats = array_keys(array_intersect($this->formatProviders, $dependencies['module'])); + $configuration_before = $configuration = $rest_config->get('configuration'); if (!empty($removed_auth) || !empty($removed_formats)) { // Try to fix dependency problems by removing affected // authentication providers and formats. foreach (array_keys($rest_config->get('configuration')) as $request_method) { foreach ($removed_formats as $format) { if (in_array($format, $rest_config->getFormats($request_method))) { - $rest_config = array_filter($rest_config[$request_method]['supported_formats'], function ($val) use ($format) { - return ($val != $format); - }); + $configuration[$request_method]['supported_formats'] = array_diff($configuration[$request_method]['supported_formats'], $removed_formats); } } foreach ($removed_auth as $auth) { if (in_array($auth, $rest_config->getAuthenticationProviders($request_method))) { - $rest_config = array_filter($rest_config[$request_method]['supported_auth'], function ($val) use ($auth) { - return ($val != $auth); - }); + $configuration[$request_method]['supported_auth'] = array_diff($configuration[$request_method]['supported_auth'], $removed_auth); } } - if (empty($rest_config->getAuthenticationProviders($request_method)) ){ + if (empty($configuration[$request_method]['supported_auth'])) { // Remove the key if there are no more authentication providers // supported by this request method. unset($configuration[$request_method]['supported_auth']); } - if (empty($rest_config->getFormats($request_method))) { + if (empty($configuration[$request_method]['supported_formats'])) { // Remove the key if there are no more formats supported by this // request method. unset($configuration[$request_method]['supported_formats']); @@ -200,7 +175,8 @@ public function onDependencyRemovalForMethodGranularity(RestResourceConfigInterf } } } - if (!empty($configuration)) { + if (!empty($configuration_before != $configuration)) { + $rest_config->set('configuration', $configuration); // Only mark the dependencies problems as fixed if there is any // configuration left. $changed = TRUE; diff --git a/core/modules/rest/src/Entity/RestResourceConfig.php b/core/modules/rest/src/Entity/RestResourceConfiguration.php similarity index 89% rename from core/modules/rest/src/Entity/RestResourceConfig.php rename to core/modules/rest/src/Entity/RestResourceConfiguration.php index 4920968..005cc25 100644 --- a/core/modules/rest/src/Entity/RestResourceConfig.php +++ b/core/modules/rest/src/Entity/RestResourceConfiguration.php @@ -4,15 +4,16 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\Entity; +use Drupal\Core\Entity\EntityWithPluginCollectionInterface; use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection; -use Drupal\rest\RestResourceConfigInterface; +use Drupal\rest\RestResourceConfigurationInterface; /** * Defines a RestResourceConfig configuration entity class. * * @ConfigEntityType( - * id = "rest_resource_config", - * label = @Translation("REST resource config"), + * id = "rest_resource_configuration", + * label = @Translation("REST resource configuration"), * config_prefix = "resource", * admin_permission = "administer rest resources", * label_callback = "getLabelFromPlugin", @@ -27,7 +28,7 @@ * } * ) */ -class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigInterface { +class RestResourceConfiguration extends ConfigEntityBase implements RestResourceConfigurationInterface, EntityWithPluginCollectionInterface { /** * The REST resource config id. @@ -66,10 +67,6 @@ class RestResourceConfig extends ConfigEntityBase implements RestResourceConfigI */ protected $pluginManager; - protected $methods = []; - protected $auth = []; - protected $formats = []; - /** * {@inheritdoc} */ @@ -79,7 +76,7 @@ public function __construct(array $values, $entity_type) { // because : is not valid for config entities. if (!isset($this->plugin_id) && isset($this->id)) { // Generate plugin_id on first entity creation. - $this->plugin_id = str_replace('__', ':', $this->id); + $this->plugin_id = str_replace('.', ':', $this->id); } } @@ -121,6 +118,7 @@ public function getMethods() { return $this->getMethodsForMethodGranularity(); } else { + throw new \InvalidArgumentException("A different granularity then 'method' is not supported yet."); // @todo Add resource-level granularity support in https://www.drupal.org/node/2721595. } } @@ -144,6 +142,7 @@ public function getAuthenticationProviders($method) { return $this->getAuthenticationProvidersForMethodGranularity($method); } else { + throw new \InvalidArgumentException("A different granularity then 'method' is not supported yet."); // @todo Add resource-level granularity support in https://www.drupal.org/node/2721595. } } @@ -173,6 +172,7 @@ public function getFormats($method) { return $this->getFormatsForMethodGranularity($method); } else { + throw new \InvalidArgumentException("A different granularity then 'method' is not supported yet."); // @todo Add resource-level granularity support in https://www.drupal.org/node/2721595. } } @@ -195,11 +195,7 @@ protected function getFormatsForMethodGranularity($method) { } /** - * Returns the plugin collections used by this entity. - * - * @return \Drupal\Component\Plugin\LazyPluginCollection[] - * An array of plugin collections, keyed by the property name they use to - * store their configuration. + * {@inheritdoc} */ public function getPluginCollections() { return [ @@ -218,7 +214,7 @@ public function calculateDependencies() { $this->addDependency($type, $dependency); } } - return $this->dependencies; + return $this; } /** diff --git a/core/modules/rest/src/RequestHandler.php b/core/modules/rest/src/RequestHandler.php index 212ae06..b7cd063 100644 --- a/core/modules/rest/src/RequestHandler.php +++ b/core/modules/rest/src/RequestHandler.php @@ -45,7 +45,7 @@ public function __construct(EntityStorageInterface $entity_storage) { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('entity_type.manager')->getStorage('rest_resource_config')); + return new static($container->get('entity_type.manager')->getStorage('rest_resource_configuration')); } /** @@ -63,7 +63,7 @@ public function handle(RouteMatchInterface $route_match, Request $request) { $method = strtolower($request->getMethod()); $resource_config_id = $route_match->getRouteObject()->getDefault('_rest_resource_config'); - /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */ + /** @var \Drupal\rest\RestResourceConfigurationInterface $resource_config */ $resource_config = $this->resourceStorage->load($resource_config_id); $resource = $resource_config->getResourcePlugin(); @@ -156,7 +156,7 @@ public function csrfToken() { * The serializer to use. * @param string $format * The response format. - * @param \Drupal\rest\RestResourceConfigInterface $resource_config + * @param \Drupal\rest\RestResourceConfigurationInterface $resource_config * The resource config. * * @return \Drupal\rest\ResourceResponse @@ -165,7 +165,7 @@ public function csrfToken() { * @todo Add test coverage for language negotiation contexts in * https://www.drupal.org/node/2135829. */ - protected function renderResponse(Request $request, ResourceResponseInterface $response, SerializerInterface $serializer, $format, RestResourceConfigInterface $resource_config) { + protected function renderResponse(Request $request, ResourceResponseInterface $response, SerializerInterface $serializer, $format, RestResourceConfigurationInterface $resource_config) { $data = $response->getResponseData(); if ($response instanceof CacheableResponseInterface) { diff --git a/core/modules/rest/src/RestPermissions.php b/core/modules/rest/src/RestPermissions.php index 473c192..a9fe81b 100644 --- a/core/modules/rest/src/RestPermissions.php +++ b/core/modules/rest/src/RestPermissions.php @@ -36,7 +36,7 @@ class RestPermissions implements ContainerInjectionInterface { */ public function __construct(ResourcePluginManager $rest_plugin_manager, EntityTypeManagerInterface $entity_type_manager) { $this->restPluginManager = $rest_plugin_manager; - $this->resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_config'); + $this->resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_configuration'); } /** @@ -53,7 +53,7 @@ public static function create(ContainerInterface $container) { */ public function permissions() { $permissions = []; - /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_configs */ + /** @var \Drupal\rest\RestResourceConfigurationInterface[] $resource_configs */ $resource_configs = $this->resourceConfigStorage->loadMultiple(); foreach ($resource_configs as $resource_config) { $plugin = $resource_config->getResourcePlugin(); diff --git a/core/modules/rest/src/RestResourceConfigInterface.php b/core/modules/rest/src/RestResourceConfigurationInterface.php similarity index 90% rename from core/modules/rest/src/RestResourceConfigInterface.php rename to core/modules/rest/src/RestResourceConfigurationInterface.php index eac95ac..4c09591 100644 --- a/core/modules/rest/src/RestResourceConfigInterface.php +++ b/core/modules/rest/src/RestResourceConfigurationInterface.php @@ -8,7 +8,7 @@ /** * Defines a configuration entity to store enabled REST resources. */ -interface RestResourceConfigInterface extends ConfigEntityInterface, EntityWithPluginCollectionInterface { +interface RestResourceConfigurationInterface extends ConfigEntityInterface, EntityWithPluginCollectionInterface { /** * Retrieves the REST resource plugin. diff --git a/core/modules/rest/src/Routing/ResourceRoutes.php b/core/modules/rest/src/Routing/ResourceRoutes.php index 5d3ae30..35b4aa2 100644 --- a/core/modules/rest/src/Routing/ResourceRoutes.php +++ b/core/modules/rest/src/Routing/ResourceRoutes.php @@ -5,7 +5,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\RouteSubscriberBase; use Drupal\rest\Plugin\Type\ResourcePluginManager; -use Drupal\rest\RestResourceConfigInterface; +use Drupal\rest\RestResourceConfigurationInterface; use Psr\Log\LoggerInterface; use Symfony\Component\Routing\RouteCollection; @@ -47,7 +47,7 @@ class ResourceRoutes extends RouteSubscriberBase { */ public function __construct(ResourcePluginManager $manager, EntityTypeManagerInterface $entity_type_manager, LoggerInterface $logger) { $this->manager = $manager; - $this->resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_config'); + $this->resourceConfigStorage = $entity_type_manager->getStorage('rest_resource_configuration'); $this->logger = $logger; } @@ -60,7 +60,7 @@ public function __construct(ResourcePluginManager $manager, EntityTypeManagerInt */ protected function alterRoutes(RouteCollection $collection) { // Iterate over all enabled REST resource configs. - /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_configs */ + /** @var \Drupal\rest\RestResourceConfigurationInterface[] $resource_configs */ $resource_configs = $this->resourceConfigStorage->loadMultiple(); // Iterate over all enabled resource plugins. foreach ($resource_configs as $resource_config) { @@ -75,13 +75,13 @@ protected function alterRoutes(RouteCollection $collection) { * This method determines where a resource is reachable, what path * replacements are used, the required HTTP method for the operation etc. * - * @param \Drupal\rest\RestResourceConfigInterface $rest_resource_config + * @param \Drupal\rest\RestResourceConfigurationInterface $rest_resource_config * The rest resource config. * * @return \Symfony\Component\Routing\RouteCollection * The route collection. */ - protected function getRoutesForResourceConfig(RestResourceConfigInterface $rest_resource_config) { + protected function getRoutesForResourceConfig(RestResourceConfigurationInterface $rest_resource_config) { $plugin = $rest_resource_config->getResourcePlugin(); $collection = new RouteCollection(); diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index ff18ba6..4ace2b7 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -65,7 +65,7 @@ protected function setUp() { $this->defaultFormat = 'hal_json'; $this->defaultMimeType = 'application/hal+json'; $this->defaultAuth = array('cookie'); - $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config'); + $this->resourceConfigStorage = $this->container->get('entity_type.manager')->getStorage('rest_resource_configuration'); // Create a test content type for node testing. if (in_array('node', static::$modules)) { $this->drupalCreateContentType(array('name' => 'resttest', 'type' => 'resttest')); @@ -270,7 +270,7 @@ protected function enableService($resource_type, $method = 'GET', $format = NULL // Enable REST API for this entity type. $resource_config_id = str_replace(':', '__', $resource_type); // get entity by id - /** @var \Drupal\rest\RestResourceConfigInterface $resource_config */ + /** @var \Drupal\rest\RestResourceConfigurationInterface $resource_config */ $resource_config = $this->resourceConfigStorage->load($resource_config_id); $resource_config = $resource_config ?: $this->resourceConfigStorage->create(['id' => $resource_config_id, 'granularity' => 'method', 'configuration' => []]); $configuration = $resource_config->get('configuration'); diff --git a/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php index 3c861d7..7154486 100644 --- a/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php +++ b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php @@ -33,7 +33,7 @@ public function setDatabaseDumpFiles() { */ public function testResourcesConvertedToConfigEntities() { /** @var \Drupal\Core\Entity\EntityStorageInterface $resource_config_storage */ - $resource_config_storage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config'); + $resource_config_storage = $this->container->get('entity_type.manager')->getStorage('rest_resource_configuration'); // Make sure we have the expected values before the update. $rest_settings = $this->config('rest.settings'); diff --git a/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php new file mode 100644 index 0000000..b413523 --- /dev/null +++ b/core/modules/rest/tests/src/Kernel/Entity/ConfigDependenciesTest.php @@ -0,0 +1,190 @@ + 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']); + + $rest_config = RestResourceConfiguration::create([ + 'plugin_id' => 'entity:entity_test', + 'granularity' => 'method', + 'configuration' => [ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + 'supported_formats' => ['hal_json'], + ], + ], + ]); + + $result = $config_dependencies->calculateDependencies($rest_config); + $this->assertEquals(['module' => [ + 'serialization', 'basic_auth', 'hal', 'entity_test', + ]], $result); + } + + /** + * @covers ::onDependencyRemoval + * @covers ::calculateDependenciesForMethodGranularity + */ + public function testOnDependencyRemovalRemoveUnrelatedDependency() { + $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']); + + $rest_config = RestResourceConfiguration::create([ + 'plugin_id' => 'entity:entity_test', + 'granularity' => 'method', + 'configuration' => [ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + 'supported_formats' => ['hal_json'], + ], + ], + ]); + + $this->assertFalse($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['node']])); + $this->assertEquals([ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + 'supported_formats' => ['hal_json'], + ], + ], $rest_config->get('configuration')); + } + + /** + * @covers ::onDependencyRemoval + * @covers ::calculateDependenciesForMethodGranularity + */ + public function testOnDependencyRemovalRemoveFormat() { + $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']); + + $rest_config = RestResourceConfiguration::create([ + 'plugin_id' => 'entity:entity_test', + 'granularity' => 'method', + 'configuration' => [ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + 'supported_formats' => ['hal_json'], + ], + ], + ]); + + $this->assertTrue($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['hal']])); + $this->assertEquals(['json'], $rest_config->getFormats('GET')); + $this->assertEquals([], $rest_config->getFormats('POST')); + $this->assertEquals([ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + ], + ], $rest_config->get('configuration')); + } + + /** + * @covers ::onDependencyRemoval + * @covers ::calculateDependenciesForMethodGranularity + */ + public function testOnDependencyRemovalRemoveAuth() { + $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']); + + $rest_config = RestResourceConfiguration::create([ + 'plugin_id' => 'entity:entity_test', + 'granularity' => 'method', + 'configuration' => [ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + 'supported_formats' => ['hal_json'], + ], + ], + ]); + + $this->assertTrue($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['basic_auth']])); + $this->assertEquals(['cookie'], $rest_config->getAuthenticationProviders('GET')); + $this->assertEquals([], $rest_config->getAuthenticationProviders('POST')); + $this->assertEquals([ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_formats' => ['hal_json'], + ], + ], $rest_config->get('configuration')); + } + + /** + * @covers ::onDependencyRemoval + * @covers ::calculateDependenciesForMethodGranularity + */ + public function testOnDependencyRemovalRemoveAuthAndFormats() { + $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']); + + $rest_config = RestResourceConfiguration::create([ + 'plugin_id' => 'entity:entity_test', + 'granularity' => 'method', + 'configuration' => [ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + 'POST' => [ + 'supported_auth' => ['basic_auth'], + 'supported_formats' => ['hal_json'], + ], + ], + ]); + + $this->assertTrue($config_dependencies->onDependencyRemoval($rest_config, ['module' => ['basic_auth', 'hal']])); + $this->assertEquals(['json'], $rest_config->getFormats('GET')); + $this->assertEquals(['cookie'], $rest_config->getAuthenticationProviders('GET')); + $this->assertEquals([], $rest_config->getFormats('POST')); + $this->assertEquals([], $rest_config->getAuthenticationProviders('POST')); + $this->assertEquals([ + 'GET' => [ + 'supported_auth' => ['cookie'], + 'supported_formats' => ['json'], + ], + ], $rest_config->get('configuration')); + } + +} diff --git a/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php b/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php index e09b175..01f8ef5 100644 --- a/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php +++ b/core/modules/rest/tests/src/Kernel/RequestHandlerTest.php @@ -8,7 +8,7 @@ use Drupal\rest\Plugin\ResourceBase; use Drupal\rest\RequestHandler; use Drupal\rest\ResourceResponse; -use Drupal\rest\RestResourceConfigInterface; +use Drupal\rest\RestResourceConfigurationInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; @@ -58,7 +58,7 @@ public function testBaseHandler() { ->shouldBeCalled(); // Setup the configuration. - $config = $this->prophesize(RestResourceConfigInterface::class); + $config = $this->prophesize(RestResourceConfigurationInterface::class); $config->getResourcePlugin()->willReturn($resource->reveal()); $config->getCacheContexts()->willReturn([]); $config->getCacheTags()->willReturn([]); @@ -100,7 +100,7 @@ public function testSerialization($data) { $resource = $this->prophesize(StubRequestHandlerResourcePlugin::class); // Setup the configuration. - $config = $this->prophesize(RestResourceConfigInterface::class); + $config = $this->prophesize(RestResourceConfigurationInterface::class); $config->getResourcePlugin()->willReturn($resource->reveal()); $config->getCacheContexts()->willReturn([]); $config->getCacheTags()->willReturn([]);