View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Tests\BrowserTestBase;
class UserSearchTest extends BrowserTestBase {
protected static $modules = [
'search',
];
protected $defaultTheme = 'stark';
public function testUserSearch() {
$user1 = $this
->drupalCreateUser([
'access user profiles',
'search content',
], "foo+bar");
$this
->drupalLogin($user1);
$keys = $user1
->getEmail();
$edit = [
'keys' => $keys,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('Your search yielded no results.');
$this
->assertSession()
->pageTextContains('no results');
$keys = 'nomatch';
$edit = [
'keys' => $keys,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('no results');
$keys = $user1
->getAccountName();
$edit = [
'keys' => $keys,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->linkExists($keys, 0, 'Search by username worked for non-admin user');
$subkey = substr($keys, 1, 5);
$edit = [
'keys' => $subkey,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->linkExists($keys, 0, 'Search by username substring worked for non-admin user');
$subkey = substr($keys, 0, 2) . '*' . substr($keys, 4, 2);
$edit = [
'keys' => $subkey,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->linkExists($keys, 0, 'Search with wildcard worked for non-admin user');
$user2 = $this
->drupalCreateUser([
'administer users',
'access user profiles',
'search content',
]);
$this
->drupalLogin($user2);
$keys = $user2
->getEmail();
$edit = [
'keys' => $keys,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($keys);
$this
->assertSession()
->pageTextContains($user2
->getAccountName());
$subkey = substr($keys, 1, 5);
$edit = [
'keys' => $subkey,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($keys);
$this
->assertSession()
->pageTextContains($user2
->getAccountName());
$subkey = substr($keys, 0, 2) . '*' . substr($keys, 4, 2);
$edit = [
'keys' => $subkey,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($user2
->getAccountName());
$keys = $user1
->getAccountName();
$edit = [
'keys' => $keys,
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($keys);
$this
->assertSession()
->pageTextContains($user1
->getEmail());
$blocked_user = $this
->drupalCreateUser();
$blocked_user
->block();
$blocked_user
->save();
$edit = [
'keys' => $blocked_user
->getAccountName(),
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains($blocked_user
->getAccountName());
$this
->drupalLogin($user1);
$edit = [
'keys' => $blocked_user
->getAccountName(),
];
$this
->drupalGet('search/user');
$this
->submitForm($edit, 'Search');
$this
->assertSession()
->pageTextContains('Your search yielded no results.');
$user3 = $this
->drupalCreateUser([
'search content',
]);
$this
->drupalLogin($user3);
$this
->drupalGet('search/user');
$this
->assertSession()
->statusCodeEquals(403);
$user4 = $this
->drupalCreateUser([
'access user profiles',
]);
$this
->drupalLogin($user4);
$this
->drupalGet('search/user');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalLogout();
}
}