You are here

public function UserTest::testQueryInvolvingRoles in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Functional/UserTest.php \Drupal\Tests\jsonapi\Functional\UserTest::testQueryInvolvingRoles()

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

File

tests/src/Functional/UserTest.php, line 423

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');
}