diff --git a/core/modules/user/src/Plugin/Condition/UserRole.php b/core/modules/user/src/Plugin/Condition/UserRole.php index 949d78d..ca2beb2 100644 --- a/core/modules/user/src/Plugin/Condition/UserRole.php +++ b/core/modules/user/src/Plugin/Condition/UserRole.php @@ -15,13 +15,9 @@ * @Condition( * id = "user_role", * label = @Translation("User Role"), - * module = "user", * context = { * "user" = { - * "type" = "entity", - * "constraints" = { - * "EntityType" = "user" - * } + * "type" = "entity:user" * } * } * ) @@ -36,8 +32,8 @@ public function buildConfigurationForm(array $form, array &$form_state) { $form['roles'] = array( '#type' => 'checkboxes', '#title' => $this->t('When the user has the following roles'), - '#default_value' => !empty($this->configuration['roles']) ? $this->configuration['roles'] : array(), - '#options' => array_map('check_plain', user_role_names()), + '#default_value' => $this->configuration['roles'], + '#options' => $this->getRoleNames(), '#description' => $this->t('If you select no roles, the condition will evaluate to TRUE for all users.'), '#required' => TRUE, ); @@ -47,9 +43,18 @@ public function buildConfigurationForm(array $form, array &$form_state) { /** * {@inheritdoc} */ + public function defaultConfiguration() { + return array( + 'roles' => array(), + ); + } + + /** + * {@inheritdoc} + */ public function validateConfigurationForm(array &$form, array &$form_state) { foreach ($form_state['values']['roles'] as $role) { - if (!in_array($role, array_keys(array_map('check_plain', user_role_names())))) { + if (!in_array($role, array_keys($this->getRoleNames()))) { form_set_error('roles', $this->t('An invalid user role has been chosen.')); } } @@ -91,4 +96,14 @@ public function evaluate() { return (bool) array_intersect(array_filter($this->configuration['roles']), $user->getRoles()); } + /** + * Returns the sanitized user role names. + * + * @return array + * An array keyed by role ID of user role names that are safe for printing. + */ + protected function getRoleNames() { + return array_map('\Drupal\Component\Utility\String::checkPlain', user_role_names()); + } + } diff --git a/core/modules/user/src/Tests/Condition/UserRoleConditionTest.php b/core/modules/user/src/Tests/Condition/UserRoleConditionTest.php index 48ea4e3..1260b61 100644 --- a/core/modules/user/src/Tests/Condition/UserRoleConditionTest.php +++ b/core/modules/user/src/Tests/Condition/UserRoleConditionTest.php @@ -7,13 +7,14 @@ namespace Drupal\user\Tests\Condition; -use Drupal\simpletest\DrupalUnitTestBase; +use Drupal\simpletest\KernelTestBase; +use Drupal\user\Entity\Role; use Drupal\user\Entity\User; /** * Tests the user role condition. */ -class UserRoleConditionTest extends DrupalUnitTestBase { +class UserRoleConditionTest extends KernelTestBase { /** * The condition plugin manager. @@ -50,6 +51,9 @@ class UserRoleConditionTest extends DrupalUnitTestBase { */ public static $modules = array('system', 'user', 'field'); + /** + * {@inheritdoc} + */ public static function getInfo() { return array( 'name' => 'User Role Condition Plugin', @@ -58,6 +62,9 @@ public static function getInfo() { ); } + /** + * {@inheritdoc} + */ protected function setUp() { parent::setUp(); @@ -69,7 +76,7 @@ protected function setUp() { // Create new role. $rid = strtolower($this->randomName(8)); $label = $this->randomString(8); - $role = entity_create('user_role', array( + $role = Role::create(array( 'id' => $rid, 'label' => $label, )); @@ -77,15 +84,15 @@ protected function setUp() { $this->role = $role; // Setup an anonymous user for our tests. - $this->anonymous = entity_create('user', array( + $this->anonymous = User::create(array( 'uid' => 0, )); $this->anonymous->save(); // Loading the anonymous user adds the correct role. - $this->anonymous = entity_load('user', $this->anonymous->id()); + $this->anonymous = User::load($this->anonymous->id()); // Setup an authenticated user for our tests. - $this->authenticated = entity_create('user', array( + $this->authenticated = User::create(array( 'name' => $this->randomName(), )); $this->authenticated->save(); @@ -99,6 +106,7 @@ protected function setUp() { public function testConditions() { // Grab the user role condition and configure it to check against // authenticated user roles. + /** @var $condition \Drupal\Core\Condition\ConditionInterface */ $condition = $this->manager->createInstance('user_role') ->setConfig('roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)) ->setContextValue('user', $this->anonymous); @@ -140,12 +148,11 @@ public function testConditions() { $condition->setConfig('roles', array(DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); $this->assertEqual($condition->summary(), 'The user is not a member of anonymous, authenticated'); - // Check a custom role + // Check a custom role. $condition->setConfig('roles', array($this->role->id() => $this->role->id())); $condition->setConfig('negate', FALSE); $this->assertTRUE($condition->execute(), 'Authenticated user is a member of the custom role.'); $this->assertEqual($condition->summary(), 'The user is a member of ' . $this->role->id()); - } }