2022-04-09 19:08:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Controllers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Engelsystem\Controllers\MessagesController;
|
|
|
|
use Engelsystem\Helpers\Authenticator;
|
|
|
|
use Engelsystem\Http\Exceptions\HttpForbidden;
|
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
|
|
|
use Engelsystem\Http\UrlGenerator;
|
|
|
|
use Engelsystem\Http\UrlGeneratorInterface;
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Models\Message;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
|
|
|
class MessagesControllerTest extends ControllerTest
|
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
/** @var MessagesController */
|
|
|
|
protected $controller;
|
|
|
|
|
|
|
|
/** @var Authenticator|MockObject */
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/** @var User */
|
2022-06-04 22:42:52 +02:00
|
|
|
protected $userA;
|
2022-04-09 19:08:53 +02:00
|
|
|
/** @var User */
|
2022-06-04 22:42:52 +02:00
|
|
|
protected $userB;
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
/** @var Carbon */
|
|
|
|
protected $now;
|
|
|
|
/** @var Carbon */
|
2022-06-04 22:42:52 +02:00
|
|
|
protected $oneMinuteAgo;
|
2022-04-09 19:08:53 +02:00
|
|
|
/** @var Carbon */
|
2022-06-04 22:42:52 +02:00
|
|
|
protected $twoMinutesAgo;
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox index: underNormalConditions -> returnsCorrectViewAndData
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::__construct
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testIndexUnderNormalConditionsReturnsCorrectViewAndData()
|
|
|
|
{
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$this->assertEquals('pages/messages/overview.twig', $view);
|
|
|
|
$this->assertArrayHasKey('conversations', $data);
|
|
|
|
$this->assertArrayHasKey('users', $data);
|
|
|
|
$this->assertArrayOrCollection($data['conversations']);
|
|
|
|
$this->assertArrayOrCollection($data['users']);
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 22:42:52 +02:00
|
|
|
* @testdox index: usersExist -> returnsUsersWithMeAtFirstPosition
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 22:42:52 +02:00
|
|
|
public function testIndexUsersExistReturnsUsersWithMeAtFirstPosition()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
User::factory(['name' => '0'])->create(); // alphabetically before me ("a"), but still listed after me
|
|
|
|
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$users = $data['users'];
|
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->assertEquals(3, count($users));
|
|
|
|
$this->assertEquals('a', $users->shift());
|
|
|
|
$this->assertEquals('0', $users->shift());
|
|
|
|
$this->assertEquals('b', $users->shift());
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox index: withNoConversation -> returnsEmptyConversationList
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testIndexWithNoConversationReturnsEmptyConversationList()
|
|
|
|
{
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$this->assertEquals(0, count($data['conversations']));
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox index: withConversation -> conversationContainsCorrectData
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testIndexWithConversationConversationContainsCorrectData()
|
|
|
|
{
|
|
|
|
// save messages in wrong order to ensure latest message considers creation date, not id.
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->twoMinutesAgo);
|
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->oneMinuteAgo);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$conversations = $data['conversations'];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($conversations));
|
|
|
|
$c = $conversations[0];
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('other_user', $c);
|
|
|
|
$this->assertArrayHasKey('latest_message', $c);
|
|
|
|
$this->assertArrayHasKey('unread_messages', $c);
|
|
|
|
|
|
|
|
$this->assertTrue($c['other_user'] instanceof User);
|
|
|
|
$this->assertTrue($c['latest_message'] instanceof Message);
|
|
|
|
$this->assertEquals('string', gettype($c['unread_messages']));
|
|
|
|
|
|
|
|
$this->assertEquals('b', $c['other_user']->name);
|
|
|
|
$this->assertEquals('b>a', $c['latest_message']->text);
|
|
|
|
$this->assertEquals(2, $c['unread_messages']);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox index: withConversations -> onlyContainsConversationsWithMe
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testIndexWithConversationsOnlyContainsConversationsWithMe()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$userC = User::factory(['name' => 'c'])->create();
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
// save messages in wrong order to ensure latest message considers creation date, not id.
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
|
|
|
$this->createMessage($this->userB, $userC, 'b>c', $this->now);
|
|
|
|
$this->createMessage($userC, $this->userA, 'c>a', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$conversations = $data['conversations'];
|
|
|
|
|
|
|
|
$this->assertEquals(2, count($conversations));
|
|
|
|
$msg0 = $conversations[0]['latest_message']->text;
|
|
|
|
$msg1 = $conversations[1]['latest_message']->text;
|
|
|
|
$this->assertTrue(($msg0 == 'a>b' && $msg1 == 'c>a') || ($msg1 == 'c>a' && $msg0 == 'a>b'));
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox index: withConversations -> conversationsOrderedByDate
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
2022-06-04 22:42:52 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testIndexWithConversationsConversationsOrderedByDate()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$userC = User::factory(['name' => 'c'])->create();
|
|
|
|
$userD = User::factory(['name' => 'd'])->create();
|
2022-04-09 19:08:53 +02:00
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
|
|
|
$this->createMessage($userD, $this->userA, 'd>a', $this->twoMinutesAgo);
|
|
|
|
$this->createMessage($this->userA, $userC, 'a>c', $this->oneMinuteAgo);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$conversations = $data['conversations'];
|
|
|
|
|
|
|
|
$this->assertEquals('a>b', $conversations[0]['latest_message']->text);
|
|
|
|
$this->assertEquals('a>c', $conversations[1]['latest_message']->text);
|
|
|
|
$this->assertEquals('d>a', $conversations[2]['latest_message']->text);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox redirectToConversation: withNoUserIdGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::redirectToConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testRedirectToConversationWithNoUserIdGivenThrowsException()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
|
|
|
$this->expectException(ValidationException::class);
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->redirectToConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox redirectToConversation: withUserIdGiven -> redirect
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::redirectToConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testRedirectToConversationWithUserIdGivenRedirect()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['user_id' => '1']);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
2022-06-04 22:42:52 +02:00
|
|
|
->with('http://localhost/messages/1#newest')
|
2022-04-09 19:08:53 +02:00
|
|
|
->willReturn($this->response);
|
|
|
|
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->redirectToConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: withNoUserIdGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationWithNoUserIdGivenThrowsException()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: withUnknownUserIdGiven -> throwsException
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationWithUnknownUserIdGivenThrowsException()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
|
|
|
$this->request->attributes->set('user_id', '1234');
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: underNormalConditions -> returnsCorrectViewAndData
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationUnderNormalConditionsReturnsCorrectViewAndData()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request->attributes->set('user_id', $this->userB->id);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$this->assertEquals('pages/messages/conversation.twig', $view);
|
|
|
|
$this->assertArrayHasKey('messages', $data);
|
|
|
|
$this->assertArrayHasKey('other_user', $data);
|
|
|
|
$this->assertArrayOrCollection($data['messages']);
|
|
|
|
$this->assertTrue($data['other_user'] instanceof User);
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: withNoMessages -> returnsEmptyMessageList
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationWithNoMessagesReturnsEmptyMessageList()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request->attributes->set('user_id', $this->userB->id);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$this->assertEquals(0, count($data['messages']));
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: withMessages -> messagesOnlyWithThatUserOrderedByDate
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationWithMessagesMessagesOnlyWithThatUserOrderedByDate()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request->attributes->set('user_id', $this->userB->id);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$userC = User::factory(['name' => 'c'])->create();
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
// to be listed
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->twoMinutesAgo);
|
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a2', $this->oneMinuteAgo);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
// not to be listed
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userA, $userC, 'a>c', $this->now);
|
|
|
|
$this->createMessage($userC, $this->userB, 'b>c', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$messages = $data['messages'];
|
|
|
|
$this->assertEquals(3, count($messages));
|
|
|
|
$this->assertEquals('b>a', $messages[0]->text);
|
|
|
|
$this->assertEquals('b>a2', $messages[1]->text);
|
|
|
|
$this->assertEquals('a>b', $messages[2]->text);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: withUnreadMessages -> messagesToMeWillStillBeReturnedAsUnread
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationWithUnreadMessagesMessagesToMeWillStillBeReturnedAsUnread()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request->attributes->set('user_id', $this->userB->id);
|
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$this->assertFalse($data['messages'][0]->read);
|
|
|
|
return $this->response;
|
|
|
|
});
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-06-04 17:55:26 +02:00
|
|
|
* @testdox messagesOfConversation: withUnreadMessages -> messagesToMeWillBeMarkedAsRead
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
2022-06-04 17:55:26 +02:00
|
|
|
public function testMessagesOfConversationWithUnreadMessagesMessagesToMeWillBeMarkedAsRead()
|
2022-04-09 19:08:53 +02:00
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request->attributes->set('user_id', $this->userB->id);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$msg = $this->createMessage($this->userB, $this->userA, 'b>a', $this->now);
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->controller->messagesOfConversation($this->request);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->assertTrue(Message::whereId($msg->id)->first()->read);
|
|
|
|
}
|
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
/**
|
|
|
|
* @testdox messagesOfConversation: withMyUserIdGiven -> returnsMessagesFromMeToMe
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
|
|
|
*/
|
|
|
|
public function testMessagesOfConversationWithMyUserIdGivenReturnsMessagesFromMeToMe()
|
|
|
|
{
|
|
|
|
$this->request->attributes->set('user_id', $this->userA->id); // myself
|
|
|
|
|
|
|
|
$this->createMessage($this->userA, $this->userA, 'a>a1', $this->now);
|
|
|
|
$this->createMessage($this->userA, $this->userA, 'a>a2', $this->twoMinutesAgo);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('withView')
|
|
|
|
->willReturnCallback(function (string $view, array $data) {
|
|
|
|
$messages = $data['messages'];
|
|
|
|
$this->assertEquals(2, count($messages));
|
|
|
|
$this->assertEquals('a>a2', $messages[0]->text);
|
|
|
|
$this->assertEquals('a>a1', $messages[1]->text);
|
|
|
|
|
|
|
|
return $this->response;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->controller->messagesOfConversation($this->request);
|
|
|
|
}
|
|
|
|
|
2022-04-09 19:08:53 +02:00
|
|
|
/**
|
|
|
|
* @testdox send: withNoTextGiven -> throwsException
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testSendWithNoTextGivenThrowsException()
|
|
|
|
{
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$this->controller->send($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox send: withNoUserIdGiven -> throwsException
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testSendWithNoUserIdGivenThrowsException()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$this->controller->send($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox send: withUnknownUserIdGiven -> throwsException
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testSendWithUnknownUserIdGivenThrowsException()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->request->attributes->set('user_id', '1234');
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$this->controller->send($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox send: withUserAndTextGiven -> savesMessage
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testSendWithUserAndTextGivenSavesMessage()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
|
|
|
$this->request->attributes->set('user_id', $this->userB->id);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
2022-06-04 22:42:52 +02:00
|
|
|
->with('http://localhost/messages/' . $this->userB->id)
|
2022-04-09 19:08:53 +02:00
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->send($this->request);
|
|
|
|
|
|
|
|
$msg = Message::whereText('a')->first();
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->assertEquals($this->userA->id, $msg->user_id);
|
|
|
|
$this->assertEquals($this->userB->id, $msg->receiver_id);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->assertFalse($msg->read);
|
|
|
|
}
|
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
/**
|
|
|
|
* @testdox send: withMyUserIdGiven -> savesMessageAlreadyMarkedAsRead
|
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
|
|
|
*/
|
|
|
|
public function testSendWithMyUserIdGivenSavesMessageAlreadyMarkedAsRead()
|
|
|
|
{
|
|
|
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
|
|
|
$this->request->attributes->set('user_id', $this->userA->id);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/messages/' . $this->userA->id)
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->send($this->request);
|
|
|
|
|
|
|
|
$msg = Message::whereText('a')->first();
|
|
|
|
$this->assertEquals($this->userA->id, $msg->user_id);
|
|
|
|
$this->assertEquals($this->userA->id, $msg->receiver_id);
|
|
|
|
$this->assertTrue($msg->read);
|
|
|
|
}
|
|
|
|
|
2022-04-09 19:08:53 +02:00
|
|
|
/**
|
|
|
|
* @testdox delete: withNoMsgIdGiven -> throwsException
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::delete
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testDeleteWithNoMsgIdGivenThrowsException()
|
|
|
|
{
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
|
|
|
$this->controller->delete($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox delete: tryingToDeleteSomeonesMessage -> throwsException
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::delete
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testDeleteTryingToDeleteSomeonesMessageThrowsException()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$msg = $this->createMessage($this->userB, $this->userA, 'a>b', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->request->attributes->set('msg_id', $msg->id);
|
2022-06-04 17:55:26 +02:00
|
|
|
$this->expectException(HttpForbidden::class);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->controller->delete($this->request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox delete: tryingToDeleteMyMessage -> deletesItAndRedirect
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::delete
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testDeleteTryingToDeleteMyMessageDeletesItAndRedirect()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$msg = $this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->request->attributes->set('msg_id', $msg->id);
|
|
|
|
$this->request->attributes->set('user_id', '1');
|
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('redirectTo')
|
|
|
|
->with('http://localhost/messages/1')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->controller->delete($this->request);
|
|
|
|
|
|
|
|
$this->assertEquals(0, count(Message::whereId($msg->id)->get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox NumberOfUnreadMessages: withNoMessages -> returns0
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testNumberOfUnreadMessagesWithNoMessagesReturns0()
|
|
|
|
{
|
|
|
|
$this->assertEquals(0, $this->controller->numberOfUnreadMessages());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox NumberOfUnreadMessages: withMessagesNotToMe -> messagesNotToMeAreIgnored
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testNumberOfUnreadMessagesWithMessagesNotToMeMessagesNotToMeAreIgnored()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$userC = User::factory(['name' => 'c'])->create();
|
2022-04-09 19:08:53 +02:00
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
|
|
|
$this->createMessage($this->userB, $userC, 'b>c', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
$this->assertEquals(0, $this->controller->numberOfUnreadMessages());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox NumberOfUnreadMessages: withMessages -> returnsSumOfUnreadMessagesSentToMe
|
2022-06-04 17:55:26 +02:00
|
|
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages
|
2022-04-09 19:08:53 +02:00
|
|
|
*/
|
|
|
|
public function testNumberOfUnreadMessagesWithMessagesReturnsSumOfUnreadMessagesSentToMe()
|
|
|
|
{
|
2022-06-04 22:42:52 +02:00
|
|
|
$userC = User::factory(['name' => 'c'])->create();
|
2022-04-09 19:08:53 +02:00
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a1', $this->now);
|
|
|
|
$this->createMessage($this->userB, $this->userA, 'b>a2', $this->now);
|
|
|
|
$this->createMessage($userC, $this->userA, 'c>a', $this->now);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->assertEquals(3, $this->controller->numberOfUnreadMessages());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup environment
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->auth = $this->createMock(Authenticator::class);
|
|
|
|
$this->app->instance(Authenticator::class, $this->auth);
|
|
|
|
|
|
|
|
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
|
|
|
|
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->userA = User::factory(['name' => 'a'])->create();
|
|
|
|
$this->userB = User::factory(['name' => 'b'])->create();
|
|
|
|
$this->setExpects($this->auth, 'user', null, $this->userA, $this->any());
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->now = Carbon::now();
|
2022-06-04 22:42:52 +02:00
|
|
|
$this->oneMinuteAgo = Carbon::now()->subMinute();
|
|
|
|
$this->twoMinutesAgo = Carbon::now()->subMinutes(2);
|
2022-04-09 19:08:53 +02:00
|
|
|
|
|
|
|
$this->controller = $this->app->get(MessagesController::class);
|
|
|
|
$this->controller->setValidator(new Validator());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function assertArrayOrCollection($obj)
|
|
|
|
{
|
|
|
|
$this->assertTrue(gettype($obj) == 'array' || $obj instanceof Collection);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function createMessage(User $from, User $to, string $text, Carbon $at): Message
|
|
|
|
{
|
|
|
|
Message::unguard(); // unguard temporarily to save custom creation dates.
|
|
|
|
$msg = new Message([
|
|
|
|
'user_id' => $from->id,
|
|
|
|
'receiver_id' => $to->id,
|
|
|
|
'text' => $text,
|
|
|
|
'created_at' => $at,
|
|
|
|
'updated_at' => $at,
|
|
|
|
]);
|
|
|
|
$msg->save();
|
|
|
|
Message::reguard();
|
|
|
|
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
}
|