function AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied()
  2. 8.9.x core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied()
  3. 10 core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied()

Tests that an inaccessible custom 403 page falls back to the default.

File

core/modules/system/tests/src/Functional/System/AccessDeniedTest.php, line 131

Class

AccessDeniedTest
Tests page access denied functionality, including custom 403 pages.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testAccessDeniedCustomPageWithAccessDenied() : void {
    // Sets up a 403 page not accessible by the anonymous user.
    $this->config('system.site')
        ->set('page.403', '/system-test/custom-4xx')
        ->save();
    $this->drupalGet('/system-test/always-denied');
    $this->assertSession()
        ->pageTextNotContains('Admin-only 4xx response');
    $this->assertSession()
        ->pageTextContains('You are not authorized to access this page.');
    $this->assertSession()
        ->statusCodeEquals(403);
    // Verify the access cacheability metadata for custom 403 is bubbled.
    $this->assertCacheContext('user.roles');
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('/system-test/always-denied');
    $this->assertSession()
        ->pageTextContains('Admin-only 4xx response');
    $this->assertSession()
        ->statusCodeEquals(403);
    // Verify the access cacheability metadata for custom 403 is bubbled.
    $this->assertCacheContext('user.roles');
}

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