From 501e8b434a655db47b276e99466f67b03007785b Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Tue, 17 May 2016 10:35:52 +0100 Subject: [PATCH] Issue #2723551 by valthebald, John Cook: Remove entity_load* usage for user entity type --- .../user/src/Plugin/views/filter/Name.php | 3 ++- .../user/src/Tests/UserRegistrationTest.php | 22 +++++++++++++------ .../user/src/Tests/Views/BulkFormTest.php | 3 ++- core/modules/user/user.module | 6 +++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/core/modules/user/src/Plugin/views/filter/Name.php b/core/modules/user/src/Plugin/views/filter/Name.php index d31c78102eb..5f63cb309bb 100644 --- a/core/modules/user/src/Plugin/views/filter/Name.php +++ b/core/modules/user/src/Plugin/views/filter/Name.php @@ -108,7 +108,8 @@ class Name extends InOperator { $this->valueOptions = array(); if ($this->value) { - $result = entity_load_multiple_by_properties('user', array('uid' => $this->value)); + $result = \Drupal::entityTypeManager()->getStorage('user') + ->loadByProperties(['uid' => $this->value]); foreach ($result as $account) { if ($account->id()) { $this->valueOptions[$account->id()] = $account->label(); diff --git a/core/modules/user/src/Tests/UserRegistrationTest.php b/core/modules/user/src/Tests/UserRegistrationTest.php index 56f21e4d417..083136268de 100644 --- a/core/modules/user/src/Tests/UserRegistrationTest.php +++ b/core/modules/user/src/Tests/UserRegistrationTest.php @@ -40,7 +40,10 @@ class UserRegistrationTest extends WebTestBase { $edit['mail'] = $mail = $edit['name'] . '@example.com'; $this->drupalPostForm('user/register', $edit, t('Create new account')); $this->assertText(t('A welcome message with further instructions has been sent to your email address.'), 'User registered successfully.'); - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + + /** @var EntityStorageInterface $storage */ + $storage = $this->container->get('entity_type.manager')->getStorage('user'); + $accounts = $storage->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $this->assertTrue($new_user->isActive(), 'New account is active after registration.'); $resetURL = user_pass_reset_url($new_user); @@ -54,7 +57,7 @@ class UserRegistrationTest extends WebTestBase { $edit['mail'] = $mail = $edit['name'] . '@example.com'; $this->drupalPostForm('user/register', $edit, t('Create new account')); $this->container->get('entity.manager')->getStorage('user')->resetCache(); - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + $accounts = $storage->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $this->assertFalse($new_user->isActive(), 'New account is blocked until approved by an administrator.'); } @@ -83,7 +86,8 @@ class UserRegistrationTest extends WebTestBase { $edit['pass[pass2]'] = $new_pass; $this->drupalPostForm('user/register', $edit, t('Create new account')); $this->container->get('entity.manager')->getStorage('user')->resetCache(); - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + $accounts = $this->container->get('entity_type.manager')->getStorage('user') + ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $this->assertNotNull($new_user, 'New account successfully created with matching passwords.'); $this->assertText(t('Registration successful. You are now logged in.'), 'Users are logged in after registering.'); @@ -108,7 +112,8 @@ class UserRegistrationTest extends WebTestBase { $this->assertText(t('The username @name has not been activated or is blocked.', array('@name' => $name)), 'User cannot log in yet.'); // Activate the new account. - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + $accounts = $this->container->get('entity_type.manager')->getStorage('user') + ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $admin_user = $this->drupalCreateUser(array('administer users')); $this->drupalLogin($admin_user); @@ -248,7 +253,8 @@ class UserRegistrationTest extends WebTestBase { $this->drupalPostForm(NULL, $edit, t('Create new account')); // Check user fields. - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + $accounts = $this->container->get('entity_type.manager')->getStorage('user') + ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $this->assertEqual($new_user->getUsername(), $name, 'Username matches.'); $this->assertEqual($new_user->getEmail(), $mail, 'Email address matches.'); @@ -338,7 +344,8 @@ class UserRegistrationTest extends WebTestBase { $edit['test_user_field[0][value]'] = $value; $this->drupalPostForm(NULL, $edit, t('Create new account')); // Check user fields. - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + $accounts = $this->container->get('entity_type.manager')->getStorage('user') + ->loadByProperties(['name' => $name, 'mail' => $mail]); $new_user = reset($accounts); $this->assertEqual($new_user->test_user_field->value, $value, 'The field value was correctly saved.'); @@ -367,7 +374,8 @@ class UserRegistrationTest extends WebTestBase { $edit['mail'] = $mail = $edit['name'] . '@example.com'; $this->drupalPostForm(NULL, $edit, t('Create new account')); // Check user fields. - $accounts = entity_load_multiple_by_properties('user', array('name' => $name, 'mail' => $mail)); + $accounts = $this->container->get('entity_type.manager')->getStorage('user') + ->loadByProperties(array('name' => $name, 'mail' => $mail)); $new_user = reset($accounts); $this->assertEqual($new_user->test_user_field[0]->value, $value, format_string('@js : The field value was correctly saved.', array('@js' => $js))); $this->assertEqual($new_user->test_user_field[1]->value, $value + 1, format_string('@js : The field value was correctly saved.', array('@js' => $js))); diff --git a/core/modules/user/src/Tests/Views/BulkFormTest.php b/core/modules/user/src/Tests/Views/BulkFormTest.php index a4ff23dbcc4..c617c174e51 100644 --- a/core/modules/user/src/Tests/Views/BulkFormTest.php +++ b/core/modules/user/src/Tests/Views/BulkFormTest.php @@ -2,6 +2,7 @@ namespace Drupal\user\Tests\Views; +use Drupal\user\Entity\User; use Drupal\user\RoleInterface; use Drupal\views\Views; @@ -130,7 +131,7 @@ class BulkFormTest extends UserTestBase { */ public function testBulkFormCombineFilter() { // Add a user. - $account = entity_load('user', $this->users[0]->id()); + User::load($this->users[0]->id()); $view = Views::getView('test_user_bulk_form_combine_filter'); $errors = $view->validate(); $this->assertEqual(reset($errors['default']), t('Field %field set in %filter is not usable for this filter type. Combined field filter only works for simple fields.', array('%field' => 'User: Bulk update', '%filter' => 'Global: Combine fields filter'))); diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 1e84fa91b7a..1394b23a2f3 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -232,7 +232,8 @@ function user_load($uid, $reset = FALSE) { * @see \Drupal\user\Entity\User::loadMultiple() */ function user_load_by_mail($mail) { - $users = entity_load_multiple_by_properties('user', array('mail' => $mail)); + $users = \Drupal::entityTypeManager()->getStorage('user') + ->loadByProperties(['mail' => $mail]); return $users ? reset($users) : FALSE; } @@ -248,7 +249,8 @@ function user_load_by_mail($mail) { * @see \Drupal\user\Entity\User::loadMultiple() */ function user_load_by_name($name) { - $users = entity_load_multiple_by_properties('user', array('name' => $name)); + $users = \Drupal::entityTypeManager()->getStorage('user') + ->loadByProperties(['name' => $name]); return $users ? reset($users) : FALSE; }