Commit 3958a93e authored by catch's avatar catch

Issue #2874069 by dagmar, dawehner: Make syslog testable

parent fdc9ff9f
......@@ -82,6 +82,18 @@ public function log($level, $message, array $context = []) {
'!message' => strip_tags($message),
]);
$this->syslogWrapper($level, $entry);
}
/**
* A syslog wrapper to make syslog functionality testable.
*
* @param int $level
* The syslog priority.
* @param string $entry
* The message to send to syslog function.
*/
protected function syslogWrapper($level, $entry) {
syslog($level, $entry);
}
......
<?php
namespace Drupal\syslog_test\Logger;
use Drupal\syslog\Logger\SysLog;
use Psr\Log\LoggerInterface;
/**
* Redirects logging messages to error_log.
*/
class SysLogTest extends SysLog implements LoggerInterface {
/**
* {@inheritdoc}
*/
protected function syslogWrapper($level, $entry) {
$log_path = \Drupal::service('file_system')->realpath('public://syslog.log');
error_log($entry . PHP_EOL, 3, $log_path);
}
}
name: 'Syslog test'
type: module
description: 'Provides a test logger for syslog module.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- syslog
services:
logger.syslog_test:
parent: logger.syslog
class: Drupal\syslog_test\Logger\SysLogTest
arguments: ['@config.factory', '@logger.log_message_parser']
tags:
- { name: logger }
<?php
namespace Drupal\Tests\syslog\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Test syslog logger functionality.
*
* @group syslog
* @coversDefaultClass \Drupal\syslog\Logger\SysLog
*/
class SyslogTest extends KernelTestBase {
public static $modules = ['syslog', 'syslog_test'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['syslog']);
}
/**
* @covers ::log
*/
public function testSyslogWriting() {
$request = Request::create('/page-not-found', 'GET', [], [], [], ['REMOTE_ADDR' => '1.2.3.4']);
$request->headers->set('Referer', 'other-site');
\Drupal::requestStack()->push($request);
$user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')->getMock();
$user->method('id')->willReturn(42);
$this->container->set('current_user', $user);
\Drupal::logger('my_module')->warning('My warning message.', ['link' => '/my-link']);
$log_filename = $this->container->get('file_system')->realpath('public://syslog.log');
$logs = explode(PHP_EOL, file_get_contents($log_filename));
$log = explode('|', $logs[0]);
global $base_url;
$this->assertEquals($base_url, $log[0]);
$this->assertEquals('my_module', $log[2]);
$this->assertEquals('1.2.3.4', $log[3]);
$this->assertEquals($base_url . '/page-not-found', $log[4]);
$this->assertEquals('other-site', $log[5]);
$this->assertEquals('42', $log[6]);
$this->assertEquals('/my-link', $log[7]);
$this->assertEquals('My warning message.', $log[8]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment