diff --git a/core/modules/views/src/Plugin/views/filter/BooleanOperator.php b/core/modules/views/src/Plugin/views/filter/BooleanOperator.php index 69b6ebac7ebe908bd0b561b9226979e0334e7727..917a30b848cb098ed09b958dc93a974322094f95 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 0000000000000000000000000000000000000000..33cb5f9790fd4b1cca0a5078b09a67a0ad716f3b --- /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 0000000000000000000000000000000000000000..7beceb9bd9eacdb9e5c87e9f26856f69e8222ad9 --- /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); + } + +}