Backup of db before drupal security update
[yaffs-website] / web / core / modules / user / src / Tests / UserAdminListingTest.php
1 <?php
2
3 namespace Drupal\user\Tests;
4
5 use Drupal\simpletest\WebTestBase;
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 WebTestBase {
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       $name = (string) $account->td[0]->span;
67       $roles = [];
68       if (isset($account->td[2]->div->ul)) {
69         foreach ($account->td[2]->div->ul->li as $element) {
70           $roles[] = (string) $element;
71         }
72       }
73       $result_accounts[$name] = [
74         'name' => $name,
75         'status' => (string) $account->td[1],
76         'roles' => $roles,
77         'member_for' => (string) $account->td[3],
78         'last_access' => (string) $account->td[4],
79       ];
80     }
81
82     $this->assertFalse(array_keys(array_diff_key($result_accounts, $accounts)), 'Ensure all accounts are listed.');
83     foreach ($result_accounts as $name => $values) {
84       $this->assertEqual($values['status'] == t('active'), $accounts[$name]->status->value, 'Ensure the status is displayed properly.');
85     }
86
87     $expected_roles = ['custom_role_1', 'custom_role_2'];
88     $this->assertEqual($result_accounts[$role_account_name]['roles'], $expected_roles, 'Ensure roles are listed properly.');
89
90     $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.');
91
92     $this->assertEqual($result_accounts[$timestamp_user]['last_access'], 'never', 'Ensure the last access time is "never".');
93   }
94
95 }