diff --git a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php index 691868d..a61af67 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/EntityWrapper.php @@ -67,7 +67,8 @@ public function __construct(array $definition, $name = NULL, ContextAwareInterfa public function getValue() { $source = $this->getIdSource(); $id = $source ? $source->getValue() : $this->id; - return $id ? entity_load($this->entityType, $id) : NULL; + // If $id is numeric, trust that it is correct. + return $id || is_numeric($id) ? entity_load($this->entityType, $id) : NULL; } /** diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 9ee6f81..b58df64 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1609,6 +1609,15 @@ function comment_preprocess_block(&$variables) { function comment_prepare_author(Comment $comment) { // The account has been pre-loaded by CommentRenderController::buildContent(). $account = $comment->uid->entity; + $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..43910c4 --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Plugin/Core/Condition/UserRole.php @@ -0,0 +1,101 @@ + 'checkboxes', + '#title' => t('Show block for specific roles'), + '#default_value' => !empty($this->configuration['roles']) ? $this->configuration['roles'] : array(), + '#options' => array_map('check_plain', user_role_names()), + '#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::buildForm($form, $form_state); + } + + /** + * 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('You have chosen an invalid user role, please check your selection and try again.')); + } + } + 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']; + $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' : '' + ) + ); + } + + /** + * 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..bd6283a --- /dev/null +++ b/core/modules/user/lib/Drupal/user/Tests/Condition/UserRoleConditionTest.php @@ -0,0 +1,131 @@ + 'User Role Condition Plugin', + 'description' => 'Tests that the User Role Condition, provided by the user module, is working properly.', + 'group' => 'Condition API', + ); + } + + /** + * Sets up the tests. + */ + 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 = new ConditionManager(); + + // 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) + ), 'user'); + $this->authenticated->save(); + } + + /** + * Tests conditions. + */ + 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('The user is a member of authenticated', $condition->summary()); + + // 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()); + + // 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()); + + // 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('The user is not a member of authenticated', $condition->summary()); + + // 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()); + } + +}