function UserSessionTest::testHasRole

Same name and namespace in other branches
  1. 10 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 71

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 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.