You are here

protected function UserSessionTest::setUp in Drupal 9

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

Overrides UnitTestCase::setUp

File

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

Class

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

Namespace

Drupal\Tests\Core\Session

Code

protected function setUp() : void {
  parent::setUp();
  $roles = [];
  $roles['role_one'] = $this
    ->getMockBuilder('Drupal\\user\\Entity\\Role')
    ->disableOriginalConstructor()
    ->setMethods([
    '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()
    ->setMethods([
    '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()
    ->setMethods([
    '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()
    ->setMethods([
    '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();
}