Skip to content
Merged
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 a unit test
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen committed Feb 19, 2021
commit 317537ee3fefbb8e51a2e0834c78ea1d923b3058
93 changes: 93 additions & 0 deletions tests/php/Controller/ChatControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\RichObjectStrings\IValidator;
use OCP\UserStatus\IManager as IUserStatusManager;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -86,6 +87,8 @@ class ChatControllerTest extends TestCase {
protected $eventDispatcher;
/** @var ITimeFactory|MockObject */
protected $timeFactory;
/** @var IValidator|MockObject */
protected $richObjectValidator;
/** @var IL10N|MockObject */
private $l;

Expand Down Expand Up @@ -117,6 +120,7 @@ public function setUp(): void {
$this->searchResult = $this->createMock(ISearchResult::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->richObjectValidator = $this->createMock(IValidator::class);
$this->l = $this->createMock(IL10N::class);

$this->room = $this->createMock(Room::class);
Expand Down Expand Up @@ -151,6 +155,7 @@ private function recreateChatController() {
$this->searchResult,
$this->timeFactory,
$this->eventDispatcher,
$this->richObjectValidator,
$this->l
);
}
Expand Down Expand Up @@ -613,6 +618,94 @@ public function testSendMessageByGuest() {
$this->assertEquals($expected, $response);
}

public function testShareObjectToChatByUser() {
$participant = $this->createMock(Participant::class);

$richData = [
'call-type' => 'one2one',
'type' => 'call',
'id' => 'R4nd0mToken',
];

$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('addSystemMessage')
->with($this->room,
'users',
$this->userId,
json_encode([
'message' => 'object_shared',
'parameters' => [
'objectType' => 'call',
'objectId' => 'R4nd0mToken',
'metaData' => [
'call-type' => 'one2one',
'type' => 'call',
'id' => 'R4nd0mToken',
],
],
]),
$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' => '{object}',
'messageParameters' => $richData,
'systemMessage' => '',
'messageType' => 'comment',
'isReplyable' => true,
'referenceId' => '',
]);

$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->shareObjectToChat($richData['type'], $richData['id'], json_encode(['call-type' => $richData['call-type']]));
$expected = new DataResponse([
'id' => 42,
'token' => 'testToken',
'actorType' => 'users',
'actorId' => $this->userId,
'actorDisplayName' => 'displayName',
'timestamp' => $date->getTimestamp(),
'message' => '{object}',
'messageParameters' => $richData,
'systemMessage' => '',
'messageType' => 'comment',
'isReplyable' => true,
'referenceId' => '',
], Http::STATUS_CREATED);

$this->assertEquals($expected->getStatus(), $response->getStatus());
$this->assertEquals($expected->getData(), $response->getData());
}

public function testReceiveHistoryByUser() {
$offset = 23;
$limit = 4;
Expand Down