diff --git a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php index 27279f5..959c83c 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php @@ -77,9 +77,13 @@ public function getValue() { } $source = $this->getIdSource(); $id = $source ? $source->getValue() : $this->id; - // If $id is numeric, trust that it is correct, this allows it to work for - // anonymous users ($id = 0). - return ($id || is_numeric($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 3402393..6b9a093 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1602,17 +1602,20 @@ 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 + // 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) { + if ($account->uid === 0) { // Make sure that correct anonymous fields are set if provided. foreach ($anonymous_fields as $field) { if (!empty($comment->{$field}->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 index c315f15..604d7d6 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 @@ -40,7 +40,7 @@ public function buildForm(array $form, array &$form_state) { '#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('Evaluate to TRUE if the user has one of the selected role(s). If you select no roles, the condition will evaluate to TRUE for all users.'), + '#description' => t('If you select no roles, the condition will evaluate to TRUE for all users.'), '#required' => TRUE, ); return $form; @@ -52,7 +52,7 @@ public function buildForm(array $form, array &$form_state) { 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('You have chosen an invalid user role, please check your selection and try again.')); + form_set_error('roles', t('An invalid user role has been chosen.')); } } parent::validateForm($form, $form_state); @@ -72,23 +72,17 @@ public function submitForm(array &$form, array &$form_state) { public function summary() { if (count($this->configuration['roles']) > 1) { $roles = $this->configuration['roles']; - $last = array_pop($roles); $roles = implode(', ', $roles); - return t('The user is @not a member of @roles or @last', - array( - '@roles' => $roles, - '@last' => $last, - '@not' => !empty($this->configuration['negate']) ? 'not' : '' - ) - ); } - $roles = array_pop($this->configuration['roles']); - return t('The user is @not a member of @roles', - array( - '@roles' => $roles, - '@not' => !empty($this->configuration['negate']) ? 'not' : '' - ) - ); + 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)); + } } /** 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 9074889..a325f6e 100644 --- a/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php @@ -37,11 +37,20 @@ class UserRoleConditionTest extends DrupalUnitTestBase { */ protected $authenticated; - public static $modules = array('system', 'user', 'field'); + /** + * A custom role for testing purposes. + * + * @var \Drupal\user\Plugin\Core\Entity\Role + */ + protected $role; /** - * Defines test information. + * Modules to enable. + * + * @var array */ + public static $modules = array('system', 'user', 'field'); + public static function getInfo() { return array( 'name' => 'User Role Condition Plugin', @@ -50,9 +59,6 @@ public static function getInfo() { ); } - /** - * Sets up the tests. - */ protected function setUp() { parent::setUp(); @@ -62,7 +68,17 @@ protected function setUp() { $this->installSchema('field', 'field_config'); $this->installSchema('field', 'field_config_instance'); - $this->manager = new ConditionManager($this->container->getParameter('container.namespaces')); + $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(); @@ -75,13 +91,13 @@ protected function setUp() { $this->authenticated = new User(array( 'name' => $this->randomName(), 'bundle' => 'user', - 'roles' => array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID) + 'roles' => array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID, $this->role->id() => $this->role->id()) ), 'user'); $this->authenticated->save(); } /** - * Tests conditions. + * Test the user_role condition. */ public function testConditions() { // Grab the user role condition and configure it to check against @@ -92,19 +108,19 @@ public function testConditions() { $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('The user is a member of authenticated', $condition->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('The user is a member of anonymous', $condition->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('The user is a member of anonymous or authenticated', $condition->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. @@ -121,11 +137,18 @@ public function testConditions() { // Check the negated summary. $condition->setConfig('negate', TRUE); - $this->assertEqual('The user is not a member of authenticated', $condition->summary()); + $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('The user is not a member of anonymous or authenticated', $condition->summary()); + $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()); + } }