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 {
public 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) {
parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), [
'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
->assertEqual($view->filter['uid']
->getValueOptions(), NULL);
}
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
->drupalPostForm($path, $edit, t('Apply'));
$this
->assertRaw(t('There are no entities matching "%value".', [
'%value' => 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
->drupalPostForm($path, $edit, t('Apply'));
$this
->assertRaw(t('There are no entities matching "%value".', [
'%value' => implode(', ', $users),
]));
$users = $this->names;
$users = array_map('strtolower', $users);
$edit = [
'options[value]' => implode(', ', $users),
];
$this
->drupalPostForm($path, $edit, t('Apply'));
$this
->assertNoRaw(t('There are no entities matching "%value".', [
'%value' => 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
->assertRaw(t('There are no entities matching "%value".', [
'%value' => implode(', ', $users),
]));
$options['query']['uid'] = [
[
'target_id' => 9999,
],
];
$this
->drupalGet($path, $options);
foreach ($this->accounts as $account) {
$this
->assertRaw($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
->assertRaw(t('There are no entities matching "%value".', [
'%value' => implode(', ', $users),
]));
$users = $this->names;
$options['query']['uid'] = implode(', ', $users);
$this
->drupalGet($path, $options);
$this
->assertNoRaw('Unable to find user');
foreach ($this->accounts as $account) {
$this
->assertRaw($account
->id());
}
$options['query']['uid'] = array_map(function ($account) {
return [
'target_id' => $account
->id(),
];
}, $this->accounts);
$this
->drupalGet($path, $options);
$this
->assertNoRaw('Unable to find user');
foreach ($this->accounts as $account) {
$this
->assertRaw($account
->id());
}
}
}