FilterBooleanOperatorTest.php 5.4 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Kernel\Handler;
4

5
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
6
use Drupal\views\Views;
7 8

/**
9
 * Tests the core Drupal\views\Plugin\views\filter\BooleanOperator handler.
10
 *
11
 * @group views
12 13
 * @see \Drupal\views\Plugin\views\filter\BooleanOperator
 */
14
class FilterBooleanOperatorTest extends ViewsKernelTestBase {
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

  /**
   * The modules to enable for this test.
   *
   * @var array
   */
  public static $modules = array('system');

  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view');

30 31 32 33 34 35
  /**
   * Map column names.
   *
   * @var array
   */
  protected $columnMap = array(
36 37 38 39 40 41 42
    'views_test_data_id' => 'id',
  );

  /**
   * Tests the BooleanOperator filter.
   */
  public function testFilterBooleanOperator() {
43
    $view = Views::getView('test_view');
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    $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));
63
    $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

    $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));
86
    $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

    $view->destroy();
    $view->setDisplay();

    // Testing the same scenario but using the reverse status and operation.
    $view->displayHandlers->get('default')->overrideOption('filters', array(
      'status' => array(
        'id' => 'status',
        'field' => 'status',
        'table' => 'views_test_data',
        'value' => 0,
        'operator' => '!=',
      ),
    ));
    $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->columnMap);
111 112
  }

113 114 115 116 117
  /**
   * Tests the boolean filter with grouped exposed form enabled.
   */
  public function testFilterGroupedExposed() {
    $filters = $this->getGroupedExposedFilters();
118
    $view = Views::getView('test_view');
119 120 121 122 123 124 125 126 127 128 129 130 131 132

    $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));
133
    $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
134 135 136 137 138 139 140 141 142 143 144 145 146 147
    $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));
148
    $this->assertIdenticalResultset($view, $expected_result, $this->columnMap);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

    $view->destroy();

    // Expecting the same results as for ['status' => 1].
    $view->setExposedInput(['status' => 3]);
    $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->columnMap);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  }

  /**
   * Provides grouped exposed filter configuration.
   *
   * @return array
   */
  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',
            ),
203 204 205 206 207 208 209
            // This group should return the same results as group 1, because it
            // is the negation of group 2.
            3 => array(
              'title' => 'Active (reverse)',
              'operator' => '!=',
              'value' => '0',
            ),
210 211 212 213 214 215 216
          ),
        ),
      ),
    );
    return $filters;
  }

217
}