function UserSessionTest::testHasPermission

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Session/UserSessionTest.php \Drupal\Tests\Core\Session\UserSessionTest::testHasPermission()
  2. 8.9.x core/tests/Drupal/Tests/Core/Session/UserSessionTest.php \Drupal\Tests\Core\Session\UserSessionTest::testHasPermission()
  3. 10 core/tests/Drupal/Tests/Core/Session/UserSessionTest.php \Drupal\Tests\Core\Session\UserSessionTest::testHasPermission()

Tests the has permission method.

See also

\Drupal\Core\Session\UserSession::hasPermission()

File

core/tests/Drupal/Tests/Core/Session/UserSessionTest.php, line 39

Class

UserSessionTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Session%21UserSession.php/class/UserSession/11.x" title="An implementation of the user account interface for the global user." class="local">\Drupal\Core\Session\UserSession</a> @group Session

Namespace

Drupal\Tests\Core\Session

Code

public function testHasPermission() : void {
    $user = $this->createUserSession();
    $permission_checker = $this->prophesize('Drupal\\Core\\Session\\PermissionCheckerInterface');
    $permission_checker->hasPermission('example permission', $user)
        ->willReturn(TRUE);
    $permission_checker->hasPermission('another example permission', $user)
        ->willReturn(FALSE);
    $container = new ContainerBuilder();
    $container->set('permission_checker', $permission_checker->reveal());
    \Drupal::setContainer($container);
    $this->assertTrue($user->hasPermission('example permission'));
    $this->assertFalse($user->hasPermission('another example permission'));
}

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