View source
<?php
namespace Drupal\system\Tests\System;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\simpletest\WebTestBase;
use Drupal\user\RoleInterface;
class AccessDeniedTest extends WebTestBase {
public static $modules = [
'block',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer site configuration',
'link to any page',
'administer blocks',
]);
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
'access user profiles',
));
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, array(
'access user profiles',
));
}
function testAccessDenied() {
$this
->drupalGet('admin');
$this
->assertText(t('Access denied'), 'Found the default 403 page');
$this
->assertResponse(403);
$this
->drupalLogin($this->adminUser);
$edit = [
'site_403' => 'user/' . $this->adminUser
->id(),
];
$this
->drupalPostForm('admin/config/system/site-information', $edit, t('Save configuration'));
$this
->assertRaw(SafeMarkup::format("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'));
$this
->drupalPlaceBlock('user_login_block', array(
'id' => 'login',
));
$this
->drupalLogout();
$this
->drupalGet('admin');
$this
->assertText($this->adminUser
->getUsername(), '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
->assertResponse(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();
$edit = [
'region' => -1,
];
$this
->drupalPostForm('admin/structure/block/manage/login', $edit, t('Save block'));
$this
->drupalLogout();
$edit = array(
'name' => $this->adminUser
->getUsername(),
'pass' => $this->adminUser->pass_raw,
);
$this
->drupalPostForm('admin/config/system/site-information', $edit, t('Log in'));
$this
->assertText(t('Site information'));
}
}