You are here

protected function BasicAuthResourceTestTrait::assertResponseWhenMissingAuthentication in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php \Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait::assertResponseWhenMissingAuthentication()
1 method overrides BasicAuthResourceTestTrait::assertResponseWhenMissingAuthentication()
LayoutRestTestBase::assertResponseWhenMissingAuthentication in core/modules/layout_builder/tests/src/Functional/Rest/LayoutRestTestBase.php
Verifies the error response in case of missing authentication.

File

core/modules/rest/tests/src/Functional/BasicAuthResourceTestTrait.php, line 34

Class

BasicAuthResourceTestTrait
Trait for ResourceTestBase subclasses testing $auth=basic_auth.

Namespace

Drupal\Tests\rest\Functional

Code

protected function assertResponseWhenMissingAuthentication($method, ResponseInterface $response) {
  if ($method !== 'GET') {
    return $this
      ->assertResourceErrorResponse(401, 'No authentication credentials provided.', $response);
  }
  $expected_page_cache_header_value = $method === 'GET' ? 'MISS' : FALSE;
  $expected_cacheability = $this
    ->getExpectedUnauthorizedAccessCacheability()
    ->addCacheableDependency($this
    ->getExpectedUnauthorizedEntityAccessCacheability(FALSE))
    ->addCacheableDependency($this
    ->config('system.site'))
    ->addCacheTags([
    'config:user.role.anonymous',
  ]);

  // Only add the 'user.roles:anonymous' cache context if its parent cache
  // context is not already present.
  if (!in_array('user.roles', $expected_cacheability
    ->getCacheContexts(), TRUE)) {
    $expected_cacheability
      ->addCacheContexts([
      'user.roles:anonymous',
    ]);
  }
  $this
    ->assertResourceErrorResponse(401, 'No authentication credentials provided.', $response, $expected_cacheability
    ->getCacheTags(), $expected_cacheability
    ->getCacheContexts(), $expected_page_cache_header_value, FALSE);
}