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. 11.x 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 40

Class

UserSessionTest
@coversDefaultClass \Drupal\Core\Session\UserSession[[api-linebreak]] @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.