function FilterTest::testFilterQuery

Same name and namespace in other branches
  1. 9 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()
  3. 11.x core/modules/views/tests/src/Functional/Plugin/FilterTest.php \Drupal\Tests\views\Functional\Plugin\FilterTest::testFilterQuery()

Test 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->assertIdentical($where[0]['conditions'][0]['field'], 'views_test_data.name', 'Where condition field matches');
    $this->assertIdentical($where[0]['conditions'][0]['value'], 'John', 'Where condition value matches');
    $this->assertIdentical($where[0]['conditions'][0]['operator'], '=', 'Where condition operator matches');
    $this->executeView($view);
    // Check that our operator and value match on the filter.
    $this->assertIdentical($view->filter['test_filter']->operator, '=');
    $this->assertIdentical($view->filter['test_filter']->value, 'John');
    // 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->assertIdentical($view->filter['test_filter']->operator, '<>');
    $this->assertIdentical($view->filter['test_filter']->value, 'John');
    // 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),
    ]));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.