|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Unit\UserMigration\Service; |
| 11 | + |
| 12 | +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; |
| 13 | +use ChristophWurst\Nextcloud\Testing\TestCase; |
| 14 | +use OCA\Mail\AppInfo\Application; |
| 15 | +use OCA\Mail\UserMigration\Service\AppConfigMigrationService; |
| 16 | +use OCP\IUser; |
| 17 | +use OCP\UserMigration\IExportDestination; |
| 18 | +use OCP\UserMigration\IImportSource; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class AppConfigMigrationServiceTest extends TestCase { |
| 22 | + private const USER_ID = '123'; |
| 23 | + private OutputInterface $output; |
| 24 | + private IUser $user; |
| 25 | + private IExportDestination $exportDestination; |
| 26 | + private IImportSource $importSource; |
| 27 | + private ServiceMockObject $serviceMock; |
| 28 | + private AppConfigMigrationService $migrationService; |
| 29 | + |
| 30 | + protected function setUp(): void { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->serviceMock = $this->createServiceMock(AppConfigMigrationService::class); |
| 34 | + $this->migrationService = $this->serviceMock->getService(); |
| 35 | + |
| 36 | + $this->output = $this->createMock(OutputInterface::class); |
| 37 | + $this->exportDestination = $this->createMock(IExportDestination::class); |
| 38 | + $this->importSource = $this->createMock(IImportSource::class); |
| 39 | + |
| 40 | + $this->user = $this->createMock(IUser::class); |
| 41 | + $this->user->method('getUID')->willReturn(self::USER_ID); |
| 42 | + } |
| 43 | + |
| 44 | + public function testExportsMultipleAppConfigurations(): void { |
| 45 | + $this->exportDestination->expects(self::once())->method('addFileContents')->with(AppConfigMigrationService::APP_CONFIGURATION_FILE, json_encode($this->getAppConfig())); |
| 46 | + |
| 47 | + $this->serviceMock->getParameter('config')->expects(self::once())->method('getUserKeys')->with(self::USER_ID, Application::APP_ID)->willReturn($this->getAppKeys()); |
| 48 | + $this->serviceMock->getParameter('config')->method('getUserValue')->with(self::USER_ID, Application::APP_ID, self::callback(function ($appConfigKey): bool { |
| 49 | + return in_array($appConfigKey, $this->getAppKeys(), true); |
| 50 | + }))->willReturnCallback(function ($userId, $appId, $appConfigKey): string { |
| 51 | + return $this->getAppValue($appConfigKey); |
| 52 | + }); |
| 53 | + |
| 54 | + $this->migrationService->exportAppConfiguration($this->user, $this->exportDestination, $this->output); |
| 55 | + } |
| 56 | + |
| 57 | + public function testExportsNoAppConfiguration(): void { |
| 58 | + $trustedSendersList = []; |
| 59 | + $this->exportDestination->expects(self::once())->method('addFileContents')->with(AppConfigMigrationService::APP_CONFIGURATION_FILE, json_encode($trustedSendersList)); |
| 60 | + |
| 61 | + $this->serviceMock->getParameter('config')->expects(self::once())->method('getUserKeys')->with(self::USER_ID, Application::APP_ID)->willReturn($trustedSendersList); |
| 62 | + $this->serviceMock->getParameter('config')->expects(self::never())->method('getUserValue'); |
| 63 | + |
| 64 | + $this->migrationService->exportAppConfiguration($this->user, $this->exportDestination, $this->output); |
| 65 | + } |
| 66 | + |
| 67 | + public function testImportMultipleAppConfigurations(): void { |
| 68 | + $this->importSource->expects(self::once())->method('getFileContents')->with(AppConfigMigrationService::APP_CONFIGURATION_FILE)->willReturn(json_encode($this->getAppConfig())); |
| 69 | + |
| 70 | + $this->serviceMock->getParameter('config')->expects(self::exactly(3))->method('setUserValue')->with(self::USER_ID, Application::APP_ID, self::callback(function ($key) { |
| 71 | + return in_array($key, $this->getAppKeys()); |
| 72 | + }), self::callback(function ($searchedValue): bool { |
| 73 | + return in_array($searchedValue, $this->getAppValues(), true); |
| 74 | + })); |
| 75 | + |
| 76 | + $this->migrationService->importAppConfiguration($this->user, $this->importSource, $this->output); |
| 77 | + } |
| 78 | + |
| 79 | + public function testImportNoAppConfiguration(): void { |
| 80 | + $trustedSendersList = []; |
| 81 | + $this->importSource->expects(self::once())->method('getFileContents')->with(AppConfigMigrationService::APP_CONFIGURATION_FILE)->willReturn(json_encode($trustedSendersList)); |
| 82 | + |
| 83 | + $this->serviceMock->getParameter('config')->expects(self::never())->method('setUserValue'); |
| 84 | + |
| 85 | + $this->migrationService->importAppConfiguration($this->user, $this->importSource, $this->output); |
| 86 | + } |
| 87 | + |
| 88 | + private function getAppConfig(): array { |
| 89 | + return [ |
| 90 | + ['key' => 'account-settings', |
| 91 | + 'value' => '[{\"accountId\":19,\"collapsed\":false}]'], |
| 92 | + ['key' => 'collect-data', |
| 93 | + 'value' => 'true' |
| 94 | + ], |
| 95 | + ['key' => 'ui-heartbeat', |
| 96 | + 'value' => '1770367800'] |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + private function getAppKeys(): array { |
| 101 | + return array_map(function (array $appConfig) { |
| 102 | + return $appConfig['key']; |
| 103 | + }, $this->getAppConfig()); |
| 104 | + } |
| 105 | + |
| 106 | + private function getAppValue(string $key): ?string { |
| 107 | + foreach ($this->getAppConfig() as $appConfig) { |
| 108 | + if ($appConfig['key'] === $key) { |
| 109 | + return $appConfig['value']; |
| 110 | + } |
| 111 | + } |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + private function getAppValues(): array { |
| 116 | + return array_map(function (array $appConfig) { |
| 117 | + return $appConfig['value']; |
| 118 | + }, $this->getAppConfig()); |
| 119 | + } |
| 120 | +} |
0 commit comments