public function AccessDeniedTest::testAccessDenied in Drupal 8
Same name and namespace in other branches
- 9 core/modules/system/tests/src/Functional/System/AccessDeniedTest.php \Drupal\Tests\system\Functional\System\AccessDeniedTest::testAccessDenied()
File
- core/modules/system/tests/src/Functional/System/AccessDeniedTest.php, line 52
Class
- AccessDeniedTest
- Tests page access denied functionality, including custom 403 pages.
Namespace
Drupal\Tests\system\Functional\System
Code
public function testAccessDenied() {
$this
->drupalGet('admin');
$this
->assertText(t('Access denied'), 'Found the default 403 page');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this
->createUser([]));
$this
->drupalGet('admin', [
'query' => [
'foo' => 'bar',
],
]);
$settings = $this
->getDrupalSettings();
$this
->assertEqual($settings['path']['currentPath'], 'admin');
$this
->assertEqual($settings['path']['currentPathIsAdmin'], TRUE);
$this
->assertEqual($settings['path']['currentQuery'], [
'foo' => 'bar',
]);
$this
->drupalLogin($this->adminUser);
$edit = [
'site_403' => 'user/' . $this->adminUser
->id(),
];
$this
->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
$this
->assertRaw(new FormattableMarkup("The path '%path' has to start with a slash.", [
'%path' => $edit['site_403'],
]));
$edit = [
'site_403' => '/user/' . $this->adminUser
->id(),
];
$this
->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
$block = $this
->drupalPlaceBlock('user_login_block', [
'id' => 'login',
]);
$this
->drupalLogout();
$this
->drupalGet('admin');
$this
->assertText($this->adminUser
->getAccountName(), 'Found the custom 403 page');
$this
->assertText(t('Username'), 'Blocks are shown on the custom 403 page');
$this
->drupalLogin($this->adminUser);
$edit = [
'site_403' => '',
];
$this
->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
$this
->drupalLogout();
$this
->drupalGet('admin');
$this
->assertText(t('Access denied'), 'Found the default 403 page');
$this
->assertSession()
->statusCodeEquals(403);
$this
->assertText(t('Username'), 'Blocks are shown on the default 403 page');
$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
->drupalPostForm('admin/config/system/site-information', $edit, t('Log in'));
$this
->assertText(t('Basic site settings'));
}