function ResourceGranularityUpdateTest::testMethodGranularityConvertedToResourceGranularity

Tests rest_post_update_simplify_resource_granularity().

File

core/modules/rest/tests/src/Functional/Update/ResourceGranularityUpdateTest.php, line 36

Class

ResourceGranularityUpdateTest
Tests method-granularity REST config is simplified to resource-granularity.

Namespace

Drupal\Tests\rest\Functional\Update

Code

public function testMethodGranularityConvertedToResourceGranularity() {
    
    /** @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.
    $resource_config_entities = $resource_config_storage->loadMultiple();
    $this->assertIdentical([
        'entity.comment',
        'entity.node',
        'entity.user',
    ], array_keys($resource_config_entities));
    $this->assertIdentical('method', $resource_config_entities['entity.node']->get('granularity'));
    $this->assertIdentical('method', $resource_config_entities['entity.comment']->get('granularity'));
    $this->assertIdentical('method', $resource_config_entities['entity.user']->get('granularity'));
    // Read the existing 'entity:comment' and 'entity:user' resource
    // configuration so we can verify it after the update.
    $comment_resource_configuration = $resource_config_entities['entity.comment']->get('configuration');
    $user_resource_configuration = $resource_config_entities['entity.user']->get('configuration');
    $this->runUpdates();
    // Make sure we have the expected values after the update.
    $resource_config_entities = $resource_config_storage->loadMultiple();
    $this->assertIdentical([
        'entity.comment',
        'entity.node',
        'entity.user',
    ], array_keys($resource_config_entities));
    // 'entity:node' should be updated.
    $this->assertIdentical('resource', $resource_config_entities['entity.node']->get('granularity'));
    $this->assertidentical($resource_config_entities['entity.node']->get('configuration'), [
        'methods' => [
            'GET',
            'POST',
            'PATCH',
            'DELETE',
        ],
        'formats' => [
            'hal_json',
        ],
        'authentication' => [
            'basic_auth',
        ],
    ]);
    // 'entity:comment' should be unchanged.
    $this->assertIdentical('method', $resource_config_entities['entity.comment']->get('granularity'));
    $this->assertIdentical($comment_resource_configuration, $resource_config_entities['entity.comment']->get('configuration'));
    // 'entity:user' should be unchanged.
    $this->assertIdentical('method', $resource_config_entities['entity.user']->get('granularity'));
    $this->assertIdentical($user_resource_configuration, $resource_config_entities['entity.user']->get('configuration'));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.