Verified Commit 4516d625 authored by Dave Long's avatar Dave Long
Browse files

Issue #2853188 by BramDriesen, aerozeppelin, SurfinSpirit, quietone,...

Issue #2853188 by BramDriesen, aerozeppelin, SurfinSpirit, quietone, Munavijayalakshmi, enyug, Anas_maw, dww, dawehner, muaz91, Lendude, smustgrave, alexpott: Add negated regular expressions for views filters (string and integer)
parent b22449b5
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -145,6 +145,12 @@ public function operators() {
        'method' => 'opRegex',
        'values' => 1,
      ],
      'not_regular_expression' => [
        'title' => $this->t('Negated regular expression'),
        'short' => $this->t('not regex'),
        'method' => 'opNotRegex',
        'values' => 1,
      ],
    ];

    // if the definition allows for the empty operator, add it.
@@ -377,6 +383,16 @@ protected function opRegex($field) {
    $this->query->addWhere($this->options['group'], $field, $this->value['value'], 'REGEXP');
  }

  /**
   * Filters by a negated regular expression.
   *
   * @param string $field
   *   The expression pointing to the queries field, for example "foo.bar".
   */
  protected function opNotRegex($field) {
    $this->query->addWhere($this->options['group'], $field, $this->value['value'], 'NOT REGEXP');
  }

  public function adminSummary() {
    if ($this->isAGroup()) {
      return $this->t('grouped');
+16 −0
Original line number Diff line number Diff line
@@ -183,6 +183,12 @@ public function operators() {
        'method' => 'opRegex',
        'values' => 1,
      ],
      'not_regular_expression' => [
        'title' => $this->t('Negated regular expression'),
        'short' => $this->t('not regex'),
        'method' => 'opNotRegex',
        'values' => 1,
      ],
    ];
    // if the definition allows for the empty operator, add it.
    if (!empty($this->definition['allow empty'])) {
@@ -442,6 +448,16 @@ protected function opRegex($field) {
    $this->query->addWhere($this->options['group'], $field, $this->value, 'REGEXP');
  }

  /**
   * Filters by a negated regular expression.
   *
   * @param string $field
   *   The expression pointing to the queries field, for example "foo.bar".
   */
  protected function opNotRegex($field) {
    $this->query->addWhere($this->options['group'], $field, $this->value, 'NOT REGEXP');
  }

  protected function opEmpty($field) {
    if ($this->operator == 'empty') {
      $operator = "IS NULL";
+85 −0
Original line number Diff line number Diff line
@@ -274,6 +274,49 @@ public function testFilterNumericRegularExpression() {
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  /**
   * Tests the numeric filter with negated 'regular_expression' operator.
   */
  public function testFilterNumericNotRegularExpression() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    // Filtering by regular expression pattern.
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'age' => [
        'id' => 'age',
        'table' => 'views_test_data',
        'field' => 'age',
        'relationship' => 'none',
        'operator' => 'not_regular_expression',
        'value' => [
          'value' => '2[8]',
        ],
      ],
    ]);

    $this->executeView($view);
    $resultset = [
      [
        'name' => 'John',
        'age' => 25,
      ],
      [
        'name' => 'George',
        'age' => 27,
      ],
      [
        'name' => 'Paul',
        'age' => 26,
      ],
      [
        'name' => 'Meredith',
        'age' => 30,
      ],
    ];
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  /**
   * Tests the "numeric" filter with grouped exposed filters.
   *
@@ -304,6 +347,41 @@ public function testFilterNumericExposedGroupedRegularExpression() {
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  /**
   * Tests the numeric filter with grouped exposed filters.
   *
   * Tests the numeric filter handler with the 'not_regular_expression' operator
   * to grouped exposed filters.
   */
  public function testFilterNumericExposedGroupedNotRegularExpression() {
    $filters = $this->getGroupedExposedFilters();
    $view = Views::getView('test_view');
    $view->newDisplay('page', 'Page', 'page_1');

    // Filter: Age, Operator: not_regular_expression, Value: 2[7-8]
    $filters['age']['group_info']['default_group'] = 7;
    $view->setDisplay('page_1');
    $view->displayHandlers->get('page_1')->overrideOption('filters', $filters);
    $view->save();

    $this->executeView($view);
    $resultset = [
      [
        'name' => 'John',
        'age' => 25,
      ],
      [
        'name' => 'Paul',
        'age' => 26,
      ],
      [
        'name' => 'Meredith',
        'age' => 30,
      ],
    ];
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  public function testFilterNumericEmpty() {
    $view = Views::getView('test_view');
    $view->setDisplay();
@@ -502,6 +580,13 @@ protected function getGroupedExposedFilters() {
                'value' => '2[7-8]',
              ],
            ],
            7 => [
              'title' => 'Age is regexp 2[7-8]',
              'operator' => 'not_regular_expression',
              'value' => [
                'value' => '2[7-8]',
              ],
            ],
          ],
        ],
      ],
+81 −0
Original line number Diff line number Diff line
@@ -415,6 +415,39 @@ public function testFilterStringGroupedExposedStarts() {
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  /**
   * Tests the string filter with negated 'regular_expression' operator.
   */
  public function testFilterStringGroupedNotRegularExpression() {
    $filters = $this->getGroupedExposedFilters();
    $view = $this->getBasicPageView();

    // Filter: Name, Operator: not_regular_expression, Value: ^Rin
    $filters['name']['group_info']['default_group'] = 6;
    $view->setDisplay('page_1');
    $view->displayHandlers->get('page_1')->overrideOption('filters', $filters);
    $view->save();
    $this->container->get('router.builder')->rebuild();

    $this->executeView($view);

    $resultset = [
      [
        'name' => 'John',
      ],
      [
        'name' => 'George',
      ],
      [
        'name' => 'Paul',
      ],
      [
        'name' => 'Meredith',
      ],
    ];
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  public function testFilterStringNotStarts() {
    $view = Views::getView('test_view');
    $view->setDisplay();
@@ -769,6 +802,49 @@ public function testFilterStringGroupedExposedEmpty() {
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  /**
   * Tests the string filter handler with the negated 'regular_expression' operator.
   */
  public function testFilterStringNotRegularExpression() {
    $view = Views::getView('test_view');
    $view->setDisplay();

    // Filtering by regular expression pattern.
    $view->displayHandlers->get('default')->overrideOption('filters', [
      'age' => [
        'id' => 'name',
        'table' => 'views_test_data',
        'field' => 'name',
        'relationship' => 'none',
        'operator' => 'not_regular_expression',
        'value' => [
          'value' => '^Rin',
        ],
      ],
    ]);

    $this->executeView($view);
    $resultset = [
      [
        'name' => 'John',
        'age' => 25,
      ],
      [
        'name' => 'George',
        'age' => 27,
      ],
      [
        'name' => 'Paul',
        'age' => 26,
      ],
      [
        'name' => 'Meredith',
        'age' => 30,
      ],
    ];
    $this->assertIdenticalResultset($view, $resultset, $this->columnMap);
  }

  protected function getGroupedExposedFilters() {
    $filters = [
      'name' => [
@@ -814,6 +890,11 @@ protected function getGroupedExposedFilters() {
              'operator' => 'longerthan',
              'value' => 7,
            ],
            6 => [
              'title' => 'Does not start with Rin',
              'operator' => 'not_regular_expression',
              'value' => '^Rin',
            ],
          ],
        ],
      ],