|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Guests\Test\Unit\Notifications; |
| 10 | + |
| 11 | +use OCA\Guests\Notifications\Notifier; |
| 12 | +use OCP\IL10N; |
| 13 | +use OCP\IURLGenerator; |
| 14 | +use OCP\IUserManager; |
| 15 | +use OCP\L10N\IFactory; |
| 16 | +use OCP\Notification\INotification; |
| 17 | +use Test\TestCase; |
| 18 | + |
| 19 | +class NotifierTest extends TestCase { |
| 20 | + private ?Notifier $notifier = null; |
| 21 | + |
| 22 | + protected function setUp(): void { |
| 23 | + parent::setUp(); |
| 24 | + |
| 25 | + $l10n = $this->createMock(IL10N::class); |
| 26 | + $l10n->method('t') |
| 27 | + ->willReturnArgument(0); |
| 28 | + $l10nFactory = $this->createMock(IFactory::class); |
| 29 | + $l10nFactory->method('get') |
| 30 | + ->willReturn($l10n); |
| 31 | + |
| 32 | + // Both accounts are gone by the time the notification is rendered. |
| 33 | + $userManager = $this->createMock(IUserManager::class); |
| 34 | + $userManager->method('get') |
| 35 | + ->willReturn(null); |
| 36 | + |
| 37 | + $this->notifier = new Notifier( |
| 38 | + $l10nFactory, |
| 39 | + $this->createMock(IURLGenerator::class), |
| 40 | + $userManager |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public function testTransferDoneFallsBackToTheNotifiedIds(): void { |
| 45 | + $parameters = $this->prepareTransferNotification('guest-transfer-done'); |
| 46 | + |
| 47 | + $this->assertSame('guest@example.tld', $parameters['guest']['id']); |
| 48 | + $this->assertSame('new_account', $parameters['user']['id']); |
| 49 | + } |
| 50 | + |
| 51 | + public function testTransferFailFallsBackToTheNotifiedIds(): void { |
| 52 | + $parameters = $this->prepareTransferNotification('guest-transfer-fail'); |
| 53 | + |
| 54 | + $this->assertSame('guest@example.tld', $parameters['guest']['id']); |
| 55 | + $this->assertSame('new_account', $parameters['user']['id']); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return array<string, array<string, mixed>> |
| 60 | + */ |
| 61 | + private function prepareTransferNotification(string $subject): array { |
| 62 | + $notification = $this->createMock(INotification::class); |
| 63 | + $notification->method('getApp') |
| 64 | + ->willReturn('guests'); |
| 65 | + $notification->method('getSubject') |
| 66 | + ->willReturn($subject); |
| 67 | + $notification->method('getSubjectParameters') |
| 68 | + ->willReturn([ |
| 69 | + 'source' => 'guest@example.tld', |
| 70 | + 'target' => 'new_account', |
| 71 | + ]); |
| 72 | + $notification->method('setRichSubject') |
| 73 | + ->willReturnSelf(); |
| 74 | + |
| 75 | + $parameters = []; |
| 76 | + $notification->expects($this->once()) |
| 77 | + ->method('setRichMessage') |
| 78 | + ->willReturnCallback(function (string $message, array $messageParameters) use (&$parameters, $notification): INotification { |
| 79 | + $parameters = $messageParameters; |
| 80 | + return $notification; |
| 81 | + }); |
| 82 | + |
| 83 | + $this->notifier->prepare($notification, 'en'); |
| 84 | + |
| 85 | + return $parameters; |
| 86 | + } |
| 87 | +} |
0 commit comments