QueryTest.php

Same filename in this branch
  1. 11.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  2. 11.x core/modules/views_ui/tests/src/Functional/QueryTest.php
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  4. 11.x core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php
Same filename and directory in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  2. 9 core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  3. 9 core/modules/views_ui/tests/src/Functional/QueryTest.php
  4. 9 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  5. 9 core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php
  6. 8.9.x core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  7. 8.9.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  8. 8.9.x core/modules/views_ui/tests/src/Functional/QueryTest.php
  9. 8.9.x core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  10. 8.9.x core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php
  11. 10 core/modules/views/tests/src/Kernel/Plugin/QueryTest.php
  12. 10 core/modules/views/tests/modules/views_test_data/src/Plugin/views/query/QueryTest.php
  13. 10 core/modules/views_ui/tests/src/Functional/QueryTest.php
  14. 10 core/tests/Drupal/KernelTests/Core/Database/QueryTest.php
  15. 10 core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php

Namespace

Drupal\Tests\views\Kernel\Plugin

File

core/modules/views/tests/src/Kernel/Plugin/QueryTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\views\Kernel\Plugin;

use Drupal\views\Views;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views_test_data\Plugin\views\query\QueryTest as QueryTestPlugin;

/**
 * Tests query plugins.
 *
 * @group views
 */
class QueryTest extends ViewsKernelTestBase {
  
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = [
    'test_view',
  ];
  
  /**
   * Provides Views metadata for the test data table.
   */
  protected function viewsData() {
    $data = parent::viewsData();
    $data['views_test_data']['table']['base']['query_id'] = 'query_test';
    return $data;
  }
  
  /**
   * Tests query plugins.
   */
  public function testQuery() : void {
    $this->_testInitQuery();
    $this->_testQueryExecute();
    $this->queryMethodsTests();
  }
  
  /**
   * Tests the ViewExecutable::initQuery method.
   */
  public function _testInitQuery() : void {
    $view = Views::getView('test_view');
    $view->setDisplay();
    $view->initQuery();
    $this->assertInstanceOf(QueryTestPlugin::class, $view->query);
  }
  
  /**
   * Executes the views query, ensures it's not empty.
   */
  public function _testQueryExecute() : void {
    $view = Views::getView('test_view');
    $view->setDisplay();
    $view->initQuery();
    $view->query
      ->setAllItems($this->dataSet());
    $this->executeView($view);
    $this->assertNotEmpty($view->result, 'Make sure the view result got filled');
  }
  
  /**
   * Tests methods provided by the QueryPluginBase.
   *
   * @see \Drupal\views\Plugin\views\query\QueryPluginBase
   */
  protected function queryMethodsTests() : void {
    $view = Views::getView('test_view');
    $view->setDisplay();
    $view->initQuery();
    $this->assertNull($view->query
      ->getLimit(), 'Default to an empty limit.');
    $rand_number = rand(5, 10);
    $view->query
      ->setLimit($rand_number);
    $this->assertEquals($rand_number, $view->query
      ->getLimit(), 'set_limit adapts the amount of items.');
  }

}

Classes

Title Deprecated Summary
QueryTest Tests query plugins.

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