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 AccessDeniedTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'block',
'node',
'system_test',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer site configuration',
'link to any page',
'administer blocks',
]);
$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 testAccessDenied() {
$this
->drupalGet('admin');
$this
->assertSession()
->pageTextContains('Access denied');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->createUser([]));
$this
->drupalGet('admin', [
'query' => [
'foo' => 'bar',
],
]);
$settings = $this
->getDrupalSettings();
$this
->assertEquals('admin', $settings['path']['currentPath']);
$this
->assertTrue($settings['path']['currentPathIsAdmin']);
$this
->assertEquals([
'foo' => 'bar',
], $settings['path']['currentQuery']);
$this
->drupalLogin($this->adminUser);
$edit = [
'site_403' => 'user/' . $this->adminUser
->id(),
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains("The path '{$edit['site_403']}' has to start with a slash.");
$edit = [
'site_403' => '/user/' . $this->adminUser
->id(),
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Save configuration');
$block = $this
->drupalPlaceBlock('user_login_block', [
'id' => 'login',
]);
$this
->drupalLogout();
$this
->drupalGet('admin');
$this
->assertSession()
->pageTextContains($this->adminUser
->getAccountName());
$this
->assertSession()
->pageTextContains('Username');
$this
->drupalLogin($this->adminUser);
$edit = [
'site_403' => '',
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalLogout();
$this
->drupalGet('admin');
$this
->assertSession()
->pageTextContains('Access denied');
$this
->assertSession()
->statusCodeEquals(403);
$this
->assertSession()
->pageTextContains('Username');
$this
->drupalLogin($this->adminUser);
$this
->config('system.site')
->set('page.403', '/user/login')
->save();
$block
->disable()
->save();
$this
->drupalLogout();
$edit = [
'name' => $this->adminUser
->getAccountName(),
'pass' => $this->adminUser->pass_raw,
];
$this
->drupalGet('admin/config/system/site-information');
$this
->submitForm($edit, 'Log in');
$this
->assertSession()
->pageTextContains('Basic site settings');
}
public function testAccessDeniedCustomPageWithAccessDenied() {
$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);
$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);
$this
->assertCacheContext('user.roles');
}
}