public function FileLogTest::testFileLog in File Log 2.0.x
Same name and namespace in other branches
- 8 tests/src/Unit/FileLogTest.php \Drupal\Tests\filelog\Unit\FileLogTest::testFileLog()
Test a particular configuration.
Ensure that it logs the correct events.
@dataProvider providerFileLog
Parameters
array $config: The filelog settings.
array $events: The events to be logged.
string $expected: The expected content of the log file after the test.
File
- tests/
src/ Unit/ FileLogTest.php, line 93
Class
- FileLogTest
- Test the file logger.
Namespace
Drupal\Tests\filelog\UnitCode
public function testFileLog(array $config, array $events, $expected = '') : void {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'filelog.settings' => $config,
]);
/** @var \Drupal\Core\State\StateInterface|\PHPUnit\Framework\MockObject\MockObject $state */
$state_data = [
'filelog.rotation' => 0,
];
$state = $this
->createMock(StateInterface::class);
$state
->method('get')
->willReturnCallback(static function ($key) use (&$state_data) {
return $state_data[$key];
});
$state
->method('set')
->willReturnCallback(static function ($key, $value) use (&$state_data) {
$state_data[$key] = $value;
});
$logger = new FileLog($configFactory, $state, $this->token, $this->time, $this->logMessageParser, $logFileManager = new LogFileManager($configFactory, $this->fileSystem));
foreach ($events as $event) {
$logger
->log($event['level'], $event['message'], $event['context']);
}
// The logger should ensure .htaccess whenever it opens the log file.
if ($expected) {
$logPath = $configFactory
->get('filelog.settings')
->get('location');
static::assertStringEqualsFile("{$logPath}/.htaccess", FileSecurity::htaccessLines(), '.htaccess file written correctly.');
static::assertEquals(0664, 0777 & fileperms($logFileManager
->getFileName()), 'Logfile has 0664 permissions.');
}
// Read log output and remove file for the next test.
$content = '';
if ($this->virtualFileSystem
->hasChild(LogFileManager::FILENAME)) {
$content = file_get_contents($this->virtualFileSystem
->getChild(LogFileManager::FILENAME)
->url());
$this->virtualFileSystem
->removeChild(LogFileManager::FILENAME);
}
static::assertEquals($expected, $content);
// Check that the timestamp was saved if and only if a log was created.
$timestamp = $state
->get('filelog.rotation');
static::assertEquals($content ? $_SERVER['REQUEST_TIME'] : 0, $timestamp);
}