function UserTest::testQueryInvolvingRoles

Same name in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Functional/UserTest.php \Drupal\Tests\jsonapi\Functional\UserTest::testQueryInvolvingRoles()
  2. 10 core/modules/jsonapi/tests/src/Functional/UserTest.php \Drupal\Tests\jsonapi\Functional\UserTest::testQueryInvolvingRoles()
  3. 11.x core/modules/jsonapi/tests/src/Functional/UserTest.php \Drupal\Tests\jsonapi\Functional\UserTest::testQueryInvolvingRoles()

Tests good error DX when trying to filter users by role.

File

core/modules/jsonapi/tests/src/Functional/UserTest.php, line 446

Class

UserTest
JSON:API integration test for the "User" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testQueryInvolvingRoles() {
    $this->setUpAuthorization('GET');
    $collection_url = Url::fromRoute('jsonapi.user--user.collection', [], [
        'query' => [
            'filter[roles.id][value]' => 'e9b1de3f-9517-4c27-bef0-0301229de792',
        ],
    ]);
    $request_options = [];
    $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
    $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
    // The 'administer users' permission is required to filter by role entities.
    $this->grantPermissionsToTestedRole([
        'administer users',
    ]);
    $response = $this->request('GET', $collection_url, $request_options);
    $expected_cache_contexts = [
        'url.path',
        'url.query_args:filter',
        'url.site',
    ];
    $this->assertResourceErrorResponse(400, "Filtering on config entities is not supported by Drupal's entity API. You tried to filter on a Role config entity.", $collection_url, $response, FALSE, [
        '4xx-response',
        'http_response',
    ], $expected_cache_contexts, FALSE, 'MISS');
}

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