Commit 35bc4a2c authored by alexpott's avatar alexpott

Issue #2277547 by heddn: Fixed User::getRoles test coverage & regression fix.

parent 22b7b7e9
......@@ -126,7 +126,7 @@ public function getRoles($exclude_locked_roles = FALSE) {
$roles = $this->roles;
if ($exclude_locked_roles) {
$roles = array_diff($roles, array('anonymous', 'authenticated'));
$roles = array_diff($roles, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID));
}
return $roles;
......
......@@ -173,7 +173,7 @@ public function getRoles($exclude_locked_roles = FALSE) {
$roles = array();
foreach ($this->get('roles') as $role) {
if (!($exclude_locked_roles && in_array($role->value, array('anonymoud', 'authenticated')))) {
if (!($exclude_locked_roles && in_array($role->value, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)))) {
$roles[] = $role->value;
}
}
......
......@@ -5,7 +5,7 @@
* Contains \Drupal\user\Tests\Plugin\Core\Entity\UserTest.
*/
namespace Drupal\user\Tests\Plugin\Core\Entity;
namespace Drupal\user\Tests\Plugin\Core\Entity {
use Drupal\Tests\Core\Session\UserSessionTest;
use Drupal\user\Entity\User;
......@@ -13,7 +13,7 @@
/**
* Tests the user object.
*
* @see \Drupal\user\Entity\User
* @coversDefaultClass \Drupal\user\Entity\User
*/
class UserTest extends UserSessionTest {
......@@ -50,4 +50,42 @@ protected function createUserSession(array $rids = array()) {
return $user;
}
/**
* Tests the method getRoles exclude or include locked roles based in param.
*
* @see \Drupal\user\Entity\User::getRoles()
* @covers ::getRoles
* @todo Move roles constants to a class/interface
*/
public function testUserGetRoles() {
// Anonymous user.
$user = $this->createUserSession(array(DRUPAL_ANONYMOUS_RID));
$this->assertEquals(array(DRUPAL_ANONYMOUS_RID), $user->getRoles());
$this->assertEquals(array(), $user->getRoles(TRUE));
// Authenticated user.
$user = $this->createUserSession(array(DRUPAL_AUTHENTICATED_RID));
$this->assertEquals(array(DRUPAL_AUTHENTICATED_RID), $user->getRoles());
$this->assertEquals(array(), $user->getRoles(TRUE));
}
}
}
namespace {
if (!defined('DRUPAL_ANONYMOUS_RID')) {
/**
* Stub Role ID for anonymous users since bootstrap.inc isn't available.
*/
define('DRUPAL_ANONYMOUS_RID', 'anonymous');
}
if (!defined('DRUPAL_AUTHENTICATED_RID')) {
/**
* Stub Role ID for authenticated users since bootstrap.inc isn't available.
*/
define('DRUPAL_AUTHENTICATED_RID', 'authenticated');
}
}
......@@ -5,7 +5,7 @@
* Contains \Drupal\Tests\Core\Session\AnonymousUserSessionTest.
*/
namespace Drupal\Tests\Core\Session;
namespace Drupal\Tests\Core\Session {
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Session\AnonymousUserSession;
......@@ -74,4 +74,35 @@ public function testAnonymousUserSessionWithNoRequest() {
$this->assertSame('', $anonymous_user->getHostname());
}
/**
* 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() {
$anonymous_user = new AnonymousUserSession();
$this->assertEquals(array(DRUPAL_ANONYMOUS_RID), $anonymous_user->getRoles());
$this->assertEquals(array(), $anonymous_user->getRoles(TRUE));
}
}
}
namespace {
if (!defined('DRUPAL_ANONYMOUS_RID')) {
/**
* Stub Role ID for anonymous users since bootstrap.inc isn't available.
*/
define('DRUPAL_ANONYMOUS_RID', 'anonymous');
}
if (!defined('DRUPAL_AUTHENTICATED_RID')) {
/**
* Stub Role ID for authenticated users since bootstrap.inc isn't available.
*/
define('DRUPAL_AUTHENTICATED_RID', 'authenticated');
}
}
......@@ -5,7 +5,7 @@
* Contains \Drupal\Tests\Core\Session\UserSessionTest.
*/
namespace Drupal\Tests\Core\Session;
namespace Drupal\Tests\Core\Session {
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Session\UserSession;
......@@ -14,7 +14,7 @@
/**
* Tests the user session object.
*
* @see \Drupal\Core\Session\UserSession
* @coversDefaultClass \Drupal\Core\Session\UserSession
*/
class UserSessionTest extends UnitTestCase {
......@@ -146,12 +146,32 @@ public function testHasPermission($permission, array $sessions_with_access, arra
/**
* Tests the method getRoles exclude or include locked roles based in param.
*
* @see \Drupal\Core\Session\UserSession::getRoles()
* @covers ::getRoles
* @todo Move roles constants to a class/interface
*/
public function testUserGetRoles() {
$this->assertEquals(array('role_two', 'authenticated'), $this->users['user_three']->getRoles());
$this->assertEquals(array('role_two', DRUPAL_AUTHENTICATED_RID), $this->users['user_three']->getRoles());
$this->assertEquals(array('role_two'), $this->users['user_three']->getRoles(TRUE));
}
}
}
namespace {
if (!defined('DRUPAL_ANONYMOUS_RID')) {
/**
* Stub Role ID for anonymous users since bootstrap.inc isn't available.
*/
define('DRUPAL_ANONYMOUS_RID', 'anonymous');
}
if (!defined('DRUPAL_AUTHENTICATED_RID')) {
/**
* Stub Role ID for authenticated users since bootstrap.inc isn't available.
*/
define('DRUPAL_AUTHENTICATED_RID', 'authenticated');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment