diff --git a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php index fdd4d38..959c83c 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php @@ -77,7 +77,13 @@ public function getValue() { } $source = $this->getIdSource(); $id = $source ? $source->getValue() : $this->id; - return $id ? entity_load($this->entityType, $id) : NULL; + // The entity_load() function uses NULL as an indicator to load all + // entities of a given type, so we cannot rely on entity_load() to + // determine what is valid since we only ever want to deal with a single + // entity at a time in this case. + if (isset($id) && $id !== FALSE) { + return entity_load($this->entityType, $id); + } } /** diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 8e9400f..6b9a093 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1602,12 +1602,27 @@ function comment_preprocess_block(&$variables) { * This helper handles anonymous authors in addition to registered comment * authors. * + * @param \Drupal\comment\Plugin\Core\Entity\Comment $comment + * A comment entity. + * * @return \Drupal\user\Plugin\Core\Entity\User * A user account, for use with theme_username() or the user_picture template. */ function comment_prepare_author(Comment $comment) { // The account has been pre-loaded by CommentRenderController::buildContent(). $account = $comment->uid->entity; + // For an anonymous commenter, name and homepage are stored in the comment. + // To make them available for rendering in the author object we need to + // transpose these values into the $account object. + $anonymous_fields = array('name', 'homepage'); + if ($account->uid === 0) { + // Make sure that correct anonymous fields are set if provided. + foreach ($anonymous_fields as $field) { + if (!empty($comment->{$field}->value)) { + $account->{$field} = $comment->{$field}->value; + } + } + } if (!$account) { $account = entity_create('user', array('uid' => 0, 'name' => $comment->name->value, 'homepage' => $comment->homepage->value)); } 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 new file mode 100644 index 0000000..604d7d6 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php @@ -0,0 +1,96 @@ + 'checkboxes', + '#title' => 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.'), + '#required' => TRUE, + ); + return $form; + } + + /** + * Overrides \Drupal\Core\Condition\ConditionPluginBase::validateForm(). + */ + public function validateForm(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.')); + } + } + parent::validateForm($form, $form_state); + } + + /** + * Overrides \Drupal\Core\Condition\ConditionPluginBase::submitForm(). + */ + public function submitForm(array &$form, array &$form_state) { + $this->configuration['roles'] = array_filter($form_state['values']['roles']); + parent::submitForm($form, $form_state); + } + + /** + * Implements \Drupal\Core\Executable\ExecutableInterface::summary(). + */ + public function summary() { + if (count($this->configuration['roles']) > 1) { + $roles = $this->configuration['roles']; + $roles = implode(', ', $roles); + } + else { + $roles = array_pop($this->configuration['roles']); + } + if (!empty($this->configuration['negate'])) { + return 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)); + } + } + + /** + * Implements \Drupal\condition\ConditionInterface::evaluate(). + */ + public function evaluate() { + $user = $this->getContextValue('user'); + return (bool) array_intersect(array_filter($this->configuration['roles']), array_keys($user->roles)); + } + +} diff --git a/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php new file mode 100644 index 0000000..a325f6e --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php @@ -0,0 +1,154 @@ + 'User Role Condition Plugin', + 'description' => 'Tests that the User Role Condition, provided by the user module, is working properly.', + 'group' => 'Condition API', + ); + } + + 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->manager = $this->container->get('plugin.manager.condition'); + + // Create new role. + $rid = strtolower($this->randomName(8)); + $label = $this->randomString(8); + $role = entity_create('user_role', array( + 'id' => $rid, + 'label' => $label, + )); + $role->save(); + $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); + // 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, $this->role->id() => $this->role->id()) + ), 'user'); + $this->authenticated->save(); + } + + /** + * Test the user_role condition. + */ + public function testConditions() { + // Grab the user role condition and configure it to check against + // authenticated user roles. + $condition = $this->manager->createInstance('user_role') + ->setConfig('roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)) + ->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(). + $this->assertEqual($condition->summary(), 'The user is a member of authenticated'); + + // Set the user role to anonymous. + $condition->setConfig('roles', array(DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID)); + $this->assertTrue($condition->execute(), 'Anonymous users pass role checks for anonymous.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The user is a member of anonymous'); + + // Set the user role to check anonymous or authenticated. + $condition->setConfig('roles', array(DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + $this->assertTrue($condition->execute(), 'Anonymous users pass role checks for anonymous or authenticated.'); + // Check for the proper summary. + $this->assertEqual($condition->summary(), 'The user is a member of anonymous, authenticated'); + + // Set the context to the authenticated user and check that they also pass + // against anonymous or authenticated roles. + $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. + $condition->setConfig('roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID)); + $this->assertTrue($condition->execute(), 'Authenticated users pass role checks for authenticated.'); + + // Test Constructor injection. + $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. + $condition->setConfig('negate', TRUE); + $this->assertEqual($condition->summary(), 'The user is not a member of authenticated'); + + // Check the complex negated summary. + $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 + $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()); + + } + +}