3 namespace Drupal\user\Tests;
5 use Drupal\simpletest\WebTestBase;
6 use Drupal\user\Entity\User;
9 * Tests the user admin listing if views is not enabled.
12 * @see user_admin_account()
14 class UserAdminListingTest extends WebTestBase {
19 public function testUserListing() {
20 $this->drupalGet('admin/people');
21 $this->assertResponse(403, 'Anonymous user does not have access to the user admin listing.');
23 // Create a bunch of users.
25 for ($i = 0; $i < 3; $i++) {
26 $account = $this->drupalCreateUser();
27 $accounts[$account->label()] = $account;
29 // Create a blocked user.
30 $account = $this->drupalCreateUser();
33 $accounts[$account->label()] = $account;
35 // Create a user at a certain timestamp.
36 $account = $this->drupalCreateUser();
37 $account->created = 1363219200;
39 $accounts[$account->label()] = $account;
40 $timestamp_user = $account->label();
42 $rid_1 = $this->drupalCreateRole([], 'custom_role_1', 'custom_role_1');
43 $rid_2 = $this->drupalCreateRole([], 'custom_role_2', 'custom_role_2');
45 $account = $this->drupalCreateUser();
46 $account->addRole($rid_1);
47 $account->addRole($rid_2);
49 $accounts[$account->label()] = $account;
50 $role_account_name = $account->label();
52 // Create an admin user and look at the listing.
53 $admin_user = $this->drupalCreateUser(['administer users']);
54 $accounts[$admin_user->label()] = $admin_user;
56 $accounts['admin'] = User::load(1);
58 $this->drupalLogin($admin_user);
60 $this->drupalGet('admin/people');
61 $this->assertResponse(200, 'The admin user has access to the user admin listing.');
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;
68 if (isset($account->td[2]->div->ul)) {
69 foreach ($account->td[2]->div->ul->li as $element) {
70 $roles[] = (string) $element;
73 $result_accounts[$name] = [
75 'status' => (string) $account->td[1],
77 'member_for' => (string) $account->td[3],
78 'last_access' => (string) $account->td[4],
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.');
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.');
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.');
92 $this->assertEqual($result_accounts[$timestamp_user]['last_access'], 'never', 'Ensure the last access time is "never".');