|
| 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-only |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Unit\UserMigration\Service; |
| 11 | + |
| 12 | +use ChristophWurst\Nextcloud\Testing\ServiceMockObject; |
| 13 | +use ChristophWurst\Nextcloud\Testing\TestCase; |
| 14 | +use OCA\Mail\Db\TrustedSender; |
| 15 | +use OCA\Mail\UserMigration\Service\TrustedSendersMigrationService; |
| 16 | +use OCP\IUser; |
| 17 | +use OCP\UserMigration\IExportDestination; |
| 18 | +use OCP\UserMigration\IImportSource; |
| 19 | +use OCP\UserMigration\UserMigrationException; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | + |
| 22 | +class TrustedSendersMigrationServiceTest extends TestCase { |
| 23 | + private const USER_ID = '123'; |
| 24 | + private OutputInterface $output; |
| 25 | + private IUser $user; |
| 26 | + private IExportDestination $exportDestination; |
| 27 | + private IImportSource $importSource; |
| 28 | + private ServiceMockObject $serviceMock; |
| 29 | + private TrustedSendersMigrationService $migrationService; |
| 30 | + |
| 31 | + protected function setUp(): void { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + $this->output = $this->createMock(OutputInterface::class); |
| 35 | + $this->exportDestination = $this->createMock(IExportDestination::class); |
| 36 | + $this->importSource = $this->createMock(IImportSource::class); |
| 37 | + |
| 38 | + $this->user = $this->createMock(IUser::class); |
| 39 | + $this->user->method('getUID')->willReturn(self::USER_ID); |
| 40 | + |
| 41 | + $this->serviceMock = $this->createServiceMock(TrustedSendersMigrationService::class); |
| 42 | + $this->migrationService = $this->serviceMock->getService(); |
| 43 | + } |
| 44 | + |
| 45 | + public function testExportsMultipleTrustedSenders(): void { |
| 46 | + $trustedSendersList = [$this->getTrustedIndividual(), $this->getTrustedDomain()]; |
| 47 | + $this->exportDestination->expects(self::once())->method('addFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE, json_encode($trustedSendersList)); |
| 48 | + |
| 49 | + $this->serviceMock->getParameter('trustedSenderService')->method('getTrusted')->with(self::USER_ID)->willReturn($trustedSendersList); |
| 50 | + $this->migrationService->exportTrustedSenders($this->user, $this->exportDestination, $this->output); |
| 51 | + } |
| 52 | + |
| 53 | + public function testExportsNoneTrustedSenders(): void { |
| 54 | + $trustedSendersList = []; |
| 55 | + $this->exportDestination->expects(self::once())->method('addFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE, json_encode($trustedSendersList)); |
| 56 | + |
| 57 | + $this->serviceMock->getParameter('trustedSenderService')->method('getTrusted')->with(self::USER_ID)->willReturn($trustedSendersList); |
| 58 | + $this->migrationService->exportTrustedSenders($this->user, $this->exportDestination, $this->output); |
| 59 | + } |
| 60 | + |
| 61 | + public function testImportMultipleTrustedSenders(): void { |
| 62 | + $trustedIndividual = $this->getTrustedIndividual(); |
| 63 | + $trustedDomain = $this->getTrustedDomain(); |
| 64 | + $trustedSendersList = [$trustedIndividual, $trustedDomain]; |
| 65 | + $this->importSource->expects(self::once())->method('getFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE)->willReturn(json_encode($trustedSendersList)); |
| 66 | + |
| 67 | + $this->serviceMock->getParameter('trustedSenderService')->expects(self::exactly(2))->method('trust')->with(self::USER_ID, self::callback(function ($email) use ($trustedIndividual, $trustedDomain) { |
| 68 | + return $email === $trustedIndividual->getEmail() || $email === $trustedDomain->getEmail(); |
| 69 | + }), self::callback(function ($type) use ($trustedIndividual, $trustedDomain) { |
| 70 | + return $type === $trustedIndividual->getType() || $type === $trustedDomain->getType(); |
| 71 | + })); |
| 72 | + |
| 73 | + $this->migrationService->importTrustedSenders($this->user, $this->importSource, $this->output); |
| 74 | + } |
| 75 | + |
| 76 | + public function testImportNoneTrustedSenders(): void { |
| 77 | + $trustedSendersList = []; |
| 78 | + $this->importSource->expects(self::once())->method('getFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE)->willReturn(json_encode($trustedSendersList)); |
| 79 | + $this->serviceMock->getParameter('trustedSenderService')->expects(self::never())->method('trust'); |
| 80 | + $this->migrationService->importTrustedSenders($this->user, $this->importSource, $this->output); |
| 81 | + } |
| 82 | + |
| 83 | + public function testImportInvalidJsonThrowsException(): void { |
| 84 | + $this->importSource->expects(self::once())->method('getFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE)->willReturn('this is not valid json {[}'); |
| 85 | + |
| 86 | + $this->serviceMock->getParameter('trustedSenderService')->expects(self::never())->method('trust'); |
| 87 | + $this->expectException(UserMigrationException::class); |
| 88 | + |
| 89 | + $this->migrationService->importTrustedSenders($this->user, $this->importSource, $this->output); |
| 90 | + } |
| 91 | + |
| 92 | + public function testImportEmptyStringThrowsException(): void { |
| 93 | + $this->importSource->expects(self::once())->method('getFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE)->willReturn(''); |
| 94 | + |
| 95 | + $this->serviceMock->getParameter('trustedSenderService')->expects(self::never())->method('trust'); |
| 96 | + $this->expectException(UserMigrationException::class); |
| 97 | + |
| 98 | + $this->migrationService->importTrustedSenders($this->user, $this->importSource, $this->output); |
| 99 | + } |
| 100 | + |
| 101 | + public function testImportJsonWithNonArrayRootThrowsException(): void { |
| 102 | + $this->importSource->expects(self::once())->method('getFileContents')->with(TrustedSendersMigrationService::TRUSTED_SENDERS_FILE)->willReturn('"just a string"'); |
| 103 | + |
| 104 | + $this->serviceMock->getParameter('trustedSenderService')->expects(self::never())->method('trust'); |
| 105 | + $this->expectException(UserMigrationException::class); |
| 106 | + |
| 107 | + $this->migrationService->importTrustedSenders($this->user, $this->importSource, $this->output); |
| 108 | + } |
| 109 | + |
| 110 | + private function getTrustedIndividual(): TrustedSender { |
| 111 | + $individualSender = new TrustedSender; |
| 112 | + |
| 113 | + $individualSender->setId(1); |
| 114 | + $individualSender->setUserId(self::USER_ID); |
| 115 | + $individualSender->setEmail('max@mustermann.com'); |
| 116 | + $individualSender->setType('individual'); |
| 117 | + |
| 118 | + return $individualSender; |
| 119 | + } |
| 120 | + |
| 121 | + private function getTrustedDomain(): TrustedSender { |
| 122 | + $domainSender = new TrustedSender(); |
| 123 | + |
| 124 | + $domainSender->setId(2); |
| 125 | + $domainSender->setUserId(self::USER_ID); |
| 126 | + $domainSender->setEmail('nextcloud.com'); |
| 127 | + $domainSender->setType('domain'); |
| 128 | + |
| 129 | + return $domainSender; |
| 130 | + } |
| 131 | +} |
0 commit comments