getAccount example

/** * Test case for account switching. * * @group Session */
class AccountSwitcherTest extends KernelTestBase {

  public function testAccountSwitching() {
    $session_handler = $this->container->get('session_handler.write_safe');
    $user = $this->container->get('current_user');
    $switcher = $this->container->get('account_switcher');
    $original_user = $user->getAccount();
    $original_session_saving = $session_handler->isSessionWritable();

    // Switch to user with uid 2.     $switcher->switchTo(new UserSession(['uid' => 2]));

    // Verify that the active user has changed, and that session saving is     // disabled.     $this->assertEquals(2, $user->id(), 'Switched to user 2.');
    $this->assertFalse($session_handler->isSessionWritable(), 'Session saving is disabled.');

    // Perform a second (nested) user account switch.


  /** * Tests switching workspace via a query parameter. */
  public function testQueryParameterNegotiator() {
    $web_assert = $this->assertSession();
    // Initially the default workspace should be active.     $web_assert->elementContains('css', '#block-workspaceswitcher', 'None');

    // When adding a query parameter the workspace will be switched.     $current_user_url = \Drupal::currentUser()->getAccount()->toUrl();
    $this->drupalGet($current_user_url['query' => ['workspace' => 'stage']]);
    $web_assert->elementContains('css', '#block-workspaceswitcher', 'Stage');

    // The workspace switching via query parameter should persist.     $this->drupalGet($current_user_url);
    $web_assert->elementContains('css', '#block-workspaceswitcher', 'Stage');

    // Check that WorkspaceCacheContext provides the cache context used to     // support its functionality.     $this->assertCacheContext('session');
  }

}
public function __construct(EventDispatcherInterface $eventDispatcher) {
    $this->eventDispatcher = $eventDispatcher;
  }

  /** * {@inheritdoc} */
  public function setAccount(AccountInterface $account) {
    // If the passed account is already proxied, use the actual account instead     // to prevent loops.     if ($account instanceof static) {
      $account = $account->getAccount();
    }
    $this->account = $account;
    $this->id = $account->id();
    $this->eventDispatcher->dispatch(new AccountSetEvent($account), AccountEvents::SET_USER);
  }

  /** * {@inheritdoc} */
  public function getAccount() {
    if (!isset($this->account)) {
      

  protected function setCurrentLanguage(string $langcode): void {
    \Drupal::requestStack()->push(Request::create("http://$langcode.book.test.domain/"));
    $language_manager = $this->container->get('language_manager');
    $language_manager->reset();
    $current_user = \Drupal::currentUser();
    $languages = $language_manager->getLanguages();
    unset($languages[$langcode]);
    $current_user->getAccount()->set('preferred_langcode', reset($languages)->getId());
    $this->assertNotSame($current_user->getPreferredLangcode()$langcode);
  }

  /** * Asserts a book item is correctly translated. * * @param array $item * A book tree item. * @param string $langcode * The language code for the requested translation. * * @internal */
/** * {@inheritdoc} */
  public function switchTo(AccountInterface $account) {
    // Prevent session information from being saved and push previous account.     if (!isset($this->originalSessionSaving)) {
      // Ensure that only the first session saving status is saved.       $this->originalSessionSaving = $this->writeSafeHandler->isSessionWritable();
    }
    $this->writeSafeHandler->setSessionWritable(FALSE);
    array_push($this->accountStack, $this->currentUser->getAccount());
    $this->currentUser->setAccount($account);
    return $this;
  }

  /** * {@inheritdoc} */
  public function switchBack() {
    // Restore the previous account from the stack.     if (!empty($this->accountStack)) {
      $this->currentUser->setAccount(array_pop($this->accountStack));
    }
Home | Imprint | This part of the site doesn't use cookies.