View source
<?php
namespace Drupal\Tests\filelog\Unit;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Utility\Token;
use Drupal\filelog\FileLogException;
use Drupal\filelog\LogFileManager;
use Drupal\filelog\LogRotator;
use function date;
use function date_default_timezone_set;
use function file_get_contents;
use function gzencode;
use function preg_match;
use function preg_match_all;
use function scandir;
use function strtr;
use function unlink;
class FileLogRotationTest extends FileLogTestBase {
protected $fileManager;
protected $token;
protected $time;
protected $fileSystem;
protected function setUp() : void {
parent::setUp();
date_default_timezone_set('UTC');
$this->fileManager = $this
->getMockBuilder(LogFileManager::class)
->disableOriginalConstructor()
->getMock();
$this->fileManager
->method('getFileName')
->willReturn('vfs://filelog/' . LogFileManager::FILENAME);
$this->token = $this
->getMockBuilder(Token::class)
->disableOriginalConstructor()
->getMock();
$this->token
->method('replace')
->willReturnCallback([
static::class,
'tokenReplace',
]);
$this->time = $this
->getMockBuilder(TimeInterface::class)
->getMock();
$this->time
->method('getRequestTime')
->willReturn(86401);
}
public function testRotation($timestamp, array $config, array $files) : void {
$root = 'vfs://filelog';
$logFile = $root . '/' . LogFileManager::FILENAME;
$data = "This is the log file content.\n";
$configs = [
'filelog.settings' => [
'rotation' => $config,
'location' => $root,
],
];
$configFactory = $this
->getConfigFactoryStub($configs);
$state = $this
->createMock(StateInterface::class);
$state
->method('get')
->with('filelog.rotation')
->willReturn($timestamp);
file_put_contents($logFile, $data);
$rotator = new LogRotator($configFactory, $state, $this->token, $this->time, $this->fileManager, $this->fileSystem);
try {
$rotator
->run();
} catch (FileLogException $exception) {
$this
->fail("Log rotation caused an exception: {$exception}");
}
foreach ($files as $name) {
$path = "{$root}/{$name}";
$compressed = preg_match('/\\.gz$/', $name) === 1;
$expected = $compressed ? gzencode($data) : $data;
$content = file_get_contents($path);
$this
->assertEquals($expected, $content);
unlink($path);
}
foreach (scandir('vfs://filelog', 0) as $name) {
if ($name === '.htaccess') {
continue;
}
$path = "{$root}/{$name}";
if ($name === LogFileManager::FILENAME) {
$this
->assertStringEqualsFile($path, '');
}
else {
$this
->assertDirectoryExists($path);
}
}
}
public function provideRotationConfig() : array {
$config = [
'schedule' => 'daily',
'delete' => FALSE,
'destination' => 'archive/[date:custom:Y/m/d].log',
'gzip' => FALSE,
];
$data[] = [
'timestamp' => 86400,
'config' => $config,
'files' => [
LogFileManager::FILENAME,
],
];
$data[] = [
'timestamp' => 86399,
'config' => $config,
'files' => [
'archive/1970/01/01.log',
],
];
$config['schedule'] = 'weekly';
$data[] = [
'timestamp' => -259200,
'config' => $config,
'files' => [
LogFileManager::FILENAME,
],
];
$data[] = [
'timestamp' => -259201,
'config' => $config,
'files' => [
'archive/1969/12/28.log',
],
];
$config['schedule'] = 'monthly';
$data[] = [
'timestamp' => 0,
'config' => $config,
'files' => [
LogFileManager::FILENAME,
],
];
$data[] = [
'timestamp' => -1,
'config' => $config,
'files' => [
'archive/1969/12/31.log',
],
];
$config['gzip'] = TRUE;
$data[] = [
'timestamp' => -1,
'config' => $config,
'files' => [
'archive/1969/12/31.log.gz',
],
];
$config['delete'] = TRUE;
$data[] = [
'timestamp' => -1,
'config' => $config,
'files' => [],
];
$config['schedule'] = 'never';
$data[] = [
'timestamp' => -100000000,
'config' => $config,
'files' => [
LogFileManager::FILENAME,
],
];
return $data;
}
public static function tokenReplace($text, array $data) : string {
preg_match_all('/\\[date:custom:(.*?)]/', $text, $matches, PREG_SET_ORDER);
$replace = [];
foreach ((array) $matches as $match) {
$replace[$match[0]] = date($match[1], $data['date']);
}
return strtr($text, $replace);
}
}