LoggingTest.php 4.85 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
   */
  function testEnableLogging() {
18
    Database::startLog('testing');
19 20 21 22

    db_query('SELECT name FROM {test} WHERE age > :age', array(':age' => 25))->fetchCol();
    db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchCol();

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

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 39 40 41 42 43 44 45 46 47 48 49
   */
  function testEnableMultiLogging() {
    Database::startLog('testing1');

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

    Database::startLog('testing2');

    db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'))->fetchCol();

    $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
   */
  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 65 66

    Database::startLog('testing1');

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

67
    db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('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 83 84 85 86 87 88 89
   *
   * 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.
   */
  function testEnableTargetLoggingNoTarget() {
    Database::startLog('testing1');

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

    // 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 93 94 95 96
    // unit tests, so we use a target here that we know with absolute certainty
    // does not exist.
    db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('target' => 'fake'))->fetchCol();

    $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
   */
  function testEnableMultiConnectionLogging() {
106
    // Clone the primary credentials to a fake connection.
107 108 109 110 111 112 113 114 115 116 117
    // 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');

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

    $old_key = db_set_active('test2');

118
    db_query('SELECT age FROM {test} WHERE name = :name', array(':name' => 'Ringo'), array('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
}