1 user.test UserUserSearchTestCase::testUserSearch()

File

core/modules/user/tests/user.test, line 2595
Tests for user.module.

Class

UserUserSearchTestCase
Test user search.

Code

function testUserSearch() {
  // Verify that a user without 'administer users' permission cannot search
  // for users by email address. Additionally, ensure that the username has a
  // plus sign to ensure searching works with that.
  $user1 = $this->backdropCreateUser(array('access user profiles', 'search content', 'use advanced search'));
  $user1->name = $edit['name'] = 'foo+bar';
  $user1->mail = $edit['mail'] = $edit['name'] . '@example.com';
  user_save($user1);
  $this->backdropLogin($user1);
  $keys = $user1->mail;
  $edit = array('keys' => $keys);
  $this->backdropPost('search/user/', $edit, t('Search'));
  $this->assertNoText($keys);
  $this->backdropLogout();

  $user2 = $this->backdropCreateUser(array('administer users', 'access user profiles', 'search content', 'use advanced search'));
  $this->backdropLogin($user2);
  $keys = $user2->mail;
  $edit = array('keys' => $keys);
  $this->backdropPost('search/user/', $edit, t('Search'));
  $this->assertText($keys);

  // Verify that wildcard search works.
  $keys = $user1->name;
  $keys = substr($keys, 0, 2) . '*' . substr($keys, 4, 2);
  $edit = array('keys' => $keys);
  $this->backdropPost('search/user/', $edit, t('Search'));
  $this->assertText($user1->name, 'Search for username wildcard resulted in user name on page for administrative user.');

  // Verify that wildcard search works for email.
  $keys = $user1->mail;
  $keys = substr($keys, 0, 2) . '*' . substr($keys, 4, 2);
  $edit = array('keys' => $keys);
  $this->backdropPost('search/user/', $edit, t('Search'));
  $this->assertText($user1->name, 'Search for email wildcard resulted in user name on page for administrative user.');

  // Create a blocked user.
  $blocked_user = $this->backdropCreateUser();
  $blocked_user->status = 0;
  $blocked_user->save();

  // Verify that users with "administer users" permissions can see blocked
  // accounts in search results.
  $edit = array('keys' => $blocked_user->name);
  $this->backdropPost('search/user', $edit, t('Search'));
  $this->assertText($blocked_user->name, 'Blocked users are listed on the user search results for users with the "administer users" permission.');

  // Verify that users without "administer users" permissions do not see
  // blocked accounts in search results.
  $this->backdropLogin($user1);
  $edit = array('keys' => $blocked_user->name);
  $this->backdropPost('search/user/', $edit, t('Search'));
  $this->assertNoText($blocked_user->name, 'Blocked users are hidden from the user search results.');

  $this->backdropLogout();
}