View source
<?php
namespace Drupal\Tests\filelog\Functional;
use Drupal\Component\FileSecurity\FileSecurity;
use Drupal\Tests\BrowserTestBase;
class FileLogSettingsTest extends BrowserTestBase {
protected static $modules = [
'dblog',
'filelog',
];
protected $defaultTheme = 'stark';
private $adminUser;
public static function setUpBeforeClass() : void {
parent::setUpBeforeClass();
require_once __DIR__ . '/../../../filelog.module';
}
protected function setUp() : void {
parent::setUp();
$logPath = $this
->config('filelog.settings')
->get('location');
static::assertStringEqualsFile("{$logPath}/.htaccess", FileSecurity::htaccessLines(), '.htaccess file written correctly on install.');
$this->adminUser = $this
->createUser([
'administer site configuration',
]);
}
public function testSettingsForm(array $settings, array $expected, string $error = NULL) : void {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('/admin/config/development/logging');
$this
->assertSession()
->pageTextContains('Log messages to file');
$this
->submitForm($settings, 'Save configuration');
if (!$error) {
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$config = $this
->config('filelog.settings')
->getRawData();
unset($config['_core']);
static::assertEquals($expected, $config, 'Configuration is saved as expected.');
$logPath = $this
->config('filelog.settings')
->get('location');
static::assertStringEqualsFile("{$logPath}/.htaccess", FileSecurity::htaccessLines(), '.htaccess file written correctly.');
}
else {
$this
->assertSession()
->pageTextNotContains('The configuration options have been saved.');
$this
->assertSession()
->pageTextContains($error);
}
}
public function providerTestSettingsForm() : array {
$default_settings = [
'enabled' => TRUE,
'location' => 'public://logs',
'rotation' => [
'schedule' => 'daily',
'delete' => FALSE,
'destination' => 'archive/[date:custom:Y/m/d].log',
'gzip' => TRUE,
],
'format' => '[[log:created]] [[log:level]] [[log:channel]] [client: [log:ip], [log:user]] [log:message]',
'level' => 7,
'channels_type' => 'exclude',
'channels' => [],
];
$test_cases = [];
$case['settings']['filelog[channels]'] = "channel1\nchannel2\nchannel3";
$case['expected'] = $default_settings;
$case['expected']['channels'] = [
'channel1',
'channel2',
'channel3',
];
$test_cases[] = $case;
$case['settings']['filelog[channels]'] = "channel1\rchannel2\nchannel3\r\nchannel4\n\n\rchannel5";
$case['expected']['channels'] = [
'channel1',
'channel2',
'channel3',
'channel4',
'channel5',
];
$test_cases[] = $case;
return $test_cases;
}
}