diff options
author | Joas Schilling <coding@schilljs.com> | 2019-07-10 16:25:33 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2019-07-25 17:39:51 +0300 |
commit | e85173e4cb76e288cafac2564499c8137ab86c23 (patch) | |
tree | 56e77ce6fcf371fd6e7f16172aabf0020afcaf85 /tests | |
parent | 4b392495dcf06a1ab0de977bfa21a01cb1098f58 (diff) |
Also parse mentions of guests
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/php/Chat/Parser/UserMentionTest.php | 170 |
1 files changed, 169 insertions, 1 deletions
diff --git a/tests/php/Chat/Parser/UserMentionTest.php b/tests/php/Chat/Parser/UserMentionTest.php index c021cf0e8..85eebdc44 100644 --- a/tests/php/Chat/Parser/UserMentionTest.php +++ b/tests/php/Chat/Parser/UserMentionTest.php @@ -24,6 +24,8 @@ declare(strict_types=1); namespace OCA\Spreed\Tests\php\Chat\Parser; use OCA\Spreed\Chat\Parser\UserMention; +use OCA\Spreed\Exceptions\ParticipantNotFoundException; +use OCA\Spreed\GuestManager; use OCA\Spreed\Model\Message; use OCA\Spreed\Participant; use OCA\Spreed\Room; @@ -40,6 +42,8 @@ class UserMentionTest extends \Test\TestCase { protected $commentsManager; /** @var IUserManager|MockObject */ protected $userManager; + /** @var GuestManager|MockObject */ + protected $guestManager; /** @var IL10N|MockObject */ protected $l; @@ -51,9 +55,14 @@ class UserMentionTest extends \Test\TestCase { $this->commentsManager = $this->createMock(ICommentsManager::class); $this->userManager = $this->createMock(IUserManager::class); + $this->guestManager = $this->createMock(GuestManager::class); $this->l = $this->createMock(IL10N::class); - $this->parser = new UserMention($this->commentsManager, $this->userManager, $this->l); + $this->parser = new UserMention( + $this->commentsManager, + $this->userManager, + $this->guestManager, + $this->l); } /** @@ -310,4 +319,163 @@ class UserMentionTest extends \Test\TestCase { $this->assertEquals($expectedMessageParameters, $chatMessage->getMessageParameters()); } + public function testGetRichMessageWithAtAll(): void { + $mentions = [ + ['type' => 'user', 'id' => 'all'], + ]; + $comment = $this->newComment($mentions); + + /** @var Room|MockObject $room */ + $room = $this->createMock(Room::class); + $room->expects($this->once()) + ->method('getType') + ->willReturn(Room::GROUP_CALL); + $room->expects($this->once()) + ->method('getToken') + ->willReturn('token'); + $room->expects($this->once()) + ->method('getDisplayName') + ->willReturn('name'); + /** @var Participant|MockObject $participant */ + $participant = $this->createMock(Participant::class); + /** @var IL10N|MockObject $l */ + $l = $this->createMock(IL10N::class); + $chatMessage = new Message($room, $participant, $comment, $l); + $chatMessage->setMessage('Mention to @all', []); + + $this->parser->parseMessage($chatMessage); + + $expectedMessageParameters = [ + 'mention-call1' => [ + 'type' => 'call', + 'id' => 'token', + 'name' => 'name', + 'call-type' => 'group', + ] + ]; + + $this->assertEquals('Mention to {mention-call1}', $chatMessage->getMessage()); + $this->assertEquals($expectedMessageParameters, $chatMessage->getMessageParameters()); + } + + public function testGetRichMessageWhenAGuestWithoutNameIsMentioned(): void { + $mentions = [ + ['type' => 'guest', 'id' => 'guest/123456'], + ]; + $comment = $this->newComment($mentions); + + /** @var Room|MockObject $room */ + $room = $this->createMock(Room::class); + /** @var Participant|MockObject $participant */ + $participant = $this->createMock(Participant::class); + /** @var IL10N|MockObject $l */ + $l = $this->createMock(IL10N::class); + + $this->guestManager->expects($this->once()) + ->method('getNameBySessionHash') + ->with('123456') + ->willThrowException(new ParticipantNotFoundException()); + $this->l->expects($this->any()) + ->method('t') + ->willReturnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + }); + + $chatMessage = new Message($room, $participant, $comment, $l); + $chatMessage->setMessage('Mention to @"guest/123456"', []); + + $this->parser->parseMessage($chatMessage); + + $expectedMessageParameters = [ + 'mention-guest1' => [ + 'type' => 'guest', + 'id' => 'guest/123456', + 'name' => 'Guest', + ] + ]; + + $this->assertEquals('Mention to {mention-guest1}', $chatMessage->getMessage()); + $this->assertEquals($expectedMessageParameters, $chatMessage->getMessageParameters()); + } + + public function testGetRichMessageWhenAGuestWithoutNameIsMentionedMultipleTimes(): void { + $mentions = [ + ['type' => 'guest', 'id' => 'guest/123456'], + ]; + $comment = $this->newComment($mentions); + + /** @var Room|MockObject $room */ + $room = $this->createMock(Room::class); + /** @var Participant|MockObject $participant */ + $participant = $this->createMock(Participant::class); + /** @var IL10N|MockObject $l */ + $l = $this->createMock(IL10N::class); + + $this->guestManager->expects($this->once()) + ->method('getNameBySessionHash') + ->with('123456') + ->willThrowException(new ParticipantNotFoundException()); + $this->l->expects($this->any()) + ->method('t') + ->willReturnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + }); + + $chatMessage = new Message($room, $participant, $comment, $l); + $chatMessage->setMessage('Mention to @"guest/123456", and again @"guest/123456"', []); + + $this->parser->parseMessage($chatMessage); + + $expectedMessageParameters = [ + 'mention-guest1' => [ + 'type' => 'guest', + 'id' => 'guest/123456', + 'name' => 'Guest', + ] + ]; + + $this->assertEquals('Mention to {mention-guest1}, and again {mention-guest1}', $chatMessage->getMessage()); + $this->assertEquals($expectedMessageParameters, $chatMessage->getMessageParameters()); + } + + public function testGetRichMessageWhenAGuestWithANameIsMentionedMultipleTimes(): void { + $mentions = [ + ['type' => 'guest', 'id' => 'guest/abcdef'], + ]; + $comment = $this->newComment($mentions); + + /** @var Room|MockObject $room */ + $room = $this->createMock(Room::class); + /** @var Participant|MockObject $participant */ + $participant = $this->createMock(Participant::class); + /** @var IL10N|MockObject $l */ + $l = $this->createMock(IL10N::class); + + $this->guestManager->expects($this->once()) + ->method('getNameBySessionHash') + ->with('abcdef') + ->willReturn('Name'); + $this->l->expects($this->any()) + ->method('t') + ->willReturnCallback(function($text, $parameters = []) { + return vsprintf($text, $parameters); + }); + + $chatMessage = new Message($room, $participant, $comment, $l); + $chatMessage->setMessage('Mention to @"guest/abcdef", and again @"guest/abcdef"', []); + + $this->parser->parseMessage($chatMessage); + + $expectedMessageParameters = [ + 'mention-guest1' => [ + 'type' => 'guest', + 'id' => 'guest/abcdef', + 'name' => 'Name', + ] + ]; + + $this->assertEquals('Mention to {mention-guest1}, and again {mention-guest1}', $chatMessage->getMessage()); + $this->assertEquals($expectedMessageParameters, $chatMessage->getMessageParameters()); + } + } |