UserTest.php in Zircon Profile 8
File
core/modules/user/tests/src/Unit/Plugin/Core/Entity/UserTest.php
View source
<?php
namespace Drupal\Tests\user\Unit\Plugin\Core\Entity;
use Drupal\Tests\Core\Session\UserSessionTest;
use Drupal\user\RoleInterface;
class UserTest extends UserSessionTest {
protected function createUserSession(array $rids = array(), $authenticated = FALSE) {
$user = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods(array(
'get',
'id',
))
->getMock();
$user
->expects($this
->any())
->method('id')
->will($this
->returnValue($authenticated ? 2 : 0));
$roles = array();
foreach ($rids as $rid) {
$roles[] = (object) array(
'target_id' => $rid,
);
}
$user
->expects($this
->any())
->method('get')
->with('roles')
->will($this
->returnValue($roles));
return $user;
}
public function testUserGetRoles() {
$user = $this
->createUserSession(array());
$this
->assertEquals(array(
RoleInterface::ANONYMOUS_ID,
), $user
->getRoles());
$this
->assertEquals(array(), $user
->getRoles(TRUE));
$user = $this
->createUserSession(array(), TRUE);
$this
->assertEquals(array(
RoleInterface::AUTHENTICATED_ID,
), $user
->getRoles());
$this
->assertEquals(array(), $user
->getRoles(TRUE));
}
}