Verified Commit f78530b4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2825860 by xurizaemon, Matroskeen, dewalt, neclimdul, joseph.olstad,...

Issue #2825860 by xurizaemon, Matroskeen, dewalt, neclimdul, joseph.olstad, ericgsmith, dpolant, hswong3i, mstrelan, aby v a, Akram Khan, Ratan Priya, robphillips, dpi, ryankavalsky, vetal4ik, jwilson3, jagermonster, Rob230, thomjjames, rajneeshb, Hygglo, CodigoVision, kndr, acbramley, Lendude, smustgrave, ExTexan, alexpott: Notice: Undefined index: value in Drupal\views\Plugin\views\filter\NumericFilter->acceptExposedInput()

(cherry picked from commit ba08bc0d)
parent ca84e6d5
Loading
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -2662,11 +2662,6 @@
	'count' => 2,
	'path' => __DIR__ . '/modules/views/src/Plugin/views/filter/NumericFilter.php',
];
$ignoreErrors[] = [
	'message' => '#^Variable \\$value might not be defined\\.$#',
	'count' => 2,
	'path' => __DIR__ . '/modules/views/src/Plugin/views/filter/NumericFilter.php',
];
$ignoreErrors[] = [
	'message' => '#^Variable \\$source might not be defined\\.$#',
	'count' => 1,
+20 −7
Original line number Diff line number Diff line
@@ -122,12 +122,12 @@ protected function setUp($import_test_views = TRUE, $modules = ['views_test_conf
   * Tests exposed grouped filters.
   */
  public function testExposedGroupedFilters() {
    // Expose the empty and not empty operators in a grouped filter.
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $this->fieldName . '_value');
    $filter_identifier = $this->fieldName . '_value';
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $filter_identifier);
    $this->submitForm([], 'Expose filter');
    $this->submitForm([], 'Grouped filters');

    // Test operators with different amount of expected values.
    // Create groups with different amount of expected values.
    $edit = [];
    // No values are required.
    $edit['options[group_info][group_items][1][title]'] = 'empty';
@@ -160,24 +160,37 @@ public function testExposedGroupedFilters() {
    $this->drupalGet($path);

    // Filter the Preview by 'empty'.
    $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption('1');
    $this->getSession()->getPage()->findField($filter_identifier)->selectOption('1');
    $this->getSession()->getPage()->pressButton('Apply');
    $this->assertIds([4]);

    // Filter the Preview by 'not empty'.
    $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption('2');
    $this->getSession()->getPage()->findField($filter_identifier)->selectOption('2');
    $this->getSession()->getPage()->pressButton('Apply');
    $this->assertIds([1, 2, 3]);

    // Filter the Preview by 'less than'.
    $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption('3');
    $this->getSession()->getPage()->findField($filter_identifier)->selectOption('3');
    $this->getSession()->getPage()->pressButton('Apply');
    $this->assertIds([2, 3]);

    // Filter the Preview by 'between'.
    $this->getSession()->getPage()->findField($this->fieldName . '_value')->selectOption('4');
    $this->getSession()->getPage()->findField($filter_identifier)->selectOption('4');
    $this->getSession()->getPage()->pressButton('Apply');
    $this->assertIds([2]);

    // Change the identifier for grouped exposed filter.
    $this->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $filter_identifier);
    $filter_identifier = 'date';
    $edit['options[group_info][identifier]'] = $filter_identifier;
    $this->submitForm($edit, 'Apply');
    $this->submitForm([], 'Save');

    // Filter results again using a new filter identifier.
    $this->drupalGet($path);
    $this->getSession()->getPage()->findField($filter_identifier)->selectOption('2');
    $this->getSession()->getPage()->pressButton('Apply');
    $this->assertIds([1, 2, 3]);
  }

  /**
+11 −8
Original line number Diff line number Diff line
@@ -420,16 +420,19 @@ public function acceptExposedInput($input) {
      return TRUE;
    }

    // rewrite the input value so that it's in the correct format so that
    // Rewrite the input value so that it's in the correct format so that
    // the parent gets the right data.
    if (!empty($this->options['expose']['identifier'])) {
      $value = &$input[$this->options['expose']['identifier']];
    $key = $this->isAGroup() ? 'group_info' : 'expose';
    if (empty($this->options[$key]['identifier'])) {
      // Invalid identifier configuration. Value can't be resolved.
      return FALSE;
    }
    $value = &$input[$this->options[$key]['identifier']];
    if (!is_array($value)) {
      $value = [
        'value' => $value,
      ];
    }
    }

    $rc = parent::acceptExposedInput($input);

+118 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\views\Unit\Plugin\views\filter;

use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\NumericFilter;
use Drupal\views\ViewExecutable;

/**
 * @coversDefaultClass \Drupal\views\Plugin\views\filter\NumericFilter
 * @group Views
 */
class NumericFilterTest extends UnitTestCase {

  /**
   * Tests the acceptExposedInput method.
   *
   * @covers ::acceptExposedInput
   * @dataProvider provideAcceptExposedInput
   */
  public function testAcceptExposedInput($options, $value, $expected): void {
    $plugin_definition = [
      'title' => $this->randomMachineName(),
    ];

    $plugin = new NumericFilter([], 'numeric', $plugin_definition);
    $translation_stub = $this->getStringTranslationStub();
    $plugin->setStringTranslation($translation_stub);

    $view = $this->prophesize(ViewExecutable::class)->reveal();
    $display = $this->prophesize(DisplayPluginBase::class)->reveal();
    $view->display_handler = $display;
    $plugin->init($view, $view->display_handler, $options);

    $this->assertSame($expected, $plugin->acceptExposedInput($value));
  }

  /**
   * Data provider for testAcceptExposedInput test.
   *
   * @return array[]
   *   The test cases.
   */
  public function provideAcceptExposedInput(): array {
    // [$options, $value, $expected]
    return [
      // Not exposed by default. Bypass parsing and return true.
      'defaults' => [[], [], TRUE],
      'exposed but not configured' => [
        [
          'exposed' => TRUE,
          'expose' => [],
          'group_info' => [],
        ],
        [],
        FALSE,
      ],
      // Exposed but not grouped.
      'exposed not grouped - missing value' => [
        [
          'exposed' => TRUE,
          'expose' => ['identifier' => 'test_id'],
        ],
        [],
        TRUE,
      ],
      'exposed not grouped - wrong group config' => [
        [
          'exposed' => TRUE,
          'group_info' => ['identifier' => 'test_id'],
        ],
        ['test_id' => ['value' => 1]],
        // Wrong identifier configured.
        FALSE,
      ],
      'exposed not grouped' => [
        [
          'exposed' => TRUE,
          'expose' => ['identifier' => 'test_id'],
        ],
        ['test_id' => ['value' => 1]],
        TRUE,
      ],
      // Exposed and grouped.
      'exposed grouped - missing value' => [
        [
          'exposed' => TRUE,
          'is_grouped' => TRUE,
          'group_info' => ['identifier' => 'test_id'],
        ],
        [],
        TRUE,
      ],
      'exposed grouped - wrong group config' => [
        [
          'exposed' => TRUE,
          'is_grouped' => TRUE,
          'expose' => ['identifier' => 'test_id'],
        ],
        ['test_id' => ['value' => 1]],
        FALSE,
      ],
      'exposed grouped' => [
        [
          'exposed' => TRUE,
          'is_grouped' => TRUE,
          'group_info' => ['identifier' => 'test_id'],
        ],
        ['test_id' => ['value' => 1]],
        TRUE,
      ],
    ];
  }

}