View source
<?php
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
class FilterBooleanOperatorTest extends ViewsKernelTestBase {
protected static $modules = [
'system',
];
public static $testViews = [
'test_view',
];
protected $columnMap = [
'views_test_data_id' => 'id',
];
public function testFilterBooleanOperator() {
$view = Views::getView('test_view');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'status' => [
'id' => 'status',
'field' => 'status',
'table' => 'views_test_data',
'value' => 0,
],
]);
$this
->executeView($view);
$expected_result = [
[
'id' => 2,
],
[
'id' => 4,
],
];
$this
->assertCount(2, $view->result);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'status' => [
'id' => 'status',
'field' => 'status',
'table' => 'views_test_data',
'value' => 1,
],
]);
$this
->executeView($view);
$expected_result = [
[
'id' => 1,
],
[
'id' => 3,
],
[
'id' => 5,
],
];
$this
->assertCount(3, $view->result);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'status' => [
'id' => 'status',
'field' => 'status',
'table' => 'views_test_data',
'value' => 0,
'operator' => '!=',
],
]);
$this
->executeView($view);
$expected_result = [
[
'id' => 1,
],
[
'id' => 3,
],
[
'id' => 5,
],
];
$this
->assertCount(3, $view->result);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
}
public function testFilterGroupedExposed() {
$filters = $this
->getGroupedExposedFilters();
$view = Views::getView('test_view');
$view
->setExposedInput([
'status' => 1,
]);
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', $filters);
$this
->executeView($view);
$expected_result = [
[
'id' => 1,
],
[
'id' => 3,
],
[
'id' => 5,
],
];
$this
->assertCount(3, $view->result);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->setExposedInput([
'status' => 2,
]);
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', $filters);
$this
->executeView($view);
$expected_result = [
[
'id' => 2,
],
[
'id' => 4,
],
];
$this
->assertCount(2, $view->result);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
$view
->destroy();
$view
->setExposedInput([
'status' => 3,
]);
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('filters', $filters);
$this
->executeView($view);
$expected_result = [
[
'id' => 1,
],
[
'id' => 3,
],
[
'id' => 5,
],
];
$this
->assertCount(3, $view->result);
$this
->assertIdenticalResultset($view, $expected_result, $this->columnMap);
}
protected function getGroupedExposedFilters() {
$filters = [
'status' => [
'id' => 'status',
'table' => 'views_test_data',
'field' => 'status',
'relationship' => 'none',
'exposed' => TRUE,
'expose' => [
'operator' => 'status_op',
'label' => 'status',
'identifier' => 'status',
],
'is_grouped' => TRUE,
'group_info' => [
'label' => 'status',
'identifier' => 'status',
'default_group' => 'All',
'group_items' => [
1 => [
'title' => 'Active',
'operator' => '=',
'value' => '1',
],
2 => [
'title' => 'Blocked',
'operator' => '=',
'value' => '0',
],
3 => [
'title' => 'Active (reverse)',
'operator' => '!=',
'value' => '0',
],
],
],
],
];
return $filters;
}
}