UserTest.php 1.56 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\user\Unit\Plugin\Core\Entity;
4 5

use Drupal\Tests\Core\Session\UserSessionTest;
6
use Drupal\user\RoleInterface;
7

8
/**
9
 * @coversDefaultClass \Drupal\user\Entity\User
10
 * @group user
11 12 13 14 15 16
 */
class UserTest extends UserSessionTest {

  /**
   * {@inheritdoc}
   */
17
  protected function createUserSession(array $rids = array(), $authenticated = FALSE) {
18
    $user = $this->getMockBuilder('Drupal\user\Entity\User')
19
      ->disableOriginalConstructor()
20
      ->setMethods(array('get', 'id'))
21 22 23 24
      ->getMock();
    $user->expects($this->any())
      ->method('id')
      // @todo Also test the uid = 1 handling.
25
      ->will($this->returnValue($authenticated ? 2 : 0));
26 27 28
    $roles = array();
    foreach ($rids as $rid) {
      $roles[] = (object) array(
29
        'target_id' => $rid,
30 31
      );
    }
32
    $user->expects($this->any())
33 34 35
      ->method('get')
      ->with('roles')
      ->will($this->returnValue($roles));
36 37 38
    return $user;
  }

39 40 41 42 43 44 45 46
  /**
   * Tests the method getRoles exclude or include locked roles based in param.
   *
   * @see \Drupal\user\Entity\User::getRoles()
   * @covers ::getRoles
   */
  public function testUserGetRoles() {
    // Anonymous user.
47
    $user = $this->createUserSession(array());
48
    $this->assertEquals(array(RoleInterface::ANONYMOUS_ID), $user->getRoles());
49 50 51
    $this->assertEquals(array(), $user->getRoles(TRUE));

    // Authenticated user.
52
    $user = $this->createUserSession(array(), TRUE);
53
    $this->assertEquals(array(RoleInterface::AUTHENTICATED_ID), $user->getRoles());
54 55 56 57
    $this->assertEquals(array(), $user->getRoles(TRUE));
  }

}