function AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied()
  2. 10 core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied()
  3. 9 core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDeniedCustomPageWithAccessDenied()
  4. 8.9.x 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 141

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');
  // Create a custom 403 page with a path alias.
  $this->drupalCreateContentType([
    'type' => 'page',
  ]);
  $this->drupalCreateNode([
    'body' => "Sorry Dave, I'm afraid you can't do that.",
    'path' => '/custom-access-denied',
  ]);
  $edit = [
    'site_403' => '/custom-access-denied',
  ];
  $this->drupalGet('admin/config/system/site-information');
  $this->submitForm($edit, 'Save configuration');
  $assert_session = $this->assertSession();
  $assert_session->statusMessageContains('The configuration options have been saved.');
  $this->assertSame('/custom-access-denied', $this->config('system.site')
    ->get('page.403'));
  $this->drupalGet('/system-test/always-denied');
  $assert_session->statusCodeEquals(403);
  $assert_session->pageTextContains("Sorry Dave, I'm afraid you can't do that.");
}

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