diff options
author | Joas Schilling <coding@schilljs.com> | 2020-03-11 14:55:51 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-04-22 16:35:36 +0300 |
commit | c28a43f1e70d220c7b062c841ef6a4b9cb88fffa (patch) | |
tree | 0565fb227354ebf108dbd51bc1d11fb682f91a8c /tests | |
parent | a05d075d738a3cee16fbe4e3878282997a20fbfd (diff) |
Handle the new reference id in the backend
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/php/CapabilitiesTest.php | 2 | ||||
-rw-r--r-- | tests/php/Chat/ChatManagerTest.php | 135 | ||||
-rw-r--r-- | tests/php/Controller/ChatControllerTest.php | 81 |
3 files changed, 166 insertions, 52 deletions
diff --git a/tests/php/CapabilitiesTest.php b/tests/php/CapabilitiesTest.php index 672f1077b..eea12ab44 100644 --- a/tests/php/CapabilitiesTest.php +++ b/tests/php/CapabilitiesTest.php @@ -96,6 +96,7 @@ class CapabilitiesTest extends TestCase { 'chat-replies', 'circles-support', 'force-mute', + 'chat-reference-id', ], 'config' => [ 'attachments' => [ @@ -187,6 +188,7 @@ class CapabilitiesTest extends TestCase { 'chat-replies', 'circles-support', 'force-mute', + 'chat-reference-id', ], 'config' => [ 'attachments' => [ diff --git a/tests/php/Chat/ChatManagerTest.php b/tests/php/Chat/ChatManagerTest.php index 4e20e31a6..e531a1c2e 100644 --- a/tests/php/Chat/ChatManagerTest.php +++ b/tests/php/Chat/ChatManagerTest.php @@ -1,8 +1,8 @@ <?php - +declare(strict_types=1); /** * - * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com) + * @copyright Copyright (c) 2017, Daniel Calviño Sánchez <danxuliu@gmail.com> * * @license GNU AGPL version 3 or any later version * @@ -57,15 +57,19 @@ class ChatManagerTest extends TestCase { $this->notifier = $this->createMock(Notifier::class); $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->chatManager = new ChatManager($this->commentsManager, - $this->dispatcher, - $this->notifier, - $this->timeFactory); + $this->chatManager = new ChatManager( + $this->commentsManager, + $this->dispatcher, + $this->notifier, + $this->timeFactory + ); } - private function newComment($id, $actorType, $actorId, $creationDateTime, $message) { + private function newComment($id, string $actorType, string $actorId, \DateTime $creationDateTime, string $message): IComment { $comment = $this->createMock(IComment::class); + $id = (string) $id; + $comment->method('getId')->willReturn($id); $comment->method('getActorType')->willReturn($actorType); $comment->method('getActorId')->willReturn($actorId); @@ -82,63 +86,99 @@ class ChatManagerTest extends TestCase { return $comment; } - public function testSendMessage() { + /** + * @param array $data + * @return IComment|MockObject + */ + private function newCommentFromArray(array $data): IComment { $comment = $this->createMock(IComment::class); - $this->commentsManager->expects($this->once()) - ->method('create') - ->with('users', 'testUser', 'chat', 1234) - ->willReturn($comment); + foreach ($data as $key => $value) { + if ($key === 'id') { + $value = (string) $value; + } + $comment->method('get' . ucfirst($key))->willReturn($value); + } - $comment->expects($this->once()) - ->method('setMessage') - ->with('testMessage'); - - $creationDateTime = new \DateTime(); - $comment->expects($this->once()) - ->method('setCreationDateTime') - ->with($creationDateTime); + return $comment; + } - $comment->expects($this->once()) - ->method('setVerb') - ->with('comment'); + protected function assertCommentEquals(array $data, IComment $comment): void { + if (isset($data['id'])) { + $id = $data['id']; + unset($data['id']); + $this->assertEquals($id, $comment->getId()); + } + + $this->assertEquals($data, [ + 'actorType' => $comment->getActorType(), + 'actorId' => $comment->getActorId(), + 'creationDateTime' => $comment->getCreationDateTime(), + 'message' => $comment->getMessage(), + 'referenceId' => $comment->getReferenceId(), + 'parentId' => $comment->getParentId(), + ]); + } - $this->commentsManager->expects($this->once()) - ->method('save') - ->with($comment); + public function dataSendMessage(): array { + return [ + 'simple message' => ['testUser1', 'testMessage1', '', '0'], + 'reference id' => ['testUser2', 'testMessage2', 'referenceId2', '0'], + 'as a reply' => ['testUser3', 'testMessage3', '', '23'], + 'reply w/ ref' => ['testUser4', 'testMessage4', 'referenceId4', '23'], + ]; + } - $chat = $this->createMock(Room::class); - $chat->expects($this->any()) - ->method('getId') - ->willReturn(1234); + /** + * @dataProvider dataSendMessage + * @param string $userId + * @param string $message + * @param string $referenceId + * @param string $parentId + */ + public function testSendMessage(string $userId, string $message, string $referenceId, string $parentId): void { + $creationDateTime = new \DateTime(); - $this->notifier->expects($this->once()) - ->method('notifyMentionedUsers') - ->with($chat, $comment); + $commentExpected = [ + 'actorType' => 'users', + 'actorId' => $userId, + 'creationDateTime' => $creationDateTime, + 'message' => $message, + 'referenceId' => $referenceId, + 'parentId' => $parentId, + ]; - $participant = $this->createMock(Participant::class); + $comment = $this->newCommentFromArray($commentExpected); - $this->chatManager->sendMessage($chat, $participant, 'users', 'testUser', 'testMessage', $creationDateTime, null); - } + if ($parentId !== '0') { + $replyTo = $this->newCommentFromArray([ + 'id' => $parentId, + ]); - public function testReplyToMessage() { - $replyTo = $this->createMock(IComment::class); - $comment = $this->createMock(IComment::class); + $comment->expects($this->once()) + ->method('setParentId') + ->with($parentId); + } else { + $replyTo = null; + } $this->commentsManager->expects($this->once()) ->method('create') - ->with('users', 'testUser', 'chat', 1234) + ->with('users', $userId, 'chat', 1234) ->willReturn($comment); $comment->expects($this->once()) ->method('setMessage') - ->with('testMessage'); + ->with($message); - $creationDateTime = new \DateTime(); $comment->expects($this->once()) ->method('setCreationDateTime') ->with($creationDateTime); + $comment->expects($referenceId === '' ? $this->never() : $this->once()) + ->method('setReferenceId') + ->with($referenceId); + $comment->expects($this->once()) ->method('setVerb') ->with('comment'); @@ -156,16 +196,11 @@ class ChatManagerTest extends TestCase { ->method('notifyMentionedUsers') ->with($chat, $comment); - $replyTo->expects($this->once()) - ->method('getId') - ->willReturn('12345'); + $participant = $this->createMock(Participant::class); - $comment->expects($this->once()) - ->method('setParentId') - ->with('12345'); + $return = $this->chatManager->sendMessage($chat, $participant, 'users', $userId, $message, $creationDateTime, $replyTo, $referenceId); - $participant = $this->createMock(Participant::class); - $this->chatManager->sendMessage($chat, $participant, 'users', 'testUser', 'testMessage', $creationDateTime, $replyTo); + $this->assertCommentEquals($commentExpected, $return); } public function testGetHistory() { diff --git a/tests/php/Controller/ChatControllerTest.php b/tests/php/Controller/ChatControllerTest.php index 67d39eb13..6833125cc 100644 --- a/tests/php/Controller/ChatControllerTest.php +++ b/tests/php/Controller/ChatControllerTest.php @@ -179,6 +179,7 @@ class ChatControllerTest extends TestCase { 'systemMessage' => '', 'messageType' => 'comment', 'isReplyable' => true, + 'referenceId' => '', ]); $this->messageParser->expects($this->once()) @@ -205,6 +206,78 @@ class ChatControllerTest extends TestCase { 'systemMessage' => '', 'messageType' => 'comment', 'isReplyable' => true, + 'referenceId' => '', + ], Http::STATUS_CREATED); + + $this->assertEquals($expected, $response); + } + + public function testSendMessageByUserWithReferenceId() { + $participant = $this->createMock(Participant::class); + + $date = new \DateTime(); + $this->timeFactory->expects($this->once()) + ->method('getDateTime') + ->willReturn($date); + /** @var IComment|MockObject $comment */ + $comment = $this->newComment(42, 'user', $this->userId, $date, 'testMessage'); + $this->chatManager->expects($this->once()) + ->method('sendMessage') + ->with($this->room, + $participant, + 'users', + $this->userId, + 'testMessage', + $this->newMessageDateTimeConstraint + ) + ->willReturn($comment); + + $chatMessage = $this->createMock(Message::class); + $chatMessage->expects($this->once()) + ->method('getVisibility') + ->willReturn(true); + $chatMessage->expects($this->once()) + ->method('toArray') + ->willReturn([ + 'id' => 42, + 'token' => 'testToken', + 'actorType' => 'users', + 'actorId' => $this->userId, + 'actorDisplayName' => 'displayName', + 'timestamp' => $date->getTimestamp(), + 'message' => 'parsedMessage', + 'messageParameters' => ['arg' => 'uments'], + 'systemMessage' => '', + 'messageType' => 'comment', + 'isReplyable' => true, + 'referenceId' => sha1('ref'), + ]); + + $this->messageParser->expects($this->once()) + ->method('createMessage') + ->with($this->room, $participant, $comment, $this->l) + ->willReturn($chatMessage); + + $this->messageParser->expects($this->once()) + ->method('parseMessage') + ->with($chatMessage); + + $this->controller->setRoom($this->room); + $this->controller->setParticipant($participant); + $response = $this->controller->sendMessage('testMessage', '', sha1('ref')); + $expected = new DataResponse([ + 'id' => 42, + 'token' => 'testToken', + 'actorType' => 'users', + 'actorId' => $this->userId, + 'actorDisplayName' => 'displayName', + 'timestamp' => $date->getTimestamp(), + 'message' => 'parsedMessage', + 'messageParameters' => ['arg' => 'uments'], + 'systemMessage' => '', + 'messageType' => 'comment', + 'isReplyable' => true, + 'referenceId' => sha1('ref'), ], Http::STATUS_CREATED); $this->assertEquals($expected, $response); @@ -257,6 +330,7 @@ class ChatControllerTest extends TestCase { 'systemMessage' => '', 'messageType' => 'comment', 'isReplyable' => true, + 'referenceId' => '', ]); $chatMessage = $this->createMock(Message::class); @@ -277,6 +351,7 @@ class ChatControllerTest extends TestCase { 'systemMessage' => '', 'messageType' => 'comment', 'isReplyable' => true, + 'referenceId' => '', ]); $this->messageParser->expects($this->exactly(2)) @@ -293,7 +368,7 @@ class ChatControllerTest extends TestCase { $this->controller->setRoom($this->room); $this->controller->setParticipant($participant); - $response = $this->controller->sendMessage('testMessage', '', 23); + $response = $this->controller->sendMessage('testMessage', '', '', 23); $expected = new DataResponse([ 'id' => 42, 'token' => 'testToken', @@ -306,6 +381,7 @@ class ChatControllerTest extends TestCase { 'systemMessage' => '', 'messageType' => 'comment', 'isReplyable' => true, + 'referenceId' => '', 'parent' => [ 'id' => 23, 'token' => 'testToken', @@ -318,6 +394,7 @@ class ChatControllerTest extends TestCase { 'systemMessage' => '', 'messageType' => 'comment', 'isReplyable' => true, + 'referenceId' => '', ], ], Http::STATUS_CREATED); @@ -354,7 +431,7 @@ class ChatControllerTest extends TestCase { $this->controller->setRoom($this->room); $this->controller->setParticipant($participant); - $response = $this->controller->sendMessage('testMessage', '', 23); + $response = $this->controller->sendMessage('testMessage', '', '', 23); $expected = new DataResponse([], Http::STATUS_BAD_REQUEST); $this->assertEquals($expected, $response); |