DevelQueryDebugTest.php in Devel 8.2
File
tests/src/Kernel/DevelQueryDebugTest.php
View source
<?php
namespace Drupal\Tests\devel\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
class DevelQueryDebugTest extends KernelTestBase {
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
->assertTrue(!empty($messages['status']));
$this
->assertCount(1, $messages['status']);
$this
->assertEquals(strip_tags($messages['status'][0]), $expected_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
->assertTrue(!empty($messages['status']));
$this
->assertCount(1, $messages['status']);
$this
->assertEquals(strip_tags($messages['status'][0]), $expected_message);
}
protected function getDrupalMessages() {
return drupal_get_messages();
}
}