test(tests): UT for NotificationEvent service

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2024-05-12 00:50:42 +02:00
parent 2328597774
commit b477fb696f
13 changed files with 259 additions and 54 deletions

View File

@@ -94,7 +94,7 @@ abstract class EventDispatcherBase implements EventDispatcherInterface
{
/** @var EventReceiver $receiver */
foreach ($this->receivers as $receiver) {
$events = $receiver->getEventsString();
$events = $receiver->getEvents();
if ($events === '*' || preg_match(sprintf('/%s/i', $events), $eventName)) {
$receiver->update($eventName, $event);

View File

@@ -82,7 +82,7 @@ final class AclHandler extends Provider implements EventReceiver
*
* @return string
*/
public function getEventsString(): string
public function getEvents(): string
{
return $this->events;
}

View File

@@ -37,6 +37,11 @@ trait EventReceiver
{
private readonly string $events;
public function getEvents(): string
{
return $this->events;
}
private function setupEvents(array $userEvents = []): void
{
$reflectionClass = new ReflectionClass($this);

View File

@@ -45,5 +45,5 @@ interface EventReceiver
*
* @return string|null
*/
public function getEventsString(): ?string;
public function getEvents(): ?string;
}

View File

@@ -107,7 +107,7 @@ final class DatabaseHandler extends Provider implements EventReceiver
*
* @return string|null
*/
public function getEventsString(): ?string
public function getEvents(): ?string
{
return $this->events;
}

View File

@@ -35,7 +35,7 @@ final class LogHandler extends LoggerBase
/**
* @inheritDoc
*/
public function getEventsString(): ?string
public function getEvents(): ?string
{
return $this->events;
}

View File

@@ -81,7 +81,7 @@ final class MailEvent extends Service implements EventReceiver
*
* @return string
*/
public function getEventsString(): string
public function getEvents(): string
{
return $this->events;
}

View File

@@ -29,7 +29,8 @@ namespace SP\Domain\Notification\Services;
use Exception;
use SP\Core\Application;
use SP\Core\Events\Event;
use SP\Domain\Common\Providers\EventsTrait;
use SP\Domain\Common\Attributes\EventReceiver as EventReceiverAttribute;
use SP\Domain\Common\Services\EventReceiver as EventReceiverTrait;
use SP\Domain\Common\Services\Service;
use SP\Domain\Core\Events\EventReceiver;
use SP\Domain\Notification\Models\Notification;
@@ -41,16 +42,11 @@ use function SP\processException;
/**
* Class NotificationEvent
*/
#[EventReceiverAttribute('request.account')]
#[EventReceiverAttribute('show.account.link')]
final class NotificationEvent extends Service implements EventReceiver
{
use EventsTrait;
public const EVENTS = [
'request.account',
'show.account.link',
];
private readonly string $events;
use EventReceiverTrait;
public function __construct(
Application $application,
@@ -58,22 +54,7 @@ final class NotificationEvent extends Service implements EventReceiver
) {
parent::__construct($application);
$this->setup();
}
private function setup(): void
{
$this->events = $this->parseEventsToRegex(self::EVENTS);
}
/**
* Devuelve los eventos que implementa el observador en formato cadena
*
* @return string
*/
public function getEventsString(): string
{
return $this->events;
$this->setupEvents();
}
/**
@@ -100,7 +81,7 @@ final class NotificationEvent extends Service implements EventReceiver
private function requestAccountNotification(Event $event): void
{
$eventMessage = $event->getEventMessage();
$userIds = $eventMessage !== null ? $eventMessage->getExtra('userId') : [];
$userIds = $eventMessage?->getExtra('userId') ?? [];
foreach ($userIds as $userId) {
$notification = new Notification(
@@ -134,11 +115,10 @@ final class NotificationEvent extends Service implements EventReceiver
private function showAccountLinkNotification(Event $event): void
{
$eventMessage = $event->getEventMessage();
$notify = $eventMessage !== null ? $eventMessage->getExtra('notify') : [];
if ($notify[0] === true) {
$userId = $eventMessage->getExtra('userId')[0];
$notify = $eventMessage?->getExtra('notify')[0] ?? null;
$userId = $eventMessage?->getExtra('userId')[0] ?? null;
if ($notify === true && $userId) {
$notification = new Notification(
[
'type' => __('Notification'),

View File

@@ -76,7 +76,7 @@ class EventDispatcherTest extends UnitaryTestCase
$event = new Event($this);
$this->eventReceiver->expects(self::once())
->method('getEventsString')
->method('getEvents')
->willReturn(self::VALID_EVENTS);
$this->eventReceiver->expects(self::once())
@@ -92,7 +92,7 @@ class EventDispatcherTest extends UnitaryTestCase
$event = new Event($this);
$this->eventReceiver->expects(self::once())
->method('getEventsString')
->method('getEvents')
->willReturn('*');
$this->eventReceiver->expects(self::once())
@@ -106,7 +106,7 @@ class EventDispatcherTest extends UnitaryTestCase
public function testNotifyWithInvalidEvent()
{
$this->eventReceiver->expects(self::once())
->method('getEventsString')
->method('getEvents')
->willReturn('anotherEvent');
$this->eventReceiver->expects(self::never())

View File

@@ -56,7 +56,7 @@ class DatabaseHandlerTest extends UnitaryTestCase
public function testGetEventsString()
{
$expected = 'test_a\.|test_b\.|upgrade\.|acl\.deny|plugin\.load\.error|show\.authToken|clear\.eventlog|clear\.track|refresh\.masterPassword|update\.masterPassword\.start|update\.masterPassword\.end|request\.account|edit\.user\.password|save\.config\.|create\.tempMasterPassword|run\.import\.start|run\.import\.end';
$out = $this->databaseHandler->getEventsString();
$out = $this->databaseHandler->getEvents();
$this->assertEquals($expected, $out);
}
@@ -68,7 +68,7 @@ class DatabaseHandlerTest extends UnitaryTestCase
$this->configData->setLogEvents([]);
$databaseHandler = new DatabaseHandler($this->application, $this->eventLogService, $this->language);
$out = $databaseHandler->getEventsString();
$out = $databaseHandler->getEvents();
$this->assertEquals($expected, $out);
}

View File

@@ -24,7 +24,7 @@
declare(strict_types=1);
namespace SP\Tests\Domain\Notification\Providers;
namespace SP\Tests\Domain\Notification\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
@@ -44,12 +44,12 @@ use SP\Tests\UnitaryTestCase;
* Class MailHandlerTest
*/
#[Group('unitary')]
class MailHandlerTest extends UnitaryTestCase
class MailEventTest extends UnitaryTestCase
{
private MockObject|MailService $mailService;
private RequestService|MockObject $requestService;
private MailEvent $mailHandler;
private MailEvent $mailEvent;
private ConfigData $configData;
public function testUpdate()
@@ -85,7 +85,7 @@ class MailHandlerTest extends UnitaryTestCase
})
);
$this->mailHandler->update('test_a.update', $event);
$this->mailEvent->update('test_a.update', $event);
}
public function testUpdateWithConfiguredEmail()
@@ -122,7 +122,7 @@ class MailHandlerTest extends UnitaryTestCase
})
);
$this->mailHandler->update('test_a.update', $event);
$this->mailEvent->update('test_a.update', $event);
}
public function testUpdateWithNoEmail()
@@ -137,7 +137,7 @@ class MailHandlerTest extends UnitaryTestCase
->expects($this->never())
->method('send');
$this->mailHandler->update('test_a.update', $event);
$this->mailEvent->update('test_a.update', $event);
}
public function testUpdateWithNoDescriptionAndDetails()
@@ -170,7 +170,7 @@ class MailHandlerTest extends UnitaryTestCase
})
);
$this->mailHandler->update('test_a.update', $event);
$this->mailEvent->update('test_a.update', $event);
}
public function testUpdateWithEmptyRecipients()
@@ -206,7 +206,7 @@ class MailHandlerTest extends UnitaryTestCase
})
);
$this->mailHandler->update('test_a.update', $event);
$this->mailEvent->update('test_a.update', $event);
}
public function testUpdateWithException()
@@ -223,13 +223,13 @@ class MailHandlerTest extends UnitaryTestCase
->method('send')
->willThrowException(new RuntimeException('test'));
$this->mailHandler->update('test_a.update', $event);
$this->mailEvent->update('test_a.update', $event);
}
public function testGetEventsString()
{
$expected = 'test_a\.|test_b\.|clear\.eventlog|refresh\.masterPassword|update\.masterPassword\.start|update\.masterPassword\.end|request\.account|edit\.user\.password|save\.config\.|create\.tempMasterPassword';
$out = $this->mailHandler->getEventsString();
$out = $this->mailEvent->getEvents();
$this->assertEquals($expected, $out);
}
@@ -241,7 +241,7 @@ class MailHandlerTest extends UnitaryTestCase
$this->configData->setMailEvents([]);
$databaseHandler = new MailEvent($this->application, $this->mailService, $this->requestService);
$out = $databaseHandler->getEventsString();
$out = $databaseHandler->getEvents();
$this->assertEquals($expected, $out);
}
@@ -264,6 +264,6 @@ class MailHandlerTest extends UnitaryTestCase
$this->mailService = $this->createMock(MailService::class);
$this->requestService = $this->createMock(RequestService::class);
$this->mailHandler = new MailEvent($this->application, $this->mailService, $this->requestService);
$this->mailEvent = new MailEvent($this->application, $this->mailService, $this->requestService);
}
}

View File

@@ -0,0 +1,220 @@
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2024, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace SP\Tests\Domain\Notification\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use RuntimeException;
use SP\Core\Events\Event;
use SP\Core\Events\EventMessage;
use SP\Domain\Notification\Models\Notification;
use SP\Domain\Notification\Ports\NotificationService;
use SP\Domain\Notification\Services\NotificationEvent;
use SP\Tests\UnitaryTestCase;
/**
* Class NotificationEventTest
*/
#[Group('unitary')]
class NotificationEventTest extends UnitaryTestCase
{
private MockObject|NotificationService $notificationService;
private NotificationEvent $notificationEvent;
public function testUpdateWithRequestAccount()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('userId', [100, 200]);
$event = new Event($this, $eventMessage);
$invokedCount = $this->exactly(2);
$this->notificationService
->expects($invokedCount)
->method('create')
->with(
self::callback(static function (Notification $notification) use ($invokedCount) {
$userId = match ($invokedCount->numberOfInvocations()) {
1 => 100,
2 => 200
};
return $notification->getType() == 'Request'
&& $notification->getComponent() === 'Accounts'
&& $notification->getUserId() === $userId
&& !empty($notification->getDescription());
})
);
$this->notificationEvent->update('request.account', $event);
}
public function testUpdateWithRequestAccountAndNoUserId()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value');
$event = new Event($this, $eventMessage);
$this->notificationService
->expects($this->never())
->method('create');
$this->notificationEvent->update('request.account', $event);
}
public function testUpdateWithRequestAccountAndException()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('userId', [100, 200]);
$event = new Event($this, $eventMessage);
$invokedCount = $this->exactly(2);
$this->notificationService
->expects($invokedCount)
->method('create')
->willThrowException(new RuntimeException('test'));
$this->notificationEvent->update('request.account', $event);
}
public function testUpdateWithShowLink()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('notify', [true])
->setExtra('userId', [100]);
$event = new Event($this, $eventMessage);
$this->notificationService
->expects($this->once())
->method('create')
->with(
self::callback(static function (Notification $notification) {
return $notification->getType() == 'Notification'
&& $notification->getComponent() === 'Accounts'
&& $notification->getUserId() === 100
&& !empty($notification->getDescription());
})
);
$this->notificationEvent->update('show.account.link', $event);
}
public function testUpdateWithShowLinkAndException()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('notify', [true])
->setExtra('userId', [100]);
$event = new Event($this, $eventMessage);
$this->notificationService
->expects($this->once())
->method('create')
->willThrowException(new RuntimeException('test'));
$this->notificationEvent->update('show.account.link', $event);
}
public function testUpdateWithShowLinkAndNoNotify()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('userId', [100]);
$event = new Event($this, $eventMessage);
$this->notificationService
->expects($this->never())
->method('create');
$this->notificationEvent->update('show.account.link', $event);
}
public function testUpdateWithShowLinkAndFalseNoNotify()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('notify', [false])
->setExtra('userId', [100]);
$event = new Event($this, $eventMessage);
$this->notificationService
->expects($this->never())
->method('create');
$this->notificationEvent->update('show.account.link', $event);
}
public function testUpdateWithShowLinkAndNoUserId()
{
$eventMessage = EventMessage::factory()
->addDescription('a_description')
->addDetail('a_detail', 'a_value')
->setExtra('notify', [true]);
$event = new Event($this, $eventMessage);
$this->notificationService
->expects($this->never())
->method('create');
$this->notificationEvent->update('show.account.link', $event);
}
public function testGetEvents()
{
$expected = 'request\.account|show\.account\.link';
$this->assertEquals($expected, $this->notificationEvent->getEvents());
}
protected function setUp(): void
{
parent::setUp();
$this->notificationService = $this->createMock(NotificationService::class);
$this->notificationEvent = new NotificationEvent($this->application, $this->notificationService);
}
}

View File

@@ -147,7 +147,7 @@ class PluginBaseTest extends UnitaryTestCase
// TODO: Implement update() method.
}
public function getEventsString(): ?string
public function getEvents(): ?string
{
// TODO: Implement getEventsString() method.
}