View source
<?php
namespace Drupal\Tests\devel\Kernel;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class DevelQueryDebugTest extends KernelTestBase {
use MessengerTrait;
public static $modules = [
'devel',
'system',
'user',
];
protected $develUser;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installConfig([
'system',
'devel',
]);
$this
->installEntitySchema('user');
$devel_role = Role::create([
'id' => 'admin',
'permissions' => [
'access devel information',
],
]);
$devel_role
->save();
$this->develUser = User::create([
'name' => $this
->randomMachineName(),
'roles' => [
$devel_role
->id(),
],
]);
$this->develUser
->save();
}
public function testSelectQueryDebugTag() {
$this
->getDrupalMessages();
$query = \Drupal::database()
->select('users', 'u');
$query
->fields('u', [
'uid',
]);
$query
->addTag('debug');
$query
->execute();
$messages = $this
->getDrupalMessages();
$this
->assertEmpty($messages);
\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]);
$actual_message = str_replace([
'"',
"'",
], [
'',
'',
], $actual_message);
$this
->assertEquals($expected_message, $actual_message);
}
public function testEntityQueryDebugTag() {
$this
->getDrupalMessages();
$query = \Drupal::entityQuery('user');
$query
->addTag('debug');
$query
->execute();
$messages = $this
->getDrupalMessages();
$this
->assertEmpty($messages);
\Drupal::currentUser()
->setAccount($this->develUser);
$expected_message = "SELECT base_table.uid AS uid, base_table.uid AS base_table_uid\nFROM\n{users} base_table";
$query = \Drupal::entityQuery('user');
$query
->addTag('debug');
$query
->execute();
$messages = $this
->getDrupalMessages();
$this
->assertNotEmpty($messages['status']);
$this
->assertCount(1, $messages['status']);
$actual_message = strip_tags($messages['status'][0]);
$actual_message = str_replace([
'"',
"'",
], [
'',
'',
], $actual_message);
$this
->assertEquals($expected_message, $actual_message);
}
protected function getDrupalMessages() {
return $this
->messenger()
->deleteAll();
}
}