View source
<?php
namespace Drupal\user\Tests\Views;
use Drupal\Component\Utility\Html;
use Drupal\views\Views;
class HandlerFilterPermissionTest extends UserKernelTestBase {
public static $testViews = array(
'test_filter_permission',
);
protected $columnMap;
public function testFilterPermission() {
$this
->setupPermissionTestData();
$column_map = array(
'uid' => 'uid',
);
$view = Views::getView('test_filter_permission');
$view
->initHandlers();
$view->filter['permission']->value = array(
'non_existent_permission',
);
$this
->executeView($view);
$this
->assertEqual(count($view->result), 4, 'A non existent permission is not filtered so everything is the result.');
$expected[] = array(
'uid' => 1,
);
$expected[] = array(
'uid' => 2,
);
$expected[] = array(
'uid' => 3,
);
$expected[] = array(
'uid' => 4,
);
$this
->assertIdenticalResultset($view, $expected, $column_map);
$view
->destroy();
$view
->initHandlers();
$view->filter['permission']->value = array(
'administer permissions',
);
$this
->executeView($view);
$this
->assertEqual(count($view->result), 2);
$expected = array();
$expected[] = array(
'uid' => 3,
);
$expected[] = array(
'uid' => 4,
);
$this
->assertIdenticalResultset($view, $expected, $column_map);
$view
->destroy();
$view
->initHandlers();
$view->filter['permission']->value = array(
'administer users',
);
$this
->executeView($view);
$this
->assertEqual(count($view->result), 1);
$expected = array();
$expected[] = array(
'uid' => 4,
);
$this
->assertIdenticalResultset($view, $expected, $column_map);
$view
->destroy();
$view
->initDisplay();
$view
->initHandlers();
$view->filter['permission']
->getValueOptions();
$value_options = $view->filter['permission']
->getValueOptions();
$permission_by_module = [];
$permissions = \Drupal::service('user.permissions')
->getPermissions();
foreach ($permissions as $name => $permission) {
$permission_by_module[$permission['provider']][$name] = $permission;
}
foreach (array(
'system' => 'System',
'user' => 'User',
) as $module => $title) {
$expected = array_map(function ($permission) {
return Html::escape(strip_tags($permission['title']));
}, $permission_by_module[$module]);
$this
->assertEqual($expected, $value_options[$title], 'Ensure the all permissions are available');
}
}
}