Unverified Commit deaf1321 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3098426 by mondrake, daffie: EntityQueryTest::testToString fails with non-core db drivers

parent a9438358
Loading
Loading
Loading
Loading
+29 −16
Changes for core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php: 29 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\KernelTests\Core\Entity;

use Drupal\Core\Database\Database;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\field\Entity\FieldConfig;
@@ -1217,22 +1218,34 @@ public function testToString() {

    $figures = $this->figures;

    $expected = <<<EOF
SELECT base_table.revision_id AS revision_id, base_table.id AS id
FROM
{entity_test_mulrev} base_table
INNER JOIN {entity_test_mulrev__{$figures}} entity_test_mulrev__$figures ON entity_test_mulrev__$figures.entity_id = base_table.id
INNER JOIN {entity_test_mulrev__{$figures}} entity_test_mulrev__{$figures}_2 ON entity_test_mulrev__{$figures}_2.entity_id = base_table.id
LEFT JOIN {entity_test_mulrev__{$figures}} entity_test_mulrev__{$figures}_3 ON entity_test_mulrev__{$figures}_3.entity_id = base_table.id
WHERE (entity_test_mulrev__{$figures}.{$figures}_color IN ('blue')) AND (entity_test_mulrev__{$figures}_2.{$figures}_color IN ('red')) AND (entity_test_mulrev__{$figures}_3.{$figures}_color IS NULL)
ORDER BY base_table.id ASC
EOF;

    // Apply table prefixes to the expected sql.
    $expected = \Drupal::database()->prefixTables($expected);
    // PostgreSQL adds " NULLS FIRST" to the end of the query so
    // assertContains() is used for maximum compatibility.
    $this->assertContains($expected, (string) $query);
    // 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 to the expected SQL.
    $expected_string = \Drupal::database()->prefixTables((string) $expected);
    // 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);
  }

}