diff options
author | Joas Schilling <coding@schilljs.com> | 2021-07-07 17:38:24 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-07-07 17:39:36 +0300 |
commit | 31a32b678086a6d70e7a3f4635cdb04a2b38b7b7 (patch) | |
tree | 1949caa8f7fbd11c3284a4d95c2ad848675f4c41 /tests | |
parent | bbed7ef20200e626eb070520b61c5fff9c07e7f3 (diff) |
Add unit tests
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/php/Chat/ChatManagerTest.php | 99 | ||||
-rw-r--r-- | tests/php/Chat/NotifierTest.php | 57 |
2 files changed, 135 insertions, 21 deletions
diff --git a/tests/php/Chat/ChatManagerTest.php b/tests/php/Chat/ChatManagerTest.php index 6bf7447ef..d64e5f04b 100644 --- a/tests/php/Chat/ChatManagerTest.php +++ b/tests/php/Chat/ChatManagerTest.php @@ -30,6 +30,7 @@ use OCA\Talk\Chat\Notifier; use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Share\RoomShareProvider; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Comments\IComment; use OCP\Comments\ICommentsManager; @@ -51,6 +52,8 @@ class ChatManagerTest extends TestCase { protected $dispatcher; /** @var INotificationManager|MockObject */ protected $notificationManager; + /** @var RoomShareProvider|MockObject */ + protected $shareProvider; /** @var ParticipantService|MockObject */ protected $participantService; /** @var Notifier|MockObject */ @@ -66,6 +69,7 @@ class ChatManagerTest extends TestCase { $this->commentsManager = $this->createMock(CommentsManager::class); $this->dispatcher = $this->createMock(IEventDispatcher::class); $this->notificationManager = $this->createMock(INotificationManager::class); + $this->shareProvider = $this->createMock(RoomShareProvider::class); $this->participantService = $this->createMock(ParticipantService::class); $this->notifier = $this->createMock(Notifier::class); $this->timeFactory = $this->createMock(ITimeFactory::class); @@ -76,6 +80,44 @@ class ChatManagerTest extends TestCase { $this->dispatcher, \OC::$server->getDatabaseConnection(), $this->notificationManager, + $this->shareProvider, + $this->participantService, + $this->notifier, + $cacheFactory, + $this->timeFactory + ); + } + + /** + * @param string[] $methods + * @return ChatManager|MockObject + */ + protected function getManager(array $methods = []): ChatManager { + $cacheFactory = $this->createMock(ICacheFactory::class); + + if (!empty($methods)) { + return $this->getMockBuilder(ChatManager::class) + ->setConstructorArgs([ + $this->commentsManager, + $this->dispatcher, + \OC::$server->getDatabaseConnection(), + $this->notificationManager, + $this->shareProvider, + $this->participantService, + $this->notifier, + $cacheFactory, + $this->timeFactory, + ]) + ->setMethods($methods) + ->getMock(); + } + + return new ChatManager( + $this->commentsManager, + $this->dispatcher, + \OC::$server->getDatabaseConnection(), + $this->notificationManager, + $this->shareProvider, $this->participantService, $this->notifier, $cacheFactory, @@ -221,7 +263,7 @@ class ChatManagerTest extends TestCase { $this->assertCommentEquals($commentExpected, $return); } - public function testGetHistory() { + public function testGetHistory(): void { $offset = 1; $limit = 42; $expected = [ @@ -245,7 +287,7 @@ class ChatManagerTest extends TestCase { $this->assertEquals($expected, $comments); } - public function testWaitForNewMessages() { + public function testWaitForNewMessages(): void { $offset = 1; $limit = 42; $timeout = 23; @@ -269,7 +311,7 @@ class ChatManagerTest extends TestCase { ->method('markMentionNotificationsRead') ->with($chat, 'userId'); - /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $user->expects($this->any()) ->method('getUID') @@ -280,7 +322,7 @@ class ChatManagerTest extends TestCase { $this->assertEquals($expected, $comments); } - public function testWaitForNewMessagesWithWaiting() { + public function testWaitForNewMessagesWithWaiting(): void { $offset = 1; $limit = 42; $timeout = 23; @@ -307,7 +349,7 @@ class ChatManagerTest extends TestCase { ->method('markMentionNotificationsRead') ->with($chat, 'userId'); - /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */ + /** @var IUser|MockObject $user */ $user = $this->createMock(IUser::class); $user->expects($this->any()) ->method('getUID') @@ -318,7 +360,7 @@ class ChatManagerTest extends TestCase { $this->assertEquals($expected, $comments); } - public function testGetUnreadCount() { + public function testGetUnreadCount(): void { /** @var Room|MockObject $chat */ $chat = $this->createMock(Room::class); $chat->expects($this->atLeastOnce()) @@ -332,7 +374,7 @@ class ChatManagerTest extends TestCase { $this->chatManager->getUnreadCount($chat, 42); } - public function testDeleteMessages() { + public function testDeleteMessages(): void { $chat = $this->createMock(Room::class); $chat->expects($this->any()) ->method('getId') @@ -348,4 +390,47 @@ class ChatManagerTest extends TestCase { $this->chatManager->deleteMessages($chat); } + + public function testClearHistory(): void { + $chat = $this->createMock(Room::class); + $chat->expects($this->any()) + ->method('getId') + ->willReturn(1234); + $chat->expects($this->any()) + ->method('getToken') + ->willReturn('t0k3n'); + + $this->commentsManager->expects($this->once()) + ->method('deleteCommentsAtObject') + ->with('chat', 1234); + + $this->shareProvider->expects($this->once()) + ->method('deleteInRoom') + ->with('t0k3n'); + + $this->notifier->expects($this->once()) + ->method('removePendingNotificationsForRoom') + ->with($chat, true); + + $this->participantService->expects($this->once()) + ->method('resetChatDetails') + ->with($chat); + + $date = new \DateTime(); + $this->timeFactory->method('getDateTime') + ->willReturn($date); + + $manager = $this->getManager(['addSystemMessage']); + $manager->expects($this->once()) + ->method('addSystemMessage') + ->with( + $chat, + 'users', + 'admin', + json_encode(['message' => 'cleared_history', 'parameters' => []]), + $date, + false + ); + $manager->clearHistory($chat, 'users', 'admin'); + } } diff --git a/tests/php/Chat/NotifierTest.php b/tests/php/Chat/NotifierTest.php index d0214a305..299e6aedd 100644 --- a/tests/php/Chat/NotifierTest.php +++ b/tests/php/Chat/NotifierTest.php @@ -83,7 +83,7 @@ class NotifierTest extends TestCase { ); } - private function newComment($id, $actorType, $actorId, $creationDateTime, $message) { + private function newComment($id, $actorType, $actorId, $creationDateTime, $message): IComment { // $mentionMatches[0] contains the whole matches, while // $mentionMatches[1] contains the matched subpattern. $mentionMatches = []; @@ -107,7 +107,7 @@ class NotifierTest extends TestCase { return $comment; } - private function newNotification($room, IComment $comment) { + private function newNotification($room, IComment $comment): INotification { $notification = $this->createMock(INotification::class); $notification->expects($this->once()) @@ -140,7 +140,7 @@ class NotifierTest extends TestCase { return $notification; } - public function testNotifyMentionedUsers() { + public function testNotifyMentionedUsers(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'Mention @anotherUser'); $room = $this->createMock(Room::class); @@ -183,7 +183,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotNotifyMentionedUserIfReplyToAuthor() { + public function testNotNotifyMentionedUserIfReplyToAuthor(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'Mention @anotherUser'); $room = $this->createMock(Room::class); @@ -211,7 +211,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, ['anotherUser']); } - public function testNotifyMentionedUsersByGuest() { + public function testNotifyMentionedUsersByGuest(): void { $comment = $this->newComment(108, 'guests', 'testSpreedSession', new \DateTime('@' . 1000000016), 'Mention @anotherUser'); $room = $this->createMock(Room::class); @@ -254,7 +254,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersWithLongMessageStartMention() { + public function testNotifyMentionedUsersWithLongMessageStartMention(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), '123456789 @anotherUserWithOddLengthName 123456789-123456789-123456789-123456789-123456789-123456789'); @@ -298,7 +298,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersWithLongMessageMiddleMention() { + public function testNotifyMentionedUsersWithLongMessageMiddleMention(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), '123456789-123456789-123456789-1234 @anotherUserWithOddLengthName 6789-123456789-123456789-123456789'); @@ -342,7 +342,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersWithLongMessageEndMention() { + public function testNotifyMentionedUsersWithLongMessageEndMention(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), '123456789-123456789-123456789-123456789-123456789-123456789 @anotherUserWithOddLengthName 123456789'); @@ -386,7 +386,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersToSelf() { + public function testNotifyMentionedUsersToSelf(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'Mention @testUser'); $room = $this->createMock(Room::class); @@ -406,7 +406,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersToUnknownUser() { + public function testNotifyMentionedUsersToUnknownUser(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'Mention @unknownUser'); $room = $this->createMock(Room::class); @@ -427,7 +427,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersToUserNotInvitedToChat() { + public function testNotifyMentionedUsersToUserNotInvitedToChat(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'Mention @userNotInOneToOneChat'); $room = $this->createMock(Room::class); @@ -458,7 +458,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersNoMentions() { + public function testNotifyMentionedUsersNoMentions(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'No mentions'); $room = $this->createMock(Room::class); @@ -475,7 +475,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testNotifyMentionedUsersSeveralMentions() { + public function testNotifyMentionedUsersSeveralMentions(): void { $comment = $this->newComment(108, 'users', 'testUser', new \DateTime('@' . 1000000016), 'Mention @anotherUser, and @unknownUser, and @testUser, and @userAbleToJoin'); $room = $this->createMock(Room::class); @@ -524,7 +524,7 @@ class NotifierTest extends TestCase { $this->notifier->notifyMentionedUsers($room, $comment, []); } - public function testRemovePendingNotificationsForRoom() { + public function testRemovePendingNotificationsForRoom(): void { $notification = $this->createMock(INotification::class); $room = $this->createMock(Room::class); @@ -556,4 +556,33 @@ class NotifierTest extends TestCase { $this->notifier->removePendingNotificationsForRoom($room); } + + public function testRemovePendingNotificationsForChatOnly(): void { + $notification = $this->createMock(INotification::class); + + $room = $this->createMock(Room::class); + $room->expects($this->any()) + ->method('getToken') + ->willReturn('Token123'); + + $this->notificationManager->expects($this->once()) + ->method('createNotification') + ->willReturn($notification); + + $notification->expects($this->once()) + ->method('setApp') + ->with('spreed') + ->willReturnSelf(); + + $notification->expects($this->exactly(1)) + ->method('setObject') + ->with('chat', 'Token123') + ->willReturnSelf(); + + $this->notificationManager->expects($this->exactly(1)) + ->method('markProcessed') + ->with($notification); + + $this->notifier->removePendingNotificationsForRoom($room, true); + } } |