Initial Drupal 11 with DDEV setup
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
name: 'Syslog test'
|
||||
type: module
|
||||
description: 'Provides a test logger for syslog module.'
|
||||
package: Testing
|
||||
version: VERSION
|
||||
dependencies:
|
||||
- drupal:syslog
|
||||
@ -0,0 +1,7 @@
|
||||
services:
|
||||
logger.syslog_test:
|
||||
parent: logger.syslog
|
||||
class: Drupal\syslog_test\Logger\SysLogTest
|
||||
arguments: ['@config.factory', '@logger.log_message_parser']
|
||||
tags:
|
||||
- { name: logger }
|
||||
14
web/core/modules/syslog/tests/src/Functional/GenericTest.php
Normal file
14
web/core/modules/syslog/tests/src/Functional/GenericTest.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Functional;
|
||||
|
||||
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
|
||||
|
||||
/**
|
||||
* Generic module test for syslog.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class GenericTest extends GenericModuleTestBase {}
|
||||
46
web/core/modules/syslog/tests/src/Functional/SyslogTest.php
Normal file
46
web/core/modules/syslog/tests/src/Functional/SyslogTest.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests syslog settings.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class SyslogTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $defaultTheme = 'stark';
|
||||
|
||||
/**
|
||||
* Tests the syslog settings page.
|
||||
*/
|
||||
public function testSettings(): void {
|
||||
$admin_user = $this->drupalCreateUser(['administer site configuration']);
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// If we're on Windows, there is no configuration form.
|
||||
if (defined('LOG_LOCAL6')) {
|
||||
$this->drupalGet('admin/config/development/logging');
|
||||
$this->submitForm(['syslog_facility' => LOG_LOCAL6], 'Save configuration');
|
||||
$this->assertSession()->pageTextContains('The configuration options have been saved.');
|
||||
|
||||
$this->drupalGet('admin/config/development/logging');
|
||||
// Should be one field.
|
||||
$field = $this->assertSession()->elementExists('xpath', '//option[@value="' . LOG_LOCAL6 . '"]');
|
||||
$this->assertSame('selected', $field->getAttribute('selected'), 'Facility value saved.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Kernel\Migrate\d6;
|
||||
|
||||
use Drupal\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade variables to syslog.settings.yml.
|
||||
*
|
||||
* @group migrate_drupal_6
|
||||
*/
|
||||
class MigrateSyslogConfigsTest extends MigrateDrupal6TestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
// Enable syslog in the source database so that requirements are met.
|
||||
$this->sourceDatabase->update('system')
|
||||
->condition('name', 'syslog')
|
||||
->fields(['status' => '1'])
|
||||
->execute();
|
||||
$this->executeMigration('d6_syslog_settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests migration of syslog variables to syslog.settings.yml.
|
||||
*/
|
||||
public function testSyslogSettings(): void {
|
||||
$config = $this->config('syslog.settings');
|
||||
$this->assertSame('drupal', $config->get('identity'));
|
||||
$this->assertSame(128, $config->get('facility'));
|
||||
$this->assertConfigSchema(\Drupal::service('config.typed'), 'syslog.settings', $config->get());
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Kernel\Migrate\d7;
|
||||
|
||||
use Drupal\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
|
||||
|
||||
/**
|
||||
* Upgrade variables to syslog.settings.yml.
|
||||
*
|
||||
* @group syslog
|
||||
*/
|
||||
class MigrateSyslogConfigsTest extends MigrateDrupal7TestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->installConfig(static::$modules);
|
||||
$this->executeMigration('d7_syslog_settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests migration of syslog variables to syslog.settings.yml.
|
||||
*/
|
||||
public function testSyslogSettings(): void {
|
||||
$config = $this->config('syslog.settings');
|
||||
// 8 == LOG_USER
|
||||
$this->assertSame(8, $config->get('facility'));
|
||||
$this->assertSame('!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message', $config->get('format'));
|
||||
$this->assertSame('drupal', $config->get('identity'));
|
||||
}
|
||||
|
||||
}
|
||||
109
web/core/modules/syslog/tests/src/Kernel/SyslogTest.php
Normal file
109
web/core/modules/syslog/tests/src/Kernel/SyslogTest.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\syslog\Kernel;
|
||||
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
|
||||
/**
|
||||
* Test syslog logger functionality.
|
||||
*
|
||||
* @group syslog
|
||||
* @coversDefaultClass \Drupal\syslog\Logger\SysLog
|
||||
*/
|
||||
class SyslogTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $modules = ['syslog', 'syslog_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->installConfig(['syslog']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::log
|
||||
*/
|
||||
public function testSyslogWriting(): void {
|
||||
|
||||
$request = Request::create('/page-not-found', 'GET', [], [], [], ['REMOTE_ADDR' => '1.2.3.4']);
|
||||
$request->headers->set('Referer', 'other-site');
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
\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]);
|
||||
|
||||
// Test that an empty format prevents writing to the syslog.
|
||||
/** @var \Drupal\Core\Config\Config $config */
|
||||
$config = $this->container->get('config.factory')->getEditable('syslog.settings');
|
||||
$config->set('format', '');
|
||||
$config->save();
|
||||
unlink($log_filename);
|
||||
\Drupal::logger('my_module')->warning('My warning message.', ['link' => '/my-link']);
|
||||
$this->assertFileDoesNotExist($log_filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that missing facility prevents writing to the syslog.
|
||||
*
|
||||
* @covers ::openConnection
|
||||
*/
|
||||
public function testSyslogMissingFacility(): void {
|
||||
$config = $this->container->get('config.factory')->getEditable('syslog.settings');
|
||||
$config->clear('facility');
|
||||
$config->save();
|
||||
\Drupal::logger('my_module')->warning('My warning message.');
|
||||
$log_filename = $this->container->get('file_system')->realpath('public://syslog.log');
|
||||
$this->assertFileDoesNotExist($log_filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests severity level logging.
|
||||
*
|
||||
* @covers ::log
|
||||
*/
|
||||
public function testSyslogSeverity(): void {
|
||||
/** @var \Drupal\Core\Config\Config $config */
|
||||
$config = $this->container->get('config.factory')->getEditable('syslog.settings');
|
||||
$config->set('format', '!type|!message|!severity');
|
||||
$config->save();
|
||||
|
||||
\Drupal::logger('my_module')->warning('My warning message.');
|
||||
|
||||
$log_filename = $this->container->get('file_system')->realpath('public://syslog.log');
|
||||
$logs = explode(PHP_EOL, file_get_contents($log_filename));
|
||||
$log = explode('|', $logs[0]);
|
||||
|
||||
$this->assertEquals('my_module', $log[0]);
|
||||
$this->assertEquals('My warning message.', $log[1]);
|
||||
$this->assertEquals('4', $log[2]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user