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;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -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 [
|
|
|
|
|
[CommentsEvent::EVENT_DELETE],
|
2016-10-12 12:06:22 -04:00
|
|
|
[CommentsEvent::EVENT_UPDATE],
|
|
|
|
|
[CommentsEvent::EVENT_PRE_UPDATE],
|
2016-10-07 19:10:49 -04:00
|
|
|
[CommentsEvent::EVENT_ADD]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('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-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->atLeastOnce())
|
|
|
|
|
->method('getComment')
|
|
|
|
|
->willReturn($comment);
|
|
|
|
|
$event->expects($this->atLeastOnce())
|
|
|
|
|
->method('getEvent')
|
2016-10-07 19:10:49 -04:00
|
|
|
->willReturn($eventType);
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|