3 namespace Drupal\system\Tests\System;
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\simpletest\WebTestBase;
7 use Drupal\user\RoleInterface;
10 * Tests page access denied functionality, including custom 403 pages.
14 class AccessDeniedTest extends WebTestBase {
21 public static $modules = ['block', 'node', 'system_test'];
25 protected function setUp() {
28 $this->drupalPlaceBlock('page_title_block');
30 // Create an administrative user.
31 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer site configuration', 'link to any page', 'administer blocks']);
32 $this->adminUser->roles[] = 'administrator';
33 $this->adminUser->save();
35 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access user profiles']);
36 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access user profiles']);
39 public function testAccessDenied() {
40 $this->drupalGet('admin');
41 $this->assertText(t('Access denied'), 'Found the default 403 page');
42 $this->assertResponse(403);
44 // Ensure that users without permission are denied access and have the
45 // correct path information in drupalSettings.
46 $this->drupalLogin($this->createUser([]));
47 $this->drupalGet('admin', ['query' => ['foo' => 'bar']]);
48 $this->assertEqual($this->drupalSettings['path']['currentPath'], 'admin');
49 $this->assertEqual($this->drupalSettings['path']['currentPathIsAdmin'], TRUE);
50 $this->assertEqual($this->drupalSettings['path']['currentQuery'], ['foo' => 'bar']);
52 $this->drupalLogin($this->adminUser);
54 // Set a custom 404 page without a starting slash.
56 'site_403' => 'user/' . $this->adminUser->id(),
58 $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
59 $this->assertRaw(SafeMarkup::format("The path '%path' has to start with a slash.", ['%path' => $edit['site_403']]));
61 // Use a custom 403 page.
63 'site_403' => '/user/' . $this->adminUser->id(),
65 $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
67 // Enable the user login block.
68 $block = $this->drupalPlaceBlock('user_login_block', ['id' => 'login']);
70 // Log out and check that the user login block is shown on custom 403 pages.
71 $this->drupalLogout();
72 $this->drupalGet('admin');
73 $this->assertText($this->adminUser->getUsername(), 'Found the custom 403 page');
74 $this->assertText(t('Username'), 'Blocks are shown on the custom 403 page');
76 // Log back in and remove the custom 403 page.
77 $this->drupalLogin($this->adminUser);
81 $this->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
83 // Logout and check that the user login block is shown on default 403 pages.
84 $this->drupalLogout();
85 $this->drupalGet('admin');
86 $this->assertText(t('Access denied'), 'Found the default 403 page');
87 $this->assertResponse(403);
88 $this->assertText(t('Username'), 'Blocks are shown on the default 403 page');
90 // Log back in, set the custom 403 page to /user/login and remove the block
91 $this->drupalLogin($this->adminUser);
92 $this->config('system.site')->set('page.403', '/user/login')->save();
93 $block->disable()->save();
95 // Check that we can log in from the 403 page.
96 $this->drupalLogout();
98 'name' => $this->adminUser->getUsername(),
99 'pass' => $this->adminUser->pass_raw,
101 $this->drupalPostForm('admin/config/system/site-information', $edit, t('Log in'));
103 // Check that we're still on the same page.
104 $this->assertText(t('Basic site settings'));
108 * Tests that an inaccessible custom 403 page falls back to the default.
110 public function testAccessDeniedCustomPageWithAccessDenied() {
111 // Sets up a 403 page not accessible by the anonymous user.
112 $this->config('system.site')->set('page.403', '/system-test/custom-4xx')->save();
114 $this->drupalGet('/system-test/always-denied');
115 $this->assertNoText('Admin-only 4xx response');
116 $this->assertText('You are not authorized to access this page.');
117 $this->assertResponse(403);
118 // Verify the access cacheability metadata for custom 403 is bubbled.
119 $this->assertCacheContext('user.roles');
121 $this->drupalLogin($this->adminUser);
122 $this->drupalGet('/system-test/always-denied');
123 $this->assertText('Admin-only 4xx response');
124 $this->assertResponse(403);
125 // Verify the access cacheability metadata for custom 403 is bubbled.
126 $this->assertCacheContext('user.roles');