diff --git a/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php b/core/modules/user/lib/Drupal/user/Plugin/Condition/UserRole.php similarity index 59% rename from core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php rename to core/modules/user/lib/Drupal/user/Plugin/Condition/UserRole.php index 4939ad1..949d78d 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Condition/UserRole.php @@ -2,19 +2,17 @@ /** * @file - * Contains \Drupal\user\Plugin\Core\Condition\UserRole. + * Contains \Drupal\user\Plugin\Condition\UserRole. */ -namespace Drupal\user\Plugin\Core\Condition; +namespace Drupal\user\Plugin\Condition; use Drupal\Core\Condition\ConditionPluginBase; -use Drupal\Component\Annotation\Plugin; -use Drupal\Core\Annotation\Translation; /** * Provides a 'User Role' condition. * - * @Plugin( + * @Condition( * id = "user_role", * label = @Translation("User Role"), * module = "user", @@ -33,14 +31,14 @@ class UserRole extends ConditionPluginBase { /** * {@inheritdoc} */ - public function buildForm(array $form, array &$form_state) { - $form = parent::buildForm($form, $form_state); + public function buildConfigurationForm(array $form, array &$form_state) { + $form = parent::buildConfigurationForm($form, $form_state); $form['roles'] = array( '#type' => 'checkboxes', - '#title' => t('When the user has the following roles'), + '#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()), - '#description' => t('If you select no roles, the condition will evaluate to TRUE for all users.'), + '#description' => $this->t('If you select no roles, the condition will evaluate to TRUE for all users.'), '#required' => TRUE, ); return $form; @@ -49,21 +47,21 @@ public function buildForm(array $form, array &$form_state) { /** * {@inheritdoc} */ - public function validateForm(array &$form, array &$form_state) { + 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())))) { - form_set_error('roles', t('An invalid user role has been chosen.')); + form_set_error('roles', $this->t('An invalid user role has been chosen.')); } } - parent::validateForm($form, $form_state); + parent::validateConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ - public function submitForm(array &$form, array &$form_state) { + public function submitConfigurationForm(array &$form, array &$form_state) { $this->configuration['roles'] = array_filter($form_state['values']['roles']); - parent::submitForm($form, $form_state); + parent::submitConfigurationForm($form, $form_state); } /** @@ -75,13 +73,13 @@ public function summary() { $roles = implode(', ', $roles); } else { - $roles = array_pop($this->configuration['roles']); + $roles = reset($this->configuration['roles']); } if (!empty($this->configuration['negate'])) { - return t('The user is not a member of @roles', array('@roles' => $roles)); + return $this->t('The user is not a member of @roles', array('@roles' => $roles)); } else { - return t('The user is a member of @roles', array('@roles' => $roles)); + return $this->t('The user is a member of @roles', array('@roles' => $roles)); } } @@ -90,7 +88,7 @@ public function summary() { */ public function evaluate() { $user = $this->getContextValue('user'); - return (bool) array_intersect(array_filter($this->configuration['roles']), array_keys($user->roles)); + return (bool) array_intersect(array_filter($this->configuration['roles']), $user->getRoles()); } } diff --git a/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php index a325f6e..48ea4e3 100644 --- a/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php @@ -8,8 +8,7 @@ namespace Drupal\user\Tests\Condition; use Drupal\simpletest\DrupalUnitTestBase; -use Drupal\Core\Condition\ConditionManager; -use Drupal\user\Plugin\Core\Entity\User; +use Drupal\user\Entity\User; /** * Tests the user role condition. @@ -26,21 +25,21 @@ class UserRoleConditionTest extends DrupalUnitTestBase { /** * An anonymous user for testing purposes. * - * @var \Drupal\user\Plugin\Core\Entity\User + * @var \Drupal\user\Entity\User */ protected $anonymous; /** * An authenticated user for testing purposes. * - * @var \Drupal\user\Plugin\Core\Entity\User + * @var \Drupal\user\Entity\User */ protected $authenticated; /** * A custom role for testing purposes. * - * @var \Drupal\user\Plugin\Core\Entity\Role + * @var \Drupal\user\Entity\Role */ protected $role; @@ -63,10 +62,7 @@ protected function setUp() { parent::setUp(); $this->installSchema('system', 'sequences'); - $this->installSchema('user', 'users'); - $this->installSchema('user', 'users_roles'); - $this->installSchema('field', 'field_config'); - $this->installSchema('field', 'field_config_instance'); + $this->installSchema('user', array('users', 'users_roles')); $this->manager = $this->container->get('plugin.manager.condition'); @@ -81,19 +77,20 @@ protected function setUp() { $this->role = $role; // Setup an anonymous user for our tests. - $anonymous = drupal_anonymous_user(); - $anonymous->save(); - // Users with a non-0 uid will not get the anonymous role, so we have to - // change the uid of the $this->anonymous user after saving. - db_update('users')->fields(array('uid' => 0))->condition('uid', $anonymous->id())->execute(); - $this->anonymous = entity_load('user', 0); + $this->anonymous = entity_create('user', array( + 'uid' => 0, + )); + $this->anonymous->save(); + // Loading the anonymous user adds the correct role. + $this->anonymous = entity_load('user', $this->anonymous->id()); + // Setup an authenticated user for our tests. - $this->authenticated = new User(array( + $this->authenticated = entity_create('user', array( 'name' => $this->randomName(), - 'bundle' => 'user', - 'roles' => array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID, $this->role->id() => $this->role->id()) - ), 'user'); + )); $this->authenticated->save(); + // Add the custom role. + $this->authenticated->addRole($this->role->id()); } /**