Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lib/Notifier/PasswordReset.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ public function __construct(

public function sendMessage(User $user, string $hashKey): void
{
if ($this->isNotifierConfigured()) {
$this->sendNotification($user, $hashKey);
if (!$this->isNotifierConfigured()) {
$this->logger?->warning(
'No password reset e-mail was sent: subscribe {notification} under "ibexa.system.<scope>.notifier.subscriptions" to enable it.',
['notification' => UserPasswordReset::class]
);

return;
}

$this->sendNotification($user, $hashKey);
}

private function sendNotification(User $user, string $token): void
Expand Down
11 changes: 9 additions & 2 deletions src/lib/Notifier/UserInvitation.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ public function __construct(

public function sendInvitation(Invitation $invitation): void
{
if ($this->isNotifierConfigured()) {
$this->sendNotification($invitation);
if (!$this->isNotifierConfigured()) {
$this->logger?->warning(
'No invitation e-mail was sent: subscribe {notification} under "ibexa.system.<scope>.notifier.subscriptions" to enable it.',
['notification' => Notification\UserInvitation::class]
);

return;
}

$this->sendNotification($invitation);
}

private function sendNotification(Invitation $invitation): void
Expand Down
83 changes: 83 additions & 0 deletions tests/lib/Notifier/PasswordResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\AdminUi\Notifier;

use Ibexa\AdminUi\Notifier\Notification\UserPasswordReset;
use Ibexa\AdminUi\Notifier\PasswordReset;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Contracts\Notifications\Service\NotificationServiceInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\Environment;

final class PasswordResetTest extends TestCase
{
public function testSendsMessageWhenNotificationIsSubscribed(): void
{
$notificationService = $this->createMock(NotificationServiceInterface::class);
$notificationService->expects(self::once())->method('send');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::never())->method('warning');

$notifier = $this->createNotifier(
[UserPasswordReset::class => ['channels' => ['email']]],
$notificationService,
$logger
);

$notifier->sendMessage($this->createMock(User::class), 'hash-key');
}

public function testLogsWarningWhenNotificationIsNotSubscribed(): void
{
$notificationService = $this->createMock(NotificationServiceInterface::class);
$notificationService->expects(self::never())->method('send');

$logger = $this->createMock(LoggerInterface::class);
$logger
->expects(self::once())
->method('warning')
->with(
self::stringContains('notifier.subscriptions'),
['notification' => UserPasswordReset::class]
);

$notifier = $this->createNotifier([], $notificationService, $logger);

$notifier->sendMessage($this->createMock(User::class), 'hash-key');
}

/**
* @param array<string, array{channels: array<string>}> $subscriptions
*/
private function createNotifier(
array $subscriptions,
NotificationServiceInterface $notificationService,
LoggerInterface $logger
): PasswordReset {
$configResolver = $this->createMock(ConfigResolverInterface::class);
$configResolver
->method('getParameter')
->with('notifications.subscriptions')
->willReturn($subscriptions);

$notifier = new PasswordReset(
$configResolver,
$this->createMock(Environment::class),
$notificationService,
$this->createMock(KernelInterface::class)
);
$notifier->setLogger($logger);

return $notifier;
}
}
95 changes: 95 additions & 0 deletions tests/lib/Notifier/UserInvitationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\AdminUi\Notifier;

use DateTimeImmutable;
use Ibexa\AdminUi\Notifier\Notification;
use Ibexa\AdminUi\Notifier\UserInvitation;
use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface;
use Ibexa\Contracts\Notifications\Service\NotificationServiceInterface;
use Ibexa\Contracts\User\Invitation\Invitation;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Twig\Environment;

final class UserInvitationTest extends TestCase
{
public function testSendsInvitationWhenNotificationIsSubscribed(): void
{
$notificationService = $this->createMock(NotificationServiceInterface::class);
$notificationService->expects(self::once())->method('send');

$logger = $this->createMock(LoggerInterface::class);
$logger->expects(self::never())->method('warning');

$sender = $this->createSender(
[Notification\UserInvitation::class => ['channels' => ['email']]],
$notificationService,
$logger
);

$sender->sendInvitation($this->createInvitation());
}

public function testLogsWarningWhenNotificationIsNotSubscribed(): void
{
$notificationService = $this->createMock(NotificationServiceInterface::class);
$notificationService->expects(self::never())->method('send');

$logger = $this->createMock(LoggerInterface::class);
$logger
->expects(self::once())
->method('warning')
->with(
self::stringContains('notifier.subscriptions'),
['notification' => Notification\UserInvitation::class]
);

$sender = $this->createSender([], $notificationService, $logger);

$sender->sendInvitation($this->createInvitation());
}

/**
* @param array<string, array{channels: array<string>}> $subscriptions
*/
private function createSender(
array $subscriptions,
NotificationServiceInterface $notificationService,
LoggerInterface $logger
): UserInvitation {
$configResolver = $this->createMock(ConfigResolverInterface::class);
$configResolver
->method('getParameter')
->with('notifications.subscriptions')
->willReturn($subscriptions);

$sender = new UserInvitation(
$this->createMock(Environment::class),
$configResolver,
$notificationService,
$this->createMock(KernelInterface::class)
);
$sender->setLogger($logger);

return $sender;
}

private function createInvitation(): Invitation
{
return new Invitation(
'invitee@example.com',
'hash',
new DateTimeImmutable('2026-01-01 00:00:00'),
'site',
false
);
}
}
Loading