View source
<?php
namespace Drupal\user\Tests\Views;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class HandlerFilterUserNameTest extends ViewTestBase {
public static $modules = array(
'views_ui',
'user_test_views',
);
public static $testViews = array(
'test_user_name',
);
protected $accounts = array();
protected $names = array();
public $columnMap = array(
'uid' => 'uid',
);
protected function setUp() {
parent::setUp();
ViewTestData::createTestViews(get_class($this), array(
'user_test_views',
));
$this
->enableViewsTestModule();
$this->accounts = array();
$this->names = array();
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 = array(
$this->accounts[0]
->id(),
);
$this
->executeView($view);
$this
->assertIdenticalResultset($view, array(
array(
'uid' => $this->accounts[0]
->id(),
),
), $this->columnMap);
}
public function testAdminUserInterface() {
$admin_user = $this
->drupalCreateUser(array(
'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 = array(
$this
->randomMachineName(),
);
$users = array_map('strtolower', $users);
$edit = array(
'options[value]' => implode(', ', $users),
);
$this
->drupalPostForm($path, $edit, t('Apply'));
$this
->assertRaw(t('There are no entities matching "%value".', array(
'%value' => implode(', ', $users),
)));
$random_name = $this
->randomMachineName();
$users = array(
$random_name,
$this->names[0],
);
$users = array_map('strtolower', $users);
$edit = array(
'options[value]' => implode(', ', $users),
);
$users = array(
$users[0],
);
$this
->drupalPostForm($path, $edit, t('Apply'));
$this
->assertRaw(t('There are no entities matching "%value".', array(
'%value' => implode(', ', $users),
)));
$users = $this->names;
$users = array_map('strtolower', $users);
$edit = array(
'options[value]' => implode(', ', $users),
);
$this
->drupalPostForm($path, $edit, t('Apply'));
$this
->assertNoRaw(t('There are no entities matching "%value".', array(
'%value' => implode(', ', $users),
)));
}
public function testExposedFilter() {
$path = 'test_user_name';
$options = array();
$users = array(
$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".', array(
'%value' => implode(', ', $users),
)));
$users = array(
$this
->randomMachineName(),
$this->names[0],
);
$users = array_map('strtolower', $users);
$options['query']['uid'] = implode(', ', $users);
$users = array(
$users[0],
);
$this
->drupalGet($path, $options);
$this
->assertRaw(t('There are no entities matching "%value".', array(
'%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());
}
}
}