2015-08-19 08:49:41 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-08-19 08:49:41 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-08-19 08:49:41 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Tests\Core\Command\Log;
|
|
|
|
|
|
2016-07-22 05:44:19 -04:00
|
|
|
use OC\Core\Command\Log\File;
|
2016-09-01 03:20:54 -04:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-08-19 08:49:41 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
2016-07-22 08:47:50 -04:00
|
|
|
class FileTest extends TestCase {
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-08-19 08:49:41 -04:00
|
|
|
protected $config;
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-08-19 08:49:41 -04:00
|
|
|
protected $consoleInput;
|
2020-08-11 15:32:18 -04:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
2015-08-19 08:49:41 -04:00
|
|
|
protected $consoleOutput;
|
|
|
|
|
|
|
|
|
|
/** @var \Symfony\Component\Console\Command\Command */
|
|
|
|
|
protected $command;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2015-08-19 08:49:41 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2016-09-01 03:20:54 -04:00
|
|
|
$config = $this->config = $this->getMockBuilder(IConfig::class)
|
2015-08-19 08:49:41 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2016-09-01 03:20:54 -04:00
|
|
|
$this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
|
|
|
|
|
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
|
2015-08-19 08:49:41 -04:00
|
|
|
|
2016-07-22 05:44:19 -04:00
|
|
|
$this->command = new File($config);
|
2015-08-19 08:49:41 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEnable(): void {
|
2021-10-25 10:16:55 -04:00
|
|
|
$this->config->method('getSystemValue')->willReturnArgument(1);
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->consoleInput->method('getOption')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnMap([
|
2015-08-19 08:49:41 -04:00
|
|
|
['enable', 'true']
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('setSystemValue')
|
2016-07-22 05:44:19 -04:00
|
|
|
->with('log_type', 'file');
|
2015-08-19 08:49:41 -04:00
|
|
|
|
|
|
|
|
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testChangeFile(): void {
|
2021-10-25 10:16:55 -04:00
|
|
|
$this->config->method('getSystemValue')->willReturnArgument(1);
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->consoleInput->method('getOption')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnMap([
|
2015-08-19 08:49:41 -04:00
|
|
|
['file', '/foo/bar/file.log']
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('setSystemValue')
|
|
|
|
|
->with('logfile', '/foo/bar/file.log');
|
|
|
|
|
|
|
|
|
|
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 02:29:47 -04:00
|
|
|
public static function changeRotateSizeProvider(): array {
|
2015-08-19 08:49:41 -04:00
|
|
|
return [
|
|
|
|
|
['42', 42],
|
|
|
|
|
['0', 0],
|
|
|
|
|
['1 kB', 1024],
|
|
|
|
|
['5MB', 5 * 1024 * 1024],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('changeRotateSizeProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testChangeRotateSize($optionValue, $configValue): void {
|
2021-10-25 10:16:55 -04:00
|
|
|
$this->config->method('getSystemValue')->willReturnArgument(1);
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->consoleInput->method('getOption')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnMap([
|
2015-08-19 08:49:41 -04:00
|
|
|
['rotate-size', $optionValue]
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('setSystemValue')
|
|
|
|
|
->with('log_rotate_size', $configValue);
|
|
|
|
|
|
|
|
|
|
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetConfiguration(): void {
|
2015-08-19 08:49:41 -04:00
|
|
|
$this->config->method('getSystemValue')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnMap([
|
2016-07-22 05:44:19 -04:00
|
|
|
['log_type', 'file', 'log_type_value'],
|
2024-09-19 05:10:31 -04:00
|
|
|
['datadirectory', \OC::$SERVERROOT . '/data', '/data/directory/'],
|
2016-07-04 05:50:32 -04:00
|
|
|
['logfile', '/data/directory/nextcloud.log', '/var/log/nextcloud.log'],
|
2018-04-10 17:52:41 -04:00
|
|
|
['log_rotate_size', 100 * 1024 * 1024, 5 * 1024 * 1024],
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2015-08-19 08:49:41 -04:00
|
|
|
|
2025-04-30 02:29:47 -04:00
|
|
|
$calls = [
|
|
|
|
|
['Log backend file: disabled'],
|
|
|
|
|
['Log file: /var/log/nextcloud.log'],
|
|
|
|
|
['Rotate at: 5 MB'],
|
|
|
|
|
];
|
2023-09-14 07:41:18 -04:00
|
|
|
$this->consoleOutput->expects($this->exactly(3))
|
2015-08-19 08:49:41 -04:00
|
|
|
->method('writeln')
|
2025-06-12 12:31:58 -04:00
|
|
|
->willReturnCallback(function (string $message) use (&$calls): void {
|
2025-04-30 02:29:47 -04:00
|
|
|
$expected = array_shift($calls);
|
|
|
|
|
$this->assertEquals($expected[0], $message);
|
|
|
|
|
});
|
2015-08-19 08:49:41 -04:00
|
|
|
|
|
|
|
|
self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
|
|
|
|
}
|
|
|
|
|
}
|