From 74e146450d0848c169039d411ee16451a64241f4 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:43:43 +0000 Subject: [PATCH 01/16] test: reproduce lost invitations for email signers with accounts Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> From 1db95756780905af6e18807941b01c5ea1ac640c Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:44:25 +0000 Subject: [PATCH 02/16] fix: deliver invitations to explicit email signers Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Listener/MailNotifyListener.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index 4c17b5f1b2..e7818a15d6 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -81,11 +81,10 @@ protected function sendSignMailNotification( return; } - $users = $this->userManager->getByEmail($email); - if (count($users) === 1) { - $userId = $users[0]->getUID(); - if ($this->notificationPreferenceResolver->isEmailNotificationDisabled( - $userId, + if ($identifyMethod->getName() === 'account') { + $users = $this->userManager->getByEmail($email); + if (count($users) === 1 && $this->notificationPreferenceResolver->isEmailNotificationDisabled( + $users[0]->getUID(), SendSignNotificationEvent::FILE_TO_SIGN, true, )) { From 3caae58a1c6a2b418d33cde032a12dd276ef69fc Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 02:44:41 +0000 Subject: [PATCH 03/16] test: preserve account email notification preferences Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> From e7cb6955d8848cd42c07f1b6a0013e3a49b93428 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:10:48 +0000 Subject: [PATCH 04/16] refactor: centralize mail recipient resolution for sign events Remove unused listener dependencies and resolve account notification preferences by UID, leaving email-identified signers on mail delivery. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Listener/MailNotifyListener.php | 37 +++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index e7818a15d6..b496892f4d 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -22,13 +22,11 @@ use OCP\EventDispatcher\IEventListener; use OCP\IUser; use OCP\IUserManager; -use OCP\IUserSession; use Psr\Log\LoggerInterface; /** @template-implements IEventListener */ class MailNotifyListener implements IEventListener { public function __construct( - protected IUserSession $userSession, protected IUserManager $userManager, protected IdentifyService $identifyService, protected MailService $mail, @@ -65,7 +63,8 @@ protected function sendSignMailNotification( IIdentifyMethod $identifyMethod, ): void { try { - if ($identifyMethod->getEntity()->isDeletedAccount()) { + $email = $this->getRecipientEmail($identifyMethod, SendSignNotificationEvent::FILE_TO_SIGN); + if ($email === null) { return; } $email = ''; @@ -145,7 +144,8 @@ protected function sendCanceledMailNotification( FileEntity $libreSignFile, ): void { try { - if ($identifyMethod->getEntity()->isDeletedAccount()) { + $email = $this->getRecipientEmail($identifyMethod, SignRequestCanceledEvent::SIGN_REQUEST_CANCELED); + if ($email === null) { return; } @@ -179,4 +179,33 @@ protected function sendCanceledMailNotification( return; } } + + private function getRecipientEmail(IIdentifyMethod $identifyMethod, string $notificationType): ?string { + $entity = $identifyMethod->getEntity(); + if ($entity->isDeletedAccount()) { + return null; + } + + if ($identifyMethod->getName() === 'email') { + $email = $entity->getIdentifierValue(); + return $email !== '' ? $email : null; + } + if ($identifyMethod->getName() !== 'account') { + return null; + } + + $user = $this->userManager->get($entity->getIdentifierValue()); + if (!$user instanceof IUser) { + return null; + } + $email = $user->getEMailAddress(); + if (empty($email) || $this->notificationPreferenceResolver->isEmailNotificationDisabled( + $user->getUID(), + $notificationType, + true, + )) { + return null; + } + return $email; + } } From edf2f1693f4dd028ea301b73c904b90819023475 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:12:02 +0000 Subject: [PATCH 05/16] test: assert mail notification intents at listener boundary Use event inputs and delivery outputs instead of passing messages through MailChannel and MailService in listener tests. Cover invitation, reminder, account preferences, shared emails, cancellation and signed notices. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../Unit/Listener/MailNotifyListenerTest.php | 192 ++++++++++++++++-- 1 file changed, 175 insertions(+), 17 deletions(-) diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index bdb002722c..d4e0d3e5a0 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -13,28 +13,25 @@ use OCA\Libresign\Db\SignRequest; use OCA\Libresign\Db\SignRequestMapper; use OCA\Libresign\Events\SignedEvent; +use OCA\Libresign\Events\SignRequestCanceledEvent; use OCA\Libresign\Listener\MailNotifyListener; -use OCA\Libresign\Service\IdentifyMethod\IdentifyService; use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod; use OCA\Libresign\Service\MailService; use OCA\Libresign\Service\NotificationPreferenceResolver; use OCP\Files\File; use OCP\IUser; use OCP\IUserManager; -use OCP\IUserSession; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; class MailNotifyListenerTest extends TestCase { - private MailService&MockObject $mailService; private IUserManager&MockObject $userManager; private NotificationPreferenceResolver&MockObject $notificationPreferenceResolver; private MailNotifyListener $listener; - public function setUp(): void { - $this->mailService = $this->createMock(MailService::class); + protected function setUp(): void { $this->userManager = $this->createMock(IUserManager::class); $this->notificationPreferenceResolver = $this->createMock(NotificationPreferenceResolver::class); $userSession = $this->createMock(IUserSession::class); @@ -43,7 +40,6 @@ public function setUp(): void { $logger = $this->createMock(LoggerInterface::class); $this->listener = new MailNotifyListener( - $userSession, $this->userManager, $identifyService, $this->mailService, @@ -53,22 +49,131 @@ public function setUp(): void { ); } - public static function provideSignedMailPreferenceScenarios(): array { + public function testEmailSignerWithExistingAccountReceivesInvitation(): void { + $account = $this->account('0F1E2D3C4B5A69788796A5B4C3D2E1F0', 'bob@example.org'); + $this->userManager->method('getByEmail')->willReturn([$account]); + $this->preferences->method('isEmailNotificationDisabled')->willReturn(true); + + $this->listener->handle($this->invitation('email', 'bob@example.org')); + + $message = $this->singleDelivery(); + $this->assertSame('bob@example.org', $message->recipient); + $this->assertSame('sign_request', $message->eventType); + $this->assertSame('mail', $message->channel); + $this->assertSame('mail', $message->historyMethod); + $payload = $message->payload; + $this->assertInstanceOf(MailPayload::class, $payload); + $this->assertTrue($payload->firstNotification); + } + + public function testExternalEmailSignerReceivesInvitation(): void { + $this->listener->handle($this->invitation('email', 'external@example.org')); + + $this->assertSame('external@example.org', $this->singleDelivery()->recipient); + } + + public function testEmailObserverReceivesInvitation(): void { + $request = $this->request(); + $request->setParticipantRole('observer'); + + $this->listener->handle($this->invitation('email', 'observer@example.org', $request)); + + $message = $this->singleDelivery(); + $this->assertSame('observer@example.org', $message->recipient); + $this->assertSame($request, $message->signRequest); + } + + public function testReminderKeepsEmailDestinationAndUsesUpdatedMailTemplate(): void { + $this->persistedRequest->setMetadata(['notify' => [['method' => 'mail', 'date' => 42]]]); + + $this->listener->handle($this->invitation('email', 'bob@example.org')); + + $message = $this->singleDelivery(); + $this->assertSame('bob@example.org', $message->recipient); + $payload = $message->payload; + $this->assertInstanceOf(MailPayload::class, $payload); + $this->assertFalse($payload->firstNotification); + } + + public static function accountPreferences(): array { + return [ + 'email notifications enabled' => [false, true], + 'email notifications disabled' => [true, false], + ]; + } + + #[DataProvider('accountPreferences')] + public function testAccountSignerRespectsMailPreference(bool $disabled, bool $shouldDeliver): void { + $account = $this->account('bob-uid', 'bob@example.org'); + $this->userManager->method('get')->with('bob-uid')->willReturn($account); + $this->preferences->expects($this->once())->method('isEmailNotificationDisabled') + ->with('bob-uid', SendSignNotificationEvent::FILE_TO_SIGN, true) + ->willReturn($disabled); + + $this->listener->handle($this->invitation('account', 'bob-uid')); + + $this->assertCount($shouldDeliver ? 1 : 0, $this->deliveries); + if ($shouldDeliver) { + $this->assertSame('bob@example.org', $this->singleDelivery()->recipient); + } + } + + public function testSharedEmailCannotBypassAccountMailPreference(): void { + $account = $this->account('bob-uid', 'shared@example.org'); + $this->userManager->method('get')->willReturn($account); + $this->userManager->method('getByEmail')->willReturn([ + $account, + $this->account('other-uid', 'shared@example.org'), + ]); + $this->preferences->method('isEmailNotificationDisabled')->willReturn(true); + + $this->listener->handle($this->invitation('account', 'bob-uid')); + + $this->assertSame([], $this->deliveries); + } + + public function testMissingAccountDoesNotDispatch(): void { + $this->userManager->method('get')->willReturn(null); + + $this->listener->handle($this->invitation('account', 'deleted-local-user')); + + $this->assertSame([], $this->deliveries); + } + + public function testDeletedSignerDoesNotDispatch(): void { + $this->listener->handle($this->invitation('email', 'deleted@example.org', null, true)); + + $this->assertSame([], $this->deliveries); + } + + public function testEmailSignerReceivesCancellationEvenWhenAddressBelongsToAccount(): void { + $this->userManager->method('getByEmail')->willReturn([ + $this->account('bob-uid', 'bob@example.org'), + ]); + $this->preferences->method('isEmailNotificationDisabled')->willReturn(true); + + $this->listener->handle(new SignRequestCanceledEvent( + $this->request(), + new FileEntity(), + $this->identifyMethod('email', 'bob@example.org'), + )); + + $message = $this->singleDelivery(); + $this->assertSame('sign_request_canceled', $message->eventType); + $this->assertSame('bob@example.org', $message->recipient); + } + + public static function signedMailPreferences(): array { return [ 'notification disabled' => [true, false], 'notification enabled' => [false, true], ]; } - #[DataProvider('provideSignedMailPreferenceScenarios')] - public function testSignedMailRespectsNotificationPreference( - bool $notificationDisabled, - bool $shouldSend, - ): void { - $this->notificationPreferenceResolver->expects($this->once()) - ->method('isEmailNotificationDisabled') - ->with('admin', SignedEvent::FILE_SIGNED, true) - ->willReturn($notificationDisabled); + #[DataProvider('signedMailPreferences')] + public function testSignedMailRespectsNotificationPreference(bool $disabled, bool $shouldDeliver): void { + $this->preferences->method('isEmailNotificationDisabled')->willReturn($disabled); + $owner = $this->account('admin', 'admin@example.org'); $owner = $this->createMock(IUser::class); $owner->method('getEMailAddress')->willReturn('admin@email.tld'); @@ -107,7 +212,60 @@ private function createSignedEvent(IUser $owner): SignedEvent { $libreSignFile, $identifyMethod, $owner, - $signedFile, + $this->createMock(File::class), + )); + + $this->assertCount($shouldDeliver ? 1 : 0, $this->deliveries); + if ($shouldDeliver) { + $this->assertSame('admin@example.org', $this->singleDelivery()->recipient); + $this->assertSame('file_signed', $this->singleDelivery()->eventType); + } + } + + private function request(): SignRequest { + $request = new SignRequest(); + $request->setId(42); + $request->setUuid('synthetic-request'); + $request->setDescription('Please sign'); + return $request; + } + + private function invitation( + string $method, + string $value, + ?SignRequest $request = null, + bool $deleted = false, + ): SendSignNotificationEvent { + return new SendSignNotificationEvent( + $request ?? $this->request(), + new FileEntity(), + $this->identifyMethod($method, $value, $deleted), + 'invitation-42', ); } + + private function identifyMethod(string $method, string $value, bool $deleted = false): IIdentifyMethod { + $entity = new IdentifyMethodEntity(); + $entity->setIdentifierKey($method); + $entity->setIdentifierValue($value); + if ($deleted) { + $entity->setMetadata(['deleted_account' => true]); + } + $identify = $this->createMock(IIdentifyMethod::class); + $identify->method('getName')->willReturn($method); + $identify->method('getEntity')->willReturn($entity); + return $identify; + } + + private function account(string $uid, string $email): IUser { + $user = $this->createMock(IUser::class); + $user->method('getUID')->willReturn($uid); + $user->method('getEMailAddress')->willReturn($email); + return $user; + } + + private function singleDelivery(): NotificationMessage { + $this->assertCount(1, $this->deliveries); + return $this->deliveries[0]; + } } From 7f77c0b7a6808442b801399575a0950fd1b15bc5 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:23:15 +0000 Subject: [PATCH 06/16] fix: preserve cancellation opt-outs for email-identified signers Keep account-based cancellation preferences when a signer was identified by email, without reintroducing invitation suppression from issue #9425. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Listener/MailNotifyListener.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index b496892f4d..8053c8aedc 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -188,7 +188,11 @@ private function getRecipientEmail(IIdentifyMethod $identifyMethod, string $noti if ($identifyMethod->getName() === 'email') { $email = $entity->getIdentifierValue(); - return $email !== '' ? $email : null; + if ($email === '' || ($notificationType === SignRequestCanceledEvent::SIGN_REQUEST_CANCELED + && $this->isEmailCancellationDisabled($email))) { + return null; + } + return $email; } if ($identifyMethod->getName() !== 'account') { return null; @@ -208,4 +212,13 @@ private function getRecipientEmail(IIdentifyMethod $identifyMethod, string $noti } return $email; } + + private function isEmailCancellationDisabled(string $email): bool { + $users = $this->userManager->getByEmail($email); + return count($users) === 1 && $this->notificationPreferenceResolver->isEmailNotificationDisabled( + $users[0]->getUID(), + SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, + true, + ); + } } From 34310bc75bc42920b8d1c83ac82591e00f829d70 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:23:49 +0000 Subject: [PATCH 07/16] test: verify cancellation policy and mail outputs without call-count coupling Cover existing-account cancellation opt-outs, external recipients, unknown methods and empty addresses. Keep mock configuration readable and assert observable delivery intents instead of internal invocation sequences. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../Unit/Listener/MailNotifyListenerTest.php | 75 ++++++++++++++----- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index d4e0d3e5a0..094e086e71 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -105,10 +105,12 @@ public static function accountPreferences(): array { #[DataProvider('accountPreferences')] public function testAccountSignerRespectsMailPreference(bool $disabled, bool $shouldDeliver): void { $account = $this->account('bob-uid', 'bob@example.org'); - $this->userManager->method('get')->with('bob-uid')->willReturn($account); - $this->preferences->expects($this->once())->method('isEmailNotificationDisabled') - ->with('bob-uid', SendSignNotificationEvent::FILE_TO_SIGN, true) - ->willReturn($disabled); + $this->userManager + ->method('get') + ->willReturn($account); + $this->preferences + ->method('isEmailNotificationDisabled') + ->willReturnCallback(static fn (string $uid): bool => $uid === 'bob-uid' && $disabled); $this->listener->handle($this->invitation('account', 'bob-uid')); @@ -125,7 +127,9 @@ public function testSharedEmailCannotBypassAccountMailPreference(): void { $account, $this->account('other-uid', 'shared@example.org'), ]); - $this->preferences->method('isEmailNotificationDisabled')->willReturn(true); + $this->preferences + ->method('isEmailNotificationDisabled') + ->willReturnCallback(static fn (string $uid): bool => $uid === 'bob-uid'); $this->listener->handle($this->invitation('account', 'bob-uid')); @@ -140,27 +144,54 @@ public function testMissingAccountDoesNotDispatch(): void { $this->assertSame([], $this->deliveries); } + public function testUnsupportedIdentificationMethodDoesNotDispatch(): void { + $this->listener->handle($this->invitation('sms', '5511999999999')); + + $this->assertSame([], $this->deliveries); + } + + public function testEmptyEmailDoesNotDispatch(): void { + $this->listener->handle($this->invitation('email', '')); + + $this->assertSame([], $this->deliveries); + } + public function testDeletedSignerDoesNotDispatch(): void { $this->listener->handle($this->invitation('email', 'deleted@example.org', null, true)); $this->assertSame([], $this->deliveries); } - public function testEmailSignerReceivesCancellationEvenWhenAddressBelongsToAccount(): void { - $this->userManager->method('getByEmail')->willReturn([ - $this->account('bob-uid', 'bob@example.org'), - ]); - $this->preferences->method('isEmailNotificationDisabled')->willReturn(true); + public static function emailCancellationPreferences(): array { + return [ + 'notifications enabled' => [false, true], + 'notifications disabled' => [true, false], + ]; + } - $this->listener->handle(new SignRequestCanceledEvent( - $this->request(), - new FileEntity(), - $this->identifyMethod('email', 'bob@example.org'), - )); + #[DataProvider('emailCancellationPreferences')] + public function testEmailSignerCancellationRespectsExistingAccountPreference(bool $disabled, bool $shouldDeliver): void { + $account = $this->account('bob-uid', 'bob@example.org'); + $this->userManager + ->method('getByEmail') + ->willReturn([$account]); + $this->preferences + ->method('isEmailNotificationDisabled') + ->willReturnCallback(static fn (string $uid): bool => $uid === 'bob-uid' && $disabled); - $message = $this->singleDelivery(); - $this->assertSame('sign_request_canceled', $message->eventType); - $this->assertSame('bob@example.org', $message->recipient); + $this->listener->handle($this->cancellation('email', 'bob@example.org')); + + $this->assertCount($shouldDeliver ? 1 : 0, $this->deliveries); + if ($shouldDeliver) { + $this->assertSame('bob@example.org', $this->singleDelivery()->recipient); + $this->assertSame('sign_request_canceled', $this->singleDelivery()->eventType); + } + } + + public function testExternalEmailSignerReceivesCancellation(): void { + $this->listener->handle($this->cancellation('email', 'carol@example.org')); + + $this->assertSame('carol@example.org', $this->singleDelivery()->recipient); } public static function signedMailPreferences(): array { @@ -244,6 +275,14 @@ private function invitation( ); } + private function cancellation(string $method, string $value): SignRequestCanceledEvent { + return new SignRequestCanceledEvent( + $this->request(), + new FileEntity(), + $this->identifyMethod($method, $value), + ); + } + private function identifyMethod(string $method, string $value, bool $deleted = false): IIdentifyMethod { $entity = new IdentifyMethodEntity(); $entity->setIdentifierKey($method); From dceb11bb3c988f1f9801ce48931ee75efed1988e Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 13:56:16 +0000 Subject: [PATCH 08/16] test: mock absence of account for external cancellation signer The Nextcloud user manager returns an empty list when no users match an email address. Model that contract explicitly in the regression test, preventing count(null) across the PHPUnit database matrix. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- tests/php/Unit/Listener/MailNotifyListenerTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index 094e086e71..1cde53c286 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -189,6 +189,8 @@ public function testEmailSignerCancellationRespectsExistingAccountPreference(boo } public function testExternalEmailSignerReceivesCancellation(): void { + $this->userManager->method('getByEmail')->with('carol@example.org')->willReturn([]); + $this->listener->handle($this->cancellation('email', 'carol@example.org')); $this->assertSame('carol@example.org', $this->singleDelivery()->recipient); From 8b091a37925235fe0d2146a0fa010d5df3470985 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 19:21:33 +0000 Subject: [PATCH 09/16] fix: enforce admin email policy for email-identified invitations Keep the only available invitation channel independent of the linked account's personal opt-out while respecting the administrator's channel setting. Cover Activity and app-config fallback paths. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Listener/MailNotifyListener.php | 13 ++++++- .../NotificationPreferenceResolver.php | 19 ++++++++++ .../Unit/Listener/MailNotifyListenerTest.php | 10 +++++ .../NotificationPreferenceResolverTest.php | 38 +++++++++++++++++++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index 8053c8aedc..c4fb62650c 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -188,8 +188,17 @@ private function getRecipientEmail(IIdentifyMethod $identifyMethod, string $noti if ($identifyMethod->getName() === 'email') { $email = $entity->getIdentifierValue(); - if ($email === '' || ($notificationType === SignRequestCanceledEvent::SIGN_REQUEST_CANCELED - && $this->isEmailCancellationDisabled($email))) { + if ($email === '') { + return null; + } + // Invitations need mail even when a matching account opted out. + // Cancellations retain the linked account's preference. + if ($notificationType === SendSignNotificationEvent::FILE_TO_SIGN + && $this->notificationPreferenceResolver->isEmailNotificationGloballyDisabled($notificationType)) { + return null; + } + if ($notificationType === SignRequestCanceledEvent::SIGN_REQUEST_CANCELED + && $this->isEmailCancellationDisabled($email)) { return null; } return $email; diff --git a/lib/Service/NotificationPreferenceResolver.php b/lib/Service/NotificationPreferenceResolver.php index a4700025b4..e6e7328d90 100644 --- a/lib/Service/NotificationPreferenceResolver.php +++ b/lib/Service/NotificationPreferenceResolver.php @@ -27,6 +27,25 @@ public function isEmailNotificationDisabled( return $this->isNotificationDisabled($userId, 'email', $type, $requireKnownActivitySetting); } + /** + * Apply the administrator's mail policy without consulting a linked user's opt-out. + */ + public function isEmailNotificationGloballyDisabled( + string $type, + bool $requireKnownActivitySetting = true, + ): bool { + if ($this->activitySettingsStore->isAvailable()) { + if ($requireKnownActivitySetting && !$this->activitySettingsStore->hasSetting($type)) { + return false; + } + return !$this->isActivitySettingEnabled($this->activitySettingsStore->getAdminSetting('email', $type)); + } + + return !$this->isActivitySettingEnabled( + $this->appConfig->getValueString('activity', sprintf('notify_email_%s', $type), '1'), + ); + } + public function isInAppNotificationDisabled( string $userId, string $type, diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index 1cde53c286..626ecbd08d 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -66,6 +66,16 @@ public function testEmailSignerWithExistingAccountReceivesInvitation(): void { $this->assertTrue($payload->firstNotification); } + public function testAdminDisabledEmailChannelSuppressesInvitation(): void { + $this->preferences + ->method('isEmailNotificationGloballyDisabled') + ->willReturn(true); + + $this->listener->handle($this->invitation('email', 'bob@example.org')); + + $this->assertSame([], $this->deliveries); + } + public function testExternalEmailSignerReceivesInvitation(): void { $this->listener->handle($this->invitation('email', 'external@example.org')); diff --git a/tests/php/Unit/Service/NotificationPreferenceResolverTest.php b/tests/php/Unit/Service/NotificationPreferenceResolverTest.php index 7251e16b6c..7656a98e81 100644 --- a/tests/php/Unit/Service/NotificationPreferenceResolverTest.php +++ b/tests/php/Unit/Service/NotificationPreferenceResolverTest.php @@ -8,6 +8,7 @@ namespace OCA\Libresign\Tests\Unit\Service; +use OCA\Libresign\Events\SendSignNotificationEvent; use OCA\Libresign\Events\SignedEvent; use OCA\Libresign\Service\ActivitySettingsStore; use OCA\Libresign\Service\NotificationPreferenceResolver; @@ -70,6 +71,43 @@ public function testEmailFallbackUsesMockedConfigValues( ); } + public static function globalEmailSettings(): array { + return [ + 'admin permits email' => ['1', false], + 'admin disables email' => ['0', true], + ]; + } + + #[DataProvider('globalEmailSettings')] + public function testGlobalEmailPreferenceUsesActivityAdminSetting(string $value, bool $disabled): void { + $this->activitySettingsStore->method('isAvailable')->willReturn(true); + $this->activitySettingsStore->method('hasSetting') + ->with(SendSignNotificationEvent::FILE_TO_SIGN)->willReturn(true); + $this->activitySettingsStore->method('getAdminSetting') + ->with('email', SendSignNotificationEvent::FILE_TO_SIGN)->willReturn($value); + $this->activitySettingsStore->expects($this->never())->method('getUserSetting'); + + $this->assertSame($disabled, $this->resolver->isEmailNotificationGloballyDisabled(SendSignNotificationEvent::FILE_TO_SIGN)); + } + + #[DataProvider('globalEmailSettings')] + public function testGlobalEmailPreferenceUsesAppConfigFallback(string $value, bool $disabled): void { + $this->activitySettingsStore->method('isAvailable')->willReturn(false); + $this->appConfig->method('getValueString') + ->with('activity', 'notify_email_libresign_file_to_sign', '1')->willReturn($value); + $this->userConfig->expects($this->never())->method('getValueString'); + + $this->assertSame($disabled, $this->resolver->isEmailNotificationGloballyDisabled(SendSignNotificationEvent::FILE_TO_SIGN)); + } + + public function testGlobalEmailPreferenceIgnoresUnregisteredActivitySetting(): void { + $this->activitySettingsStore->method('isAvailable')->willReturn(true); + $this->activitySettingsStore->method('hasSetting')->willReturn(false); + $this->activitySettingsStore->expects($this->never())->method('getAdminSetting'); + + $this->assertFalse($this->resolver->isEmailNotificationGloballyDisabled(SendSignNotificationEvent::FILE_TO_SIGN)); + } + public function testInAppFallbackUsesNotificationChannel(): void { $this->activitySettingsStore->expects($this->once()) ->method('isAvailable') From 2bbf731219cb5b8889faa3909ca1c402027adb1c Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 19:22:40 +0000 Subject: [PATCH 10/16] test: preserve notification event types and history ownership Use UID/type-aware preference maps, assert the listener never mutates legacy notification history, and mirror deleted-account metadata fixtures. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../Unit/Listener/MailNotifyListenerTest.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index 626ecbd08d..268a5277d8 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -120,7 +120,9 @@ public function testAccountSignerRespectsMailPreference(bool $disabled, bool $sh ->willReturn($account); $this->preferences ->method('isEmailNotificationDisabled') - ->willReturnCallback(static fn (string $uid): bool => $uid === 'bob-uid' && $disabled); + ->willReturnMap([ + ['bob-uid', SendSignNotificationEvent::FILE_TO_SIGN, true, $disabled], + ]); $this->listener->handle($this->invitation('account', 'bob-uid')); @@ -139,7 +141,9 @@ public function testSharedEmailCannotBypassAccountMailPreference(): void { ]); $this->preferences ->method('isEmailNotificationDisabled') - ->willReturnCallback(static fn (string $uid): bool => $uid === 'bob-uid'); + ->willReturnMap([ + ['bob-uid', SendSignNotificationEvent::FILE_TO_SIGN, true, true], + ]); $this->listener->handle($this->invitation('account', 'bob-uid')); @@ -187,7 +191,9 @@ public function testEmailSignerCancellationRespectsExistingAccountPreference(boo ->willReturn([$account]); $this->preferences ->method('isEmailNotificationDisabled') - ->willReturnCallback(static fn (string $uid): bool => $uid === 'bob-uid' && $disabled); + ->willReturnMap([ + ['bob-uid', SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true, $disabled], + ]); $this->listener->handle($this->cancellation('email', 'bob@example.org')); @@ -300,7 +306,10 @@ private function identifyMethod(string $method, string $value, bool $deleted = f $entity->setIdentifierKey($method); $entity->setIdentifierValue($value); if ($deleted) { - $entity->setMetadata(['deleted_account' => true]); + $entity->setMetadata(['deleted_account' => [ + 'account' => $value, + 'display_name' => 'Deleted user', + ]]); } $identify = $this->createMock(IIdentifyMethod::class); $identify->method('getName')->willReturn($method); From 18f92ed38164fa7e1e7749dbd2b608e42a919e95 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 23:57:03 +0000 Subject: [PATCH 11/16] test: isolate Behat email invitation admin settings Restore global notification settings after the account-search opt-out scenario and explicitly enable invitation mail in the PDF validation scenario, avoiding an implicit dependency on shard execution order. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- tests/integration/features/identify-account/search.feature | 1 + tests/integration/features/page/validate.feature | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/integration/features/identify-account/search.feature b/tests/integration/features/identify-account/search.feature index 82a80f068b..3d36a20fb9 100644 --- a/tests/integration/features/identify-account/search.feature +++ b/tests/integration/features/identify-account/search.feature @@ -173,3 +173,4 @@ Feature: search | (jq).ocs.data[0].identify | notification-global-off | | (jq).ocs.data[0].method | account | | (jq).ocs.data[0].acceptsEmailNotifications | false | + And run the command "config:app:set activity notify_email_libresign_file_to_sign --value=1" with result code 0 diff --git a/tests/integration/features/page/validate.feature b/tests/integration/features/page/validate.feature index 8dab524ea9..a0f0536a58 100644 --- a/tests/integration/features/page/validate.feature +++ b/tests/integration/features/page/validate.feature @@ -74,6 +74,7 @@ Feature: page/validate Scenario: Unauthenticated email signer can fetch PDF and gets controlled error after source deletion Given as user "admin" + And run the command "config:app:set activity notify_email_libresign_file_to_sign --value=1" with result code 0 And sending "post" to ocs "/apps/libresign/api/v1/policies/system/identify_methods" | value | (string){"can_create_account":false,"factors":[{"name":"email","enabled":true,"requirement":"required","signatureMethods":{"clickToSign":{"enabled":true}}}]} | And my inbox is empty From c47cc817b4c988965434c37a146b2f98be9c5e9a Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:37:20 +0000 Subject: [PATCH 12/16] test: assert invitation type in admin email preference check Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- tests/php/Unit/Listener/MailNotifyListenerTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index 268a5277d8..6425952377 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -69,7 +69,9 @@ public function testEmailSignerWithExistingAccountReceivesInvitation(): void { public function testAdminDisabledEmailChannelSuppressesInvitation(): void { $this->preferences ->method('isEmailNotificationGloballyDisabled') - ->willReturn(true); + ->willReturnMap([ + [SendSignNotificationEvent::FILE_TO_SIGN, true], + ]); $this->listener->handle($this->invitation('email', 'bob@example.org')); From 00228af9e684f629f4c5a87fd1f1849e53e94e45 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:50:12 +0000 Subject: [PATCH 13/16] refactor: clarify mail recipient resolution by identifier Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Listener/MailNotifyListener.php | 50 ++++++++++++++++------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index c4fb62650c..32647c54a6 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -63,7 +63,7 @@ protected function sendSignMailNotification( IIdentifyMethod $identifyMethod, ): void { try { - $email = $this->getRecipientEmail($identifyMethod, SendSignNotificationEvent::FILE_TO_SIGN); + $email = $this->resolveEligibleRecipientEmail($identifyMethod, SendSignNotificationEvent::FILE_TO_SIGN); if ($email === null) { return; } @@ -144,7 +144,7 @@ protected function sendCanceledMailNotification( FileEntity $libreSignFile, ): void { try { - $email = $this->getRecipientEmail($identifyMethod, SignRequestCanceledEvent::SIGN_REQUEST_CANCELED); + $email = $this->resolveEligibleRecipientEmail($identifyMethod, SignRequestCanceledEvent::SIGN_REQUEST_CANCELED); if ($email === null) { return; } @@ -180,34 +180,40 @@ protected function sendCanceledMailNotification( } } - private function getRecipientEmail(IIdentifyMethod $identifyMethod, string $notificationType): ?string { + private function resolveEligibleRecipientEmail(IIdentifyMethod $identifyMethod, string $notificationType): ?string { $entity = $identifyMethod->getEntity(); if ($entity->isDeletedAccount()) { return null; } - if ($identifyMethod->getName() === 'email') { - $email = $entity->getIdentifierValue(); - if ($email === '') { - return null; - } - // Invitations need mail even when a matching account opted out. - // Cancellations retain the linked account's preference. - if ($notificationType === SendSignNotificationEvent::FILE_TO_SIGN - && $this->notificationPreferenceResolver->isEmailNotificationGloballyDisabled($notificationType)) { - return null; - } - if ($notificationType === SignRequestCanceledEvent::SIGN_REQUEST_CANCELED - && $this->isEmailCancellationDisabled($email)) { - return null; - } - return $email; - } - if ($identifyMethod->getName() !== 'account') { + return match ($identifyMethod->getName()) { + 'email' => $this->resolveEmailIdentifiedRecipient($entity->getIdentifierValue(), $notificationType), + 'account' => $this->resolveAccountIdentifiedRecipient($entity->getIdentifierValue(), $notificationType), + default => null, + }; + } + + private function resolveEmailIdentifiedRecipient(string $email, string $notificationType): ?string { + if ($email === '') { return null; } - $user = $this->userManager->get($entity->getIdentifierValue()); + // Email-identified invitations ignore a matching account's personal opt-out, + // but still honour the administrator's policy. Cancellations retain the + // linked account's preference when its email address uniquely matches. + $disabled = match ($notificationType) { + SendSignNotificationEvent::FILE_TO_SIGN => + $this->notificationPreferenceResolver->isEmailNotificationGloballyDisabled($notificationType), + SignRequestCanceledEvent::SIGN_REQUEST_CANCELED => + $this->isEmailCancellationDisabled($email), + default => true, + }; + + return $disabled ? null : $email; + } + + private function resolveAccountIdentifiedRecipient(string $uid, string $notificationType): ?string { + $user = $this->userManager->get($uid); if (!$user instanceof IUser) { return null; } From 503c88795522e55a9390a381f61c37edb3a5a093 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:02:58 +0000 Subject: [PATCH 14/16] refactor: make email recipient policy self-documenting refactor: make email recipient policy self-documenting Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> [skip ci] --- lib/Listener/MailNotifyListener.php | 19 ++++++++++--------- .../Unit/Listener/MailNotifyListenerTest.php | 8 ++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index 32647c54a6..de625272c9 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -198,14 +198,11 @@ private function resolveEmailIdentifiedRecipient(string $email, string $notifica return null; } - // Email-identified invitations ignore a matching account's personal opt-out, - // but still honour the administrator's policy. Cancellations retain the - // linked account's preference when its email address uniquely matches. $disabled = match ($notificationType) { - SendSignNotificationEvent::FILE_TO_SIGN => - $this->notificationPreferenceResolver->isEmailNotificationGloballyDisabled($notificationType), - SignRequestCanceledEvent::SIGN_REQUEST_CANCELED => - $this->isEmailCancellationDisabled($email), + SendSignNotificationEvent::FILE_TO_SIGN + => $this->notificationPreferenceResolver->isEmailNotificationGloballyDisabled($notificationType), + SignRequestCanceledEvent::SIGN_REQUEST_CANCELED + => $this->isCancellationDisabledForUniqueLinkedAccount($email), default => true, }; @@ -228,9 +225,13 @@ private function resolveAccountIdentifiedRecipient(string $uid, string $notifica return $email; } - private function isEmailCancellationDisabled(string $email): bool { + private function isCancellationDisabledForUniqueLinkedAccount(string $email): bool { $users = $this->userManager->getByEmail($email); - return count($users) === 1 && $this->notificationPreferenceResolver->isEmailNotificationDisabled( + if (count($users) !== 1) { + return false; + } + + return $this->notificationPreferenceResolver->isEmailNotificationDisabled( $users[0]->getUID(), SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true, diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index 6425952377..b0487328bb 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -49,10 +49,10 @@ protected function setUp(): void { ); } - public function testEmailSignerWithExistingAccountReceivesInvitation(): void { - $account = $this->account('0F1E2D3C4B5A69788796A5B4C3D2E1F0', 'bob@example.org'); - $this->userManager->method('getByEmail')->willReturn([$account]); - $this->preferences->method('isEmailNotificationDisabled')->willReturn(true); + public function testEmailSignerInvitationDoesNotConsultLinkedAccountPreferences(): void { + $this->userManager->expects($this->never())->method('getByEmail'); + $this->userManager->expects($this->never())->method('get'); + $this->preferences->expects($this->never())->method('isEmailNotificationDisabled'); $this->listener->handle($this->invitation('email', 'bob@example.org')); From 64334bdde382c9187dca90779fd5dc0bf1da4b7e Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:33:08 +0000 Subject: [PATCH 15/16] fix(mail): honor resolved recipient policy on stable35 Avoid re-resolving recipients after eligibility and preference checks. Keep the existing stable mail delivery and history paths. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Listener/MailNotifyListener.php | 46 ----------------------------- 1 file changed, 46 deletions(-) diff --git a/lib/Listener/MailNotifyListener.php b/lib/Listener/MailNotifyListener.php index de625272c9..9f2d690b4a 100644 --- a/lib/Listener/MailNotifyListener.php +++ b/lib/Listener/MailNotifyListener.php @@ -67,29 +67,6 @@ protected function sendSignMailNotification( if ($email === null) { return; } - $email = ''; - if ($identifyMethod->getName() === 'account') { - $userId = $identifyMethod->getEntity()->getIdentifierValue(); - $email = $this->userManager - ->get($userId) - ->getEMailAddress(); - } elseif ($identifyMethod->getName() === 'email') { - $email = $identifyMethod->getEntity()->getIdentifierValue(); - } - if (empty($email)) { - return; - } - - if ($identifyMethod->getName() === 'account') { - $users = $this->userManager->getByEmail($email); - if (count($users) === 1 && $this->notificationPreferenceResolver->isEmailNotificationDisabled( - $users[0]->getUID(), - SendSignNotificationEvent::FILE_TO_SIGN, - true, - )) { - return; - } - } $isFirstNotification = $this->signRequestMapper->incrementNotificationCounter($signRequest, 'mail'); if ($isFirstNotification) { @@ -149,29 +126,6 @@ protected function sendCanceledMailNotification( return; } - $email = ''; - if ($identifyMethod->getName() === 'account') { - $userId = $identifyMethod->getEntity()->getIdentifierValue(); - $user = $this->userManager->get($userId); - if ($user) { - $email = $user->getEMailAddress(); - } - } elseif ($identifyMethod->getName() === 'email') { - $email = $identifyMethod->getEntity()->getIdentifierValue(); - } - - if (empty($email)) { - return; - } - - $users = $this->userManager->getByEmail($email); - if (count($users) === 1) { - $userId = $users[0]->getUID(); - if ($this->notificationPreferenceResolver->isEmailNotificationDisabled($userId, SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true)) { - return; - } - } - $this->mail->notifyCanceledRequest($signRequest, $email, $libreSignFile); } catch (\InvalidArgumentException $e) { From dc199b7a5455c3f58b02b4ee0f1080614f25d463 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Sat, 14 Nov 2026 22:33:37 +0000 Subject: [PATCH 16/16] test(mail): adapt recipient policy regressions to stable35 notification pipeline Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../Unit/Listener/MailNotifyListenerTest.php | 349 ++++++++---------- 1 file changed, 156 insertions(+), 193 deletions(-) diff --git a/tests/php/Unit/Listener/MailNotifyListenerTest.php b/tests/php/Unit/Listener/MailNotifyListenerTest.php index b0487328bb..c362938edd 100644 --- a/tests/php/Unit/Listener/MailNotifyListenerTest.php +++ b/tests/php/Unit/Listener/MailNotifyListenerTest.php @@ -12,9 +12,11 @@ use OCA\Libresign\Db\IdentifyMethod as IdentifyMethodEntity; use OCA\Libresign\Db\SignRequest; use OCA\Libresign\Db\SignRequestMapper; +use OCA\Libresign\Events\SendSignNotificationEvent; use OCA\Libresign\Events\SignedEvent; use OCA\Libresign\Events\SignRequestCanceledEvent; use OCA\Libresign\Listener\MailNotifyListener; +use OCA\Libresign\Service\IdentifyMethod\IdentifyService; use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod; use OCA\Libresign\Service\MailService; use OCA\Libresign\Service\NotificationPreferenceResolver; @@ -27,280 +29,246 @@ use Psr\Log\LoggerInterface; class MailNotifyListenerTest extends TestCase { + private MailService&MockObject $mailService; private IUserManager&MockObject $userManager; - private NotificationPreferenceResolver&MockObject $notificationPreferenceResolver; + private NotificationPreferenceResolver&MockObject $preferences; + private SignRequestMapper&MockObject $signRequestMapper; private MailNotifyListener $listener; + private bool $firstNotification = true; protected function setUp(): void { + $this->mailService = $this->createMock(MailService::class); $this->userManager = $this->createMock(IUserManager::class); - $this->notificationPreferenceResolver = $this->createMock(NotificationPreferenceResolver::class); - $userSession = $this->createMock(IUserSession::class); - $identifyService = $this->createMock(IdentifyService::class); - $signRequestMapper = $this->createMock(SignRequestMapper::class); - $logger = $this->createMock(LoggerInterface::class); + $this->preferences = $this->createMock(NotificationPreferenceResolver::class); + $this->signRequestMapper = $this->createMock(SignRequestMapper::class); + $this->signRequestMapper->method('incrementNotificationCounter') + ->willReturnCallback(fn (): bool => $this->firstNotification); $this->listener = new MailNotifyListener( $this->userManager, - $identifyService, + $this->createMock(IdentifyService::class), $this->mailService, - $signRequestMapper, - $logger, - $this->notificationPreferenceResolver, + $this->signRequestMapper, + $this->createMock(LoggerInterface::class), + $this->preferences, ); } - public function testEmailSignerInvitationDoesNotConsultLinkedAccountPreferences(): void { + public function testEmailInvitationIgnoresLinkedAccountOptOut(): void { $this->userManager->expects($this->never())->method('getByEmail'); $this->userManager->expects($this->never())->method('get'); $this->preferences->expects($this->never())->method('isEmailNotificationDisabled'); + $this->preferences->expects($this->once()) + ->method('isEmailNotificationGloballyDisabled') + ->with(SendSignNotificationEvent::FILE_TO_SIGN) + ->willReturn(false); + $this->mailService->expects($this->once()) + ->method('notifyUnsignedUser') + ->with($this->isInstanceOf(SignRequest::class), 'bob@example.org', 'Review document'); $this->listener->handle($this->invitation('email', 'bob@example.org')); - - $message = $this->singleDelivery(); - $this->assertSame('bob@example.org', $message->recipient); - $this->assertSame('sign_request', $message->eventType); - $this->assertSame('mail', $message->channel); - $this->assertSame('mail', $message->historyMethod); - $payload = $message->payload; - $this->assertInstanceOf(MailPayload::class, $payload); - $this->assertTrue($payload->firstNotification); } - public function testAdminDisabledEmailChannelSuppressesInvitation(): void { - $this->preferences + public function testEmailInvitationHonorsAdminDisabledChannel(): void { + $this->preferences->expects($this->once()) ->method('isEmailNotificationGloballyDisabled') - ->willReturnMap([ - [SendSignNotificationEvent::FILE_TO_SIGN, true], - ]); + ->with(SendSignNotificationEvent::FILE_TO_SIGN) + ->willReturn(true); + $this->userManager->expects($this->never())->method('getByEmail'); + $this->signRequestMapper->expects($this->never())->method('getById'); + $this->mailService->expects($this->never())->method('notifyUnsignedUser'); + $this->mailService->expects($this->never())->method('notifySignDataUpdated'); $this->listener->handle($this->invitation('email', 'bob@example.org')); - - $this->assertSame([], $this->deliveries); - } - - public function testExternalEmailSignerReceivesInvitation(): void { - $this->listener->handle($this->invitation('email', 'external@example.org')); - - $this->assertSame('external@example.org', $this->singleDelivery()->recipient); } - public function testEmailObserverReceivesInvitation(): void { - $request = $this->request(); - $request->setParticipantRole('observer'); - - $this->listener->handle($this->invitation('email', 'observer@example.org', $request)); + public function testExternalEmailInvitationIsSentWithoutAccountLookup(): void { + $this->userManager->expects($this->never())->method('getByEmail'); + $this->mailService->expects($this->once()) + ->method('notifyUnsignedUser') + ->with($this->isInstanceOf(SignRequest::class), 'external@example.org', 'Review document'); - $message = $this->singleDelivery(); - $this->assertSame('observer@example.org', $message->recipient); - $this->assertSame($request, $message->signRequest); + $this->listener->handle($this->invitation('email', 'external@example.org')); } - public function testReminderKeepsEmailDestinationAndUsesUpdatedMailTemplate(): void { - $this->persistedRequest->setMetadata(['notify' => [['method' => 'mail', 'date' => 42]]]); + public function testReminderUsesUpdatedMailTemplate(): void { + $this->firstNotification = false; + $this->mailService->expects($this->never())->method('notifyUnsignedUser'); + $this->mailService->expects($this->once()) + ->method('notifySignDataUpdated') + ->with($this->isInstanceOf(SignRequest::class), 'bob@example.org', 'Review document'); $this->listener->handle($this->invitation('email', 'bob@example.org')); - - $message = $this->singleDelivery(); - $this->assertSame('bob@example.org', $message->recipient); - $payload = $message->payload; - $this->assertInstanceOf(MailPayload::class, $payload); - $this->assertFalse($payload->firstNotification); } public static function accountPreferences(): array { return [ - 'email notifications enabled' => [false, true], - 'email notifications disabled' => [true, false], + 'account allows email' => [false, true], + 'account disables email' => [true, false], ]; } #[DataProvider('accountPreferences')] - public function testAccountSignerRespectsMailPreference(bool $disabled, bool $shouldDeliver): void { - $account = $this->account('bob-uid', 'bob@example.org'); - $this->userManager - ->method('get') - ->willReturn($account); - $this->preferences + public function testAccountInvitationUsesUidAndItsPreference(bool $disabled, bool $shouldSend): void { + $this->userManager->method('get')->with('account-uid') + ->willReturn($this->account('account-uid', 'shared@example.org')); + $this->userManager->expects($this->never())->method('getByEmail'); + $this->preferences->expects($this->once()) ->method('isEmailNotificationDisabled') - ->willReturnMap([ - ['bob-uid', SendSignNotificationEvent::FILE_TO_SIGN, true, $disabled], - ]); + ->with('account-uid', SendSignNotificationEvent::FILE_TO_SIGN, true) + ->willReturn($disabled); + $this->mailService->expects($shouldSend ? $this->once() : $this->never()) + ->method('notifyUnsignedUser') + ->with($this->anything(), 'shared@example.org', 'Review document'); - $this->listener->handle($this->invitation('account', 'bob-uid')); - - $this->assertCount($shouldDeliver ? 1 : 0, $this->deliveries); - if ($shouldDeliver) { - $this->assertSame('bob@example.org', $this->singleDelivery()->recipient); - } + $this->listener->handle($this->invitation('account', 'account-uid')); } - public function testSharedEmailCannotBypassAccountMailPreference(): void { - $account = $this->account('bob-uid', 'shared@example.org'); - $this->userManager->method('get')->willReturn($account); - $this->userManager->method('getByEmail')->willReturn([ - $account, - $this->account('other-uid', 'shared@example.org'), - ]); - $this->preferences - ->method('isEmailNotificationDisabled') - ->willReturnMap([ - ['bob-uid', SendSignNotificationEvent::FILE_TO_SIGN, true, true], - ]); + public function testMissingAccountIsSkippedWithoutDereference(): void { + $this->userManager->method('get')->with('absent-uid')->willReturn(null); + $this->mailService->expects($this->never())->method('notifyUnsignedUser'); - $this->listener->handle($this->invitation('account', 'bob-uid')); - - $this->assertSame([], $this->deliveries); + $this->listener->handle($this->invitation('account', 'absent-uid')); } - public function testMissingAccountDoesNotDispatch(): void { - $this->userManager->method('get')->willReturn(null); - - $this->listener->handle($this->invitation('account', 'deleted-local-user')); + public function testAccountWithoutEmailIsSkipped(): void { + $this->userManager->method('get')->with('account-uid') + ->willReturn($this->account('account-uid', '')); + $this->mailService->expects($this->never())->method('notifyUnsignedUser'); - $this->assertSame([], $this->deliveries); + $this->listener->handle($this->invitation('account', 'account-uid')); } - public function testUnsupportedIdentificationMethodDoesNotDispatch(): void { - $this->listener->handle($this->invitation('sms', '5511999999999')); + public function testEmptyAndUnsupportedDestinationsAreSkipped(): void { + $this->mailService->expects($this->never())->method('notifyUnsignedUser'); - $this->assertSame([], $this->deliveries); - } - - public function testEmptyEmailDoesNotDispatch(): void { $this->listener->handle($this->invitation('email', '')); - - $this->assertSame([], $this->deliveries); + $this->listener->handle($this->invitation('phone', '+1234567890')); } - public function testDeletedSignerDoesNotDispatch(): void { - $this->listener->handle($this->invitation('email', 'deleted@example.org', null, true)); + public function testDeletedIdentityDoesNotReceiveInvitationOrCancellation(): void { + $this->mailService->expects($this->never())->method('notifyUnsignedUser'); + $this->mailService->expects($this->never())->method('notifyCanceledRequest'); - $this->assertSame([], $this->deliveries); + $this->listener->handle($this->invitation('account', 'deleted-uid', true)); + $this->listener->handle($this->cancellation('account', 'deleted-uid', true)); } - public static function emailCancellationPreferences(): array { + public static function cancellationPreferences(): array { return [ - 'notifications enabled' => [false, true], - 'notifications disabled' => [true, false], + 'linked account allows cancellation email' => [false, true], + 'linked account disables cancellation email' => [true, false], ]; } - #[DataProvider('emailCancellationPreferences')] - public function testEmailSignerCancellationRespectsExistingAccountPreference(bool $disabled, bool $shouldDeliver): void { - $account = $this->account('bob-uid', 'bob@example.org'); - $this->userManager - ->method('getByEmail') - ->willReturn([$account]); - $this->preferences + #[DataProvider('cancellationPreferences')] + public function testEmailCancellationRetainsUniqueLinkedAccountPreference(bool $disabled, bool $shouldSend): void { + $this->userManager->method('getByEmail')->with('linked@example.org') + ->willReturn([$this->account('account-uid', 'linked@example.org')]); + $this->preferences->expects($this->once()) ->method('isEmailNotificationDisabled') - ->willReturnMap([ - ['bob-uid', SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true, $disabled], - ]); + ->with('account-uid', SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true) + ->willReturn($disabled); + $this->mailService->expects($shouldSend ? $this->once() : $this->never()) + ->method('notifyCanceledRequest') + ->with($this->anything(), 'linked@example.org', $this->isInstanceOf(FileEntity::class)); - $this->listener->handle($this->cancellation('email', 'bob@example.org')); + $this->listener->handle($this->cancellation('email', 'linked@example.org')); + } - $this->assertCount($shouldDeliver ? 1 : 0, $this->deliveries); - if ($shouldDeliver) { - $this->assertSame('bob@example.org', $this->singleDelivery()->recipient); - $this->assertSame('sign_request_canceled', $this->singleDelivery()->eventType); - } + public function testExternalEmailCancellationIsSent(): void { + $this->userManager->method('getByEmail')->with('external@example.org')->willReturn([]); + $this->preferences->expects($this->never())->method('isEmailNotificationDisabled'); + $this->mailService->expects($this->once()) + ->method('notifyCanceledRequest') + ->with($this->anything(), 'external@example.org', $this->isInstanceOf(FileEntity::class)); + + $this->listener->handle($this->cancellation('email', 'external@example.org')); } - public function testExternalEmailSignerReceivesCancellation(): void { - $this->userManager->method('getByEmail')->with('carol@example.org')->willReturn([]); + public function testSharedEmailCancellationIsNotSuppressedByOneUser(): void { + $this->userManager->method('getByEmail')->with('shared@example.org') + ->willReturn([ + $this->account('first', 'shared@example.org'), + $this->account('second', 'shared@example.org'), + ]); + $this->preferences->expects($this->never())->method('isEmailNotificationDisabled'); + $this->mailService->expects($this->once()) + ->method('notifyCanceledRequest') + ->with($this->anything(), 'shared@example.org', $this->isInstanceOf(FileEntity::class)); - $this->listener->handle($this->cancellation('email', 'carol@example.org')); + $this->listener->handle($this->cancellation('email', 'shared@example.org')); + } - $this->assertSame('carol@example.org', $this->singleDelivery()->recipient); + #[DataProvider('accountPreferences')] + public function testAccountCancellationChecksSelectedUid(bool $disabled, bool $shouldSend): void { + $this->userManager->method('get')->with('account-uid') + ->willReturn($this->account('account-uid', 'shared@example.org')); + $this->userManager->expects($this->never())->method('getByEmail'); + $this->preferences->expects($this->once()) + ->method('isEmailNotificationDisabled') + ->with('account-uid', SignRequestCanceledEvent::SIGN_REQUEST_CANCELED, true) + ->willReturn($disabled); + $this->mailService->expects($shouldSend ? $this->once() : $this->never()) + ->method('notifyCanceledRequest') + ->with($this->anything(), 'shared@example.org', $this->isInstanceOf(FileEntity::class)); + + $this->listener->handle($this->cancellation('account', 'account-uid')); } public static function signedMailPreferences(): array { return [ - 'notification disabled' => [true, false], - 'notification enabled' => [false, true], + 'owner enables signed mail' => [false, true], + 'owner disables signed mail' => [true, false], ]; } #[DataProvider('signedMailPreferences')] - public function testSignedMailRespectsNotificationPreference(bool $disabled, bool $shouldDeliver): void { - $this->preferences->method('isEmailNotificationDisabled')->willReturn($disabled); + public function testSignedFileMailStillUsesOwnerPreference(bool $disabled, bool $shouldSend): void { + $this->preferences->expects($this->once()) + ->method('isEmailNotificationDisabled') + ->with('admin', SignedEvent::FILE_SIGNED, true) + ->willReturn($disabled); $owner = $this->account('admin', 'admin@example.org'); - - $owner = $this->createMock(IUser::class); - $owner->method('getEMailAddress')->willReturn('admin@email.tld'); $owner->method('getDisplayName')->willReturn('Admin'); - - $expectation = $shouldSend ? $this->once() : $this->never(); - $this->mailService->expects($expectation) - ->method('notifySignedUser'); - - $this->listener->handle($this->createSignedEvent($owner)); - } - - private function createSignedEvent(IUser $owner): SignedEvent { - $signRequest = new SignRequest(); - $signRequest->setId(42); - $signRequest->setFileId(10); - $signRequest->setDisplayName('Signer Name'); - - $libreSignFile = new FileEntity(); - $libreSignFile->setId(10); - $libreSignFile->setUuid('file-uuid'); - $libreSignFile->setName('Contract.pdf'); - $libreSignFile->setUserId('admin'); - - $identifyEntity = new IdentifyMethodEntity(); - $identifyEntity->setIdentifierKey('account'); - $identifyEntity->setIdentifierValue('signer1'); - - $identifyMethod = $this->createMock(IIdentifyMethod::class); - $identifyMethod->method('getEntity')->willReturn($identifyEntity); - - $signedFile = $this->createMock(File::class); - - return new SignedEvent( - $signRequest, - $libreSignFile, - $identifyMethod, + $this->mailService->expects($shouldSend ? $this->once() : $this->never()) + ->method('notifySignedUser') + ->with($this->anything(), 'admin@example.org', $this->anything(), 'Admin'); + $this->listener->handle(new SignedEvent( + $this->request(), + $this->file(), + $this->identifyMethod('account', 'signer-uid'), $owner, $this->createMock(File::class), )); + } - $this->assertCount($shouldDeliver ? 1 : 0, $this->deliveries); - if ($shouldDeliver) { - $this->assertSame('admin@example.org', $this->singleDelivery()->recipient); - $this->assertSame('file_signed', $this->singleDelivery()->eventType); - } + private function invitation(string $method, string $value, bool $deleted = false): SendSignNotificationEvent { + return new SendSignNotificationEvent( + $this->request(), $this->file(), $this->identifyMethod($method, $value, $deleted), + ); + } + + private function cancellation(string $method, string $value, bool $deleted = false): SignRequestCanceledEvent { + return new SignRequestCanceledEvent( + $this->request(), $this->file(), $this->identifyMethod($method, $value, $deleted), + ); } private function request(): SignRequest { $request = new SignRequest(); $request->setId(42); - $request->setUuid('synthetic-request'); - $request->setDescription('Please sign'); + $request->setFileId(10); + $request->setDescription('Review document'); return $request; } - private function invitation( - string $method, - string $value, - ?SignRequest $request = null, - bool $deleted = false, - ): SendSignNotificationEvent { - return new SendSignNotificationEvent( - $request ?? $this->request(), - new FileEntity(), - $this->identifyMethod($method, $value, $deleted), - 'invitation-42', - ); - } - - private function cancellation(string $method, string $value): SignRequestCanceledEvent { - return new SignRequestCanceledEvent( - $this->request(), - new FileEntity(), - $this->identifyMethod($method, $value), - ); + private function file(): FileEntity { + $file = new FileEntity(); + $file->setId(10); + $file->setUserId('admin'); + $file->setName('Contract.pdf'); + return $file; } private function identifyMethod(string $method, string $value, bool $deleted = false): IIdentifyMethod { @@ -319,15 +287,10 @@ private function identifyMethod(string $method, string $value, bool $deleted = f return $identify; } - private function account(string $uid, string $email): IUser { + private function account(string $uid, string $email): IUser&MockObject { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn($uid); $user->method('getEMailAddress')->willReturn($email); return $user; } - - private function singleDelivery(): NotificationMessage { - $this->assertCount(1, $this->deliveries); - return $this->deliveries[0]; - } }