diff --git a/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php b/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php index 24c6c51..180eab2 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php +++ b/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php @@ -31,7 +31,7 @@ class UserRole extends ConditionPluginBase { /** - * Implements \Drupal\Core\Form\FormInterface::buildForm(). + * Overrides \Drupal\Core\Condition\ConditionPluginBase::buildForm(). */ public function buildForm(array $form, array &$form_state) { $form['roles'] = array( @@ -42,11 +42,11 @@ public function buildForm(array $form, array &$form_state) { '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'), '#required' => TRUE, ); - return parent::form($form, $form_state); + return parent::buildForm($form, $form_state); } /** - * Implements \Drupal\Core\Form\FormInterface::validateForm(). + * Overrides \Drupal\Core\Condition\ConditionPluginBase::validateForm(). */ public function validateForm(array &$form, array &$form_state) { foreach ($form_state['values']['roles'] as $role) { @@ -54,14 +54,15 @@ public function validateForm(array &$form, array &$form_state) { form_set_error('roles', t('You have chosen an invalid user role, please check your selection and try again.')); } } + parent::validateForm($form, $form_state); } /** - * Implements \Drupal\Core\Form\FormInterface::submitForm(). + * Overrides \Drupal\Core\Condition\ConditionPluginBase::submitForm(). */ public function submitForm(array &$form, array &$form_state) { $this->configuration['roles'] = array_filter($form_state['values']['roles']); - parent::submit($form, $form_state); + parent::submitForm($form, $form_state); } /** @@ -94,7 +95,7 @@ public function summary() { */ public function evaluate() { $user = $this->getContextValue('user'); - return (array_intersect(array_filter($this->configuration['roles']), array_keys($user->roles))) ? TRUE : FALSE; + return (bool) (array_intersect(array_filter($this->configuration['roles']), array_keys($user->roles))) ? TRUE : FALSE; } } 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 6ecefa8..4521588 100644 --- a/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php @@ -16,6 +16,27 @@ */ class UserRoleConditionTest extends DrupalUnitTestBase { + /** + * The condition plugin manager. + * + * @var \Drupal\Core\Condition\ConditionManager + */ + protected $manager; + + /** + * An anonymous user for testing purposes. + * + * @var \Drupal\user\Plugin\Core\Entity\User + */ + protected $anonymous; + + /** + * An authenticated user for testing purposes. + * + * @var \Drupal\user\Plugin\Core\Entity\User + */ + protected $authenticated; + public static $modules = array('system', 'user', 'field'); public static function getInfo() { @@ -28,36 +49,37 @@ public static function getInfo() { protected function setUp() { parent::setUp(); + $this->installSchema('system', 'sequences'); - $this->installSchema('system', 'cache_path'); $this->installSchema('user', 'users'); - $this->installSchema('user', 'role_permission'); - $this->installSchema('user', 'users_data'); $this->installSchema('user', 'users_roles'); $this->installSchema('field', 'field_config'); $this->installSchema('field', 'field_config_instance'); + + $this->manager = new ConditionManager(); + + // Setup an anonymous user for our tests. $anonymous = new User((array) drupal_anonymous_user(), '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(); - $authenticated = new User(array('name' => $this->randomName(), 'bundle' => 'user', 'roles' => array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)), 'user'); - $authenticated->save(); + $this->anonymous = entity_load('user', 0); + + // Setup an authenticated user for our tests. + $this->authenticated = new User(array('name' => $this->randomName(), 'bundle' => 'user', 'roles' => array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)), 'user'); + $this->authenticated->save(); } /** * Tests conditions. */ function testConditions() { - $manager = new ConditionManager(); - - // Initialize the contexts we intend on using. - $anonymous = entity_load('user', 0); - $authenticated = user_load(2); - // Grab the user role condition and configure it to check against // authenticated user roles. - $condition = $manager->createInstance('user_role') + $condition = $this->manager->createInstance('user_role') ->setConfig('roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)) - ->setContextValue('user', $anonymous); + ->setContextValue('user', $this->anonymous); $this->assertFalse($condition->execute(), 'Anonymous users fail role checks for authenticated.'); // Check for the proper summary. // Summaries require an extra space due to negate handling in summary(). @@ -77,7 +99,7 @@ function testConditions() { // Set the context to the authenticated user and check that they also pass // against anonymous or authenticated roles. - $condition->setContextValue('user', $authenticated); + $condition->setContextValue('user', $this->authenticated); $this->assertTrue($condition->execute(), 'Authenticated users pass role checks for anonymous or authenticated.'); // Set the role to just authenticated and recheck. @@ -85,7 +107,7 @@ function testConditions() { $this->assertTrue($condition->execute(), 'Authenticated users pass role checks for authenticated.'); // Test Constructor injection. - $condition = $manager->createInstance('user_role', array('roles' => array(DRUPAL_AUTHENTICATED_RID), 'context' => array('user' => $authenticated))); + $condition = $this->manager->createInstance('user_role', array('roles' => array(DRUPAL_AUTHENTICATED_RID), 'context' => array('user' => $this->authenticated))); $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.'); // Check the negated summary.