class UserSessionTest

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

Tests Drupal\Core\Session\UserSession.

Attributes

#[CoversClass(UserSession::class)] #[Group('Session')]

Hierarchy

Expanded class hierarchy of UserSessionTest

File

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

Namespace

Drupal\Tests\Core\Session
View source
class UserSessionTest extends UnitTestCase {
  
  /**
   * Setups a user session for the test.
   *
   * @param array $rids
   *   The rids of the user.
   * @param bool $authenticated
   *   TRUE if it is an authenticated user.
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The created user session.
   */
  protected function createUserSession(array $rids = [], $authenticated = FALSE) : UserSession {
    array_unshift($rids, $authenticated ? RoleInterface::AUTHENTICATED_ID : RoleInterface::ANONYMOUS_ID);
    return new UserSession([
      'roles' => $rids,
    ]);
  }
  
  /**
   * Tests the has permission method.
   *
   * @see \Drupal\Core\Session\UserSession::hasPermission()
   */
  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'));
  }
  
  /**
   * Tests the method getRoles exclude or include locked roles based in param.
   *
   * @todo Move roles constants to a class/interface
   * @legacy-covers ::getRoles
   */
  public function testUserGetRoles() : void {
    $user = $this->createUserSession([
      'role_two',
    ], TRUE);
    $this->assertEquals([
      RoleInterface::AUTHENTICATED_ID,
      'role_two',
    ], $user->getRoles());
    $this->assertEquals([
      'role_two',
    ], $user->getRoles(TRUE));
  }
  
  /**
   * Tests the hasRole method.
   */
  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));
  }
  
  /**
   * Tests that ::getLastAccessedTime() returns an integer.
   *
   * @legacy-covers ::getLastAccessedTime
   */
  public function testGetLastAccessedTime() : void {
    $user = new UserSession([
      'access' => '1234567890',
    ]);
    $this->assertSame(1234567890, $user->getLastAccessedTime());
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
DrupalTestCaseTrait::$root protected property The Drupal root directory.
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution. 1
DrupalTestCaseTrait::getDrupalRoot protected static function Returns the Drupal root directory. 1
DrupalTestCaseTrait::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 357
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.
UserSessionTest::createUserSession protected function Setups a user session for the test.
UserSessionTest::testGetLastAccessedTime public function Tests that ::getLastAccessedTime() returns an integer.
UserSessionTest::testHasPermission public function Tests the has permission method.
UserSessionTest::testHasRole public function Tests the hasRole method.
UserSessionTest::testUserGetRoles public function Tests the method getRoles exclude or include locked roles based in param.

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