You are here

public function FilterTest::testFilterQuery in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Functional/Plugin/FilterTest.php \Drupal\Tests\views\Functional\Plugin\FilterTest::testFilterQuery()
  2. 10 core/modules/views/tests/src/Functional/Plugin/FilterTest.php \Drupal\Tests\views\Functional\Plugin\FilterTest::testFilterQuery()

Tests query of the row plugin.

File

core/modules/views/tests/src/Functional/Plugin/FilterTest.php, line 61

Class

FilterTest
Tests general filter plugin functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testFilterQuery() {

  // Check that we can find the test filter plugin.
  $plugin = $this->container
    ->get('plugin.manager.views.filter')
    ->createInstance('test_filter');
  $this
    ->assertInstanceOf(FilterPlugin::class, $plugin);
  $view = Views::getView('test_filter');
  $view
    ->initDisplay();

  // Change the filtering.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'test_filter' => [
      'id' => 'test_filter',
      'table' => 'views_test_data',
      'field' => 'name',
      'operator' => '=',
      'value' => 'John',
      'group' => 0,
    ],
  ]);
  $this
    ->executeView($view);

  // Make sure the query have where data.
  $this
    ->assertTrue(!empty($view->query->where));

  // Check the data added.
  $where = $view->query->where;
  $this
    ->assertSame('views_test_data.name', $where[0]['conditions'][0]['field'], 'Where condition field matches');
  $this
    ->assertSame('John', $where[0]['conditions'][0]['value'], 'Where condition value matches');
  $this
    ->assertSame('=', $where[0]['conditions'][0]['operator'], 'Where condition operator matches');
  $this
    ->executeView($view);

  // Check that our operator and value match on the filter.
  $this
    ->assertSame('=', $view->filter['test_filter']->operator);
  $this
    ->assertSame('John', $view->filter['test_filter']->value);

  // Check that we have a single element, as a result of applying the '= John'
  // filter.
  $this
    ->assertCount(1, $view->result, new FormattableMarkup('Results were returned. @count results.', [
    '@count' => count($view->result),
  ]));
  $view
    ->destroy();
  $view
    ->initDisplay();

  // Change the filtering.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'test_filter' => [
      'id' => 'test_filter',
      'table' => 'views_test_data',
      'field' => 'name',
      'operator' => '<>',
      'value' => 'John',
      'group' => 0,
    ],
  ]);
  $this
    ->executeView($view);

  // Check that our operator and value match on the filter.
  $this
    ->assertSame('<>', $view->filter['test_filter']->operator);
  $this
    ->assertSame('John', $view->filter['test_filter']->value);

  // Check if we have the other elements in the dataset, as a result of
  // applying the '<> John' filter.
  $this
    ->assertCount(4, $view->result, new FormattableMarkup('Results were returned. @count results.', [
    '@count' => count($view->result),
  ]));
  $view
    ->destroy();
  $view
    ->initDisplay();

  // Set the test_enable option to FALSE. The 'where' clause should not be
  // added to the query.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'test_filter' => [
      'id' => 'test_filter',
      'table' => 'views_test_data',
      'field' => 'name',
      'operator' => '<>',
      'value' => 'John',
      'group' => 0,
      // Disable this option, so nothing should be added to the query.
      'test_enable' => FALSE,
    ],
  ]);

  // Execute the view again.
  $this
    ->executeView($view);

  // Check if we have all 5 results.
  $this
    ->assertCount(5, $view->result, new FormattableMarkup('All @count results returned', [
    '@count' => count($view->displayHandlers),
  ]));
}