Issue #3250397 by alexpott, mondrake, ressa, daffie, xjm: DbLog triggers PHP deprecation on PHP8.1 when running from CLI

(cherry picked from commit 5948cfbca6)
(cherry picked from commit 21b92bdaa6)
merge-requests/512/merge
xjm 2022-03-05 18:19:16 -06:00
parent 7ce0090eea
commit 175596294e
3 changed files with 38 additions and 3 deletions

View File

@ -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();

View File

@ -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.

View File

@ -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,