diff --git a/core/lib/Drupal/Core/Database/Log.php b/core/lib/Drupal/Core/Database/Log.php index 32cf4e7..5db9117 100644 --- a/core/lib/Drupal/Core/Database/Log.php +++ b/core/lib/Drupal/Core/Database/Log.php @@ -152,11 +152,11 @@ public function findCaller($stack = NULL) { // Drop all stack entries before and including methods from the database // connection class. - $connection_class = get_class(Database::getConnection($this->connectionKey)); - while ((!empty($stack[0]['class']) ? $stack[0]['class'] : '') !== $connection_class) { + $connection_class = $this->getConnectionClass(); + while (count($stack) > 2 && (!empty($stack[0]['class']) ? $stack[0]['class'] : '') !== $connection_class) { array_shift($stack); } - while ((!empty($stack[0]['class']) ? $stack[0]['class'] : '') === $connection_class) { + while (count($stack) > 2 && (!empty($stack[0]['class']) ? $stack[0]['class'] : '') === $connection_class) { array_shift($stack); } @@ -181,4 +181,14 @@ public function findCaller($stack = NULL) { } } + /** + * Gets the class name of the database connection. + * + * @return string + * Fully qualified class name of the database connection. + */ + protected function getConnectionClass() { + return get_class(Database::getConnection($this->connectionKey)); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php index 225065a..eb493d6 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/LoggingTest.php @@ -143,8 +143,6 @@ public function testGetLoggingWrongKey() { * @covers ::findCaller */ public function testContribDriverLog() { - $log = new Log('default'); - $stack = [ [ 'file' => '/var/www/core/lib/Drupal/Core/Database/Log.php', @@ -252,6 +250,14 @@ public function testContribDriverLog() { ], ]; + $mock_builder = $this->getMockBuilder(Log::class); + $log = $mock_builder + ->setMethods(['getConnectionClass']) + ->getMock(); + $log->expects($this->any()) + ->method('getConnectionClass') + ->will($this->returnValue('Drupal\\Driver\\Database\\dbal\\Connection')); + $result = $log->findCaller($stack); $expected_result = [ 'class' => 'Drupal\\KernelTests\\Core\\Database\\LoggingTest',