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
Prev Previous commit
Next Next commit
Add unit tests
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Jul 7, 2021
commit 6fc56553f55f806ce5be501ec9a239fe15966cdf
2 changes: 1 addition & 1 deletion lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function removePendingNotificationsForRoom(Room $chat, bool $chatOnly = f
$notification->setObject('chat', $chat->getToken());
$this->notificationManager->markProcessed($notification);

if ($chatOnly) {
if (!$chatOnly) {
$notification->setObject('room', $chat->getToken());
$this->notificationManager->markProcessed($notification);

Expand Down
99 changes: 92 additions & 7 deletions tests/php/Chat/ChatManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;
Expand All @@ -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 */
Expand All @@ -66,6 +69,7 @@ public function setUp(): void {
$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);
Expand All @@ -76,6 +80,44 @@ public function setUp(): void {
$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,
Expand Down Expand Up @@ -221,7 +263,7 @@ public function testSendMessage(string $userId, string $message, string $referen
$this->assertCommentEquals($commentExpected, $return);
}

public function testGetHistory() {
public function testGetHistory(): void {
$offset = 1;
$limit = 42;
$expected = [
Expand All @@ -245,7 +287,7 @@ public function testGetHistory() {
$this->assertEquals($expected, $comments);
}

public function testWaitForNewMessages() {
public function testWaitForNewMessages(): void {
$offset = 1;
$limit = 42;
$timeout = 23;
Expand All @@ -269,7 +311,7 @@ public function testWaitForNewMessages() {
->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')
Expand All @@ -280,7 +322,7 @@ public function testWaitForNewMessages() {
$this->assertEquals($expected, $comments);
}

public function testWaitForNewMessagesWithWaiting() {
public function testWaitForNewMessagesWithWaiting(): void {
$offset = 1;
$limit = 42;
$timeout = 23;
Expand All @@ -307,7 +349,7 @@ public function testWaitForNewMessagesWithWaiting() {
->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')
Expand All @@ -318,7 +360,7 @@ public function testWaitForNewMessagesWithWaiting() {
$this->assertEquals($expected, $comments);
}

public function testGetUnreadCount() {
public function testGetUnreadCount(): void {
/** @var Room|MockObject $chat */
$chat = $this->createMock(Room::class);
$chat->expects($this->atLeastOnce())
Expand All @@ -332,7 +374,7 @@ public function testGetUnreadCount() {
$this->chatManager->getUnreadCount($chat, 42);
}

public function testDeleteMessages() {
public function testDeleteMessages(): void {
$chat = $this->createMock(Room::class);
$chat->expects($this->any())
->method('getId')
Expand All @@ -348,4 +390,47 @@ public function testDeleteMessages() {

$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');
}
}
57 changes: 43 additions & 14 deletions tests/php/Chat/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function setUp(): void {
);
}

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 = [];
Expand All @@ -107,7 +107,7 @@ private function newComment($id, $actorType, $actorId, $creationDateTime, $messa
return $comment;
}

private function newNotification($room, IComment $comment) {
private function newNotification($room, IComment $comment): INotification {
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand Down Expand Up @@ -140,7 +140,7 @@ private function newNotification($room, IComment $comment) {
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);
Expand Down Expand Up @@ -183,7 +183,7 @@ public function testNotifyMentionedUsers() {
$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);
Expand Down Expand Up @@ -211,7 +211,7 @@ public function testNotNotifyMentionedUserIfReplyToAuthor() {
$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);
Expand Down Expand Up @@ -254,7 +254,7 @@ public function testNotifyMentionedUsersByGuest() {
$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');

Expand Down Expand Up @@ -298,7 +298,7 @@ public function testNotifyMentionedUsersWithLongMessageStartMention() {
$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');

Expand Down Expand Up @@ -342,7 +342,7 @@ public function testNotifyMentionedUsersWithLongMessageMiddleMention() {
$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');

Expand Down Expand Up @@ -386,7 +386,7 @@ public function testNotifyMentionedUsersWithLongMessageEndMention() {
$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);
Expand All @@ -406,7 +406,7 @@ public function testNotifyMentionedUsersToSelf() {
$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);
Expand All @@ -427,7 +427,7 @@ public function testNotifyMentionedUsersToUnknownUser() {
$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);
Expand Down Expand Up @@ -458,7 +458,7 @@ public function testNotifyMentionedUsersToUserNotInvitedToChat() {
$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);
Expand All @@ -475,7 +475,7 @@ public function testNotifyMentionedUsersNoMentions() {
$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);
Expand Down Expand Up @@ -524,7 +524,7 @@ public function testNotifyMentionedUsersSeveralMentions() {
$this->notifier->notifyMentionedUsers($room, $comment, []);
}

public function testRemovePendingNotificationsForRoom() {
public function testRemovePendingNotificationsForRoom(): void {
$notification = $this->createMock(INotification::class);

$room = $this->createMock(Room::class);
Expand Down Expand Up @@ -556,4 +556,33 @@ public function testRemovePendingNotificationsForRoom() {

$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);
}
}