/** * Simplify method-granularity REST resource config to resource-granularity. * * @see https://www.drupal.org/node/2721595 */ function rest_post_update_resource_granularity() { /** @var \Drupal\rest\RestResourceConfigInterface[] $resource_config_entities */ $resource_config_entities = RestResourceConfig::loadMultiple(); foreach ($resource_config_entities as $resource_config_entity) { if ($resource_config_entity->get('granularity') === RestResourceConfigInterface::METHOD_GRANULARITY) { $configuration = $resource_config_entity->get('configuration'); $format_and_auth_configuration = []; foreach (array_keys($configuration) as $method) { $format_and_auth_configuration['format'][$method] = implode(',', $configuration[$method]['supported_formats']); $format_and_auth_configuration['auth'][$method] = implode(',', $configuration[$method]['supported_auth']); } // If each method has the same formats and the same authentication // providers configured, convert it to 'granularity: resource', which has // a simpler/less verbose configuration. if (count(array_unique($format_and_auth_configuration['format'])) === 1 && count(array_unique($format_and_auth_configuration['auth'])) === 1) { $first_method = array_keys($configuration)[0]; $resource_config_entity->set('configuration', ['methods' => array_keys($configuration), 'formats' => $configuration[$first_method]['supported_formats'], 'authentication' => $configuration[$first_method]['supported_auth']]); $resource_config_entity->set('granularity', RestResourceConfigInterface::RESOURCE_GRANULARITY); $resource_config_entity->save(); } } } }
/** * @covers ::onDependencyRemoval * @covers ::onDependencyRemovalForResourceGranularity * * @dataProvider providerOnDependencyRemovalForResourceGranularity */ public function testOnDependencyRemovalForResourceGranularity(array $configuration, $module, $expected_configuration) { assert('is_string($module)'); assert('$expected_configuration === FALSE || is_array($expected_configuration)'); $config_dependencies = new ConfigDependencies(['hal_json' => 'hal', 'json' => 'serialization'], ['basic_auth' => 'basic_auth']); $rest_config = RestResourceConfig::create($configuration); $this->assertSame(!empty($expected_configuration), $config_dependencies->onDependencyRemoval($rest_config, ['module' => [$module]])); if (!empty($expected_configuration)) { $this->assertEquals($expected_configuration, $rest_config->get('configuration')); } }
/** * @covers ::calculateDependencies */ public function testCalculateDependencies() { $rest_config = RestResourceConfig::create(['plugin_id' => 'entity:entity_test', 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, 'configuration' => ['GET' => ['supported_auth' => ['cookie'], 'supported_formats' => ['json']], 'POST' => ['supported_auth' => ['basic_auth'], 'supported_formats' => ['hal_json']]]]); $rest_config->calculateDependencies(); $this->assertEquals(['module' => ['basic_auth', 'entity_test', 'hal', 'serialization', 'user']], $rest_config->getDependencies()); }