Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / src / Functional / UserAdminListingTest.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 the user admin listing if views is not enabled.
10  *
11  * @group user
12  * @see user_admin_account()
13  */
14 class UserAdminListingTest extends BrowserTestBase {
15
16   /**
17    * Tests the listing.
18    */
19   public function testUserListing() {
20     $this->drupalGet('admin/people');
21     $this->assertResponse(403, 'Anonymous user does not have access to the user admin listing.');
22
23     // Create a bunch of users.
24     $accounts = [];
25     for ($i = 0; $i < 3; $i++) {
26       $account = $this->drupalCreateUser();
27       $accounts[$account->label()] = $account;
28     }
29     // Create a blocked user.
30     $account = $this->drupalCreateUser();
31     $account->block();
32     $account->save();
33     $accounts[$account->label()] = $account;
34
35     // Create a user at a certain timestamp.
36     $account = $this->drupalCreateUser();
37     $account->created = 1363219200;
38     $account->save();
39     $accounts[$account->label()] = $account;
40     $timestamp_user = $account->label();
41
42     $rid_1 = $this->drupalCreateRole([], 'custom_role_1', 'custom_role_1');
43     $rid_2 = $this->drupalCreateRole([], 'custom_role_2', 'custom_role_2');
44
45     $account = $this->drupalCreateUser();
46     $account->addRole($rid_1);
47     $account->addRole($rid_2);
48     $account->save();
49     $accounts[$account->label()] = $account;
50     $role_account_name = $account->label();
51
52     // Create an admin user and look at the listing.
53     $admin_user = $this->drupalCreateUser(['administer users']);
54     $accounts[$admin_user->label()] = $admin_user;
55
56     $accounts['admin'] = User::load(1);
57
58     $this->drupalLogin($admin_user);
59
60     $this->drupalGet('admin/people');
61     $this->assertResponse(200, 'The admin user has access to the user admin listing.');
62
63     $result = $this->xpath('//table[contains(@class, "responsive-enabled")]/tbody/tr');
64     $result_accounts = [];
65     foreach ($result as $account) {
66       $account_columns = $account->findAll('css', 'td');
67       $name = $account_columns[0]->getText();
68       $roles = [];
69       $account_roles = $account_columns[2]->findAll('css', 'td div ul li');
70       if (!empty($account_roles)) {
71         foreach ($account_roles as $element) {
72           $roles[] = $element->getText();
73         }
74       }
75
76       $result_accounts[$name] = [
77         'name' => $name,
78         'status' => $account_columns[1]->getText(),
79         'roles' => $roles,
80         'member_for' => $account_columns[3]->getText(),
81         'last_access' => $account_columns[4]->getText(),
82       ];
83     }
84
85     $this->assertFalse(array_keys(array_diff_key($result_accounts, $accounts)), 'Ensure all accounts are listed.');
86     foreach ($result_accounts as $name => $values) {
87       $this->assertEqual($values['status'] == t('active'), $accounts[$name]->status->value, 'Ensure the status is displayed properly.');
88     }
89
90     $expected_roles = ['custom_role_1', 'custom_role_2'];
91     $this->assertEqual($result_accounts[$role_account_name]['roles'], $expected_roles, 'Ensure roles are listed properly.');
92
93     $this->assertEqual($result_accounts[$timestamp_user]['member_for'], \Drupal::service('date.formatter')->formatTimeDiffSince($accounts[$timestamp_user]->created->value), 'Ensure the right member time is displayed.');
94
95     $this->assertEqual($result_accounts[$timestamp_user]['last_access'], 'never', 'Ensure the last access time is "never".');
96   }
97
98 }