function UserSessionTest::testHasRole

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

Tests the hasRole method.

@covers ::hasRole

File

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

Class

UserSessionTest
@coversDefaultClass \Drupal\Core\Session\UserSession[[api-linebreak]] @group Session

Namespace

Drupal\Tests\Core\Session

Code

public function testHasRole() : void {
  $user1 = $this->createUserSession([
    'role_one',
  ]);
  $user2 = $this->createUserSession([
    'role_one',
    'role_two',
  ]);
  $user3 = $this->createUserSession([
    'role_two',
  ], TRUE);
  $user4 = $this->createUserSession();
  $this->assertTrue($user1->hasRole('role_one'));
  $this->assertFalse($user2->hasRole('no role'));
  $this->assertTrue($user3->hasRole(RoleInterface::AUTHENTICATED_ID));
  $this->assertFalse($user3->hasRole(RoleInterface::ANONYMOUS_ID));
  $this->assertTrue($user4->hasRole(RoleInterface::ANONYMOUS_ID));
}

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