You are here

public function DevelQueryDebugTest::testSelectQueryDebugTag in Devel 8.3

Same name and namespace in other branches
  1. 8 tests/src/Kernel/DevelQueryDebugTest.php \Drupal\Tests\devel\Kernel\DevelQueryDebugTest::testSelectQueryDebugTag()
  2. 8.2 tests/src/Kernel/DevelQueryDebugTest.php \Drupal\Tests\devel\Kernel\DevelQueryDebugTest::testSelectQueryDebugTag()
  3. 4.x tests/src/Kernel/DevelQueryDebugTest.php \Drupal\Tests\devel\Kernel\DevelQueryDebugTest::testSelectQueryDebugTag()

Tests devel_query_debug_alter() for select queries.

File

tests/src/Kernel/DevelQueryDebugTest.php, line 57

Class

DevelQueryDebugTest
Tests query debug.

Namespace

Drupal\Tests\devel\Kernel

Code

public function testSelectQueryDebugTag() {

  // Clear the messages stack.
  $this
    ->getDrupalMessages();

  // Ensures that no debug message is shown to user without the adequate
  // permissions.
  $query = \Drupal::database()
    ->select('users', 'u');
  $query
    ->fields('u', [
    'uid',
  ]);
  $query
    ->addTag('debug');
  $query
    ->execute();
  $messages = $this
    ->getDrupalMessages();
  $this
    ->assertEmpty($messages);

  // Ensures that the SQL debug message is shown to user with the adequate
  // permissions. We expect only one status message containing the SQL for
  // the debugged query.
  \Drupal::currentUser()
    ->setAccount($this->develUser);
  $expected_message = "SELECT u.uid AS uid\nFROM\n{users} u";
  $query = \Drupal::database()
    ->select('users', 'u');
  $query
    ->fields('u', [
    'uid',
  ]);
  $query
    ->addTag('debug');
  $query
    ->execute();
  $messages = $this
    ->getDrupalMessages();
  $this
    ->assertNotEmpty($messages['status']);
  $this
    ->assertCount(1, $messages['status']);
  $actual_message = strip_tags($messages['status'][0]);

  // In Drupal 9 the literals are quoted, but not in Drupal 8. We only need
  // the actual content, so remove all quotes from the actual message found.
  $actual_message = str_replace([
    '"',
    "'",
  ], [
    '',
    '',
  ], $actual_message);
  $this
    ->assertEquals($expected_message, $actual_message);
}