LoggingTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public function testEnableLogging() {
  14. Database::startLog('testing');
  15. db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
  16. db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'])->fetchCol();
  17. // Trigger a call that does not have file in the backtrace.
  18. call_user_func_array('db_query', ['SELECT age FROM {test} WHERE name = :name', [':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. public function testEnableMultiLogging() {
  29. Database::startLog('testing1');
  30. db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
  31. Database::startLog('testing2');
  32. db_query('SELECT age FROM {test} WHERE name = :name', [':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. public 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', [':age' => 25])->fetchCol();
  48. db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['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. public function testEnableTargetLoggingNoTarget() {
  62. Database::startLog('testing1');
  63. db_query('SELECT name FROM {test} WHERE age > :age', [':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', [':name' => 'Ringo'], ['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. public 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', [':age' => 25])->fetchCol();
  86. $old_key = db_set_active('test2');
  87. db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['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. public function testGetLoggingWrongKey() {
  98. $result = Database::getLog('wrong');
  99. $this->assertEqual($result, [], 'The function getLog with a wrong key returns an empty array.');
  100. }
  101. }