View source
<?php
namespace Drupal\Tests\masquerade\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\block\Functional\AssertBlockAppearsTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\Role;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
abstract class MasqueradeWebTestBase extends BrowserTestBase {
use AssertBlockAppearsTrait;
use StringTranslationTrait;
use AssertPageCacheContextsAndTagsTrait;
protected static $modules = [
'masquerade',
'user',
'block',
];
protected $defaultTheme = 'stark';
protected $admin_user, $auth_user, $editor_user, $masquerade_user, $moderator_user;
protected $leadEditorUser;
protected $superUser;
protected $admin_role, $editor_role, $masquerade_role, $moderator_role;
protected $leadRole;
protected function setUp() {
parent::setUp();
$this->admin_role = Role::create([
'id' => 'administrator',
'label' => 'Administrator',
]);
$this->admin_role
->set('is_admin', TRUE)
->save();
$this->masquerade_role = Role::create([
'id' => 'masquerade',
'label' => 'Masquerade',
]);
$this->masquerade_role
->grantPermission('masquerade as authenticated')
->save();
$this->editor_role = Role::create([
'id' => 'editor',
'label' => 'Editor',
]);
$this->editor_role
->grantPermission('masquerade as masquerade')
->grantPermission('masquerade as authenticated')
->save();
$this->leadRole = Role::create([
'id' => 'lead',
'label' => 'Lead Editor',
]);
$this->leadRole
->grantPermission('masquerade as editor')
->grantPermission('masquerade as authenticated')
->save();
$this->moderator_role = Role::create([
'id' => 'moderator',
'label' => 'Moderator',
]);
$this->moderator_role
->grantPermission('masquerade as any user')
->save();
$this->admin_user = $this
->drupalCreateUser();
$this->admin_user
->setUsername('admin_user');
$this->admin_user
->addRole($this->admin_role
->id());
$this->admin_user
->save();
$this->moderator_user = $this
->drupalCreateUser();
$this->moderator_user
->setUsername('moderator_user');
$this->moderator_user
->addRole($this->moderator_role
->id());
$this->moderator_user
->save();
$this->editor_user = $this
->drupalCreateUser();
$this->editor_user
->setUsername('editor_user');
$this->editor_user
->addRole($this->editor_role
->id());
$this->editor_user
->save();
$this->leadEditorUser = $this
->drupalCreateUser();
$this->leadEditorUser
->setUsername('lead_editor_user');
$this->leadEditorUser
->addRole($this->leadRole
->id());
$this->leadEditorUser
->save();
$this->superUser = $this
->drupalCreateUser();
$this->superUser
->setUsername('super_user');
$this->superUser
->addRole($this->editor_role
->id());
$this->superUser
->addRole($this->admin_role
->id());
$this->superUser
->save();
$this->masquerade_user = $this
->drupalCreateUser();
$this->masquerade_user
->setUsername('masquerade_user');
$this->masquerade_user
->addRole($this->masquerade_role
->id());
$this->masquerade_user
->save();
$this->auth_user = $this
->drupalCreateUser();
$this
->drupalPlaceBlock('system_menu_block:account');
}
protected function masqueradeAs(AccountInterface $account) {
$this
->drupalGet('user/' . $account
->id());
$this
->clickLink($this
->t('Masquerade as @name', [
'@name' => $account
->getDisplayName(),
]));
$this
->assertSession()
->pageTextContains('You are now masquerading as ' . $account
->label());
if (isset($this->session_id)) {
}
}
protected function unmasquerade(AccountInterface $account) {
$this
->drupalGet('user/' . $account
->id());
$this
->clickLink('Unmasquerade');
$this
->assertSession()
->pageTextContains('You are no longer masquerading as ' . $account
->label());
if (isset($this->session_id)) {
}
}
protected function assertSessionByUid($uid, $masquerading = NULL) {
$result = \Drupal::database()
->query('SELECT * FROM {sessions} WHERE uid = :uid', [
':uid' => $uid,
])
->fetchAll();
if (empty($result)) {
$this
->fail("No session found for uid {$uid}");
}
elseif (count($result) > 1) {
$this
->fail("Found more than 1 session for uid {$uid}.");
}
else {
$session = reset($result);
if ($masquerading) {
$expected = '"masquerading";s:' . strlen($masquerading) . ':"' . $masquerading . '"';
self::assertNotFalse(strpos($session->session, $expected), new FormattableMarkup('Session flag "masquerading" equals @uid.', [
'@uid' => $masquerading,
]));
}
else {
$expected = empty($session->session) || strpos($session->session, 'masquerading') === FALSE;
self::assertTrue($expected, 'Session flag "masquerading" is not set.');
}
}
}
protected function assertNoSessionByUid($uid) {
$result = \Drupal::database()
->query('SELECT * FROM {sessions} WHERE uid = :uid', [
':uid' => $uid,
])
->fetchAll();
self::assertEmpty($result, "No session for uid {$uid} found.");
}
protected function drupalGetToken($value = '') {
$private_key = $this->container
->get('private_key')
->get();
$session_metadata = $this->container
->get('session_manager.metadata_bag');
$seed = $session_metadata
->getCsrfTokenSeed();
return Crypt::hmacBase64($value, $seed . $private_key . Settings::getHashSalt());
}
}