2015-03-11 06:53:31 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 10:49:16 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2019-12-03 13:57:53 -05:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 05:57:22 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 10:49:16 -04:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 09:56:42 -05:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 13:57:53 -05:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 09:02:16 -05:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2022-03-31 09:34:57 -04:00
|
|
|
* @author Côme Chilliet <come.chilliet@nextcloud.com>
|
2016-01-12 09:02:16 -05:00
|
|
|
*
|
|
|
|
|
* @license AGPL-3.0
|
|
|
|
|
*
|
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-12 09:02:16 -05:00
|
|
|
*
|
2015-03-11 06:53:31 -04:00
|
|
|
*/
|
2016-05-25 10:04:15 -04:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2015-03-11 06:53:31 -04:00
|
|
|
|
2018-04-11 05:46:36 -04:00
|
|
|
use OC\SystemConfig;
|
2015-08-30 13:13:01 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
|
2022-03-31 09:34:57 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin;
|
2022-06-09 18:36:52 -04:00
|
|
|
use OCA\DAV\Exception\ServerMaintenanceMode;
|
2022-03-31 09:34:57 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-03-11 06:53:31 -04:00
|
|
|
use Sabre\DAV\Exception\NotFound;
|
|
|
|
|
use Sabre\DAV\Server;
|
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
2016-05-25 10:04:15 -04:00
|
|
|
class ExceptionLoggerPluginTest extends TestCase {
|
2015-03-11 06:53:31 -04:00
|
|
|
|
|
|
|
|
/** @var Server */
|
|
|
|
|
private $server;
|
|
|
|
|
|
2022-03-31 09:34:57 -04:00
|
|
|
/** @var ExceptionLoggerPlugin */
|
2015-03-11 06:53:31 -04:00
|
|
|
private $plugin;
|
|
|
|
|
|
2022-03-31 09:34:57 -04:00
|
|
|
/** @var LoggerInterface | \PHPUnit\Framework\MockObject\MockObject */
|
2015-03-11 06:53:31 -04:00
|
|
|
private $logger;
|
|
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
private function init(): void {
|
2018-04-11 05:46:36 -04:00
|
|
|
$config = $this->createMock(SystemConfig::class);
|
|
|
|
|
$config->expects($this->any())
|
|
|
|
|
->method('getValue')
|
2020-04-09 07:53:40 -04:00
|
|
|
->willReturnCallback(function ($key, $default) {
|
2018-04-11 05:46:36 -04:00
|
|
|
switch ($key) {
|
|
|
|
|
case 'loglevel':
|
|
|
|
|
return 0;
|
|
|
|
|
default:
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2015-03-11 06:53:31 -04:00
|
|
|
$this->server = new Server();
|
2022-03-31 09:34:57 -04:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
|
$this->plugin = new ExceptionLoggerPlugin('unit-test', $this->logger);
|
2015-03-11 06:53:31 -04:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider providesExceptions
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testLogging(string $expectedLogLevel, \Throwable $e): void {
|
2015-03-11 06:53:31 -04:00
|
|
|
$this->init();
|
|
|
|
|
|
2022-03-31 09:34:57 -04:00
|
|
|
$this->logger->expects($this->once())
|
|
|
|
|
->method($expectedLogLevel)
|
|
|
|
|
->with($e->getMessage(), ['app' => 'unit-test','exception' => $e]);
|
|
|
|
|
|
|
|
|
|
$this->plugin->logException($e);
|
2015-03-11 06:53:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function providesExceptions() {
|
|
|
|
|
return [
|
2022-03-31 09:34:57 -04:00
|
|
|
['debug', new NotFound()],
|
2022-06-09 18:36:52 -04:00
|
|
|
['debug', new ServerMaintenanceMode('System is in maintenance mode.')],
|
|
|
|
|
// Faking a translation
|
|
|
|
|
['debug', new ServerMaintenanceMode('Syst3m 1s 1n m41nt3n4nc3 m0d3.')],
|
|
|
|
|
['debug', new ServerMaintenanceMode('Upgrade needed')],
|
2022-03-31 09:34:57 -04:00
|
|
|
['critical', new InvalidPath('This path leads to nowhere')]
|
2015-03-11 06:53:31 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|