View source
<?php
namespace Drupal\Tests\user\Functional\Views;
use Drupal\views\Views;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class HandlerFilterUserNameTest extends ViewTestBase {
protected static $modules = [
'views_ui',
'user_test_views',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_user_name',
];
protected $accounts = [];
protected $names = [];
public $columnMap = [
'uid' => 'uid',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
ViewTestData::createTestViews(static::class, [
'user_test_views',
]);
$this
->enableViewsTestModule();
$this->accounts = [];
$this->names = [];
for ($i = 0; $i < 3; $i++) {
$this->accounts[] = $account = $this
->drupalCreateUser();
$this->names[] = $account
->label();
}
}
public function testUserNameApi() {
$view = Views::getView('test_user_name');
$view
->initHandlers();
$view->filter['uid']->value = [
$this->accounts[0]
->id(),
];
$this
->executeView($view);
$this
->assertIdenticalResultset($view, [
[
'uid' => $this->accounts[0]
->id(),
],
], $this->columnMap);
$this
->assertNull($view->filter['uid']
->getValueOptions());
}
public function testAdminUserInterface() {
$admin_user = $this
->drupalCreateUser([
'administer views',
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$path = 'admin/structure/views/nojs/handler/test_user_name/default/filter/uid';
$this
->drupalGet($path);
$users = [
$this
->randomMachineName(),
];
$users = array_map('strtolower', $users);
$edit = [
'options[value]' => implode(', ', $users),
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('There are no users matching "' . implode(', ', $users) . '".');
$random_name = $this
->randomMachineName();
$users = [
$random_name,
$this->names[0],
];
$users = array_map('strtolower', $users);
$edit = [
'options[value]' => implode(', ', $users),
];
$users = [
$users[0],
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextContains('There are no users matching "' . implode(', ', $users) . '".');
$users = $this->names;
$users = array_map('strtolower', $users);
$edit = [
'options[value]' => implode(', ', $users),
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Apply');
$this
->assertSession()
->pageTextNotContains('There are no users matching "' . implode(', ', $users) . '".');
}
public function testExposedFilter() {
$path = 'test_user_name';
$options = [];
$users = [
$this
->randomMachineName(),
];
$users = array_map('strtolower', $users);
$options['query']['uid'] = implode(', ', $users);
$this
->drupalGet($path, $options);
$this
->assertSession()
->pageTextContains('There are no users matching "' . implode(', ', $users) . '".');
$options['query']['uid'] = [
[
'target_id' => 9999,
],
];
$this
->drupalGet($path, $options);
foreach ($this->accounts as $account) {
$this
->assertSession()
->pageTextContains($account
->id());
}
$users = [
$this
->randomMachineName(),
$this->names[0],
];
$users = array_map('strtolower', $users);
$options['query']['uid'] = implode(', ', $users);
$users = [
$users[0],
];
$this
->drupalGet($path, $options);
$this
->assertSession()
->pageTextContains('There are no users matching "' . implode(', ', $users) . '".');
$users = $this->names;
$options['query']['uid'] = implode(', ', $users);
$this
->drupalGet($path, $options);
$this
->assertSession()
->pageTextNotContains('Unable to find user');
foreach ($this->accounts as $account) {
$this
->assertSession()
->pageTextContains($account
->id());
}
$options['query']['uid'] = array_map(function ($account) {
return [
'target_id' => $account
->id(),
];
}, $this->accounts);
$this
->drupalGet($path, $options);
$this
->assertSession()
->pageTextNotContains('Unable to find user');
foreach ($this->accounts as $account) {
$this
->assertSession()
->pageTextContains($account
->id());
}
}
}