function AccountProxyTest::testHasRole

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

@covers ::hasRole

File

core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php, line 59

Class

AccountProxyTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Session%21AccountProxy.php/class/AccountProxy/11.x" title="A proxied implementation of AccountInterface." class="local">\Drupal\Core\Session\AccountProxy</a> @group Session

Namespace

Drupal\Tests\Core\Session

Code

public function testHasRole() : void {
    $dispatcher = $this->prophesize(EventDispatcherInterface::class);
    $dispatcher->dispatch(Argument::any(), Argument::any())
        ->willReturn(new Event());
    $account_proxy = new AccountProxy($dispatcher->reveal());
    $this->assertTrue($account_proxy->hasRole(RoleInterface::ANONYMOUS_ID));
    $current_user = $this->prophesize(UserSession::class);
    $current_user->id()
        ->willReturn(2);
    $current_user->hasRole(RoleInterface::AUTHENTICATED_ID)
        ->willReturn(TRUE);
    $account_proxy->setAccount($current_user->reveal());
    $this->assertTrue($account_proxy->hasRole(RoleInterface::AUTHENTICATED_ID));
}

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