1. 8.3.x core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
  2. 8.0.x core/modules/system/src/Tests/Database/LoggingTest.php
  3. 8.1.x core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
  4. 8.2.x core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
  5. 8.4.x core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php

Namespace

Drupal\KernelTests\Core\Database

File

core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php
View source
  1. <?php
  2. namespace Drupal\KernelTests\Core\Database;
  3. use Drupal\Core\Database\Database;
  4. /**
  5. * Tests the query logging facility.
  6. *
  7. * @group Database
  8. */
  9. class LoggingTest extends DatabaseTestBase {
  10. /**
  11. * Tests that we can log the existence of a query.
  12. */
  13. function testEnableLogging() {
  14. Database::startLog('testing');
  15. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  16. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchCol();
  17. // Trigger a call that does not have file in the backtrace.
  18. call_user_func_array('db_query', array('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo')))->fetchCol();
  19. $queries = Database::getLog('testing', 'default');
  20. $this->assertEqual(count($queries), 3, 'Correct number of queries recorded.');
  21. foreach ($queries as $query) {
  22. $this->assertEqual($query['caller']['function'], __FUNCTION__, 'Correct function in query log.');
  23. }
  24. }
  25. /**
  26. * Tests that we can run two logs in parallel.
  27. */
  28. function testEnableMultiLogging() {
  29. Database::startLog('testing1');
  30. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  31. Database::startLog('testing2');
  32. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchCol();
  33. $queries1 = Database::getLog('testing1');
  34. $queries2 = Database::getLog('testing2');
  35. $this->assertEqual(count($queries1), 2, 'Correct number of queries recorded for log 1.');
  36. $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for log 2.');
  37. }
  38. /**
  39. * Tests logging queries against multiple targets on the same connection.
  40. */
  41. function testEnableTargetLogging() {
  42. // Clone the primary credentials to a replica connection and to another fake
  43. // connection.
  44. $connection_info = Database::getConnectionInfo('default');
  45. Database::addConnectionInfo('default', 'replica', $connection_info['default']);
  46. Database::startLog('testing1');
  47. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  48. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'replica'));//->fetchCol();
  49. $queries1 = Database::getLog('testing1');
  50. $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
  51. $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
  52. $this->assertEqual($queries1[1]['target'], 'replica', 'Second query used replica target.');
  53. }
  54. /**
  55. * Tests that logs to separate targets use the same connection properly.
  56. *
  57. * This test is identical to the one above, except that it doesn't create
  58. * a fake target so the query should fall back to running on the default
  59. * target.
  60. */
  61. function testEnableTargetLoggingNoTarget() {
  62. Database::startLog('testing1');
  63. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  64. // We use "fake" here as a target because any non-existent target will do.
  65. // However, because all of the tests in this class share a single page
  66. // request there is likely to be a target of "replica" from one of the other
  67. // unit tests, so we use a target here that we know with absolute certainty
  68. // does not exist.
  69. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'fake'))->fetchCol();
  70. $queries1 = Database::getLog('testing1');
  71. $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
  72. $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
  73. $this->assertEqual($queries1[1]['target'], 'default', 'Second query used default target as fallback.');
  74. }
  75. /**
  76. * Tests that we can log queries separately on different connections.
  77. */
  78. function testEnableMultiConnectionLogging() {
  79. // Clone the primary credentials to a fake connection.
  80. // That both connections point to the same physical database is irrelevant.
  81. $connection_info = Database::getConnectionInfo('default');
  82. Database::addConnectionInfo('test2', 'default', $connection_info['default']);
  83. Database::startLog('testing1');
  84. Database::startLog('testing1', 'test2');
  85. db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
  86. $old_key = db_set_active('test2');
  87. db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'replica'))->fetchCol();
  88. db_set_active($old_key);
  89. $queries1 = Database::getLog('testing1');
  90. $queries2 = Database::getLog('testing1', 'test2');
  91. $this->assertEqual(count($queries1), 1, 'Correct number of queries recorded for first connection.');
  92. $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for second connection.');
  93. }
  94. /**
  95. * Tests that getLog with a wrong key return an empty array.
  96. */
  97. function testGetLoggingWrongKey() {
  98. $result = Database::getLog('wrong');
  99. $this->assertEqual($result, [], 'The function getLog with a wrong key returns an empty array.');
  100. }
  101. }

Classes

Namesort descending Description
LoggingTest Tests the query logging facility.