You are here

public function ConnectionTest::testPostgresqlReservedWords in Drupal 8

Test the escapeTable(), escapeField() and escapeAlias() methods with all possible reserved words in PostgreSQL.

File

core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php, line 147

Class

ConnectionTest
Tests of the core database system.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testPostgresqlReservedWords() {
  if (Database::getConnection()
    ->databaseType() !== 'pgsql') {
    $this
      ->markTestSkipped("This test only runs for PostgreSQL");
  }
  $db = Database::getConnection('default', 'default');
  $stmt = $db
    ->query("SELECT word FROM pg_get_keywords() WHERE catcode IN ('R', 'T')");
  $stmt
    ->execute();
  foreach ($stmt
    ->fetchAllAssoc('word') as $word => $row) {
    $expected = '"' . $word . '"';
    $this
      ->assertIdentical($db
      ->escapeTable($word), $expected, new FormattableMarkup('The reserved word %word was correctly escaped when used as a table name.', [
      '%word' => $word,
    ]));
    $this
      ->assertIdentical($db
      ->escapeField($word), $expected, new FormattableMarkup('The reserved word %word was correctly escaped when used as a column name.', [
      '%word' => $word,
    ]));
    $this
      ->assertIdentical($db
      ->escapeAlias($word), $expected, new FormattableMarkup('The reserved word %word was correctly escaped when used as an alias.', [
      '%word' => $word,
    ]));
  }
}