|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\Settings\Tests; |
| 10 | + |
| 11 | +use OC\IntegrityCheck\Checker; |
| 12 | +use OCA\Settings\SetupChecks\CodeIntegrity; |
| 13 | +use OCP\IL10N; |
| 14 | +use OCP\IURLGenerator; |
| 15 | +use OCP\SetupCheck\SetupResult; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use Test\TestCase; |
| 18 | + |
| 19 | +class CodeIntegrityTest extends TestCase { |
| 20 | + |
| 21 | + private IL10N&MockObject $l10n; |
| 22 | + private IURLGenerator&MockObject $urlGenerator; |
| 23 | + private Checker&MockObject $checker; |
| 24 | + |
| 25 | + protected function setUp(): void { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->l10n = $this->getMockBuilder(IL10N::class) |
| 29 | + ->disableOriginalConstructor()->getMock(); |
| 30 | + $this->l10n->expects($this->any()) |
| 31 | + ->method('t') |
| 32 | + ->willReturnCallback(function ($message, array $replace) { |
| 33 | + return vsprintf($message, $replace); |
| 34 | + }); |
| 35 | + $this->urlGenerator = $this->createMock(IURLGenerator::class); |
| 36 | + $this->checker = $this->createMock(Checker::class); |
| 37 | + } |
| 38 | + |
| 39 | + public function testSkipOnDisabled(): void { |
| 40 | + $this->checker->expects($this->atLeastOnce()) |
| 41 | + ->method('isCodeCheckEnforced') |
| 42 | + ->willReturn(false); |
| 43 | + |
| 44 | + $check = new CodeIntegrity( |
| 45 | + $this->l10n, |
| 46 | + $this->urlGenerator, |
| 47 | + $this->checker, |
| 48 | + ); |
| 49 | + $this->assertEquals(SetupResult::INFO, $check->run()->getSeverity()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testSuccessOnEmptyResults(): void { |
| 53 | + $this->checker->expects($this->atLeastOnce()) |
| 54 | + ->method('isCodeCheckEnforced') |
| 55 | + ->willReturn(true); |
| 56 | + $this->checker->expects($this->atLeastOnce()) |
| 57 | + ->method('getResults') |
| 58 | + ->willReturn([]); |
| 59 | + $this->checker->expects(($this->atLeastOnce())) |
| 60 | + ->method('hasPassedCheck') |
| 61 | + ->willReturn(true); |
| 62 | + |
| 63 | + $check = new CodeIntegrity( |
| 64 | + $this->l10n, |
| 65 | + $this->urlGenerator, |
| 66 | + $this->checker, |
| 67 | + ); |
| 68 | + $this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testCheckerIsReRunWithoutResults(): void { |
| 72 | + $this->checker->expects($this->atLeastOnce()) |
| 73 | + ->method('isCodeCheckEnforced') |
| 74 | + ->willReturn(true); |
| 75 | + $this->checker->expects($this->atLeastOnce()) |
| 76 | + ->method('getResults') |
| 77 | + ->willReturn(null); |
| 78 | + $this->checker->expects(($this->atLeastOnce())) |
| 79 | + ->method('hasPassedCheck') |
| 80 | + ->willReturn(true); |
| 81 | + |
| 82 | + // This is important and must be called |
| 83 | + $this->checker->expects($this->once()) |
| 84 | + ->method('runInstanceVerification'); |
| 85 | + |
| 86 | + $check = new CodeIntegrity( |
| 87 | + $this->l10n, |
| 88 | + $this->urlGenerator, |
| 89 | + $this->checker, |
| 90 | + ); |
| 91 | + $this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity()); |
| 92 | + } |
| 93 | + |
| 94 | + public function testCheckerIsNotReReInAdvance(): void { |
| 95 | + $this->checker->expects($this->atLeastOnce()) |
| 96 | + ->method('isCodeCheckEnforced') |
| 97 | + ->willReturn(true); |
| 98 | + $this->checker->expects($this->atLeastOnce()) |
| 99 | + ->method('getResults') |
| 100 | + ->willReturn(['mocked']); |
| 101 | + $this->checker->expects(($this->atLeastOnce())) |
| 102 | + ->method('hasPassedCheck') |
| 103 | + ->willReturn(true); |
| 104 | + |
| 105 | + // There are results thus this must never be called |
| 106 | + $this->checker->expects($this->never()) |
| 107 | + ->method('runInstanceVerification'); |
| 108 | + |
| 109 | + $check = new CodeIntegrity( |
| 110 | + $this->l10n, |
| 111 | + $this->urlGenerator, |
| 112 | + $this->checker, |
| 113 | + ); |
| 114 | + $this->assertEquals(SetupResult::SUCCESS, $check->run()->getSeverity()); |
| 115 | + } |
| 116 | + |
| 117 | + public function testErrorOnMissingIntegrity(): void { |
| 118 | + $this->checker->expects($this->atLeastOnce()) |
| 119 | + ->method('isCodeCheckEnforced') |
| 120 | + ->willReturn(true); |
| 121 | + $this->checker->expects($this->atLeastOnce()) |
| 122 | + ->method('getResults') |
| 123 | + ->willReturn(['mocked']); |
| 124 | + $this->checker->expects(($this->atLeastOnce())) |
| 125 | + ->method('hasPassedCheck') |
| 126 | + ->willReturn(false); |
| 127 | + |
| 128 | + $check = new CodeIntegrity( |
| 129 | + $this->l10n, |
| 130 | + $this->urlGenerator, |
| 131 | + $this->checker, |
| 132 | + ); |
| 133 | + $this->assertEquals(SetupResult::ERROR, $check->run()->getSeverity()); |
| 134 | + } |
| 135 | +} |
0 commit comments