diff --git a/core/modules/comment/comment.tokens.inc b/core/modules/comment/comment.tokens.inc index 715b63d..f5afcab 100644 --- a/core/modules/comment/comment.tokens.inc +++ b/core/modules/comment/comment.tokens.inc @@ -216,7 +216,7 @@ function comment_tokens($type, $tokens, array $data = array(), array $options = } if (($author_tokens = token_find_with_prefix($tokens, 'author')) && $account = $comment->uid->entity) { - $replacements += token_generate('user', $author_tokens, array('user' => $account), $options); + $replacements += token_generate('user', $author_tokens, array('user' => $account->getBCEntity()), $options); } } elseif ($type == 'node' & !empty($data['node'])) { diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index 7907464..1388d13 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -50,7 +50,8 @@ function testCommentLinks() { // Remove additional user permissions from $this->web_user added by setUp(), // since this test is limited to anonymous and authenticated roles only. - entity_delete_multiple('user_role', array(key($this->web_user->roles))); + $roles = $this->web_user->roles; + entity_delete_multiple('user_role', array(reset($roles))); // Matrix of possible environmental conditions and configuration settings. // See setEnvironment() for details. diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php index cee642e..eb257dc 100644 --- a/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php +++ b/core/modules/config/lib/Drupal/config/Tests/ConfigLocaleOverride.php @@ -88,7 +88,7 @@ function testConfigLocaleUserOverride() { )); $user_config_context = config_context_enter('Drupal\user\UserConfigContext'); - $user_config_context->setAccount($account); + $user_config_context->setAccount($account->getOriginalEntity()); $config = config('config_test.system'); $this->assertIdentical($config->get('foo'), 'fr bar'); // Ensure the non-overriden value is still the same. @@ -111,7 +111,7 @@ function testConfigLocaleUserOverride() { )); $config_factory = drupal_container()->get('config.factory'); - $config_factory->enterContext($user_config_context->setAccount($account)); + $config_factory->enterContext($user_config_context->setAccount($account->getOriginalEntity())); // Should not have to re-initialize the configuration object to get new // overrides as the new context will have a different uuid. $config = config('config_test.system'); @@ -127,7 +127,7 @@ function testConfigLocaleUserOverride() { )); // Create a new user config context to stack on top of the existign one. $en_user_config_context = config_context_enter('Drupal\user\UserConfigContext'); - $en_user_config_context->setAccount($account); + $en_user_config_context->setAccount($account->getOriginalEntity()); $config = config('config_test.system'); $this->assertIdentical($config->get('foo'), 'en bar'); @@ -178,7 +178,7 @@ function testConfigLocaleUserAndGlobalOverride() { )); $user_config_context = config_context_enter('Drupal\user\UserConfigContext'); - $user_config_context->setAccount($account); + $user_config_context->setAccount($account->getOriginalEntity()); $config = config('config_test.system'); $this->assertIdentical($config->get('foo'), 'fr bar'); // Ensure the value overriden from global $conf works. diff --git a/core/modules/contact/lib/Drupal/contact/MessageFormController.php b/core/modules/contact/lib/Drupal/contact/MessageFormController.php index c4352cf..b3a9ed8 100644 --- a/core/modules/contact/lib/Drupal/contact/MessageFormController.php +++ b/core/modules/contact/lib/Drupal/contact/MessageFormController.php @@ -167,7 +167,7 @@ public function save(array $form, array &$form_state) { $to = implode(', ', $category->recipients); $recipient_langcode = language_default()->langcode; } - elseif ($message->recipient instanceof User) { + elseif ($message->recipient && $message->recipient->getOriginalEntity() instanceof User) { // Send to the user in the user's preferred language. $to = $message->recipient->mail; $recipient_langcode = user_preferred_langcode($message->recipient); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php b/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php index 05b0cad..96d1b8d 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileFieldWidgetTest.php @@ -242,7 +242,7 @@ function testPrivateFileComment() { $user = $this->drupalCreateUser(array('access comments')); // Grant the admin user required comment permissions. - user_role_grant_permissions(key($this->admin_user->roles), array('administer comment fields')); + user_role_grant_permissions($this->admin_user->roles[1], array('administer comment fields')); // Revoke access comments permission from anon user, grant post to // authenticated. diff --git a/core/modules/node/node.module b/core/modules/node/node.module index 4c7392b..edd56d5 100644 --- a/core/modules/node/node.module +++ b/core/modules/node/node.module @@ -2527,7 +2527,7 @@ function node_access($op, $node, $account = NULL, $langcode = NULL) { // Make sure that if an account is passed, that it is a fully loaded user // object. if ($account && !($account instanceof User)) { - $account = user_load($account->uid); + $account = entity_load('user', $account->uid); } $method = $op . 'Access'; diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php index 0642032..773cd18 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityAccessTest.php @@ -82,7 +82,7 @@ function testEntityAccess() { 'update' => FALSE, 'delete' => FALSE, 'view' => FALSE, - ), $entity, $custom_user); + ), $entity, $custom_user->getOriginalEntity()); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityApiTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityApiTest.php index 1a8947c..6269bed 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityApiTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityApiTest.php @@ -7,7 +7,8 @@ namespace Drupal\system\Tests\Entity; -use Drupal\user\Plugin\Core\Entity\User; +use Drupal\Core\Entity\EntityInterface; + /** * Tests the basic Entity API. @@ -50,10 +51,10 @@ public function testCRUD() { * * @param string $entity_type * The entity type to run the tests with. - * @param \Drupal\user\Plugin\Core\Entity\User $user1 + * @param \Drupal\Core\Entity\EntityInterface $user1 * The user to run the tests with. */ - protected function assertCRUD($entity_type, User $user1) { + protected function assertCRUD($entity_type, EntityInterface $user1) { // Create some test entities. $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->uid)); $entity->save(); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php index 3c5679b..7cfc95c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityFieldTest.php @@ -117,19 +117,19 @@ protected function assertReadWrite($entity_type) { $this->assertTrue($entity->user_id[0] instanceof FieldItemInterface, format_string('%entity_type: Field item implements interface', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->uid, $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual($this->entity_user->name, $entity->user_id->entity->name, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual($this->entity_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); // Change the assigned user by entity. $new_user = $this->createUser(); $entity->user_id->entity = $new_user; $this->assertEqual($new_user->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual($new_user->name, $entity->user_id->entity->name, format_string('%entity_type: Updated user name value can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual($new_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: Updated user name value can be read.', array('%entity_type' => $entity_type))); // Change the assigned user by id. $new_user = $this->createUser(); $entity->user_id->target_id = $new_user->uid; $this->assertEqual($new_user->uid, $entity->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual($new_user->name, $entity->user_id->entity->name, format_string('%entity_type: Updated user name value can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual($new_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: Updated user name value can be read.', array('%entity_type' => $entity_type))); // Try unsetting a field. $entity->name->value = NULL; @@ -217,7 +217,7 @@ protected function assertReadWrite($entity_type) { )); $this->assertEqual($this->entity_name, $entity->name->value, format_string('%entity_type: Name value can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->uid, $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual($this->entity_user->name, $entity->user_id->entity->name, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual($this->entity_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_field_text, $entity->field_test_text->value, format_string('%entity_type: Text field can be read.', array('%entity_type' => $entity_type))); // Test copying field values. @@ -328,7 +328,7 @@ protected function assertSave($entity_type) { $this->assertEqual(LANGUAGE_NOT_SPECIFIED, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); $this->assertEqual(language_load(LANGUAGE_NOT_SPECIFIED), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_user->uid, $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array('%entity_type' => $entity_type))); - $this->assertEqual($this->entity_user->name, $entity->user_id->entity->name, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); + $this->assertEqual($this->entity_user->name, $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entity_field_text, $entity->field_test_text->value, format_string('%entity_type: Text field can be read.', array('%entity_type' => $entity_type))); } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php index b747e79..1209e7b 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityUnitTestBase.php @@ -51,7 +51,7 @@ protected function createUser($values = array(), $permissions = array()) { )); $role->save(); user_role_grant_permissions($role->id(), $permissions); - $values['roles'][$role->id()] = $role->id(); + $values['roles'][] = $role->id(); } $account = entity_create('user', $values + array( diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php index 09e7a7a..4116b4e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/FieldAccessTest.php @@ -70,12 +70,12 @@ function testFieldAccess() { $values = array('name' => 'test'); $account = entity_create('user', $values); - $this->assertFalse($entity->field_test_text->access('view', $account), 'Access to the field was denied.'); + $this->assertFalse($entity->field_test_text->access('view', $account->getOriginalEntity()), 'Access to the field was denied.'); $entity->field_test_text = 'access alter value'; - $this->assertFalse($entity->field_test_text->access('view', $account), 'Access to the field was denied.'); + $this->assertFalse($entity->field_test_text->access('view', $account->getOriginalEntity()), 'Access to the field was denied.'); $entity->field_test_text = 'standard value'; - $this->assertTrue($entity->field_test_text->access('view', $account), 'Access to the field was granted.'); + $this->assertTrue($entity->field_test_text->access('view', $account->getOriginalEntity()), 'Access to the field was granted.'); } } diff --git a/core/modules/user/lib/Drupal/user/ProfileFormController.php b/core/modules/user/lib/Drupal/user/ProfileFormController.php index c2b139d..bc36fbd 100644 --- a/core/modules/user/lib/Drupal/user/ProfileFormController.php +++ b/core/modules/user/lib/Drupal/user/ProfileFormController.php @@ -30,6 +30,15 @@ protected function actions(array $form, array &$form_state) { } /** + * Overrides Drupal\Core\Entity\EntityFormController::buildEntity(). + */ + public function buildEntity(array $form, array &$form_state) { + // Change the roles array to a list of enabled roles. + $form_state['values']['roles'] = array_keys(array_filter($form_state['values']['roles'])); + return parent::buildEntity($form, $form_state); + } + + /** * Overrides Drupal\Core\Entity\EntityFormController::submit(). */ public function submit(array $form, array &$form_state) { diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php b/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php index 3b83b52..0769fff 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php @@ -60,8 +60,8 @@ function testUserAdmin() { // Filter the users by role. Grab the system-generated role name for User C. $roles = $user_c->roles; - unset($roles[DRUPAL_AUTHENTICATED_RID]); - $edit['role'] = key($roles); + unset($roles[array_search(DRUPAL_AUTHENTICATED_RID, $roles)]); + $edit['role'] = reset($roles); $this->drupalPost('admin/people', $edit, t('Refine')); // Check if the correct users show up when filtered by role. diff --git a/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php b/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php index 2455b10..3fc3b4b 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserPermissionsTest.php @@ -28,8 +28,8 @@ function setUp() { // Find the new role ID. $all_rids = $this->admin_user->roles; - unset($all_rids[DRUPAL_AUTHENTICATED_RID]); - $this->rid = key($all_rids); + unset($all_rids[array_search(DRUPAL_AUTHENTICATED_RID, $all_rids)]); + $this->rid = reset($all_rids); } /** diff --git a/core/modules/user/lib/Drupal/user/UserStorageController.php b/core/modules/user/lib/Drupal/user/UserStorageController.php index b0eef51..20978c8 100644 --- a/core/modules/user/lib/Drupal/user/UserStorageController.php +++ b/core/modules/user/lib/Drupal/user/UserStorageController.php @@ -65,6 +65,12 @@ public function save(EntityInterface $entity) { $entity->uid->value = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField()); $entity->enforceIsNew(); } + + // There are some cases that pre-set ->original for performance. Make sure + // original is not a BC decorator. + if ($entity->original instanceof \Drupal\Core\Entity\EntityBCDecorator) { + $entity->original = $entity->original->getOriginalEntity(); + } parent::save($entity); } @@ -148,7 +154,7 @@ protected function postSave(EntityInterface $entity, $update) { if ($entity->status->value != $entity->original->status->value) { // The user's status is changing; conditionally send notification email. $op = $entity->status->value == 1 ? 'status_activated' : 'status_blocked'; - _user_mail_notify($op, $entity); + _user_mail_notify($op, $entity->getBCEntity()); } } else { diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 1faa9ce..54aec69 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -177,7 +177,7 @@ function user_uri($user) { * @see user_format_name() */ function user_label($entity_type, $entity) { - return user_format_name($entity); + return user_format_name($entity->getBCEntity()); } /** @@ -343,7 +343,7 @@ function user_load($uid, $reset = FALSE) { */ function user_load_by_mail($mail) { $users = entity_load_multiple_by_properties('user', array('mail' => $mail)); - return reset($users)->getBCEntity(); + return $users ? reset($users)->getBCEntity() : FALSE; } /** @@ -359,7 +359,7 @@ function user_load_by_mail($mail) { */ function user_load_by_name($name) { $users = entity_load_multiple_by_properties('user', array('name' => $name)); - return reset($users)->getBCEntity(); + return $users ? reset($users)->getBCEntity() : FALSE; } /** @@ -440,7 +440,7 @@ function user_role_permissions($roles) { $role_permissions = $fetch = array(); - foreach ($roles as $rid => $name) { + foreach ($roles as $rid) { if (isset($cache[$rid])) { $role_permissions[$rid] = $cache[$rid]; } @@ -491,6 +491,9 @@ function user_access($string, $account = NULL) { $account = $user; } + // Make sure we are working with the BC decorator. + $account = $account instanceof User ? $account->getBCEntity() : $account; + // User #1 has all privileges: if ($account->uid == 1) { return TRUE; @@ -2341,6 +2344,7 @@ function theme_user_signature($variables) { */ function user_preferred_langcode($account, $type = NULL, $default = NULL) { $language_list = language_list(); + $account = $account->getBCEntity(); if (isset($type)) { $preferred_langcode = $account->{'preferred_' . $type . '_langcode'}; } diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc index 140b767..4e57868 100644 --- a/core/modules/user/user.pages.inc +++ b/core/modules/user/user.pages.inc @@ -67,7 +67,7 @@ function user_pass_submit($form, &$form_state) { $account = $form_state['values']['account']; // Mail one time login URL and instructions using current language. - $mail = _user_mail_notify('password_reset', $account, $language_interface->langcode); + $mail = _user_mail_notify('password_reset', $account->getBCEntity(), $language_interface->langcode); if (!empty($mail)) { watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail)); drupal_set_message(t('Further instructions have been sent to your e-mail address.')); diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/AccessTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/AccessTest.php index b4c5118..fcbddcb 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/AccessTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/AccessTest.php @@ -37,11 +37,12 @@ protected function setUp() { $this->admin_user = $this->drupalCreateUser(array('access all views')); $this->web_user = $this->drupalCreateUser(); - $this->web_role = current($this->web_user->roles); + $this->web_role = $this->web_user->roles[0]; $this->normal_role = $this->drupalCreateRole(array()); $this->normal_user = $this->drupalCreateUser(array('views_test_data test permission')); - $this->normal_user->roles[$this->normal_role] = $this->normal_role; + // @todo: How to add a new item to the roles field list. + $this->normal_user->getOriginalEntity()->roles[2]->value = $this->normal_role; // @todo when all the plugin information is cached make a reset function and // call it here. }