3 namespace Drupal\Tests\user\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\user\Entity\User;
9 * Tests account deleting of users.
13 class UserDeleteTest extends BrowserTestBase {
16 * Test deleting multiple users.
18 public function testUserDeleteMultiple() {
19 // Create a few users with permissions, so roles will be created.
20 $user_a = $this->drupalCreateUser(['access user profiles']);
21 $user_b = $this->drupalCreateUser(['access user profiles']);
22 $user_c = $this->drupalCreateUser(['access user profiles']);
24 $uids = [$user_a->id(), $user_b->id(), $user_c->id()];
26 // These users should have a role
27 $query = db_select('user__roles', 'r');
28 $roles_created = $query
29 ->fields('r', ['entity_id'])
30 ->condition('entity_id', $uids, 'IN')
35 $this->assertTrue($roles_created > 0, 'Role assignments created for new users and deletion of role assignments can be tested');
36 // We should be able to load one of the users.
37 $this->assertTrue(User::load($user_a->id()), 'User is created and deletion of user can be tested');
39 user_delete_multiple($uids);
40 // Test if the roles assignments are deleted.
41 $query = db_select('user__roles', 'r');
42 $roles_after_deletion = $query
43 ->fields('r', ['entity_id'])
44 ->condition('entity_id', $uids, 'IN')
48 $this->assertTrue($roles_after_deletion == 0, 'Role assignments deleted along with users');
49 // Test if the users are deleted, User::load() will return NULL.
50 $this->assertNull(User::load($user_a->id()), format_string('User with id @uid deleted.', ['@uid' => $user_a->id()]));
51 $this->assertNull(User::load($user_b->id()), format_string('User with id @uid deleted.', ['@uid' => $user_b->id()]));
52 $this->assertNull(User::load($user_c->id()), format_string('User with id @uid deleted.', ['@uid' => $user_c->id()]));