PageNotFoundTest.php in Drupal 9
File
core/modules/system/tests/src/Functional/System/PageNotFoundTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\user\RoleInterface;
class PageNotFoundTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'system_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
'link to any page',
]);
$this->adminUser->roles[] = 'administrator';
$this->adminUser
->save();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
'access user profiles',
]);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'access user profiles',
]);
}
public function testPageNotFound() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this
->randomMachineName(10));
$this
->assertSession()
->pageTextContains('Page not found');
$edit = [
'site_404' => 'user/' . $this->adminUser
->id(),
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains("The path '{$edit['site_404']}' has to start with a slash.");
$edit = [
'site_404' => '/user/' . $this->adminUser
->id(),
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet($this
->randomMachineName(10));
$this
->assertSession()
->pageTextContains($this->adminUser
->getAccountName());
}
public function testPageNotFoundCustomPageWithAccessDenied() {
$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);
$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);
$this
->assertCacheContext('user.roles');
}
}