function PageNotFoundTest::testPageNotFoundCustomPageWithAccessDenied

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/src/Functional/System/PageNotFoundTest.php \Drupal\Tests\system\Functional\System\PageNotFoundTest::testPageNotFoundCustomPageWithAccessDenied()
  2. 10 core/modules/system/tests/src/Functional/System/PageNotFoundTest.php \Drupal\Tests\system\Functional\System\PageNotFoundTest::testPageNotFoundCustomPageWithAccessDenied()
  3. 9 core/modules/system/tests/src/Functional/System/PageNotFoundTest.php \Drupal\Tests\system\Functional\System\PageNotFoundTest::testPageNotFoundCustomPageWithAccessDenied()
  4. 8.9.x core/modules/system/tests/src/Functional/System/PageNotFoundTest.php \Drupal\Tests\system\Functional\System\PageNotFoundTest::testPageNotFoundCustomPageWithAccessDenied()

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

File

core/modules/system/tests/src/Functional/System/PageNotFoundTest.php, line 87

Class

PageNotFoundTest
Tests page not found functionality, including custom 404 pages.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testPageNotFoundCustomPageWithAccessDenied() : void {
  // Sets up a 404 page not accessible by the anonymous user.
  $this->config('system.site')
    ->set('page.404', '/system-test/custom-4xx')
    ->save();
  $this->drupalGet('/this-path-does-not-exist');
  $this->assertSession()
    ->pageTextNotContains('Admin-only 4xx response');
  $this->assertSession()
    ->pageTextContains('The requested page could not be found.');
  $this->assertSession()
    ->statusCodeEquals(404);
  // Verify the access cacheability metadata for custom 404 is bubbled.
  $this->assertCacheContext('user.roles');
  $this->drupalLogin($this->adminUser);
  $this->drupalGet('/this-path-does-not-exist');
  $this->assertSession()
    ->pageTextContains('Admin-only 4xx response');
  $this->assertSession()
    ->pageTextNotContains('The requested page could not be found.');
  $this->assertSession()
    ->statusCodeEquals(404);
  // Verify the access cacheability metadata for custom 404 is bubbled.
  $this->assertCacheContext('user.roles');
  // Create a custom 404 page with a path alias.
  $this->drupalCreateContentType([
    'type' => 'page',
  ]);
  $this->drupalCreateNode([
    'body' => "I have absolutely no idea what you're talking about.",
    'path' => '/custom-page-not-found',
  ]);
  $edit = [
    'site_404' => '/custom-page-not-found',
  ];
  $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-page-not-found', $this->config('system.site')
    ->get('page.404'));
  $this->drupalGet('/not-a-page');
  $assert_session->statusCodeEquals(404);
  $assert_session->pageTextContains("I have absolutely no idea what you're talking about.");
}

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