class AdminAccountSwitcherTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/DefaultContent/AdminAccountSwitcherTest.php \Drupal\KernelTests\Core\DefaultContent\AdminAccountSwitcherTest
@covers \Drupal\Core\DefaultContent\AdminAccountSwitcher
@group DefaultContent
Hierarchy
- class \Drupal\KernelTests\Core\DefaultContent\AdminAccountSwitcherTest
Expanded class hierarchy of AdminAccountSwitcherTest
File
-
core/
tests/ Drupal/ KernelTests/ Core/ DefaultContent/ AdminAccountSwitcherTest.php, line 19
Namespace
Drupal\KernelTests\Core\DefaultContentView source
class AdminAccountSwitcherTest extends KernelTestBase {
use UserCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installEntitySchema('user');
}
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) : void {
parent::register($container);
$container->getDefinition(AdminAccountSwitcher::class)
->setPublic(TRUE);
}
/**
* Tests switching to a user with an administrative role.
*/
public function testSwitchToAdministrator() : void {
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this->createUser(admin: TRUE);
$this->assertSame($account->id(), $this->container
->get(AdminAccountSwitcher::class)
->switchToAdministrator()
->id());
$this->assertSame($account->id(), $this->container
->get('current_user')
->id());
}
/**
* Tests that there is an error if there are no administrative users.
*/
public function testNoAdministratorsExist() : void {
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this->createUser();
$this->assertSame(1, (int) $account->id());
$this->expectException(AccessException::class);
$this->expectExceptionMessage("There are no user accounts with administrative roles.");
$this->container
->get(AdminAccountSwitcher::class)
->switchToAdministrator();
}
/**
* Tests switching to user 1 when the superuser access policy is enabled.
*/
public function testSuperUser() : void {
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this->createUser();
$this->assertSame(1, (int) $account->id());
$switcher = new AdminAccountSwitcher($this->container
->get(AccountSwitcherInterface::class), $this->container
->get(EntityTypeManagerInterface::class), TRUE);
$this->assertSame(1, (int) $switcher->switchToAdministrator()
->id());
}
public function testSwitchToAndSwitchBack() : void {
$this->assertTrue($this->container
->get('current_user')
->isAnonymous());
/** @var \Drupal\Core\Session\AccountInterface $account */
$account = $this->createUser();
$switcher = $this->container
->get(AdminAccountSwitcher::class);
$this->assertSame($switcher, $switcher->switchTo($account));
$this->assertSame($account->id(), $this->container
->get('current_user')
->id());
$this->assertSame($switcher, $switcher->switchBack());
$this->assertTrue($this->container
->get('current_user')
->isAnonymous());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
AdminAccountSwitcherTest::$modules | protected static | property | Modules to install. |
AdminAccountSwitcherTest::register | public | function | Registers test-specific services. |
AdminAccountSwitcherTest::setUp | protected | function | |
AdminAccountSwitcherTest::testNoAdministratorsExist | public | function | Tests that there is an error if there are no administrative users. |
AdminAccountSwitcherTest::testSuperUser | public | function | Tests switching to user 1 when the superuser access policy is enabled. |
AdminAccountSwitcherTest::testSwitchToAdministrator | public | function | Tests switching to a user with an administrative role. |
AdminAccountSwitcherTest::testSwitchToAndSwitchBack | public | function | |
ExtensionListTestTrait::getModulePath | protected | function | Gets the path for the specified module. |
ExtensionListTestTrait::getThemePath | protected | function | Gets the path for the specified theme. |
StorageCopyTrait::replaceStorageContents | protected static | function | Copy the configuration from one storage to another and remove stale items. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.