You are here

public function ConnectionTest::testPostgresqlReservedWords in Zircon Profile 8

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

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

File

core/modules/system/src/Tests/Database/ConnectionTest.php, line 164
Contains \Drupal\system\Tests\Database\ConnectionTest.

Class

ConnectionTest
Tests of the core database system.

Namespace

Drupal\system\Tests\Database

Code

public function testPostgresqlReservedWords() {
  if (Database::getConnection()
    ->databaseType() !== 'pgsql') {
    return;
  }
  $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, format_string('The reserved word %word was correctly escaped when used as a table name.', array(
      '%word' => $word,
    )));
    $this
      ->assertIdentical($db
      ->escapeField($word), $expected, format_string('The reserved word %word was correctly escaped when used as a column name.', array(
      '%word' => $word,
    )));
    $this
      ->assertIdentical($db
      ->escapeAlias($word), $expected, format_string('The reserved word %word was correctly escaped when used as an alias.', array(
      '%word' => $word,
    )));
  }
}