aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/RepairTest.php
blob: ab60b14d47e59071b709c6fd9127846bc6d3b8c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test;

use OC\Repair;
use OC\Repair\Events\RepairErrorEvent;
use OC\Repair\Events\RepairInfoEvent;
use OC\Repair\Events\RepairStepEvent;
use OC\Repair\Events\RepairWarningEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Migration\IRepairStep;
use Psr\Log\LoggerInterface;

class TestRepairStep implements IRepairStep {
	private bool $warning;

	public function __construct(bool $warning = false) {
		$this->warning = $warning;
	}

	public function getName() {
		return 'Test Name';
	}

	public function run(\OCP\Migration\IOutput $out) {
		if ($this->warning) {
			$out->warning('Simulated warning');
		} else {
			$out->info('Simulated info');
		}
	}
}

class RepairTest extends TestCase {
	private Repair $repair;

	/** @var string[] */
	private array $outputArray = [];

	protected function setUp(): void {
		parent::setUp();
		$dispatcher = \OC::$server->get(IEventDispatcher::class);
		$this->repair = new Repair($dispatcher, $this->createMock(LoggerInterface::class));

		$dispatcher->addListener(RepairWarningEvent::class, function (RepairWarningEvent $event) {
			$this->outputArray[] = 'warning: ' . $event->getMessage();
		});
		$dispatcher->addListener(RepairInfoEvent::class, function (RepairInfoEvent $event) {
			$this->outputArray[] = 'info: ' . $event->getMessage();
		});
		$dispatcher->addListener(RepairStepEvent::class, function (RepairStepEvent $event) {
			$this->outputArray[] = 'step: ' . $event->getStepName();
		});
		$dispatcher->addListener(RepairErrorEvent::class, function (RepairErrorEvent $event) {
			$this->outputArray[] = 'error: ' . $event->getMessage();
		});
	}

	public function testRunRepairStep() {
		$this->repair->addStep(new TestRepairStep(false));
		$this->repair->run();

		$this->assertEquals(
			[
				'step: Test Name',
				'info: Simulated info',
			],
			$this->outputArray
		);
	}

	public function testRunRepairStepThatFail() {
		$this->repair->addStep(new TestRepairStep(true));
		$this->repair->run();

		$this->assertEquals(
			[
				'step: Test Name',
				'warning: Simulated warning',
			],
			$this->outputArray
		);
	}

	public function testRunRepairStepsWithException() {
		$mock = $this->createMock(TestRepairStep::class);
		$mock->expects($this->any())
			->method('run')
			->will($this->throwException(new \Exception('Exception text')));
		$mock->expects($this->any())
			->method('getName')
			->willReturn('Exception Test');

		$this->repair->addStep($mock);
		$this->repair->addStep(new TestRepairStep(false));

		$thrown = false;
		try {
			$this->repair->run();
		} catch (\Exception $e) {
			$thrown = true;
		}

		$this->assertFalse($thrown);
		// jump out after exception
		$this->assertEquals(
			[
				'step: Exception Test',
				'error: Exception text',
				'step: Test Name',
				'info: Simulated info',
			],
			$this->outputArray
		);
	}

	public function testRunRepairStepsContinueAfterWarning() {
		$this->repair->addStep(new TestRepairStep(true));
		$this->repair->addStep(new TestRepairStep(false));
		$this->repair->run();

		$this->assertEquals(
			[
				'step: Test Name',
				'warning: Simulated warning',
				'step: Test Name',
				'info: Simulated info',
			],
			$this->outputArray
		);
	}
}