From 175596294ecf798237187ad595ed9bed69f204e5 Mon Sep 17 00:00:00 2001 From: xjm Date: Sat, 5 Mar 2022 18:19:16 -0600 Subject: [PATCH] Issue #3250397 by alexpott, mondrake, ressa, daffie, xjm: DbLog triggers PHP deprecation on PHP8.1 when running from CLI (cherry picked from commit 5948cfbca617bbe31654404a04cdc2289c192c03) (cherry picked from commit 21b92bdaa66c1ea429bbc9a8d105af5a6b94df60) --- core/lib/Drupal/Core/Logger/LoggerChannel.php | 2 +- .../Tests/Core/Command/QuickStartTest.php | 2 + .../Tests/Core/Logger/LoggerChannelTest.php | 37 ++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Logger/LoggerChannel.php b/core/lib/Drupal/Core/Logger/LoggerChannel.php index 13dff9ce38c..21563f2d477 100644 --- a/core/lib/Drupal/Core/Logger/LoggerChannel.php +++ b/core/lib/Drupal/Core/Logger/LoggerChannel.php @@ -111,7 +111,7 @@ class LoggerChannel implements LoggerChannelInterface { if ($this->requestStack && $request = $this->requestStack->getCurrentRequest()) { $context['request_uri'] = $request->getUri(); $context['referer'] = $request->headers->get('Referer', ''); - $context['ip'] = $request->getClientIP(); + $context['ip'] = $request->getClientIP() ?: ''; if ($this->currentUser) { $context['uid'] = $this->currentUser->id(); diff --git a/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php b/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php index 2b73d0c76e4..0fcfe4cf2ec 100644 --- a/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php +++ b/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php @@ -116,6 +116,8 @@ class QuickStartTest extends TestCase { }); // The progress bar uses STDERR to write messages. $this->assertStringContainsString('Congratulations, you installed Drupal!', $process->getErrorOutput()); + // Ensure the command does not trigger any PHP deprecations. + $this->assertStringNotContainsString('Deprecated', $process->getErrorOutput()); $this->assertNotFalse($port, "Web server running on port $port"); // Give the server a couple of seconds to be ready. diff --git a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php index d5275cd61e0..45c7bc42322 100644 --- a/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php +++ b/core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php @@ -97,6 +97,39 @@ class LoggerChannelTest extends UnitTestCase { $this->assertEquals('3210', $index_order); } + /** + * Tests that $context['ip'] is a string even when the request's IP is NULL. + */ + public function testNullIp(): void { + // Create a logger that will fail if $context['ip'] is not an empty string. + $logger = $this->createMock(LoggerInterface::class); + $expected = function ($context) { + return $context['channel'] == 'test' && $context['ip'] === ''; + }; + $logger->expects($this->once()) + ->method('log') + ->with($this->anything(), 'Test message', $this->callback($expected)); + + // Set up a request stack that has a request that will return NULL when + // ::getClientIp() is called. + $requestStack = new RequestStack(); + $request_mock = $this->getMockBuilder(Request::class) + ->onlyMethods(['getClientIp']) + ->getMock(); + $request_mock->expects($this->any()) + ->method('getClientIp') + ->willReturn(NULL); + $requestStack->push($request_mock); + + // Set up the logger channel for testing. + $channel = new LoggerChannel('test'); + $channel->addLogger($logger); + $channel->setRequestStack($requestStack); + + // Perform the test. + $channel->log(rand(0, 7), 'Test message'); + } + /** * Data provider for self::testLog(). */ @@ -117,14 +150,14 @@ class LoggerChannelTest extends UnitTestCase { // No request or account. $cases[] = [ function ($context) { - return $context['channel'] == 'test' && empty($context['uid']) && empty($context['ip']); + return $context['channel'] == 'test' && empty($context['uid']) && $context['ip'] === ''; }, ]; // With account but not request. Since the request is not available the // current user should not be used. $cases[] = [ function ($context) { - return $context['uid'] === 0 && empty($context['ip']); + return $context['uid'] === 0 && $context['ip'] === ''; }, NULL, $account_mock,