You are here

public function EntityQueryTest::testToString in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testToString()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testToString()

Tests __toString().

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 1307

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testToString() {
  $query = $this->storage
    ->getQuery()
    ->accessCheck(FALSE);
  $group_blue = $query
    ->andConditionGroup()
    ->condition("{$this->figures}.color", [
    'blue',
  ], 'IN');
  $group_red = $query
    ->andConditionGroup()
    ->condition("{$this->figures}.color", [
    'red',
  ], 'IN');
  $null_group = $query
    ->andConditionGroup()
    ->notExists("{$this->figures}.color");
  $this->queryResults = $query
    ->condition($group_blue)
    ->condition($group_red)
    ->condition($null_group)
    ->sort('id');
  $figures = $this->figures;

  // Matching the SQL statement against an hardcoded statement leads to
  // failures with database drivers that override the
  // Drupal\Core\Database\Query\Select class. We build a dynamic query via
  // the db API to check that its SQL matches the one generated by the
  // EntityQuery. This way we ensure that the database driver is free to
  // create its own comparable SQL statement.
  $connection = Database::getConnection();
  $expected = $connection
    ->select("entity_test_mulrev", "base_table");
  $expected
    ->addField("base_table", "revision_id", "revision_id");
  $expected
    ->addField("base_table", "id", "id");
  $expected
    ->join("entity_test_mulrev__{$figures}", "entity_test_mulrev__{$figures}", '[entity_test_mulrev__' . $figures . '].[entity_id] = [base_table].[id]');
  $expected
    ->join("entity_test_mulrev__{$figures}", "entity_test_mulrev__{$figures}_2", '[entity_test_mulrev__' . $figures . '_2].[entity_id] = [base_table].[id]');
  $expected
    ->addJoin("LEFT", "entity_test_mulrev__{$figures}", "entity_test_mulrev__{$figures}_3", '[entity_test_mulrev__' . $figures . '_3].[entity_id] = [base_table].[id]');
  $expected
    ->condition("entity_test_mulrev__{$figures}.{$figures}_color", [
    "blue",
  ], "IN");
  $expected
    ->condition("entity_test_mulrev__{$figures}_2.{$figures}_color", [
    "red",
  ], "IN");
  $expected
    ->isNull("entity_test_mulrev__{$figures}_3.{$figures}_color");
  $expected
    ->orderBy("base_table.id");

  // Apply table prefixes and quote identifiers for the expected SQL.
  $expected_string = $connection
    ->prefixTables((string) $expected);
  $expected_string = $connection
    ->quoteIdentifiers($expected_string);

  // Resolve placeholders in the expected SQL to their values.
  $quoted = [];
  foreach ($expected
    ->getArguments() as $key => $value) {
    $quoted[$key] = $connection
      ->quote($value);
  }
  $expected_string = strtr($expected_string, $quoted);
  $this
    ->assertSame($expected_string, (string) $query);
}