function LoggerChannelTest::testNullIp

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest::testNullIp()
  2. 10 core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest::testNullIp()

Tests that $context['ip'] is a string even when the request's IP is NULL.

File

core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php, line 114

Class

LoggerChannelTest
@coversDefaultClass \Drupal\Core\Logger\LoggerChannel @group Logger

Namespace

Drupal\Tests\Core\Logger

Code

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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.