|
| 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\Db\InternalAddress; |
| 15 | +use OCA\Mail\UserMigration\Service\InternalAddressesMigrationService; |
| 16 | +use OCP\IUser; |
| 17 | +use OCP\UserMigration\IExportDestination; |
| 18 | +use OCP\UserMigration\IImportSource; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +class InternalAddressesMigrationServiceTest 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 InternalAddressesMigrationService $migrationService; |
| 29 | + |
| 30 | + protected function setUp(): void { |
| 31 | + parent::setUp(); |
| 32 | + |
| 33 | + $this->serviceMock = $this->createServiceMock(InternalAddressesMigrationService::class); |
| 34 | + $this->migrationService = $this->serviceMock->getService(); |
| 35 | + |
| 36 | + $this->user = $this->createMock(IUser::class); |
| 37 | + $this->user->method('getUID')->willReturn(self::USER_ID); |
| 38 | + |
| 39 | + $this->output = $this->createMock(OutputInterface::class); |
| 40 | + $this->exportDestination = $this->createMock(IExportDestination::class); |
| 41 | + $this->importSource = $this->createMock(IImportSource::class); |
| 42 | + } |
| 43 | + |
| 44 | + public function testExportsMultipleInternalAddresses(): void { |
| 45 | + $trustedSendersList = [$this->getTrustedIndividual(), $this->getTrustedDomain()]; |
| 46 | + $this->exportDestination->expects(self::once())->method('addFileContents')->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE, json_encode($trustedSendersList)); |
| 47 | + |
| 48 | + $this->serviceMock->getParameter('internalAddressService')->method('getInternalAddresses')->with(self::USER_ID)->willReturn($trustedSendersList); |
| 49 | + |
| 50 | + $this->migrationService->exportInternalAddresses($this->user, $this->exportDestination, $this->output); |
| 51 | + } |
| 52 | + |
| 53 | + public function testExportsNoneInternalAddress(): void { |
| 54 | + $trustedSendersList = []; |
| 55 | + $this->exportDestination->expects(self::once())->method('addFileContents')->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE, json_encode($trustedSendersList)); |
| 56 | + |
| 57 | + $this->serviceMock->getParameter('internalAddressService')->method('getInternalAddresses')->with(self::USER_ID)->willReturn($trustedSendersList); |
| 58 | + |
| 59 | + $this->migrationService->exportInternalAddresses($this->user, $this->exportDestination, $this->output); |
| 60 | + } |
| 61 | + |
| 62 | + public function testImportMultipleInternalAddresses(): void { |
| 63 | + $trustedIndividual = $this->getTrustedIndividual(); |
| 64 | + $trustedDomain = $this->getTrustedDomain(); |
| 65 | + $trustedSendersList = [$trustedIndividual, $trustedDomain]; |
| 66 | + $this->importSource->expects(self::once())->method('getFileContents')->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE)->willReturn(json_encode($trustedSendersList)); |
| 67 | + |
| 68 | + $this->serviceMock->getParameter('internalAddressService')->expects(self::exactly(2))->method('add')->with(self::USER_ID, self::callback(function ($email) use ($trustedIndividual, $trustedDomain) { |
| 69 | + return $email === $trustedIndividual->getAddress() || $email === $trustedDomain->getAddress(); |
| 70 | + }), self::callback(function ($type) use ($trustedIndividual, $trustedDomain) { |
| 71 | + return $type === $trustedIndividual->getType() || $type === $trustedDomain->getType(); |
| 72 | + })); |
| 73 | + |
| 74 | + $this->migrationService->importInternalAddresses($this->user, $this->importSource, $this->output); |
| 75 | + } |
| 76 | + |
| 77 | + public function testImportNoneInternalAddress(): void { |
| 78 | + $trustedSendersList = []; |
| 79 | + $this->importSource->expects(self::once())->method('getFileContents')->with(InternalAddressesMigrationService::INTERNAL_ADDRESSES_FILE)->willReturn(json_encode($trustedSendersList)); |
| 80 | + $this->serviceMock->getParameter('internalAddressService')->expects(self::never())->method('add'); |
| 81 | + |
| 82 | + $this->migrationService->importInternalAddresses($this->user, $this->importSource, $this->output); |
| 83 | + } |
| 84 | + |
| 85 | + private function getTrustedIndividual(): InternalAddress { |
| 86 | + $individualSender = new InternalAddress; |
| 87 | + |
| 88 | + $individualSender->setId(1); |
| 89 | + $individualSender->setUserId(self::USER_ID); |
| 90 | + $individualSender->setAddress('max@mustermann.com'); |
| 91 | + $individualSender->setType('individual'); |
| 92 | + |
| 93 | + return $individualSender; |
| 94 | + } |
| 95 | + |
| 96 | + private function getTrustedDomain(): InternalAddress { |
| 97 | + $domainSender = new InternalAddress(); |
| 98 | + |
| 99 | + $domainSender->setId(2); |
| 100 | + $domainSender->setUserId(self::USER_ID); |
| 101 | + $domainSender->setAddress('nextcloud.com'); |
| 102 | + $domainSender->setType('domain'); |
| 103 | + |
| 104 | + return $domainSender; |
| 105 | + } |
| 106 | +} |
0 commit comments