2018-04-24 16:14:00 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2018-04-24 16:14:00 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-04-24 16:14:00 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test\Log;
|
2020-04-09 05:48:10 -04:00
|
|
|
|
2018-04-24 16:14:00 -04:00
|
|
|
use OC\Log\Errorlog;
|
|
|
|
|
use OC\Log\File;
|
|
|
|
|
use OC\Log\LogFactory;
|
|
|
|
|
use OC\Log\Syslog;
|
2018-06-06 16:40:06 -04:00
|
|
|
use OC\Log\Systemdlog;
|
2018-04-26 17:54:11 -04:00
|
|
|
use OC\SystemConfig;
|
2025-06-30 10:56:59 -04:00
|
|
|
use OCP\AppFramework\QueryException;
|
2018-04-24 16:14:00 -04:00
|
|
|
use OCP\IServerContainer;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class LogFactoryTest
|
|
|
|
|
*
|
|
|
|
|
* @package Test\Log
|
|
|
|
|
*/
|
|
|
|
|
class LogFactoryTest extends TestCase {
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-24 16:14:00 -04:00
|
|
|
protected $c;
|
|
|
|
|
|
|
|
|
|
/** @var LogFactory */
|
|
|
|
|
protected $factory;
|
|
|
|
|
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
|
2018-04-26 17:54:11 -04:00
|
|
|
protected $systemConfig;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2018-04-24 16:14:00 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->c = $this->createMock(IServerContainer::class);
|
2018-04-26 17:54:11 -04:00
|
|
|
$this->systemConfig = $this->createMock(SystemConfig::class);
|
2018-04-24 16:14:00 -04:00
|
|
|
|
2018-04-26 17:54:11 -04:00
|
|
|
$this->factory = new LogFactory($this->c, $this->systemConfig);
|
2018-04-24 16:14:00 -04:00
|
|
|
}
|
|
|
|
|
|
2025-04-30 02:29:47 -04:00
|
|
|
public static function fileTypeProvider(): array {
|
2018-04-24 16:14:00 -04:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'file'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'nextcloud'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'owncloud'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'krzxkyr_default'
|
|
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $type
|
2025-06-30 10:56:59 -04:00
|
|
|
* @throws QueryException
|
2018-04-24 16:14:00 -04:00
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('fileTypeProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testFile(string $type): void {
|
2024-09-19 05:10:31 -04:00
|
|
|
$datadir = \OC::$SERVERROOT . '/data';
|
2018-04-24 16:14:00 -04:00
|
|
|
$defaultLog = $datadir . '/nextcloud.log';
|
|
|
|
|
|
2018-10-02 12:37:57 -04:00
|
|
|
$this->systemConfig->expects($this->exactly(3))
|
2018-04-26 17:54:11 -04:00
|
|
|
->method('getValue')
|
2025-04-30 02:29:47 -04:00
|
|
|
->willReturnMap([
|
|
|
|
|
['datadirectory', $datadir, $datadir],
|
|
|
|
|
['logfile', $defaultLog, $defaultLog],
|
|
|
|
|
['logfilemode', 0640, 0640],
|
|
|
|
|
]);
|
2018-04-24 16:14:00 -04:00
|
|
|
|
|
|
|
|
$log = $this->factory->get($type);
|
|
|
|
|
$this->assertInstanceOf(File::class, $log);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 02:29:47 -04:00
|
|
|
public static function logFilePathProvider():array {
|
2018-04-24 16:14:00 -04:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
'/dev/null',
|
|
|
|
|
'/dev/null'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'/xdev/youshallfallback',
|
2024-09-19 05:10:31 -04:00
|
|
|
\OC::$SERVERROOT . '/data/nextcloud.log'
|
2018-04-24 16:14:00 -04:00
|
|
|
]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @throws QueryException
|
2018-04-24 16:14:00 -04:00
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('logFilePathProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testFileCustomPath($path, $expected): void {
|
2024-09-19 05:10:31 -04:00
|
|
|
$datadir = \OC::$SERVERROOT . '/data';
|
2018-04-24 16:14:00 -04:00
|
|
|
$defaultLog = $datadir . '/nextcloud.log';
|
|
|
|
|
|
2018-10-02 12:37:57 -04:00
|
|
|
$this->systemConfig->expects($this->exactly(3))
|
2018-04-26 17:54:11 -04:00
|
|
|
->method('getValue')
|
2025-04-30 02:29:47 -04:00
|
|
|
->willReturnMap([
|
|
|
|
|
['datadirectory', $datadir, $datadir],
|
|
|
|
|
['logfile', $defaultLog, $path],
|
|
|
|
|
['logfilemode', 0640, 0640],
|
|
|
|
|
]);
|
2018-04-24 16:14:00 -04:00
|
|
|
|
|
|
|
|
$log = $this->factory->get('file');
|
|
|
|
|
$this->assertInstanceOf(File::class, $log);
|
|
|
|
|
$this->assertSame($expected, $log->getLogFilePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @throws QueryException
|
2018-04-24 16:14:00 -04:00
|
|
|
*/
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testErrorLog(): void {
|
2018-04-24 16:14:00 -04:00
|
|
|
$log = $this->factory->get('errorlog');
|
|
|
|
|
$this->assertInstanceOf(Errorlog::class, $log);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @throws QueryException
|
2018-04-24 16:14:00 -04:00
|
|
|
*/
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSystemLog(): void {
|
2018-04-24 16:14:00 -04:00
|
|
|
$this->c->expects($this->once())
|
|
|
|
|
->method('resolve')
|
|
|
|
|
->with(Syslog::class)
|
|
|
|
|
->willReturn($this->createMock(Syslog::class));
|
|
|
|
|
|
|
|
|
|
$log = $this->factory->get('syslog');
|
|
|
|
|
$this->assertInstanceOf(Syslog::class, $log);
|
|
|
|
|
}
|
2018-06-06 16:40:06 -04:00
|
|
|
|
|
|
|
|
/**
|
2025-06-30 10:56:59 -04:00
|
|
|
* @throws QueryException
|
2018-06-06 16:40:06 -04:00
|
|
|
*/
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSystemdLog(): void {
|
2018-06-06 16:40:06 -04:00
|
|
|
$this->c->expects($this->once())
|
|
|
|
|
->method('resolve')
|
|
|
|
|
->with(Systemdlog::class)
|
|
|
|
|
->willReturn($this->createMock(Systemdlog::class));
|
|
|
|
|
|
|
|
|
|
$log = $this->factory->get('systemd');
|
|
|
|
|
$this->assertInstanceOf(Systemdlog::class, $log);
|
|
|
|
|
}
|
2018-04-24 16:14:00 -04:00
|
|
|
}
|