|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Libresign\Tests\Unit\Service\DocMdp; |
| 10 | + |
| 11 | +use OCA\Libresign\AppInfo\Application; |
| 12 | +use OCA\Libresign\Enum\DocMdpLevel; |
| 13 | +use OCA\Libresign\Service\DocMdp\ConfigService; |
| 14 | +use OCP\IAppConfig; |
| 15 | +use OCP\IL10N; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\MockObject\MockObject; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +final class ConfigServiceTest extends TestCase { |
| 21 | + private IAppConfig&MockObject $appConfig; |
| 22 | + private IL10N&MockObject $l10n; |
| 23 | + |
| 24 | + protected function setUp(): void { |
| 25 | + $this->appConfig = $this->createMock(IAppConfig::class); |
| 26 | + $this->l10n = $this->createMock(IL10N::class); |
| 27 | + $this->l10n->method('t')->willReturnArgument(0); |
| 28 | + } |
| 29 | + |
| 30 | + private function createService(): ConfigService { |
| 31 | + return new ConfigService($this->appConfig, $this->l10n); |
| 32 | + } |
| 33 | + |
| 34 | + #[DataProvider('providerGetLevelCases')] |
| 35 | + public function testGetLevelReturnsExpectedEnum(int $storedLevel, DocMdpLevel $expectedLevel): void { |
| 36 | + $this->appConfig |
| 37 | + ->expects($this->once()) |
| 38 | + ->method('getValueInt') |
| 39 | + ->with(Application::APP_ID, 'docmdp_level', DocMdpLevel::CERTIFIED_FORM_FILLING->value) |
| 40 | + ->willReturn($storedLevel); |
| 41 | + |
| 42 | + $service = $this->createService(); |
| 43 | + |
| 44 | + $this->assertSame($expectedLevel, $service->getLevel()); |
| 45 | + } |
| 46 | + |
| 47 | + #[DataProvider('providerGetConfigCases')] |
| 48 | + public function testGetConfigReturnsExpectedState(int $storedLevel, bool $expectedEnabled, int $expectedDefaultLevel): void { |
| 49 | + $this->appConfig |
| 50 | + ->method('getValueInt') |
| 51 | + ->with(Application::APP_ID, 'docmdp_level', DocMdpLevel::CERTIFIED_FORM_FILLING->value) |
| 52 | + ->willReturn($storedLevel); |
| 53 | + |
| 54 | + $service = $this->createService(); |
| 55 | + $config = $service->getConfig(); |
| 56 | + |
| 57 | + $this->assertSame($expectedEnabled, $config['enabled']); |
| 58 | + $this->assertSame($expectedDefaultLevel, $config['defaultLevel']); |
| 59 | + } |
| 60 | + |
| 61 | + public function testSetEnabledFalseSetsNotCertifiedLevel(): void { |
| 62 | + $this->appConfig |
| 63 | + ->expects($this->once()) |
| 64 | + ->method('setValueInt') |
| 65 | + ->with(Application::APP_ID, 'docmdp_level', DocMdpLevel::NOT_CERTIFIED->value); |
| 66 | + |
| 67 | + $service = $this->createService(); |
| 68 | + $service->setEnabled(false); |
| 69 | + } |
| 70 | + |
| 71 | + public function testSetEnabledTrueRestoresDefaultLevelWhenCurrentlyDisabled(): void { |
| 72 | + $this->appConfig |
| 73 | + ->expects($this->once()) |
| 74 | + ->method('getValueInt') |
| 75 | + ->with(Application::APP_ID, 'docmdp_level', DocMdpLevel::CERTIFIED_FORM_FILLING->value) |
| 76 | + ->willReturn(DocMdpLevel::NOT_CERTIFIED->value); |
| 77 | + |
| 78 | + $this->appConfig |
| 79 | + ->expects($this->once()) |
| 80 | + ->method('setValueInt') |
| 81 | + ->with(Application::APP_ID, 'docmdp_level', DocMdpLevel::CERTIFIED_FORM_FILLING->value); |
| 82 | + |
| 83 | + $service = $this->createService(); |
| 84 | + $service->setEnabled(true); |
| 85 | + } |
| 86 | + |
| 87 | + public function testSetEnabledTrueKeepsCurrentLevelWhenAlreadyEnabled(): void { |
| 88 | + $this->appConfig |
| 89 | + ->expects($this->once()) |
| 90 | + ->method('getValueInt') |
| 91 | + ->with(Application::APP_ID, 'docmdp_level', DocMdpLevel::CERTIFIED_FORM_FILLING->value) |
| 92 | + ->willReturn(DocMdpLevel::CERTIFIED_NO_CHANGES_ALLOWED->value); |
| 93 | + |
| 94 | + $this->appConfig |
| 95 | + ->expects($this->never()) |
| 96 | + ->method('setValueInt'); |
| 97 | + |
| 98 | + $service = $this->createService(); |
| 99 | + $service->setEnabled(true); |
| 100 | + } |
| 101 | + |
| 102 | + public static function providerGetLevelCases(): array { |
| 103 | + return [ |
| 104 | + 'default/fallback level is form filling' => [ |
| 105 | + DocMdpLevel::CERTIFIED_FORM_FILLING->value, |
| 106 | + DocMdpLevel::CERTIFIED_FORM_FILLING, |
| 107 | + ], |
| 108 | + 'explicit no changes level' => [ |
| 109 | + DocMdpLevel::CERTIFIED_NO_CHANGES_ALLOWED->value, |
| 110 | + DocMdpLevel::CERTIFIED_NO_CHANGES_ALLOWED, |
| 111 | + ], |
| 112 | + 'explicit annotations level' => [ |
| 113 | + DocMdpLevel::CERTIFIED_FORM_FILLING_AND_ANNOTATIONS->value, |
| 114 | + DocMdpLevel::CERTIFIED_FORM_FILLING_AND_ANNOTATIONS, |
| 115 | + ], |
| 116 | + ]; |
| 117 | + } |
| 118 | + |
| 119 | + public static function providerGetConfigCases(): array { |
| 120 | + return [ |
| 121 | + 'not configured defaults to enabled and level 2' => [ |
| 122 | + DocMdpLevel::CERTIFIED_FORM_FILLING->value, |
| 123 | + true, |
| 124 | + 2, |
| 125 | + ], |
| 126 | + 'explicitly disabled with level 0' => [ |
| 127 | + DocMdpLevel::NOT_CERTIFIED->value, |
| 128 | + false, |
| 129 | + 0, |
| 130 | + ], |
| 131 | + 'configured certifying level is reflected as enabled' => [ |
| 132 | + DocMdpLevel::CERTIFIED_NO_CHANGES_ALLOWED->value, |
| 133 | + true, |
| 134 | + 1, |
| 135 | + ], |
| 136 | + ]; |
| 137 | + } |
| 138 | +} |
0 commit comments