function LoggerChannelTest::testNullIp

Same name and namespace in other branches
  1. 10 core/tests/Drupal/Tests/Core/Logger/LoggerChannelTest.php \Drupal\Tests\Core\Logger\LoggerChannelTest::testNullIp()
  2. 11.x 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 103

Class

LoggerChannelTest
@coversDefaultClass <a href="/api/drupal/core%21lib%21Drupal%21Core%21Logger%21LoggerChannel.php/class/LoggerChannel/9" title="Defines a logger channel that most implementations will use." class="local">\Drupal\Core\Logger\LoggerChannel</a> @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.