|
| 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 OCA\Settings\Tests; |
| 11 | + |
| 12 | +use OCA\Settings\Hooks; |
| 13 | +use OCP\Activity\IEvent; |
| 14 | +use OCP\Activity\IManager as IActivityManager; |
| 15 | +use OCP\Defaults; |
| 16 | +use OCP\IConfig; |
| 17 | +use OCP\IGroupManager; |
| 18 | +use OCP\IL10N; |
| 19 | +use OCP\IURLGenerator; |
| 20 | +use OCP\IUser; |
| 21 | +use OCP\IUserManager; |
| 22 | +use OCP\IUserSession; |
| 23 | +use OCP\L10N\IFactory; |
| 24 | +use OCP\Mail\EMailDetails; |
| 25 | +use OCP\Mail\EMailDetailsRow; |
| 26 | +use OCP\Mail\IEMailTemplate; |
| 27 | +use OCP\Mail\IMailer; |
| 28 | +use OCP\Mail\IMessage; |
| 29 | +use OCP\User\Events\PasswordUpdatedEvent; |
| 30 | +use OCP\User\Events\UserChangedEvent; |
| 31 | +use PHPUnit\Framework\MockObject\MockObject; |
| 32 | +use Test\TestCase; |
| 33 | + |
| 34 | +class HooksTest extends TestCase { |
| 35 | + private IMailer&MockObject $mailer; |
| 36 | + private IEMailTemplate&MockObject $template; |
| 37 | + private IUser&MockObject $user; |
| 38 | + private Hooks $hooks; |
| 39 | + |
| 40 | + protected function setUp(): void { |
| 41 | + parent::setUp(); |
| 42 | + |
| 43 | + $activityManager = $this->createMock(IActivityManager::class); |
| 44 | + $activityManager->method('generateEvent')->willReturn($this->createMock(IEvent::class)); |
| 45 | + |
| 46 | + $urlGenerator = $this->createMock(IURLGenerator::class); |
| 47 | + $urlGenerator->method('getAbsoluteURL')->willReturn('https://cloud.example.com/'); |
| 48 | + |
| 49 | + $l10n = $this->createMock(IL10N::class); |
| 50 | + $l10n->method('t')->willReturnCallback(fn (string $text, array $parameters = []): string => vsprintf($text, $parameters)); |
| 51 | + $l10nFactory = $this->createMock(IFactory::class); |
| 52 | + $l10nFactory->method('get')->with('settings')->willReturn($l10n); |
| 53 | + |
| 54 | + $this->user = $this->createMock(IUser::class); |
| 55 | + $this->user->method('getUID')->willReturn('alice'); |
| 56 | + $this->user->method('getDisplayName')->willReturn('Alice'); |
| 57 | + $this->user->method('getLastLogin')->willReturn(1); |
| 58 | + |
| 59 | + $userSession = $this->createMock(IUserSession::class); |
| 60 | + $userSession->method('getUser')->willReturn($this->user); |
| 61 | + |
| 62 | + $this->template = $this->createMock(IEMailTemplate::class); |
| 63 | + $this->mailer = $this->createMock(IMailer::class); |
| 64 | + $this->mailer->method('createEMailTemplate')->willReturn($this->template); |
| 65 | + $this->mailer->method('createMessage')->willReturn($this->createMock(IMessage::class)); |
| 66 | + |
| 67 | + $this->hooks = new Hooks( |
| 68 | + $activityManager, |
| 69 | + $this->createMock(IGroupManager::class), |
| 70 | + $this->createMock(IUserManager::class), |
| 71 | + $userSession, |
| 72 | + $urlGenerator, |
| 73 | + $this->mailer, |
| 74 | + $this->createMock(IConfig::class), |
| 75 | + $l10nFactory, |
| 76 | + $this->createMock(Defaults::class), |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testPasswordChangedMail(): void { |
| 81 | + $this->user->method('getEMailAddress')->willReturn('alice@example.com'); |
| 82 | + |
| 83 | + $this->template->expects($this->once())->method('addBodyText') |
| 84 | + ->with('Your password on https://cloud.example.com/ was changed.'); |
| 85 | + $this->template->expects($this->once())->method('addBodyNote') |
| 86 | + ->with('If you did not request this, please contact an administrator.', '', IEMailTemplate::NOTE_WARNING); |
| 87 | + $this->mailer->expects($this->once())->method('send'); |
| 88 | + |
| 89 | + $this->hooks->handle(new PasswordUpdatedEvent($this->user, 'secret')); |
| 90 | + } |
| 91 | + |
| 92 | + public function testEmailChangedMail(): void { |
| 93 | + $this->user->method('getEMailAddress')->willReturn('new@example.com'); |
| 94 | + |
| 95 | + $this->template->expects($this->once())->method('addBodyText') |
| 96 | + ->with('Your email address on https://cloud.example.com/ was changed.'); |
| 97 | + $this->template->expects($this->once())->method('addBodyDetails') |
| 98 | + ->with($this->callback(fn (EMailDetails $details): bool => $this->detailsToArray($details) === [ |
| 99 | + 'title' => 'Alice', |
| 100 | + 'initials' => 'Alice', |
| 101 | + 'rows' => [ |
| 102 | + ['Previous email address', [[EMailDetailsRow::PART_TEXT, 'old@example.com']]], |
| 103 | + ['New email address', [[EMailDetailsRow::PART_TEXT, 'new@example.com']]], |
| 104 | + ], |
| 105 | + ])); |
| 106 | + $this->template->expects($this->once())->method('addBodyNote') |
| 107 | + ->with('If you did not request this, please contact an administrator.', '', IEMailTemplate::NOTE_WARNING); |
| 108 | + $this->mailer->expects($this->once())->method('send'); |
| 109 | + |
| 110 | + $this->hooks->handle(new UserChangedEvent($this->user, 'eMailAddress', 'new@example.com', 'old@example.com')); |
| 111 | + } |
| 112 | + |
| 113 | + public function testEmailRemovedMail(): void { |
| 114 | + $this->user->method('getEMailAddress')->willReturn(null); |
| 115 | + |
| 116 | + $this->template->expects($this->once())->method('addBodyDetails') |
| 117 | + ->with($this->callback(fn (EMailDetails $details): bool => $this->detailsToArray($details)['rows'] === [ |
| 118 | + ['Previous email address', [[EMailDetailsRow::PART_TEXT, 'old@example.com']]], |
| 119 | + ['New email address', [[EMailDetailsRow::PART_MUTED, 'No email address set']]], |
| 120 | + ])); |
| 121 | + $this->mailer->expects($this->once())->method('send'); |
| 122 | + |
| 123 | + $this->hooks->handle(new UserChangedEvent($this->user, 'eMailAddress', null, 'old@example.com')); |
| 124 | + } |
| 125 | + |
| 126 | + private function detailsToArray(EMailDetails $details): array { |
| 127 | + return [ |
| 128 | + 'title' => $details->getTitle(), |
| 129 | + 'initials' => $details->getInitialsName(), |
| 130 | + 'rows' => array_map( |
| 131 | + fn (EMailDetailsRow $row): array => [ |
| 132 | + $row->getLabel(), |
| 133 | + array_map(fn (array $part): array => [$part['type'], $part['text']], $row->getParts()), |
| 134 | + ], |
| 135 | + $details->getRows(), |
| 136 | + ), |
| 137 | + ]; |
| 138 | + } |
| 139 | +} |
0 commit comments