UserDeleteTest.php in Drupal 8
File
core/modules/user/tests/src/Kernel/UserDeleteTest.php
View source
<?php
namespace Drupal\Tests\user\Kernel;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;
class UserDeleteTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'system',
'user',
];
public function testUserDeleteMultiple() {
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('user', [
'users_data',
]);
$this
->installEntitySchema('user');
$user_a = $this
->createUser([
'access user profiles',
]);
$user_b = $this
->createUser([
'access user profiles',
]);
$user_c = $this
->createUser([
'access user profiles',
]);
$uids = [
$user_a
->id(),
$user_b
->id(),
$user_c
->id(),
];
$connection = Database::getConnection();
$query = $connection
->select('user__roles', 'r');
$roles_created = $query
->fields('r', [
'entity_id',
])
->condition('entity_id', $uids, 'IN')
->countQuery()
->execute()
->fetchField();
$this
->assertGreaterThan(0, $roles_created);
$this
->assertNotNull(User::load($user_a
->id()));
$storage = $this->container
->get('entity_type.manager')
->getStorage('user');
$users = $storage
->loadMultiple($uids);
$storage
->delete($users);
$query = $connection
->select('user__roles', 'r');
$roles_after_deletion = $query
->fields('r', [
'entity_id',
])
->condition('entity_id', $uids, 'IN')
->countQuery()
->execute()
->fetchField();
$this
->assertEquals(0, $roles_after_deletion);
$this
->assertNull(User::load($user_a
->id()));
$this
->assertNull(User::load($user_b
->id()));
$this
->assertNull(User::load($user_c
->id()));
}
}