View source
<?php
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
class FilterCombineTest extends ViewsKernelTestBase {
protected static $modules = [
'entity_test',
];
public static $testViews = [
'test_view',
'entity_test_fields',
];
protected $columnMap = [
'views_test_data_name' => 'name',
'views_test_data_job' => 'job',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('entity_test');
}
public function testFilterCombineContains() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'contains',
'fields' => [
'name',
'job',
],
'value' => 'iNg',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Ginger',
'job' => NULL,
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineWord() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'word',
'fields' => [
'name',
'job',
],
'value' => 'singer ringo',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
[
'name' => 'Ringo',
'job' => 'Drummer',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineAllWords() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'allwords',
'fields' => [
'name',
'job',
'age',
],
'value' => '25 "john singer"',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
$this
->assertStringContainsString('CONCAT_WS(', $view->query
->query());
}
public function testFilterCombineContainsFieldsOverwritten() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'contains',
'fields' => [
'name',
'job',
'dummy',
],
'value' => 'ing',
],
]);
$this
->executeView($view);
$this
->assertTrue($view->build_info['fail'], "View build has been marked as failed.");
$errors = $view
->validate();
$this
->assertEquals(t('Field %field set in %filter is not set in display %display.', [
'%field' => 'dummy',
'%filter' => 'Global: Combine fields filter',
'%display' => 'Default',
]), reset($errors['default']));
}
public function testNonFieldsRow() {
$view = Views::getView('entity_test_fields');
$view
->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('row', [
'type' => 'entity:entity_test',
'options' => [
'view_mode' => 'teaser',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'name' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'contains',
'fields' => [
'name',
],
'value' => 'ing',
],
]);
$this
->executeView($view);
$errors = $view
->validate();
$this
->assertEquals(t('%display: %filter can only be used on displays that use fields. Set the style or row format for that display to one using fields to use the combine field filter.', [
'%filter' => 'Global: Combine fields filter',
'%display' => 'Default',
]), reset($errors['default']));
$this
->assertStringNotContainsString('CONCAT_WS(', $view->query
->query());
}
public function testFilterCombineEqual() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => '=',
'fields' => [
'job',
],
'value' => 'sInger',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineStarts() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'starts',
'fields' => [
'job',
],
'value' => 'sIn',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineNotStarts() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'not_starts',
'fields' => [
'job',
],
'value' => 'sIn',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Paul',
'job' => 'Songwriter',
],
[
'name' => 'Meredith',
'job' => 'Speaker',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineEnds() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'ends',
'fields' => [
'job',
],
'value' => 'Ger',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'John',
'job' => 'Singer',
],
[
'name' => 'George',
'job' => 'Singer',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineNotEnds() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'not_ends',
'fields' => [
'job',
],
'value' => 'Ger',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Paul',
'job' => 'Songwriter',
],
[
'name' => 'Meredith',
'job' => 'Speaker',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
public function testFilterCombineNot() {
$view = Views::getView('test_view');
$view
->setDisplay();
$fields = $view->displayHandlers
->get('default')
->getOption('fields');
$view->displayHandlers
->get('default')
->overrideOption('fields', $fields + [
'job' => [
'id' => 'job',
'table' => 'views_test_data',
'field' => 'job',
'relationship' => 'none',
],
]);
$view->displayHandlers
->get('default')
->overrideOption('filters', [
'age' => [
'id' => 'combine',
'table' => 'views',
'field' => 'combine',
'relationship' => 'none',
'operator' => 'not',
'fields' => [
'job',
],
'value' => 'singer',
],
]);
$this
->executeView($view);
$resultset = [
[
'name' => 'Ringo',
'job' => 'Drummer',
],
[
'name' => 'Paul',
'job' => 'Songwriter',
],
[
'name' => 'Meredith',
'job' => 'Speaker',
],
];
$this
->assertIdenticalResultset($view, $resultset, $this->columnMap);
}
protected function dataSet() {
$data_set = parent::dataSet();
$data_set[] = [
'name' => 'Ginger',
'age' => 25,
'job' => NULL,
'created' => gmmktime(0, 0, 0, 1, 2, 2000),
'status' => 1,
];
return $data_set;
}
protected function schemaDefinition() {
$schema = parent::schemaDefinition();
unset($schema['views_test_data']['fields']['job']['not null']);
return $schema;
}
}