You are here

function SelectComplexTest::testCountQueryRemovals in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectComplexTest.php \Drupal\system\Tests\Database\SelectComplexTest::testCountQueryRemovals()

Tests that countQuery removes 'all_fields' statements and ordering clauses.

File

core/modules/system/src/Tests/Database/SelectComplexTest.php, line 210
Contains \Drupal\system\Tests\Database\SelectComplexTest.

Class

SelectComplexTest
Tests the Select query builder with more complex queries.

Namespace

Drupal\system\Tests\Database

Code

function testCountQueryRemovals() {
  $query = db_select('test');
  $query
    ->fields('test');
  $query
    ->orderBy('name');
  $count = $query
    ->countQuery();

  // Check that the 'all_fields' statement is handled properly.
  $tables = $query
    ->getTables();
  $this
    ->assertEqual($tables['test']['all_fields'], 1, 'Query correctly sets \'all_fields\' statement.');
  $tables = $count
    ->getTables();
  $this
    ->assertFalse(isset($tables['test']['all_fields']), 'Count query correctly unsets \'all_fields\' statement.');

  // Check that the ordering clause is handled properly.
  $orderby = $query
    ->getOrderBy();

  // The orderby string is different for PostgreSQL.
  // @see Drupal\Core\Database\Driver\pgsql\Select::orderBy()
  $db_type = Database::getConnection()
    ->databaseType();
  $this
    ->assertEqual($orderby['name'], $db_type == 'pgsql' ? 'ASC NULLS FIRST' : 'ASC', 'Query correctly sets ordering clause.');
  $orderby = $count
    ->getOrderBy();
  $this
    ->assertFalse(isset($orderby['name']), 'Count query correctly unsets ordering clause.');

  // Make sure that the count query works.
  $count = $count
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 4, 'Counted the correct number of records.');
}