View source
<?php
namespace Drupal\Tests\devel\Functional;
use Drupal\Component\Render\FormattableMarkup;
class DevelSwitchUserTest extends DevelBrowserTestBase {
protected $block;
protected $develUser;
protected $switchUser;
protected $webUser;
protected function setUp() {
parent::setUp();
$this->block = $this
->drupalPlaceBlock('devel_switch_user', [
'id' => 'switch-user',
'label' => 'Switch Hit',
]);
$this->develUser = $this
->drupalCreateUser([
'access devel information',
'switch users',
], 'Devel User Four');
$this->switchUser = $this
->drupalCreateUser([
'switch users',
], 'Switch User Five');
$this->webUser = $this
->drupalCreateUser([], 'Web User Six');
}
public function testSwitchUserFunctionality() {
$this
->drupalLogin($this->webUser);
$this
->drupalGet('');
$this
->assertSession()
->pageTextNotContains($this->block
->label());
$this
->drupalGet('/devel/switch/' . $this->webUser
->getDisplayName());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogin($this->develUser);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($this->block
->label(), 'Block title was found.');
$this
->drupalGet('/devel/switch');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('/devel/switch/' . $this->switchUser
->getDisplayName());
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('/user/' . $this->switchUser
->id());
$this
->clickLink($this->switchUser
->getDisplayName());
$this
->assertSessionByUid($this->switchUser
->id());
$this
->assertNoSessionByUid($this->develUser
->id());
$this
->clickLink($this->develUser
->getDisplayName());
$this
->assertNoSessionByUid($this->switchUser
->id());
$this
->assertSessionByUid($this->develUser
->id());
$edit = [
'userid' => $this->switchUser
->getDisplayName(),
];
$this
->drupalPostForm(NULL, $edit, 'Switch');
$this
->assertSessionByUid($this->switchUser
->id());
$this
->assertNoSessionByUid($this->develUser
->id());
}
public function testSwitchUserBlockConfiguration() {
$anonymous = \Drupal::config('user.settings')
->get('anonymous');
for ($i = 0; $i < 12; $i++) {
$this
->drupalCreateUser();
}
$this
->drupalLogin($this->develUser);
$this
->drupalGet('');
$this
->assertSession()
->pageTextContains($this->block
->label(), 'Block title was found.');
$this
->assertSwitchUserSearchForm();
$this
->assertSwitchUserListCount(12);
$this
->assertSwitchUserListNoContainsUser($anonymous);
$this
->setBlockConfiguration('list_size', 4);
$this
->drupalGet('');
$this
->assertSwitchUserListCount(4);
$this
->setBlockConfiguration('include_anon', TRUE);
$this
->drupalGet('');
$this
->assertSwitchUserListContainsUser($anonymous);
$this
->setBlockConfiguration('show_form', FALSE);
$this
->drupalGet('');
$this
->assertSwitchUserNoSearchForm();
}
public function testSwitchUserListItems() {
$anonymous = \Drupal::config('user.settings')
->get('anonymous');
$this
->setBlockConfiguration('list_size', 2);
$this
->drupalLogin($this->webUser);
$this
->drupalLogin($this->develUser);
$this
->drupalGet('');
$this
->assertSwitchUserListCount(2);
$this
->assertSwitchUserListContainsUser($this->develUser
->getDisplayName());
$this
->assertSwitchUserListContainsUser($this->switchUser
->getDisplayName());
$this->switchUser
->set('status', 0)
->save();
$this
->drupalGet('');
$this
->assertSwitchUserListCount(2);
$this
->assertSwitchUserListContainsUser($this->develUser
->getDisplayName());
$this
->assertSwitchUserListContainsUser($this->webUser
->getDisplayName());
$this
->assertSwitchUserListNoContainsUser($this->switchUser
->getDisplayName());
$this
->setBlockConfiguration('include_anon', TRUE);
$this
->drupalGet('');
$this
->assertSwitchUserListCount(2);
$this
->assertSwitchUserListContainsUser($this->develUser
->getDisplayName());
$this
->assertSwitchUserListContainsUser($anonymous);
$this
->drupalLogout();
$this->develUser
->delete();
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('');
$this
->assertSwitchUserListCount(2);
$this
->assertSwitchUserListContainsUser($anonymous);
$roles = user_roles(TRUE, 'switch users');
\Drupal::entityTypeManager()
->getStorage('user_role')
->delete($roles);
$this
->drupalGet('');
$this
->assertSwitchUserListCount(2);
$this
->assertSwitchUserListContainsUser($anonymous);
}
public function assertSwitchUserListCount($number) {
$result = $this
->xpath('//div[@id=:block]//ul/li/a', [
':block' => 'block-switch-user',
]);
$this
->assertTrue(count($result) == $number, 'The number of users shown in switch user is correct.');
}
public function assertSwitchUserListContainsUser($username) {
$result = $this
->xpath('//div[@id=:block]//ul/li/a[normalize-space()=:user]', [
':block' => 'block-switch-user',
':user' => $username,
]);
$this
->assertTrue(count($result) > 0, new FormattableMarkup('User "%user" is included in the switch user list.', [
'%user' => $username,
]));
}
public function assertSwitchUserListNoContainsUser($username) {
$result = $this
->xpath('//div[@id=:block]//ul/li/a[normalize-space()=:user]', [
':block' => 'block-switch-user',
':user' => $username,
]);
$this
->assertTrue(count($result) == 0, new FormattableMarkup('User "%user" is not included in the switch user list.', [
'%user' => $username,
]));
}
public function assertSwitchUserSearchForm() {
$result = $this
->xpath('//div[@id=:block]//form[contains(@class, :form)]', [
':block' => 'block-switch-user',
':form' => 'devel-switchuser-form',
]);
$this
->assertTrue(count($result) > 0, 'The search form is shown.');
}
public function assertSwitchUserNoSearchForm() {
$result = $this
->xpath('//div[@id=:block]//form[contains(@class, :form)]', [
':block' => 'block-switch-user',
':form' => 'devel-switchuser-form',
]);
$this
->assertTrue(count($result) == 0, 'The search form is not shown.');
}
protected function setBlockConfiguration($key, $value) {
$block = $this->block
->getPlugin();
$block
->setConfigurationValue($key, $value);
$this->block
->save();
}
protected function assertSessionByUid($uid) {
$query = \Drupal::database()
->select('sessions');
$query
->fields('sessions', [
'uid',
]);
$query
->condition('uid', $uid);
$result = $query
->execute()
->fetchAll();
$this
->assertNotEmpty($result, sprintf('No session found for uid %s', $uid));
$this
->assertTrue(count($result) == 1, sprintf('Found more than one session for uid %s', $uid));
}
protected function assertNoSessionByUid($uid) {
$query = \Drupal::database()
->select('sessions');
$query
->fields('sessions', [
'uid',
]);
$query
->condition('uid', $uid);
$result = $query
->execute()
->fetchAll();
$this
->assertTrue(empty($result), "No session for uid {$uid} found.");
}
}