You are here

public function LoggingTest::testEnableLogging in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php \Drupal\KernelTests\Core\Database\LoggingTest::testEnableLogging()

Tests that we can log the existence of a query.

This test is only marked as legacy to be able to test the deprecated db_query function().

@group legacy

@expectedDeprecationMessage db_query() is deprecated in drupal:8.0.0. It will be removed before drupal:9.0.0. Instead, get a database connection injected into your service from the container and call query() on it. For example, $injected_database->query($query, $args, $options). See https://www.drupal.org/node/2993033

File

core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php, line 24

Class

LoggingTest
Tests the query logging facility.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testEnableLogging() {
  Database::startLog('testing');
  $this->connection
    ->query('SELECT name FROM {test} WHERE age > :age', [
    ':age' => 25,
  ])
    ->fetchCol();
  $this->connection
    ->query('SELECT age FROM {test} WHERE name = :name', [
    ':name' => 'Ringo',
  ])
    ->fetchCol();

  // Trigger a call that does not have file in the backtrace.
  call_user_func_array([
    Database::getConnection(),
    'query',
  ], [
    'SELECT age FROM {test} WHERE name = :name',
    [
      ':name' => 'Ringo',
    ],
  ])
    ->fetchCol();

  // Make sure that the caller is also detected correctly for the deprecated
  // db_query() function.
  db_query('SELECT name FROM {test} WHERE age > :age', [
    ':age' => 25,
  ])
    ->fetchCol();
  $queries = Database::getLog('testing', 'default');
  $this
    ->assertCount(4, $queries, 'Correct number of queries recorded.');
  foreach ($queries as $query) {
    $this
      ->assertEqual($query['caller']['function'], __FUNCTION__, 'Correct function in query log.');
  }
}