function AccessPolicyProcessorTest::testAccountSwitcher

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php \Drupal\Tests\Core\Session\AccessPolicyProcessorTest::testAccountSwitcher()

Tests if the account switcher switches properly when user cache context is present.

@dataProvider accountSwitcherProvider

Parameters

bool $has_user_context: Whether a user based cache context is present.

bool $is_current_user: Whether the passed in account is the current user.

bool $should_call_switcher: Whether the account switcher should be called.

File

core/tests/Drupal/Tests/Core/Session/AccessPolicyProcessorTest.php, line 191

Class

AccessPolicyProcessorTest
Tests the AccessPolicyProcessor service.

Namespace

Drupal\Tests\Core\Session

Code

public function testAccountSwitcher(bool $has_user_context, bool $is_current_user, bool $should_call_switcher) : void {
    $account = $this->prophesize(AccountInterface::class);
    $account->id()
        ->willReturn(2);
    $account = $account->reveal();
    $current_user = $this->prophesize(AccountProxyInterface::class);
    $current_user->id()
        ->willReturn($is_current_user ? 2 : 13);
    $account_switcher = $this->prophesize(AccountSwitcherInterface::class);
    if ($should_call_switcher) {
        $account_switcher->switchTo($account)
            ->shouldBeCalledTimes(1);
        $account_switcher->switchBack()
            ->shouldBeCalledTimes(1);
    }
    else {
        $account_switcher->switchTo($account)
            ->shouldNotBeCalled();
        $account_switcher->switchBack()
            ->shouldNotBeCalled();
    }
    $processor = $this->setUpAccessPolicyProcessor(NULL, NULL, NULL, $current_user->reveal(), $account_switcher->reveal());
    $processor->addAccessPolicy(new BarAccessPolicy());
    if ($has_user_context) {
        $processor->addAccessPolicy(new UserContextAccessPolicy());
    }
    $processor->processAccessPolicies($account, 'bar');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.