Commit d7ca8aef authored by catch's avatar catch

Issue #1723774 by googletorp, Berdir, corvus_ch: user_delete_multiple() does...

Issue #1723774 by googletorp, Berdir, corvus_ch: user_delete_multiple() does not use UserStorageController.
parent b65fd7fe
<?php
/**
* @file
* Definition of Drupal\user\Tests\UserDeleteTest.
*/
namespace Drupal\user\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests user_delete() and user_delete_multiple() behavior.
*/
class UserDeleteTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'User delete test',
'description' => 'Test account deleting of users.',
'group' => 'User',
);
}
/**
* Test deleting multiple users.
*/
function testUserDeleteMultiple() {
// Create a few users with permissions, so roles will be created.
$user_a = $this->drupalCreateUser(array('access content'));
$user_b = $this->drupalCreateUser(array('access content'));
$user_c = $this->drupalCreateUser(array('access content'));
$uids = array($user_a->uid, $user_b->uid, $user_c->uid);
// These users should have a role
$query = db_select('users_roles', 'r');
$roles_created = $query
->fields('r', array('uid'))
->condition('uid', $uids)
->countQuery()
->execute()
->fetchField();
$this->assertTrue($roles_created > 0, 'Role assigments created for new users and deletion of role assigments can be tested');
// We should be able to load one of the users.
$this->assertTrue(user_load($user_a->uid), 'User is created and deltion of user can be tested');
// Delete the users.
user_delete_multiple($uids);
// Test if the roles assignments are deleted.
$query = db_select('users_roles', 'r');
$roles_after_deletion = $query
->fields('r', array('uid'))
->condition('uid', $uids)
->countQuery()
->execute()
->fetchField();
$this->assertTrue($roles_after_deletion == 0, 'Role assigments deleted along with users');
// Test if the users are deleted, user_load() will return FALSE.
$this->assertFalse(user_load($user_a->uid), format_string('User with id @uid deleted.', array('@uid' => $user_a->uid)));
$this->assertFalse(user_load($user_b->uid), format_string('User with id @uid deleted.', array('@uid' => $user_b->uid)));
$this->assertFalse(user_load($user_c->uid), format_string('User with id @uid deleted.', array('@uid' => $user_c->uid)));
}
}
......@@ -221,4 +221,16 @@ protected function postSave(StorableInterface $entity, $update) {
}
}
}
/**
* Overrides Drupal\entity\DatabaseStorageController::postDelete().
*/
protected function postDelete($entities) {
db_delete('users_roles')
->condition('uid', array_keys($entities), 'IN')
->execute();
db_delete('authmap')
->condition('uid', array_keys($entities), 'IN')
->execute();
}
}
......@@ -2018,41 +2018,7 @@ function user_delete($uid) {
* @see hook_user_delete()
*/
function user_delete_multiple(array $uids) {
if (!empty($uids)) {
$accounts = user_load_multiple($uids);
$transaction = db_transaction();
try {
foreach ($accounts as $uid => $account) {
// Allow modules to act prior to user deletion.
module_invoke_all('user_predelete', $account);
module_invoke_all('entity_predelete', $account, 'user');
field_attach_delete('user', $account);
drupal_session_destroy_uid($account->uid);
}
db_delete('users')
->condition('uid', $uids, 'IN')
->execute();
db_delete('users_roles')
->condition('uid', $uids, 'IN')
->execute();
db_delete('authmap')
->condition('uid', $uids, 'IN')
->execute();
// Allow modules to respond to user deletion.
module_invoke_all('user_delete', $account);
module_invoke_all('entity_delete', $account, 'user');
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('user', $e);
throw $e;
}
entity_get_controller('user')->resetCache();
}
entity_get_controller('user')->delete($uids);
}
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment