Issue #2784739 by slasher13, daffie, miiimooo, voleger, MerryHamster, chanderbhushan, anmolgoyal74, mohit1604, Prashant.c, Meenakshi.g, kostyashupenko, Juterpillar, sylvain lavielle, gawaksh, Lendude, mradcliffe, andypost, alexpott: Fix PostgreSQL operator in views

merge-requests/251/head
catch 2021-01-11 20:40:06 +00:00
parent 5fb97954e8
commit e4e0d0b44b
4 changed files with 346 additions and 25 deletions

View File

@ -133,13 +133,14 @@ class Combine extends StringFilter {
*/
public function opEqual($expression) {
$placeholder = $this->placeholder();
$operator = $this->operator();
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->value]);
}
protected function opContains($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
}
/**
@ -165,8 +166,7 @@ class Combine extends StringFilter {
// Switch between the 'word' and 'allwords' operator.
$type = $this->operator == 'word' ? 'OR' : 'AND';
$group = $this->query->setWhereGroup($type);
$operator = $this->connection->mapConditionOperator('LIKE');
$operator = isset($operator['operator']) ? $operator['operator'] : 'LIKE';
$operator = $this->getConditionOperator('LIKE');
foreach ($matches as $match_key => $match) {
$temp_placeholder = $placeholder . '_' . $match_key;
@ -178,27 +178,32 @@ class Combine extends StringFilter {
protected function opStartsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
}
protected function opNotStartsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => $this->connection->escapeLike($this->value) . '%']);
}
protected function opEndsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
}
protected function opNotEndsWith($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value)]);
}
protected function opNotLike($expression) {
$placeholder = $this->placeholder();
$this->query->addWhereExpression($this->options['group'], "$expression NOT LIKE $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhereExpression($this->options['group'], "$expression $operator $placeholder", [$placeholder => '%' . $this->connection->escapeLike($this->value) . '%']);
}
protected function opRegex($expression) {

View File

@ -299,8 +299,29 @@ class StringFilter extends FilterPluginBase {
}
}
/**
* Get the query operator.
*
* @return string
* Returns LIKE or NOT LIKE or the database specific equivalent based on the
* query's operator.
*/
public function operator() {
return $this->operator == '=' ? 'LIKE' : 'NOT LIKE';
return $this->getConditionOperator($this->operator == '=' ? 'LIKE' : 'NOT LIKE');
}
/**
* Get specified condition operator mapping value.
*
* @param string $operator
* Condition operator.
*
* @return string
* Specified condition operator mapping value.
*/
protected function getConditionOperator($operator) {
$mapping = $this->connection->mapConditionOperator($operator);
return isset($mapping['operator']) ? $mapping['operator'] : $operator;
}
/**
@ -325,7 +346,8 @@ class StringFilter extends FilterPluginBase {
}
protected function opContains($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', 'LIKE');
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opContainsWord($field) {
@ -337,6 +359,7 @@ class StringFilter extends FilterPluginBase {
}
preg_match_all(static::WORDS_PATTERN, ' ' . $this->value, $matches, PREG_SET_ORDER);
$operator = $this->getConditionOperator('LIKE');
foreach ($matches as $match) {
$phrase = FALSE;
// Strip off phrase quotes
@ -347,7 +370,7 @@ class StringFilter extends FilterPluginBase {
$words = trim($match[2], ',?!();:-');
$words = $phrase ? [$words] : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
foreach ($words as $word) {
$where->condition($field, '%' . $this->connection->escapeLike(trim($word, " ,!?")) . '%', 'LIKE');
$where->condition($field, '%' . $this->connection->escapeLike(trim($word, " ,!?")) . '%', $operator);
}
}
@ -361,23 +384,28 @@ class StringFilter extends FilterPluginBase {
}
protected function opStartsWith($field) {
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', 'LIKE');
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opNotStartsWith($field) {
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', 'NOT LIKE');
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhere($this->options['group'], $field, $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opEndsWith($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), 'LIKE');
$operator = $this->getConditionOperator('LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), $operator);
}
protected function opNotEndsWith($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), 'NOT LIKE');
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value), $operator);
}
protected function opNotLike($field) {
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', 'NOT LIKE');
$operator = $this->getConditionOperator('NOT LIKE');
$this->query->addWhere($this->options['group'], $field, '%' . $this->connection->escapeLike($this->value) . '%', $operator);
}
protected function opShorterThan($field) {

View File

@ -69,7 +69,7 @@ class FilterCombineTest extends ViewsKernelTestBase {
'name',
'job',
],
'value' => 'ing',
'value' => 'iNg',
],
]);
@ -281,6 +281,294 @@ class FilterCombineTest extends ViewsKernelTestBase {
$this->assertStringNotContainsString('CONCAT_WS(', $view->query->query());
}
/**
* Tests the Combine field filter using the 'equal' operator.
*/
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',
],
]);
// Change the filtering.
$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);
}
/**
* Tests the Combine field filter using the 'starts' operator.
*/
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',
],
]);
// Change the filtering.
$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);
}
/**
* Tests the Combine field filter using the 'not_starts' operator.
*/
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',
],
]);
// Change the filtering.
$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);
}
/**
* Tests the Combine field filter using the 'ends' operator.
*/
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',
],
]);
// Change the filtering.
$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);
}
/**
* Tests the Combine field filter using the 'not_ends' operator.
*/
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',
],
]);
// Change the filtering.
$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);
}
/**
* Tests the Combine field filter using the 'not' operator.
*/
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',
],
]);
// Change the filtering.
$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);
}
/**
* Additional data to test the NULL issue.
*/

View File

@ -207,7 +207,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'name',
'relationship' => 'none',
'operator' => 'contains',
'value' => 'ing',
'value' => 'iNg',
],
]);
@ -253,7 +253,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'description',
'relationship' => 'none',
'operator' => 'word',
'value' => 'actor',
'value' => 'aCtor',
],
]);
@ -382,7 +382,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'description',
'relationship' => 'none',
'operator' => 'starts',
'value' => 'George',
'value' => 'gEorge',
],
]);
@ -427,7 +427,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'description',
'relationship' => 'none',
'operator' => 'not_starts',
'value' => 'George',
'value' => 'gEorge',
],
]);
@ -486,7 +486,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'description',
'relationship' => 'none',
'operator' => 'ends',
'value' => 'Beatles.',
'value' => 'bEatles.',
],
]);
@ -537,7 +537,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'description',
'relationship' => 'none',
'operator' => 'not_ends',
'value' => 'Beatles.',
'value' => 'bEatles.',
],
]);
@ -590,7 +590,7 @@ class FilterStringTest extends ViewsKernelTestBase {
'field' => 'description',
'relationship' => 'none',
'operator' => 'not',
'value' => 'Beatles.',
'value' => 'bEatles.',
],
]);