Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserDeleteTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\user\Entity\User;
7
8 /**
9  * Tests account deleting of users.
10  *
11  * @group user
12  */
13 class UserDeleteTest extends BrowserTestBase {
14
15   /**
16    * Test deleting multiple users.
17    */
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']);
23
24     $uids = [$user_a->id(), $user_b->id(), $user_c->id()];
25
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')
31       ->countQuery()
32       ->execute()
33       ->fetchField();
34
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');
38     // Delete the users.
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')
45       ->countQuery()
46       ->execute()
47       ->fetchField();
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()]));
53   }
54
55 }