From cda222ec2438ec71acd51009e31bcea6f16d58ed Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Tue, 7 Mar 2017 10:25:47 +0000 Subject: [PATCH] Issue #2808321 by vaplas, Jo Fitzgerald, Lendude, alexpott, xjm, Chi, benqwerty: Fix default value "EQUAL" in $query_operator param of BooleanOperator::queryOpBoolean() --- .../Plugin/views/filter/BooleanOperator.php | 21 +++---- .../FilterBooleanOperatorDefaultTest.php | 25 +++++++++ .../FilterBooleanOperatorDefaultTest.php | 56 +++++++++++++++++++ 3 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 core/modules/views/tests/modules/views_test_data/src/Plugin/views/filter/FilterBooleanOperatorDefaultTest.php create mode 100644 core/modules/views/tests/src/Kernel/Handler/FilterBooleanOperatorDefaultTest.php diff --git a/core/modules/views/src/Plugin/views/filter/BooleanOperator.php b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php index 69b6ebac7ebe..917a30b848cb 100644 --- a/core/modules/views/src/Plugin/views/filter/BooleanOperator.php +++ b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php @@ -74,14 +74,14 @@ protected function operators() { 'method' => 'queryOpBoolean', 'short' => $this->t('='), 'values' => 1, - 'query_operator' => static::EQUAL, + 'query_operator' => self::EQUAL, ], '!=' => [ 'title' => $this->t('Is not equal to'), 'method' => 'queryOpBoolean', 'short' => $this->t('!='), 'values' => 1, - 'query_operator' => static::NOT_EQUAL, + 'query_operator' => self::NOT_EQUAL, ], ]; } @@ -231,13 +231,13 @@ public function query() { * @param string $field * The field name to add the where condition for. * @param string $query_operator - * (optional) Either static::EQUAL or static::NOT_EQUAL. Defaults to - * static::EQUAL. + * (optional) Either self::EQUAL or self::NOT_EQUAL. Defaults to + * self::EQUAL. */ - protected function queryOpBoolean($field, $query_operator = EQUAL) { + protected function queryOpBoolean($field, $query_operator = self::EQUAL) { if (empty($this->value)) { if ($this->accept_null) { - if ($query_operator == static::EQUAL) { + if ($query_operator === self::EQUAL) { $condition = (new Condition('OR')) ->condition($field, 0, $query_operator) ->isNull($field); @@ -255,12 +255,13 @@ protected function queryOpBoolean($field, $query_operator = EQUAL) { } else { if (!empty($this->definition['use_equal'])) { - // Forces an '=' operator instead of a '<>' for performance reasons. - if ($query_operator == static::EQUAL) { - $this->query->addWhere($this->options['group'], $field, 1, static::EQUAL); + // Forces a self::EQUAL operator instead of a self::NOT_EQUAL for + // performance reasons. + if ($query_operator === self::EQUAL) { + $this->query->addWhere($this->options['group'], $field, 1, self::EQUAL); } else { - $this->query->addWhere($this->options['group'], $field, 0, static::EQUAL); + $this->query->addWhere($this->options['group'], $field, 0, self::EQUAL); } } else { diff --git a/core/modules/views/tests/modules/views_test_data/src/Plugin/views/filter/FilterBooleanOperatorDefaultTest.php b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/filter/FilterBooleanOperatorDefaultTest.php new file mode 100644 index 000000000000..33cb5f9790fd --- /dev/null +++ b/core/modules/views/tests/modules/views_test_data/src/Plugin/views/filter/FilterBooleanOperatorDefaultTest.php @@ -0,0 +1,25 @@ +<?php + +namespace Drupal\views_test_data\Plugin\views\filter; + +use Drupal\views\Plugin\views\filter\BooleanOperator; + +/** + * Filter to test queryOpBoolean() with default operator. + * + * @ingroup views_filter_handlers + * + * @ViewsFilter("boolean_default") + */ +class FilterBooleanOperatorDefaultTest extends BooleanOperator { + + /** + * {@inheritdoc} + */ + public function query() { + $this->ensureMyTable(); + $field = "$this->tableAlias.$this->realField"; + $this->queryOpBoolean($field); + } + +} diff --git a/core/modules/views/tests/src/Kernel/Handler/FilterBooleanOperatorDefaultTest.php b/core/modules/views/tests/src/Kernel/Handler/FilterBooleanOperatorDefaultTest.php new file mode 100644 index 000000000000..7beceb9bd9ea --- /dev/null +++ b/core/modules/views/tests/src/Kernel/Handler/FilterBooleanOperatorDefaultTest.php @@ -0,0 +1,56 @@ +<?php + +namespace Drupal\Tests\views\Kernel\Handler; + +use Drupal\Tests\views\Kernel\ViewsKernelTestBase; +use Drupal\views\Views; + +/** + * Tests the queryOpBoolean() with default operator. + * + * @group views + * @see \Drupal\views\Plugin\views\filter\BooleanOperator + */ +class FilterBooleanOperatorDefaultTest extends ViewsKernelTestBase { + + /** + * {@inheritdoc} + */ + public static $modules = ['system', 'views_test_data']; + + /** + * {@inheritdoc} + */ + public static $testViews = ['test_view']; + + /** + * {@inheritdoc} + */ + protected function viewsData() { + $views_data = parent::viewsData(); + + $views_data['views_test_data']['status']['filter']['id'] = 'boolean_default'; + + return $views_data; + } + + /** + * Tests the queryOpBoolean() with default operator. + */ + public function testFilterBooleanOperatorDefault() { + $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); + $this->assertCount(2, $view->result); + } + +} -- GitLab