UserSessionTest.php 5.41 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\Core\Session;
4 5 6 7

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Session\UserSession;
use Drupal\Tests\UnitTestCase;
8
use Drupal\user\RoleInterface;
9

10
/**
11
 * @coversDefaultClass \Drupal\Core\Session\UserSession
12
 * @group Session
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 */
class UserSessionTest extends UnitTestCase {

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

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

    return $data;
  }

  /**
   * Setups a user session for the test.
   *
   * @param array $rids
   *   The rids of the user.
42 43
   * @param bool $authenticated
   *   TRUE if it is an authenticated user.
44 45 46 47
   *
   * @return \Drupal\Core\Session\AccountInterface
   *   The created user session.
   */
48
  protected function createUserSession(array $rids = array(), $authenticated = FALSE) {
49
    array_unshift($rids, $authenticated ? RoleInterface::AUTHENTICATED_ID : RoleInterface::ANONYMOUS_ID);
50 51 52 53 54 55 56 57 58 59
    return new UserSession(array('roles' => $rids));
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $roles = array();
60
    $roles['role_one'] = $this->getMockBuilder('Drupal\user\Entity\Role')
61 62 63 64 65 66 67 68 69 70 71
      ->disableOriginalConstructor()
      ->setMethods(array('hasPermission'))
      ->getMock();
    $roles['role_one']->expects($this->any())
      ->method('hasPermission')
      ->will($this->returnValueMap(array(
        array('example permission', TRUE),
        array('another example permission', FALSE),
        array('last example permission', FALSE),
      )));

72
    $roles['role_two'] = $this->getMockBuilder('Drupal\user\Entity\Role')
73 74 75 76 77 78 79 80 81 82 83
      ->disableOriginalConstructor()
      ->setMethods(array('hasPermission'))
      ->getMock();
    $roles['role_two']->expects($this->any())
      ->method('hasPermission')
      ->will($this->returnValueMap(array(
        array('example permission', TRUE),
        array('another example permission', TRUE),
        array('last example permission', FALSE),
      )));

84 85 86 87 88 89 90 91 92 93 94 95
    $roles['anonymous'] = $this->getMockBuilder('Drupal\user\Entity\Role')
      ->disableOriginalConstructor()
      ->setMethods(array('hasPermission'))
      ->getMock();
    $roles['anonymous']->expects($this->any())
      ->method('hasPermission')
      ->will($this->returnValueMap(array(
        array('example permission', FALSE),
        array('another example permission', FALSE),
        array('last example permission', FALSE),
      )));

96
    $role_storage = $this->getMockBuilder('Drupal\user\RoleStorage')
97 98 99 100 101 102 103 104
      ->disableOriginalConstructor()
      ->setMethods(array('loadMultiple'))
      ->getMock();
    $role_storage->expects($this->any())
      ->method('loadMultiple')
      ->will($this->returnValueMap(array(
        array(array(), array()),
        array(NULL, $roles),
105 106 107 108
        array(array('anonymous'), array($roles['anonymous'])),
        array(array('anonymous', 'role_one'), array($roles['role_one'])),
        array(array('anonymous', 'role_two'), array($roles['role_two'])),
        array(array('anonymous', 'role_one', 'role_two'), array($roles['role_one'], $roles['role_two'])),
109 110
      )));

111
    $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
112
    $entity_manager->expects($this->any())
113
      ->method('getStorage')
114 115 116
      ->with($this->equalTo('user_role'))
      ->will($this->returnValue($role_storage));
    $container = new ContainerBuilder();
117
    $container->set('entity.manager', $entity_manager);
118 119 120 121
    \Drupal::setContainer($container);

    $this->users['user_one'] = $this->createUserSession(array('role_one'));
    $this->users['user_two'] = $this->createUserSession(array('role_one', 'role_two'));
122
    $this->users['user_three'] = $this->createUserSession(array('role_two'), TRUE);
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    $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
   *
138
   * @see \Drupal\Core\Session\UserSession::hasPermission()
139 140 141 142 143 144 145 146 147 148
   */
  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));
    }
  }

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

160
}