diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/filter/BooleanOperatorString.php b/core/modules/views/lib/Drupal/views/Plugin/views/filter/BooleanOperatorString.php index b92bcbf000e..5f3537dee45 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/filter/BooleanOperatorString.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/filter/BooleanOperatorString.php @@ -37,7 +37,7 @@ class BooleanOperatorString extends BooleanOperator { else { $where .= "<> ''"; } - $this->query->addWhere($this->options['group'], $where); + $this->query->addWhereExpression($this->options['group'], $where); } } diff --git a/core/modules/views/lib/Drupal/views/Tests/Handler/FilterBooleanOperatorStringTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterBooleanOperatorStringTest.php new file mode 100644 index 00000000000..d3c386242c7 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FilterBooleanOperatorStringTest.php @@ -0,0 +1,232 @@ + 'id', + ); + + public static function getInfo() { + return array( + 'name' => 'Filter: Boolean string operator', + 'description' => 'Test the core Drupal\views\Plugin\views\filter\BooleanOperatorString handler.', + 'group' => 'Views Handlers', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->installSchema('system', array('menu_router', 'variable', 'key_value_expire')); + } + + + /** + * {@inheritdoc} + */ + protected function schemaDefinition() { + $schema = parent::schemaDefinition(); + + $schema['views_test_data']['fields']['status'] = array( + 'description' => 'The status of this record', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '', + ); + + return $schema; + } + + /** + * {@inheritdoc} + */ + protected function viewsData() { + $views_data = parent::viewsData(); + + $views_data['views_test_data']['status']['filter']['id'] = 'boolean_string'; + + return $views_data; + } + + /** + * {@inheritdoc} + */ + protected function dataSet() { + $data = parent::dataSet(); + + foreach ($data as &$row) { + if ($row['status']) { + $row['status'] = 'Enabled'; + } + else { + $row['status'] = ''; + } + } + + return $data; + } + + /** + * Tests the BooleanOperatorString filter. + */ + public function testFilterBooleanOperatorString() { + $view = Views::getView('test_view'); + $view->setDisplay(); + + // Add a the status boolean filter. + $view->displayHandlers->get('default')->overrideOption('filters', array( + 'status' => array( + 'id' => 'status', + 'field' => 'status', + 'table' => 'views_test_data', + 'value' => 0, + ), + )); + $this->executeView($view); + + $expected_result = array( + array('id' => 2), + array('id' => 4), + ); + + $this->assertEqual(2, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, $this->column_map); + + $view->destroy(); + $view->setDisplay(); + + // Add the status boolean filter. + $view->displayHandlers->get('default')->overrideOption('filters', array( + 'status' => array( + 'id' => 'status', + 'field' => 'status', + 'table' => 'views_test_data', + 'value' => 1, + ), + )); + $this->executeView($view); + + $expected_result = array( + array('id' => 1), + array('id' => 3), + array('id' => 5), + ); + + $this->assertEqual(3, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, $this->column_map); + } + + /** + * Tests the Boolean filter with grouped exposed form enabled. + */ + public function testFilterGroupedExposed() { + $filters = $this->getGroupedExposedFilters(); + $view = Views::getView('test_view'); + + $view->setExposedInput(array('status' => 1)); + $view->setDisplay(); + $view->displayHandlers->get('default')->overrideOption('filters', $filters); + + $this->executeView($view); + + $expected_result = array( + array('id' => 1), + array('id' => 3), + array('id' => 5), + ); + + $this->assertEqual(3, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, $this->column_map); + $view->destroy(); + + $view->setExposedInput(array('status' => 2)); + $view->setDisplay(); + $view->displayHandlers->get('default')->overrideOption('filters', $filters); + + $this->executeView($view); + + $expected_result = array( + array('id' => 2), + array('id' => 4), + ); + + $this->assertEqual(2, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, $this->column_map); + } + + /** + * Provides grouped exposed filter configuration. + * + * @return array + * Returns the filter configuration for exposed filters. + */ + protected function getGroupedExposedFilters() { + $filters = array( + 'status' => array( + 'id' => 'status', + 'table' => 'views_test_data', + 'field' => 'status', + 'relationship' => 'none', + 'exposed' => TRUE, + 'expose' => array( + 'operator' => 'status_op', + 'label' => 'status', + 'identifier' => 'status', + ), + 'is_grouped' => TRUE, + 'group_info' => array( + 'label' => 'status', + 'identifier' => 'status', + 'default_group' => 'All', + 'group_items' => array( + 1 => array( + 'title' => 'Active', + 'operator' => '=', + 'value' => '1', + ), + 2 => array( + 'title' => 'Blocked', + 'operator' => '=', + 'value' => '0', + ), + ), + ), + ), + ); + return $filters; + } + +} diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php b/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php index 5a00bd7081a..2f1b2ab09f3 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\views\Tests; +use Drupal\Core\Database\Query\SelectInterface; use Drupal\simpletest\WebTestBase; use Drupal\views\ViewExecutable; use Drupal\block\Plugin\Core\Entity\Block; @@ -228,7 +229,11 @@ abstract class ViewTestBase extends WebTestBase { $view->setDisplay(); $view->preExecute($args); $view->execute(); - $this->verbose('
Executed view: ' . ((string) $view->build_info['query']) . '
'); + $verbose_message = '
Executed view: ' . ((string) $view->build_info['query']). '
'; + if ($view->build_info['query'] instanceof SelectInterface) { + $verbose_message .= '
Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '
'; + } + $this->verbose($verbose_message); } /** diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php index 1213cc0c74a..ab8173afb4d 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewUnitTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\views\Tests; +use Drupal\Core\Database\Query\SelectInterface; use Drupal\views\ViewExecutable; use Drupal\views\ViewsBundle; use Drupal\simpletest\DrupalUnitTestBase; @@ -222,7 +223,11 @@ abstract class ViewUnitTestBase extends DrupalUnitTestBase { $view->setDisplay(); $view->preExecute($args); $view->execute(); - $this->verbose('
Executed view: ' . ((string) $view->build_info['query']) . '
'); + $verbose_message = '
Executed view: ' . ((string) $view->build_info['query']). '
'; + if ($view->build_info['query'] instanceof SelectInterface) { + $verbose_message .= '
Arguments: ' . print_r($view->build_info['query']->getArguments(), TRUE) . '
'; + } + $this->verbose($verbose_message); } /**