function DevelQueryDebugTest::testSelectQueryDebugTag

Same name in other branches
  1. 8.x-1.x tests/src/Kernel/DevelQueryDebugTest.php \Drupal\Tests\devel\Kernel\DevelQueryDebugTest::testSelectQueryDebugTag()
  2. 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 58

Class

DevelQueryDebugTest
Tests query debug.

Namespace

Drupal\Tests\devel\Kernel

Code

public function testSelectQueryDebugTag() : void {
    // 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\\n
FROM\\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);
}