Commit 5948cfbc authored by Jess's avatar Jess
Browse files

Issue #3250397 by alexpott, mondrake, ressa, daffie, xjm: DbLog triggers PHP...

Issue #3250397 by alexpott, mondrake, ressa, daffie, xjm: DbLog triggers PHP deprecation on PHP8.1 when running from CLI
parent 752ad024
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -111,7 +111,7 @@ public function log($level, $message, array $context = []) {
    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();
+2 −0
Original line number Diff line number Diff line
@@ -116,6 +116,8 @@ public function testQuickStartCommand() {
    });
    // 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.
+35 −2
Original line number Diff line number Diff line
@@ -97,6 +97,39 @@ public function testSortLoggers() {
    $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 @@ public function providerTestLog() {
    // 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,