View source
<?php
namespace Drupal\Tests\jsonlog\Unit;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\jsonlog\Logger\JsonLog;
use Drupal\jsonlog\Logger\JsonLogData;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Request;
class JsonLogTest extends UnitTestCase {
const DEFAULT_THRESHOLD = 4;
const DEFAULT_LOG_DIR = '/var/log';
const DEFAULT_TIME_FORMAT = 'Ymd';
const DEFAULT_SITE_ID = 'jsonlog_test';
private $requestStackMock;
private $jsonLogger;
protected function setUp() {
parent::setUp();
$config_stub['jsonlog.settings'] = [
'jsonlog_severity_threshold' => self::DEFAULT_THRESHOLD,
'jsonlog_truncate' => 64,
'jsonlog_siteid' => self::DEFAULT_SITE_ID,
'jsonlog_canonical' => '',
'jsonlog_file_time' => self::DEFAULT_TIME_FORMAT,
'jsonlog_dir' => self::DEFAULT_LOG_DIR,
'jsonlog_tags' => 'test',
];
$configFactoryMock = $this
->getConfigFactoryStub($config_stub);
$messageParserMock = $this
->getEmptyMessageParserMock();
$moduleHandlerMock = $this
->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->requestStackMock = $this
->createMock('Symfony\\Component\\HttpFoundation\\RequestStack');
$this->jsonLogger = new JsonLog($configFactoryMock, $messageParserMock, $moduleHandlerMock, $this->requestStackMock);
}
public function testCanPrepareLogFile() {
$request_mock = $this
->createMock('Symfony\\Component\\HttpFoundation\\Request');
$request_mock
->expects($this
->exactly(1))
->method('getRealMethod')
->willReturn('POST');
$this
->setupContainerCurrentRequest($request_mock);
$test_context = [
'user' => NULL,
'uid' => 12345,
'ip' => '127.0.0.9',
'request_uri' => 'admin/help',
'channel' => '500',
'link' => '',
'referer' => '',
];
$level = self::DEFAULT_THRESHOLD - 1;
$log_entry = $this->jsonLogger
->prepareLog($level, 'test', $test_context);
$this
->assertFalse($log_entry === FALSE, 'Log entry constructed');
$this
->assertEquals(JsonLogData::class, get_class($log_entry));
$this
->assertEquals('500', $log_entry
->getData()['subtype'], 'Correct subtype logged');
$this
->assertEquals('127.0.0.9', $log_entry
->getData()['client_ip'], 'Correct client_ip logged');
$this
->assertEquals('admin/help', $log_entry
->getData()['request_uri'], 'Correct request_uri logged');
$this
->assertEquals('POST', $log_entry
->getData()['method'], 'Correct method logged');
$this
->assertEquals(12345, $log_entry
->getData()['uid'], 'Uid logged.');
}
public function testCannotPrepareLogFileIfThresholdExceeded() {
$level = self::DEFAULT_THRESHOLD + 1;
$log_entry = $this->jsonLogger
->prepareLog($level, 'test', []);
$this
->assertTrue($log_entry === FALSE, 'Log entry not built');
}
public function testGetCorrectFilename() {
$time = date(self::DEFAULT_TIME_FORMAT);
$filename = $this->jsonLogger
->getFileName(self::DEFAULT_TIME_FORMAT);
$this
->assertEquals(self::DEFAULT_LOG_DIR . '/' . self::DEFAULT_SITE_ID . '.' . $time . '.json.log', $filename, 'Correct filename constructed.');
}
private function getEmptyMessageParserMock() {
$messageParserMock = $this
->createMock('Drupal\\Core\\Logger\\LogMessageParserInterface');
$messageParserMock
->expects($this
->atMost(1))
->method('parseMessagePlaceholders')
->withAnyParameters()
->willReturn([]);
return $messageParserMock;
}
private function setupContainerCurrentRequest(Request $request) {
$this->requestStackMock
->expects($this
->any())
->method('getCurrentRequest')
->willReturn($request);
$containerMock = $this
->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
$containerMock
->expects($this
->any())
->method('get')
->with('request_stack')
->willReturn($this->requestStackMock);
\Drupal::setContainer($containerMock);
}
}