LoggingTest.php 5.07 KB
Newer Older
1 2
<?php

3
namespace Drupal\KernelTests\Core\Database;
4 5 6 7

use Drupal\Core\Database\Database;

/**
8 9 10
 * Tests the query logging facility.
 *
 * @group Database
11 12 13 14
 */
class LoggingTest extends DatabaseTestBase {

  /**
15
   * Tests that we can log the existence of a query.
16
   */
17
  public function testEnableLogging() {
18
    Database::startLog('testing');
19

20 21
    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
    db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'])->fetchCol();
22

23
    // Trigger a call that does not have file in the backtrace.
24
    call_user_func_array('db_query', ['SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo']])->fetchCol();
25

26 27
    $queries = Database::getLog('testing', 'default');

28
    $this->assertEqual(count($queries), 3, 'Correct number of queries recorded.');
29 30

    foreach ($queries as $query) {
31
      $this->assertEqual($query['caller']['function'], __FUNCTION__, 'Correct function in query log.');
32 33 34 35
    }
  }

  /**
36
   * Tests that we can run two logs in parallel.
37
   */
38
  public function testEnableMultiLogging() {
39 40
    Database::startLog('testing1');

41
    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
42 43 44

    Database::startLog('testing2');

45
    db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'])->fetchCol();
46 47 48 49

    $queries1 = Database::getLog('testing1');
    $queries2 = Database::getLog('testing2');

50 51
    $this->assertEqual(count($queries1), 2, 'Correct number of queries recorded for log 1.');
    $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for log 2.');
52 53 54
  }

  /**
55
   * Tests logging queries against multiple targets on the same connection.
56
   */
57
  public function testEnableTargetLogging() {
58
    // Clone the primary credentials to a replica connection and to another fake
59 60
    // connection.
    $connection_info = Database::getConnectionInfo('default');
61
    Database::addConnectionInfo('default', 'replica', $connection_info['default']);
62 63 64

    Database::startLog('testing1');

65
    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
66

67
    db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'replica'])->fetchCol();
68 69 70

    $queries1 = Database::getLog('testing1');

71 72
    $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
    $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
73
    $this->assertEqual($queries1[1]['target'], 'replica', 'Second query used replica target.');
74 75 76
  }

  /**
77
   * Tests that logs to separate targets use the same connection properly.
78 79 80 81 82
   *
   * This test is identical to the one above, except that it doesn't create
   * a fake target so the query should fall back to running on the default
   * target.
   */
83
  public function testEnableTargetLoggingNoTarget() {
84 85
    Database::startLog('testing1');

86
    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
87 88 89

    // We use "fake" here as a target because any non-existent target will do.
    // However, because all of the tests in this class share a single page
90
    // request there is likely to be a target of "replica" from one of the other
91 92
    // unit tests, so we use a target here that we know with absolute certainty
    // does not exist.
93
    db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'fake'])->fetchCol();
94 95 96

    $queries1 = Database::getLog('testing1');

97 98 99
    $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
    $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
    $this->assertEqual($queries1[1]['target'], 'default', 'Second query used default target as fallback.');
100 101 102
  }

  /**
103
   * Tests that we can log queries separately on different connections.
104
   */
105
  public function testEnableMultiConnectionLogging() {
106
    // Clone the primary credentials to a fake connection.
107 108 109 110 111 112 113
    // That both connections point to the same physical database is irrelevant.
    $connection_info = Database::getConnectionInfo('default');
    Database::addConnectionInfo('test2', 'default', $connection_info['default']);

    Database::startLog('testing1');
    Database::startLog('testing1', 'test2');

114
    db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
115 116 117

    $old_key = db_set_active('test2');

118
    db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'replica'])->fetchCol();
119 120 121 122 123 124

    db_set_active($old_key);

    $queries1 = Database::getLog('testing1');
    $queries2 = Database::getLog('testing1', 'test2');

125 126
    $this->assertEqual(count($queries1), 1, 'Correct number of queries recorded for first connection.');
    $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for second connection.');
127
  }
128

129 130 131
  /**
   * Tests that getLog with a wrong key return an empty array.
   */
132
  public function testGetLoggingWrongKey() {
133 134 135 136 137
    $result = Database::getLog('wrong');

    $this->assertEqual($result, [], 'The function getLog with a wrong key returns an empty array.');
  }

138
}