2016-05-09 04:02:07 -04:00
|
|
|
<?php
|
2025-05-13 16:23:39 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-05-09 04:02:07 -04:00
|
|
|
/**
|
2024-05-27 11:39:07 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-05-09 04:02:07 -04:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Comments\Tests\Unit\Notification;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Comments\Activity\Listener as ActivityListener;
|
2024-06-18 08:11:32 -04:00
|
|
|
use OCA\Comments\Listener\CommentsEventListener;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Comments\Notification\Listener as NotificationListener;
|
2016-05-09 04:02:07 -04:00
|
|
|
use OCP\Comments\CommentsEvent;
|
2025-10-29 05:44:33 -04:00
|
|
|
use OCP\Comments\Events\BeforeCommentUpdatedEvent;
|
|
|
|
|
use OCP\Comments\Events\CommentAddedEvent;
|
|
|
|
|
use OCP\Comments\Events\CommentDeletedEvent;
|
|
|
|
|
use OCP\Comments\Events\CommentUpdatedEvent;
|
2016-05-09 04:02:07 -04:00
|
|
|
use OCP\Comments\IComment;
|
2025-05-13 16:23:39 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2016-05-09 04:02:07 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class EventHandlerTest extends TestCase {
|
2025-05-13 16:23:39 -04:00
|
|
|
protected ActivityListener&MockObject $activityListener;
|
|
|
|
|
protected NotificationListener&MockObject $notificationListener;
|
|
|
|
|
protected CommentsEventListener $eventHandler;
|
2016-05-09 04:02:07 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2016-05-09 04:02:07 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-05-13 16:23:39 -04:00
|
|
|
$this->activityListener = $this->createMock(ActivityListener::class);
|
|
|
|
|
$this->notificationListener = $this->createMock(NotificationListener::class);
|
2016-10-14 09:36:05 -04:00
|
|
|
|
2024-06-18 08:11:32 -04:00
|
|
|
$this->eventHandler = new CommentsEventListener($this->activityListener, $this->notificationListener);
|
2016-05-09 04:02:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNotFiles(): void {
|
2025-05-13 16:23:39 -04:00
|
|
|
/** @var IComment|MockObject $comment */
|
|
|
|
|
$comment = $this->createMock(IComment::class);
|
2016-05-09 04:02:07 -04:00
|
|
|
$comment->expects($this->once())
|
|
|
|
|
->method('getObjectType')
|
|
|
|
|
->willReturn('smiles');
|
|
|
|
|
|
2025-05-13 16:23:39 -04:00
|
|
|
/** @var CommentsEvent|MockObject $event */
|
|
|
|
|
$event = $this->createMock(CommentsEvent::class);
|
2016-05-09 04:02:07 -04:00
|
|
|
$event->expects($this->once())
|
|
|
|
|
->method('getComment')
|
|
|
|
|
->willReturn($comment);
|
|
|
|
|
$event->expects($this->never())
|
|
|
|
|
->method('getEvent');
|
|
|
|
|
|
|
|
|
|
$this->eventHandler->handle($event);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-13 16:23:39 -04:00
|
|
|
public static function handledProvider(): array {
|
2016-10-07 19:10:49 -04:00
|
|
|
return [
|
2025-10-29 05:44:33 -04:00
|
|
|
['delete'],
|
|
|
|
|
['update'],
|
|
|
|
|
['pre_update'],
|
|
|
|
|
['add']
|
2016-10-07 19:10:49 -04:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 08:21:48 -05:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'handledProvider')]
|
2025-05-13 16:23:39 -04:00
|
|
|
public function testHandled(string $eventType): void {
|
|
|
|
|
/** @var IComment|MockObject $comment */
|
|
|
|
|
$comment = $this->createMock(IComment::class);
|
2016-05-09 04:02:07 -04:00
|
|
|
$comment->expects($this->once())
|
|
|
|
|
->method('getObjectType')
|
|
|
|
|
->willReturn('files');
|
|
|
|
|
|
2025-10-29 05:44:33 -04:00
|
|
|
$event = match ($eventType) {
|
|
|
|
|
'add' => new CommentAddedEvent($comment),
|
|
|
|
|
'pre_update' => new BeforeCommentUpdatedEvent($comment),
|
|
|
|
|
'update' => new CommentUpdatedEvent($comment),
|
|
|
|
|
'delete' => new CommentDeletedEvent($comment),
|
|
|
|
|
};
|
2016-05-09 04:02:07 -04:00
|
|
|
|
2016-10-14 09:36:05 -04:00
|
|
|
$this->notificationListener->expects($this->once())
|
2016-05-09 04:02:07 -04:00
|
|
|
->method('evaluate')
|
|
|
|
|
->with($event);
|
|
|
|
|
|
2016-10-14 09:36:05 -04:00
|
|
|
$this->activityListener->expects($this->any())
|
2016-05-09 04:02:07 -04:00
|
|
|
->method('commentEvent')
|
|
|
|
|
->with($event);
|
|
|
|
|
|
|
|
|
|
$this->eventHandler->handle($event);
|
|
|
|
|
}
|
|
|
|
|
}
|