You are here

class UserSessionTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Session/UserSessionTest.php \Drupal\Tests\Core\Session\UserSessionTest
  2. 9 core/tests/Drupal/Tests/Core/Session/UserSessionTest.php \Drupal\Tests\Core\Session\UserSessionTest

@coversDefaultClass \Drupal\Core\Session\UserSession @group Session

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitWarnings

Expanded class hierarchy of UserSessionTest

1 file declares its use of UserSessionTest
UserTest.php in core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php

File

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

Namespace

Drupal\Tests\Core\Session
View source
class UserSessionTest extends UnitTestCase {

  /**
   * The user sessions used in the test.
   *
   * @var \Drupal\Core\Session\AccountInterface[]
   */
  protected $users = [];

  /**
   * Provides test data for getHasPermission().
   *
   * @return array
   */
  public function providerTestHasPermission() {
    $data = [];
    $data[] = [
      'example permission',
      [
        'user_one',
        'user_two',
      ],
      [
        'user_last',
      ],
    ];
    $data[] = [
      'another example permission',
      [
        'user_two',
      ],
      [
        'user_one',
        'user_last',
      ],
    ];
    $data[] = [
      'final example permission',
      [],
      [
        'user_one',
        'user_two',
        'user_last',
      ],
    ];
    return $data;
  }

  /**
   * 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) {
    array_unshift($rids, $authenticated ? RoleInterface::AUTHENTICATED_ID : RoleInterface::ANONYMOUS_ID);
    return new UserSession([
      'roles' => $rids,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $roles = [];
    $roles['role_one'] = $this
      ->getMockBuilder('Drupal\\user\\Entity\\Role')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'hasPermission',
    ])
      ->getMock();
    $roles['role_one']
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap([
      [
        'example permission',
        TRUE,
      ],
      [
        'another example permission',
        FALSE,
      ],
      [
        'last example permission',
        FALSE,
      ],
    ]);
    $roles['role_two'] = $this
      ->getMockBuilder('Drupal\\user\\Entity\\Role')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'hasPermission',
    ])
      ->getMock();
    $roles['role_two']
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap([
      [
        'example permission',
        TRUE,
      ],
      [
        'another example permission',
        TRUE,
      ],
      [
        'last example permission',
        FALSE,
      ],
    ]);
    $roles['anonymous'] = $this
      ->getMockBuilder('Drupal\\user\\Entity\\Role')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'hasPermission',
    ])
      ->getMock();
    $roles['anonymous']
      ->expects($this
      ->any())
      ->method('hasPermission')
      ->willReturnMap([
      [
        'example permission',
        FALSE,
      ],
      [
        'another example permission',
        FALSE,
      ],
      [
        'last example permission',
        FALSE,
      ],
    ]);
    $role_storage = $this
      ->getMockBuilder('Drupal\\user\\RoleStorage')
      ->setConstructorArgs([
      'role',
      new MemoryCache(),
    ])
      ->disableOriginalConstructor()
      ->onlyMethods([
      'loadMultiple',
    ])
      ->getMock();
    $role_storage
      ->expects($this
      ->any())
      ->method('loadMultiple')
      ->willReturnMap([
      [
        [],
        [],
      ],
      [
        NULL,
        $roles,
      ],
      [
        [
          'anonymous',
        ],
        [
          $roles['anonymous'],
        ],
      ],
      [
        [
          'anonymous',
          'role_one',
        ],
        [
          $roles['role_one'],
        ],
      ],
      [
        [
          'anonymous',
          'role_two',
        ],
        [
          $roles['role_two'],
        ],
      ],
      [
        [
          'anonymous',
          'role_one',
          'role_two',
        ],
        [
          $roles['role_one'],
          $roles['role_two'],
        ],
      ],
    ]);
    $entity_type_manager = $this
      ->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
    $entity_type_manager
      ->expects($this
      ->any())
      ->method('getStorage')
      ->with($this
      ->equalTo('user_role'))
      ->will($this
      ->returnValue($role_storage));
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $entity_type_manager);
    \Drupal::setContainer($container);
    $this->users['user_one'] = $this
      ->createUserSession([
      'role_one',
    ]);
    $this->users['user_two'] = $this
      ->createUserSession([
      'role_one',
      'role_two',
    ]);
    $this->users['user_three'] = $this
      ->createUserSession([
      'role_two',
    ], TRUE);
    $this->users['user_last'] = $this
      ->createUserSession();
  }

  /**
   * Tests the has permission method.
   *
   * @param string $permission
   *   The permission to check.
   * @param \Drupal\Core\Session\AccountInterface[] $sessions_with_access
   *   The users with access.
   * @param \Drupal\Core\Session\AccountInterface[] $sessions_without_access
   *   The users without access.
   *
   * @dataProvider providerTestHasPermission
   *
   * @see \Drupal\Core\Session\UserSession::hasPermission()
   */
  public function testHasPermission($permission, array $sessions_with_access, array $sessions_without_access) {
    foreach ($sessions_with_access as $name) {
      $this
        ->assertTrue($this->users[$name]
        ->hasPermission($permission));
    }
    foreach ($sessions_without_access as $name) {
      $this
        ->assertFalse($this->users[$name]
        ->hasPermission($permission));
    }
  }

  /**
   * Tests the method getRoles exclude or include locked roles based in param.
   *
   * @covers ::getRoles
   * @todo Move roles constants to a class/interface
   */
  public function testUserGetRoles() {
    $this
      ->assertEquals([
      RoleInterface::AUTHENTICATED_ID,
      'role_two',
    ], $this->users['user_three']
      ->getRoles());
    $this
      ->assertEquals([
      'role_two',
    ], $this->users['user_three']
      ->getRoles(TRUE));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function
UserSessionTest::$users protected property The user sessions used in the test.
UserSessionTest::createUserSession protected function Setups a user session for the test. 1
UserSessionTest::providerTestHasPermission public function Provides test data for getHasPermission().
UserSessionTest::setUp protected function Overrides UnitTestCase::setUp
UserSessionTest::testHasPermission public function Tests the has permission method.
UserSessionTest::testUserGetRoles public function Tests the method getRoles exclude or include locked roles based in param. 1