2016-04-27 04:51:02 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-04-27 04:51:02 -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-or-later
|
2016-04-27 04:51:02 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
|
2022-08-23 05:01:01 -04:00
|
|
|
use OC\Repair;
|
2023-11-23 04:22:34 -05:00
|
|
|
use OC\Repair\Events\RepairErrorEvent;
|
2022-08-23 05:01:01 -04:00
|
|
|
use OC\Repair\Events\RepairInfoEvent;
|
|
|
|
|
use OC\Repair\Events\RepairStepEvent;
|
|
|
|
|
use OC\Repair\Events\RepairWarningEvent;
|
2023-11-23 04:22:34 -05:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Migration\IOutput;
|
2023-11-23 04:22:34 -05:00
|
|
|
use OCP\Migration\IRepairStep;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2021-02-01 08:56:56 -05:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-04-27 04:51:02 -04:00
|
|
|
|
2022-08-23 05:01:01 -04:00
|
|
|
class TestRepairStep implements IRepairStep {
|
2025-06-12 12:31:58 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private bool $warning = false,
|
|
|
|
|
) {
|
2016-04-27 04:51:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
|
return 'Test Name';
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
public function run(IOutput $out) {
|
2016-04-27 04:51:02 -04:00
|
|
|
if ($this->warning) {
|
|
|
|
|
$out->warning('Simulated warning');
|
2020-04-10 08:19:56 -04:00
|
|
|
} else {
|
2016-04-27 04:51:02 -04:00
|
|
|
$out->info('Simulated info');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class RepairTest extends TestCase {
|
2022-08-23 05:01:01 -04:00
|
|
|
private Repair $repair;
|
2016-04-27 04:51:02 -04:00
|
|
|
|
|
|
|
|
/** @var string[] */
|
2022-08-23 05:01:01 -04:00
|
|
|
private array $outputArray = [];
|
2016-04-27 04:51:02 -04:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-04-27 04:51:02 -04:00
|
|
|
parent::setUp();
|
2025-06-12 12:31:58 -04:00
|
|
|
$dispatcher = Server::get(IEventDispatcher::class);
|
2024-01-30 06:14:52 -05:00
|
|
|
$this->repair = new Repair($dispatcher, $this->createMock(LoggerInterface::class));
|
2016-04-27 04:51:02 -04:00
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$dispatcher->addListener(RepairWarningEvent::class, function (RepairWarningEvent $event): void {
|
2022-08-23 05:01:01 -04:00
|
|
|
$this->outputArray[] = 'warning: ' . $event->getMessage();
|
2016-04-27 04:51:02 -04:00
|
|
|
});
|
2025-06-12 12:31:58 -04:00
|
|
|
$dispatcher->addListener(RepairInfoEvent::class, function (RepairInfoEvent $event): void {
|
2022-08-23 05:01:01 -04:00
|
|
|
$this->outputArray[] = 'info: ' . $event->getMessage();
|
2016-04-27 04:51:02 -04:00
|
|
|
});
|
2025-06-12 12:31:58 -04:00
|
|
|
$dispatcher->addListener(RepairStepEvent::class, function (RepairStepEvent $event): void {
|
2022-08-23 05:01:01 -04:00
|
|
|
$this->outputArray[] = 'step: ' . $event->getStepName();
|
|
|
|
|
});
|
2025-06-12 12:31:58 -04:00
|
|
|
$dispatcher->addListener(RepairErrorEvent::class, function (RepairErrorEvent $event): void {
|
2022-08-23 05:01:01 -04:00
|
|
|
$this->outputArray[] = 'error: ' . $event->getMessage();
|
2016-04-27 04:51:02 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRunRepairStep(): void {
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->repair->addStep(new TestRepairStep(false));
|
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2016-04-27 04:51:02 -04:00
|
|
|
'step: Test Name',
|
|
|
|
|
'info: Simulated info',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->outputArray
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRunRepairStepThatFail(): void {
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->repair->addStep(new TestRepairStep(true));
|
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2016-04-27 04:51:02 -04:00
|
|
|
'step: Test Name',
|
|
|
|
|
'warning: Simulated warning',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->outputArray
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRunRepairStepsWithException(): void {
|
2017-04-20 06:27:32 -04:00
|
|
|
$mock = $this->createMock(TestRepairStep::class);
|
2016-04-27 04:51:02 -04:00
|
|
|
$mock->expects($this->any())
|
|
|
|
|
->method('run')
|
2025-06-12 12:34:54 -04:00
|
|
|
->willThrowException(new \Exception('Exception text'));
|
2016-04-27 04:51:02 -04:00
|
|
|
$mock->expects($this->any())
|
|
|
|
|
->method('getName')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('Exception Test');
|
2016-04-27 04:51:02 -04:00
|
|
|
|
|
|
|
|
$this->repair->addStep($mock);
|
|
|
|
|
$this->repair->addStep(new TestRepairStep(false));
|
|
|
|
|
|
|
|
|
|
$thrown = false;
|
|
|
|
|
try {
|
|
|
|
|
$this->repair->run();
|
2020-04-10 08:19:56 -04:00
|
|
|
} catch (\Exception $e) {
|
2016-04-27 04:51:02 -04:00
|
|
|
$thrown = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-23 05:01:01 -04:00
|
|
|
$this->assertFalse($thrown);
|
2016-04-27 04:51:02 -04:00
|
|
|
// jump out after exception
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2016-04-27 04:51:02 -04:00
|
|
|
'step: Exception Test',
|
2022-08-23 05:01:01 -04:00
|
|
|
'error: Exception text',
|
|
|
|
|
'step: Test Name',
|
|
|
|
|
'info: Simulated info',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->outputArray
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRunRepairStepsContinueAfterWarning(): void {
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->repair->addStep(new TestRepairStep(true));
|
|
|
|
|
$this->repair->addStep(new TestRepairStep(false));
|
|
|
|
|
$this->repair->run();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2016-04-27 04:51:02 -04:00
|
|
|
'step: Test Name',
|
|
|
|
|
'warning: Simulated warning',
|
|
|
|
|
'step: Test Name',
|
|
|
|
|
'info: Simulated info',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2016-04-27 04:51:02 -04:00
|
|
|
$this->outputArray
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|