Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function markNotificationAsRead(Notification $notification): void
$this->innerService->markNotificationAsRead($notification);
}

public function markNotificationAsUnread(Notification $notification): void
{
$this->innerService->markNotificationAsUnread($notification);
}

public function getPendingNotificationCount(): int
{
return $this->innerService->getPendingNotificationCount();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Contracts\Core\Repository\Events\Notification;

use Ibexa\Contracts\Core\Repository\Event\BeforeEvent;
use Ibexa\Contracts\Core\Repository\Values\Notification\Notification;

final class BeforeMarkNotificationAsUnreadEvent extends BeforeEvent
{
/** @var \Ibexa\Contracts\Core\Repository\Values\Notification\Notification */
private $notification;

public function __construct(Notification $notification)
{
$this->notification = $notification;
}

public function getNotification(): Notification
{
return $this->notification;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Contracts\Core\Repository\Events\Notification;

use Ibexa\Contracts\Core\Repository\Event\AfterEvent;
use Ibexa\Contracts\Core\Repository\Values\Notification\Notification;

final class MarkNotificationAsUnreadEvent extends AfterEvent
{
/** @var \Ibexa\Contracts\Core\Repository\Values\Notification\Notification */
private $notification;

public function __construct(Notification $notification)
{
$this->notification = $notification;
}

public function getNotification(): Notification
{
return $this->notification;
}
}
10 changes: 10 additions & 0 deletions src/contracts/Repository/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public function getNotification(int $notificationId): Notification;
*/
public function markNotificationAsRead(Notification $notification): void;

/**
* Mark notification as unread so it no longer bother the user.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Notification\Notification $notification
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException
*/
public function markNotificationAsUnread(Notification $notification): void;

/**
* Get count of unread users notifications.
*
Expand Down
20 changes: 20 additions & 0 deletions src/lib/Event/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeCreateNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeDeleteNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeMarkNotificationAsReadEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeMarkNotificationAsUnreadEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\CreateNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\DeleteNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\MarkNotificationAsReadEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\MarkNotificationAsUnreadEvent;
use Ibexa\Contracts\Core\Repository\NotificationService as NotificationServiceInterface;
use Ibexa\Contracts\Core\Repository\Values\Notification\CreateStruct;
use Ibexa\Contracts\Core\Repository\Values\Notification\Notification;
Expand Down Expand Up @@ -52,6 +54,24 @@ public function markNotificationAsRead(Notification $notification): void
);
}

public function markNotificationAsUnread(Notification $notification): void
{
$eventData = [$notification];

$beforeEvent = new BeforeMarkNotificationAsUnreadEvent(...$eventData);

$this->eventDispatcher->dispatch($beforeEvent);
if ($beforeEvent->isPropagationStopped()) {
return;
}

$this->innerService->markNotificationAsUnread($notification);

$this->eventDispatcher->dispatch(
new MarkNotificationAsUnreadEvent(...$eventData)
);
}

public function createNotification(CreateStruct $createStruct): Notification
{
$eventData = [$createStruct];
Expand Down
25 changes: 25 additions & 0 deletions src/lib/Repository/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
}

if ($notification->ownerId !== $currentUserId) {
throw new UnauthorizedException($notification->id, 'Notification');

Check failure on line 115 in src/lib/Repository/NotificationService.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Ignored error pattern #^Parameter \#1 \$module of class Ibexa\\Core\\Base\\Exceptions\\UnauthorizedException constructor expects string, int\<min, \-1\>\|int\<1, max\> given\.$# (argument.type) in path /home/runner/work/core/core/src/lib/Repository/NotificationService.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 115 in src/lib/Repository/NotificationService.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Ignored error pattern #^Parameter \#1 \$module of class Ibexa\\Core\\Base\\Exceptions\\UnauthorizedException constructor expects string, int\<min, \-1\>\|int\<1, max\> given\.$# (argument.type) in path /home/runner/work/core/core/src/lib/Repository/NotificationService.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 115 in src/lib/Repository/NotificationService.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Ignored error pattern #^Parameter \#1 \$module of class Ibexa\\Core\\Base\\Exceptions\\UnauthorizedException constructor expects string, int\<min, \-1\>\|int\<1, max\> given\.$# (argument.type) in path /home/runner/work/core/core/src/lib/Repository/NotificationService.php is expected to occur 1 time, but occurred 2 times.
}

if (!$notification->isPending) {
Expand All @@ -125,6 +125,31 @@
$this->persistenceHandler->updateNotification($notification, $updateStruct);
}

/**
* {@inheritdoc}
*/
public function markNotificationAsUnread(APINotification $notification): void
{
$currentUserId = $this->getCurrentUserId();

if (!$notification->id) {
throw new NotFoundException('Notification', $notification->id);
}

if ($notification->ownerId !== $currentUserId) {
throw new UnauthorizedException($notification->id, 'Notification');

Check failure on line 140 in src/lib/Repository/NotificationService.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Parameter #1 $module of class Ibexa\Core\Base\Exceptions\UnauthorizedException constructor expects string, int<min, -1>|int<1, max> given.

Check failure on line 140 in src/lib/Repository/NotificationService.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Parameter #1 $module of class Ibexa\Core\Base\Exceptions\UnauthorizedException constructor expects string, int<min, -1>|int<1, max> given.

Check failure on line 140 in src/lib/Repository/NotificationService.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Parameter #1 $module of class Ibexa\Core\Base\Exceptions\UnauthorizedException constructor expects string, int<min, -1>|int<1, max> given.
}

if ($notification->isPending) {
return;
}

$updateStruct = new UpdateStruct();
$updateStruct->isPending = true;

$this->persistenceHandler->updateNotification($notification, $updateStruct);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions src/lib/Repository/SiteAccessAware/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public function markNotificationAsRead(Notification $notification): void
$this->service->markNotificationAsRead($notification);
}

/**
* Mark notification as unread so it no longer bother the user.
*
* @param \Ibexa\Contracts\Core\Repository\Values\Notification\Notification $notification
*/
public function markNotificationAsUnread(Notification $notification): void
{
$this->service->markNotificationAsUnread($notification);
}

/**
* Get count of unread users notifications.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/Core/Repository/NotificationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@
$this->assertFalse($notification->isPending);
}

/**
* @covers \Ibexa\Contracts\Core\Repository\NotificationService::markNotificationAsUnread()
*/
public function testMarkNotificationAsUnread()

Check failure on line 83 in tests/integration/Core/Repository/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Integration\Core\Repository\NotificationServiceTest::testMarkNotificationAsUnread() has no return type specified.

Check failure on line 83 in tests/integration/Core/Repository/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Integration\Core\Repository\NotificationServiceTest::testMarkNotificationAsUnread() has no return type specified.

Check failure on line 83 in tests/integration/Core/Repository/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Integration\Core\Repository\NotificationServiceTest::testMarkNotificationAsUnread() has no return type specified.
{
$repository = $this->getRepository();

$notificationId = $this->generateId('notification', 5);
/* BEGIN: Use Case */
$notificationService = $repository->getNotificationService();

$notification = $notificationService->getNotification($notificationId);
$notificationService->markNotificationAsUnread($notification);
$notification = $notificationService->getNotification($notificationId);
/* END: Use Case */

self::assertTrue($notification->isPending);
}

/**
* @covers \Ibexa\Contracts\Core\Repository\NotificationService::getPendingNotificationCount()
*/
Expand Down
59 changes: 59 additions & 0 deletions tests/lib/Event/NotificationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeCreateNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeDeleteNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeMarkNotificationAsReadEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\BeforeMarkNotificationAsUnreadEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\CreateNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\DeleteNotificationEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\MarkNotificationAsReadEvent;
use Ibexa\Contracts\Core\Repository\Events\Notification\MarkNotificationAsUnreadEvent;
use Ibexa\Contracts\Core\Repository\NotificationService as NotificationServiceInterface;
use Ibexa\Contracts\Core\Repository\Values\Notification\CreateStruct;
use Ibexa\Contracts\Core\Repository\Values\Notification\Notification;
Expand Down Expand Up @@ -200,6 +202,31 @@
$this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
}

public function testMarkNotificationAsUnreadEvents()

Check failure on line 205 in tests/lib/Event/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Core\Event\NotificationServiceTest::testMarkNotificationAsUnreadEvents() has no return type specified.

Check failure on line 205 in tests/lib/Event/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Core\Event\NotificationServiceTest::testMarkNotificationAsUnreadEvents() has no return type specified.

Check failure on line 205 in tests/lib/Event/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Core\Event\NotificationServiceTest::testMarkNotificationAsUnreadEvents() has no return type specified.
{
$traceableEventDispatcher = $this->getEventDispatcher(
BeforeMarkNotificationAsUnreadEvent::class,
MarkNotificationAsUnreadEvent::class
);

$parameters = [
$this->createMock(Notification::class),
];

$innerServiceMock = $this->createMock(NotificationServiceInterface::class);

$service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
$service->markNotificationAsUnread(...$parameters);

$calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());

self::assertSame($calledListeners, [
[BeforeMarkNotificationAsUnreadEvent::class, 0],
[MarkNotificationAsUnreadEvent::class, 0],
]);
self::assertSame([], $traceableEventDispatcher->getNotCalledListeners());
}

public function testMarkNotificationAsReadStopPropagationInBeforeEvents()
{
$traceableEventDispatcher = $this->getEventDispatcher(
Expand Down Expand Up @@ -231,6 +258,38 @@
[MarkNotificationAsReadEvent::class, 0],
]);
}

public function testMarkNotificationAsUnreadStopPropagationInBeforeEvents()

Check failure on line 262 in tests/lib/Event/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Core\Event\NotificationServiceTest::testMarkNotificationAsUnreadStopPropagationInBeforeEvents() has no return type specified.

Check failure on line 262 in tests/lib/Event/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Core\Event\NotificationServiceTest::testMarkNotificationAsUnreadStopPropagationInBeforeEvents() has no return type specified.

Check failure on line 262 in tests/lib/Event/NotificationServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Core\Event\NotificationServiceTest::testMarkNotificationAsUnreadStopPropagationInBeforeEvents() has no return type specified.
{
$traceableEventDispatcher = $this->getEventDispatcher(
BeforeMarkNotificationAsUnreadEvent::class,
MarkNotificationAsUnreadEvent::class
);

$parameters = [
$this->createMock(Notification::class),
];

$innerServiceMock = $this->createMock(NotificationServiceInterface::class);

$traceableEventDispatcher->addListener(BeforeMarkNotificationAsUnreadEvent::class, static function (BeforeMarkNotificationAsUnreadEvent $event) {
$event->stopPropagation();
}, 10);

$service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
$service->markNotificationAsUnread(...$parameters);

$calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
$notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());

self::assertSame($calledListeners, [
[BeforeMarkNotificationAsUnreadEvent::class, 10],
]);
self::assertSame($notCalledListeners, [
[BeforeMarkNotificationAsUnreadEvent::class, 0],
[MarkNotificationAsUnreadEvent::class, 0],
]);
}
}

class_alias(NotificationServiceTest::class, 'eZ\Publish\Core\Event\Tests\NotificationServiceTest');
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@
$decoratedService->markNotificationAsRead(...$parameters);
}

public function testMarkNotificationAsUnreadDecorator()

Check failure on line 70 in tests/lib/Repository/Decorator/NotificationServiceDecoratorTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Method Ibexa\Tests\Core\Repository\Decorator\NotificationServiceDecoratorTest::testMarkNotificationAsUnreadDecorator() has no return type specified.

Check failure on line 70 in tests/lib/Repository/Decorator/NotificationServiceDecoratorTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Method Ibexa\Tests\Core\Repository\Decorator\NotificationServiceDecoratorTest::testMarkNotificationAsUnreadDecorator() has no return type specified.

Check failure on line 70 in tests/lib/Repository/Decorator/NotificationServiceDecoratorTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Method Ibexa\Tests\Core\Repository\Decorator\NotificationServiceDecoratorTest::testMarkNotificationAsUnreadDecorator() has no return type specified.
{
$serviceMock = $this->createServiceMock();
$decoratedService = $this->createDecorator($serviceMock);

$parameters = [$this->createMock(Notification::class)];

$serviceMock->expects(self::once())->method('markNotificationAsunread')->with(...$parameters);

$decoratedService->markNotificationAsunread(...$parameters);
}

public function testGetPendingNotificationCountDecorator()
{
$serviceMock = $this->createServiceMock();
Expand Down
Loading