core/modules/rest/rest.install | 12 ++++- .../Update/RestConfigurationEntitiesUpdateTest.php | 61 +++++++++++++++++++++ .../update/drupal-8.rest-rest_update_8201.php | 63 ++++++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) diff --git a/core/modules/rest/rest.install b/core/modules/rest/rest.install index ac4ce9c..c40d8cf 100644 --- a/core/modules/rest/rest.install +++ b/core/modules/rest/rest.install @@ -25,11 +25,17 @@ function rest_requirements($phase) { } /** + * @defgroup updates-8.1.x-to-8.2.x Updates from 8.1.x to 8.2.x + * @{ + * Update functions from 8.1.x to 8.2.x. + */ + +/** * Install the REST config entity type and convert old settings-based config. * * @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_8100() { +function rest_update_8201() { \Drupal::entityDefinitionUpdateManager()->installEntityType(\Drupal::entityTypeManager()->getDefinition('rest_resource_config')); foreach (\Drupal::config('rest.settings')->get('resources') as $key => $resource) { $resource = RestResourceConfig::create([ @@ -43,3 +49,7 @@ function rest_update_8100() { ->clear('resources') ->save(); } + +/** + * @} End of "defgroup updates-8.1.x-to-8.2.x". + */ diff --git a/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php new file mode 100644 index 0000000..3c861d7 --- /dev/null +++ b/core/modules/rest/src/Tests/Update/RestConfigurationEntitiesUpdateTest.php @@ -0,0 +1,61 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz', + __DIR__ . '/../../../../rest/tests/fixtures/update/drupal-8.rest-rest_update_8201.php', + ]; + } + + /** + * Tests rest_update_8201(). + */ + public function testResourcesConvertedToConfigEntities() { + /** @var \Drupal\Core\Entity\EntityStorageInterface $resource_config_storage */ + $resource_config_storage = $this->container->get('entity_type.manager')->getStorage('rest_resource_config'); + + // Make sure we have the expected values before the update. + $rest_settings = $this->config('rest.settings'); + $this->assertTrue(array_key_exists('resources', $rest_settings->getRawData())); + $this->assertTrue(array_key_exists('entity:node', $rest_settings->getRawData()['resources'])); + $resource_config_entities = $resource_config_storage->loadMultiple(); + $this->assertIdentical([], array_keys($resource_config_entities)); + + // Read the existing 'entity:node' resource configuration so we can verify + // it after the update. + $node_configuration = $rest_settings->getRawData()['resources']['entity:node']; + + $this->runUpdates(); + + // Make sure we have the expected values after the update. + $rest_settings = $this->config('rest.settings'); + $this->assertFalse(array_key_exists('resources', $rest_settings->getRawData())); + $resource_config_entities = $resource_config_storage->loadMultiple(); + $this->assertIdentical(['entity__node'], array_keys($resource_config_entities)); + $node_resource_config_entity = $resource_config_entities['entity__node']; + $this->assertIdentical('method', $node_resource_config_entity->get('granularity')); + $this->assertIdentical($node_configuration,$node_resource_config_entity->get('configuration')); + } + +} diff --git a/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8201.php b/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8201.php new file mode 100644 index 0000000..f035e94 --- /dev/null +++ b/core/modules/rest/tests/fixtures/update/drupal-8.rest-rest_update_8201.php @@ -0,0 +1,63 @@ +insert('key_value') + ->fields([ + 'collection' => 'system.schema', + 'name' => 'rest', + 'value' => 'i:8000;', + ]) + ->fields([ + 'collection' => 'system.schema', + 'name' => 'serialization', + 'value' => 'i:8000;', + ]) + ->execute(); + +// Update core.extension. +$extensions = $connection->select('config') + ->fields('config', ['data']) + ->condition('collection', '') + ->condition('name', 'core.extension') + ->execute() + ->fetchField(); +$extensions = unserialize($extensions); +$extensions['module']['rest'] = 8000; +$extensions['module']['serialization'] = 8000; +$connection->update('config') + ->fields([ + 'data' => serialize($extensions), + ]) + ->condition('collection', '') + ->condition('name', 'core.extension') + ->execute(); + +// Install the rest configuration. +$config = [ + 'resources' => [ + 'entity:node' => [ + 'GET' => [ + 'supported_formats' => ['json'], + 'supported_auth' => [], + ], + ], + ], + 'link_domain' => '~', +]; +$data = $connection->insert('config') + ->fields([ + 'name' => 'rest.settings', + 'data' => serialize($config), + 'collection' => '' + ]) + ->execute();