diff --git a/core/lib/Drupal/Core/Database/Log.php b/core/lib/Drupal/Core/Database/Log.php index 5db9117..f56ed68 100644 --- a/core/lib/Drupal/Core/Database/Log.php +++ b/core/lib/Drupal/Core/Database/Log.php @@ -125,8 +125,9 @@ public function log(StatementInterface $statement, $args, $time) { * Determine the routine that called this query. * * We define "the routine that called this query" as the first entry in - * the call stack that is not inside the includes/Drupal/Database directory, - * does not begin with db_ and does have a file (which excludes + * the call stack that is coming after any method called from the database + * Connection class, is not inside the Drupal\Core\Database namespace, does + * not begin with db_ and does have a file (which excludes * call_user_func_array(), anonymous functions and similar). That makes the * climbing logic very simple, and handles the variable stack depth caused by * the query builders. @@ -134,10 +135,6 @@ public function log(StatementInterface $statement, $args, $time) { * See the @link http://php.net/debug_backtrace debug_backtrace() @endlink * function. * - * @param array $stack - * (optional) The backtrace to find the caller in. It falls back to the - * current backtrace. - * * @return * This method returns a stack trace entry similar to that generated by * debug_backtrace(). However, it flattens the trace entry and the trace @@ -145,13 +142,11 @@ public function log(StatementInterface $statement, $args, $time) { * called into the database system, not the function and args of the * database call itself. */ - public function findCaller($stack = NULL) { - if ($stack === NULL) { - $stack = debug_backtrace(); - } + public function findCaller() { + $stack = $this->getDebugBacktrace(); - // Drop all stack entries before and including methods from the database - // connection class. + // Drop all stack entries before and including methods called from the + // database connection class. $connection_class = $this->getConnectionClass(); while (count($stack) > 2 && (!empty($stack[0]['class']) ? $stack[0]['class'] : '') !== $connection_class) { array_shift($stack); @@ -191,4 +186,14 @@ protected function getConnectionClass() { return get_class(Database::getConnection($this->connectionKey)); } + /** + * Gets the debug backtrace. + * + * @return array[] + * The debug backtrace. + */ + protected function getDebugBacktrace() { + return debug_backtrace(); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php index eb493d6..b4e3773 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php @@ -138,11 +138,18 @@ public function testGetLoggingWrongKey() { } /** - * Tests that a log called by a custom database driver return proper caller. + * Tests that a log called by a custom database driver returns proper caller. + * + * @param string $connection_class + * The connection class to be tested. + * @param array $expected_entry + * The expected stack entry. * * @covers ::findCaller + * + * @dataProvider providerContribDriverLog */ - public function testContribDriverLog() { + public function testContribDriverLog($connection_class, array $expected_entry) { $stack = [ [ 'file' => '/var/www/core/lib/Drupal/Core/Database/Log.php', @@ -252,22 +259,58 @@ public function testContribDriverLog() { $mock_builder = $this->getMockBuilder(Log::class); $log = $mock_builder - ->setMethods(['getConnectionClass']) + ->setMethods(['getConnectionClass', 'getDebugBacktrace']) ->getMock(); - $log->expects($this->any()) + $log->expects($this->once()) ->method('getConnectionClass') - ->will($this->returnValue('Drupal\\Driver\\Database\\dbal\\Connection')); + ->will($this->returnValue($connection_class)); + $log->expects($this->once()) + ->method('getDebugBacktrace') + ->will($this->returnValue($stack)); $result = $log->findCaller($stack); - $expected_result = [ - 'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest', - 'function' => 'testEnableLogging', - 'file' => '/var/www/tests/Drupal/KernelTests/Core/Database/LoggingTest.php', - 'line' => 20, - 'type' => '->', - 'args' => [], + $this->assertEquals($expected_entry, $result); + } + + /** + * Provides data for the testContribDriverLog test. + * + * @return array[] + * A simple array of simple arrays, each having the following elements: + * - the contrib driver Connection class fully qualified name + * - the stack entry expected to be returned. + * + * @see ::testContribDriverLog() + */ + public function providerContribDriverLog() { + return [ + // Test that if the driver Connection is in the stack trace, the first + // non-database entry is returned. + [ + 'Drupal\\Driver\\Database\\dbal\\Connection', + [ + 'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest', + 'function' => 'testEnableLogging', + 'file' => '/var/www/tests/Drupal/KernelTests/Core/Database/LoggingTest.php', + 'line' => 20, + 'type' => '->', + 'args' => [], + ] + ], + // Test that if the driver Connection is not in the stack trace, the very + // first backtrace entry is returned. + [ + 'Drupal\\Driver\\Database\\fake\\Connection', + [ + 'file' => '-', + 'line' => 52, + 'function' => '__phpunit_run_isolated_test', + 'args' => [], + 'class' => null, + 'type' => null, + ] + ], ]; - $this->assertEquals($expected_result, $result); } }