Enabled to write messages to myself
This commit is contained in:
parent
71d183df01
commit
865873c099
|
@ -16,23 +16,7 @@
|
||||||
<div class="col-12 col-lg-8 offset-lg-2">
|
<div class="col-12 col-lg-8 offset-lg-2">
|
||||||
<div class="row conversation">
|
<div class="row conversation">
|
||||||
{% for msg in messages %}
|
{% for msg in messages %}
|
||||||
{% if msg.user_id == other_user.id %}
|
{% if msg.user_id == user.id %}
|
||||||
<div class="col-12">
|
|
||||||
<div>
|
|
||||||
<div class="message alert alert-secondary position-relative">
|
|
||||||
<div>{{ msg.text | nl2br }}</div>
|
|
||||||
<div class="text-end">
|
|
||||||
<small
|
|
||||||
class="opacity-75">{{ msg.created_at.format(__('Y-m-d H:i')) }}</small>
|
|
||||||
</div>
|
|
||||||
{% if msg.read == false %}
|
|
||||||
<span class="position-absolute top-0 start-100 translate-middle-x p-2
|
|
||||||
bg-danger rounded-circle"></span>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
{% else %}
|
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<div class="d-flex justify-content-end">
|
<div class="d-flex justify-content-end">
|
||||||
<div class="message alert alert-primary">
|
<div class="message alert alert-primary">
|
||||||
|
@ -49,8 +33,25 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{% else %}
|
||||||
|
<div class="col-12">
|
||||||
|
<div>
|
||||||
|
<div class="message alert alert-secondary position-relative">
|
||||||
|
<div>{{ msg.text | nl2br }}</div>
|
||||||
|
<div class="text-end">
|
||||||
|
<small
|
||||||
|
class="opacity-75">{{ msg.created_at.format(__('Y-m-d H:i')) }}</small>
|
||||||
|
</div>
|
||||||
|
{% if msg.read == false %}
|
||||||
|
<span class="position-absolute top-0 start-100 translate-middle-x p-2
|
||||||
|
bg-danger rounded-circle"></span>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
<div id="newest">{# id for scrolling to the newest messages #}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form action="" enctype="multipart/form-data" method="post">
|
<form action="" enctype="multipart/form-data" method="post">
|
||||||
|
|
|
@ -36,14 +36,14 @@
|
||||||
{% for c in conversations %}
|
{% for c in conversations %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
{{ m.user(c.other_user, {'show_pronoun_if_configured': true, 'url': url('messages/' ~ c.other_user.id)}) }}
|
{{ m.user(c.other_user, {'show_pronoun_if_configured': true, 'url': url('messages/' ~ c.other_user.id ~ '#newest')}) }}
|
||||||
|
|
||||||
{% if c.unread_messages > 0 %}
|
{% if c.unread_messages > 0 %}
|
||||||
<span class="badge bg-danger">{{ c.unread_messages }}</span>
|
<span class="badge bg-danger">{{ c.unread_messages }}</span>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ url('messages/' ~ c.other_user.id) }}">
|
<a href="{{ url('messages/' ~ c.other_user.id ~ '#newest') }}">
|
||||||
{{ c.latest_message.text|length > 100 ? c.latest_message.text|slice(0, 100) ~ '…' : c.latest_message.text }}
|
{{ c.latest_message.text|length > 100 ? c.latest_message.text|slice(0, 100) ~ '…' : c.latest_message.text }}
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
|
|
|
@ -104,6 +104,7 @@ class MessagesController extends BaseController
|
||||||
->mapWithKeys(function ($u) {
|
->mapWithKeys(function ($u) {
|
||||||
return [ $u->id => $u->name ];
|
return [ $u->id => $u->name ];
|
||||||
});
|
});
|
||||||
|
$users->prepend($currentUser->name, $currentUser->id);
|
||||||
|
|
||||||
return $this->response->withView(
|
return $this->response->withView(
|
||||||
'pages/messages/overview.twig',
|
'pages/messages/overview.twig',
|
||||||
|
@ -120,22 +121,18 @@ class MessagesController extends BaseController
|
||||||
public function redirectToConversation(Request $request): Response
|
public function redirectToConversation(Request $request): Response
|
||||||
{
|
{
|
||||||
$data = $this->validate($request, ['user_id' => 'required|int']);
|
$data = $this->validate($request, ['user_id' => 'required|int']);
|
||||||
return $this->redirect->to('/messages/' . $data['user_id']);
|
return $this->redirect->to('/messages/' . $data['user_id'] . '#newest');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of messages between the current user and a user with the given id. Unread messages will be marked
|
* Returns a list of messages between the current user and a user with the given id. Unread messages will be marked
|
||||||
* as read during this call. Still, they will be shown as unread in the frontend to highlight them to the user as new.
|
* as read during this call. Still, they will be shown as unread in the frontend to show that they are new.
|
||||||
*/
|
*/
|
||||||
public function messagesOfConversation(Request $request): Response
|
public function messagesOfConversation(Request $request): Response
|
||||||
{
|
{
|
||||||
$currentUser = $this->auth->user();
|
$currentUser = $this->auth->user();
|
||||||
$otherUser = $this->user->findOrFail($request->getAttribute('user_id'));
|
$otherUser = $this->user->findOrFail($request->getAttribute('user_id'));
|
||||||
|
|
||||||
if ($currentUser->id == $otherUser->id) {
|
|
||||||
throw new HttpForbidden('You can not start a conversation with yourself.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$messages = $this->message
|
$messages = $this->message
|
||||||
->where(function ($query) use ($currentUser, $otherUser) {
|
->where(function ($query) use ($currentUser, $otherUser) {
|
||||||
$query->whereUserId($currentUser->id)
|
$query->whereUserId($currentUser->id)
|
||||||
|
@ -175,15 +172,11 @@ class MessagesController extends BaseController
|
||||||
|
|
||||||
$otherUser = $this->user->findOrFail($request->getAttribute('user_id'));
|
$otherUser = $this->user->findOrFail($request->getAttribute('user_id'));
|
||||||
|
|
||||||
if ($otherUser->id == $currentUser->id) {
|
|
||||||
throw new HttpForbidden('You can not send a message to yourself.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$newMessage = new Message();
|
$newMessage = new Message();
|
||||||
$newMessage->sender()->associate($currentUser);
|
$newMessage->sender()->associate($currentUser);
|
||||||
$newMessage->receiver()->associate($otherUser);
|
$newMessage->receiver()->associate($otherUser);
|
||||||
$newMessage->text = $data['text'];
|
$newMessage->text = $data['text'];
|
||||||
$newMessage->read = false;
|
$newMessage->read = $otherUser->id == $currentUser->id; // if its to myself, I obviously read it.
|
||||||
$newMessage->save();
|
$newMessage->save();
|
||||||
|
|
||||||
return $this->redirect->to('/messages/' . $otherUser->id);
|
return $this->redirect->to('/messages/' . $otherUser->id);
|
||||||
|
@ -202,7 +195,6 @@ class MessagesController extends BaseController
|
||||||
|
|
||||||
if ($msg->user_id == $currentUser->id) {
|
if ($msg->user_id == $currentUser->id) {
|
||||||
$msg->delete();
|
$msg->delete();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new HttpForbidden('You can not delete a message you haven\'t send');
|
throw new HttpForbidden('You can not delete a message you haven\'t send');
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,20 +28,25 @@ class MessagesControllerTest extends ControllerTest
|
||||||
protected $auth;
|
protected $auth;
|
||||||
|
|
||||||
/** @var User */
|
/** @var User */
|
||||||
protected $user_a;
|
protected $userA;
|
||||||
/** @var User */
|
/** @var User */
|
||||||
protected $user_b;
|
protected $userB;
|
||||||
|
|
||||||
/** @var Carbon */
|
/** @var Carbon */
|
||||||
protected $now;
|
protected $now;
|
||||||
/** @var Carbon */
|
/** @var Carbon */
|
||||||
protected $one_minute_ago;
|
protected $oneMinuteAgo;
|
||||||
/** @var Carbon */
|
/** @var Carbon */
|
||||||
protected $two_minutes_ago;
|
protected $twoMinutesAgo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @testdox index: underNormalConditions -> returnsCorrectViewAndData
|
* @testdox index: underNormalConditions -> returnsCorrectViewAndData
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::__construct
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::index
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
||||||
*/
|
*/
|
||||||
public function testIndexUnderNormalConditionsReturnsCorrectViewAndData()
|
public function testIndexUnderNormalConditionsReturnsCorrectViewAndData()
|
||||||
{
|
{
|
||||||
|
@ -60,18 +65,26 @@ class MessagesControllerTest extends ControllerTest
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @testdox index: otherUsersExist -> returnsUsersWithoutMeOrderedByName
|
* @testdox index: usersExist -> returnsUsersWithMeAtFirstPosition
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::index
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
||||||
*/
|
*/
|
||||||
public function testIndexOtherUsersExistReturnsUsersWithoutMeOrderedByName()
|
public function testIndexUsersExistReturnsUsersWithMeAtFirstPosition()
|
||||||
{
|
{
|
||||||
|
User::factory(['name' => '0'])->create(); // alphabetically before me ("a"), but still listed after me
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
->willReturnCallback(function (string $view, array $data) {
|
->willReturnCallback(function (string $view, array $data) {
|
||||||
$users = $data['users'];
|
$users = $data['users'];
|
||||||
|
|
||||||
$this->assertEquals(1, count($users));
|
$this->assertEquals(3, count($users));
|
||||||
$this->assertEquals('b', $users[$this->user_b->id]);
|
$this->assertEquals('a', $users->shift());
|
||||||
|
$this->assertEquals('0', $users->shift());
|
||||||
|
$this->assertEquals('b', $users->shift());
|
||||||
|
|
||||||
return $this->response;
|
return $this->response;
|
||||||
});
|
});
|
||||||
|
@ -82,6 +95,10 @@ class MessagesControllerTest extends ControllerTest
|
||||||
/**
|
/**
|
||||||
* @testdox index: withNoConversation -> returnsEmptyConversationList
|
* @testdox index: withNoConversation -> returnsEmptyConversationList
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::index
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
||||||
*/
|
*/
|
||||||
public function testIndexWithNoConversationReturnsEmptyConversationList()
|
public function testIndexWithNoConversationReturnsEmptyConversationList()
|
||||||
{
|
{
|
||||||
|
@ -98,13 +115,17 @@ class MessagesControllerTest extends ControllerTest
|
||||||
/**
|
/**
|
||||||
* @testdox index: withConversation -> conversationContainsCorrectData
|
* @testdox index: withConversation -> conversationContainsCorrectData
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::index
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
||||||
*/
|
*/
|
||||||
public function testIndexWithConversationConversationContainsCorrectData()
|
public function testIndexWithConversationConversationContainsCorrectData()
|
||||||
{
|
{
|
||||||
// save messages in wrong order to ensure latest message considers creation date, not id.
|
// save messages in wrong order to ensure latest message considers creation date, not id.
|
||||||
$this->createMessage($this->user_a, $this->user_b, 'a>b', $this->now);
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a', $this->two_minutes_ago);
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->twoMinutesAgo);
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a', $this->one_minute_ago);
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->oneMinuteAgo);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -135,15 +156,19 @@ class MessagesControllerTest extends ControllerTest
|
||||||
/**
|
/**
|
||||||
* @testdox index: withConversations -> onlyContainsConversationsWithMe
|
* @testdox index: withConversations -> onlyContainsConversationsWithMe
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::index
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
||||||
*/
|
*/
|
||||||
public function testIndexWithConversationsOnlyContainsConversationsWithMe()
|
public function testIndexWithConversationsOnlyContainsConversationsWithMe()
|
||||||
{
|
{
|
||||||
$user_c = User::factory(['name' => 'c'])->create();
|
$userC = User::factory(['name' => 'c'])->create();
|
||||||
|
|
||||||
// save messages in wrong order to ensure latest message considers creation date, not id.
|
// save messages in wrong order to ensure latest message considers creation date, not id.
|
||||||
$this->createMessage($this->user_a, $this->user_b, 'a>b', $this->now);
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
||||||
$this->createMessage($this->user_b, $user_c, 'b>c', $this->now);
|
$this->createMessage($this->userB, $userC, 'b>c', $this->now);
|
||||||
$this->createMessage($user_c, $this->user_a, 'c>a', $this->now);
|
$this->createMessage($userC, $this->userA, 'c>a', $this->now);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -164,15 +189,19 @@ class MessagesControllerTest extends ControllerTest
|
||||||
/**
|
/**
|
||||||
* @testdox index: withConversations -> conversationsOrderedByDate
|
* @testdox index: withConversations -> conversationsOrderedByDate
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::index
|
* @covers \Engelsystem\Controllers\MessagesController::index
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::listConversations
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::latestMessagePerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessagesPerConversation
|
||||||
|
* @covers \Engelsystem\Controllers\MessagesController::raw
|
||||||
*/
|
*/
|
||||||
public function testIndexWithConversationsConversationsOrderedByDate()
|
public function testIndexWithConversationsConversationsOrderedByDate()
|
||||||
{
|
{
|
||||||
$user_c = User::factory(['name' => 'c'])->create();
|
$userC = User::factory(['name' => 'c'])->create();
|
||||||
$user_d = User::factory(['name' => 'd'])->create();
|
$userD = User::factory(['name' => 'd'])->create();
|
||||||
|
|
||||||
$this->createMessage($this->user_a, $this->user_b, 'a>b', $this->now);
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
||||||
$this->createMessage($user_d, $this->user_a, 'd>a', $this->two_minutes_ago);
|
$this->createMessage($userD, $this->userA, 'd>a', $this->twoMinutesAgo);
|
||||||
$this->createMessage($this->user_a, $user_c, 'a>c', $this->one_minute_ago);
|
$this->createMessage($this->userA, $userC, 'a>c', $this->oneMinuteAgo);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -205,12 +234,10 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testRedirectToConversationWithUserIdGivenRedirect()
|
public function testRedirectToConversationWithUserIdGivenRedirect()
|
||||||
{
|
{
|
||||||
$this->request = $this->request->withParsedBody([
|
$this->request = $this->request->withParsedBody(['user_id' => '1']);
|
||||||
'user_id' => '1',
|
|
||||||
]);
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('redirectTo')
|
->method('redirectTo')
|
||||||
->with('http://localhost/messages/1')
|
->with('http://localhost/messages/1#newest')
|
||||||
->willReturn($this->response);
|
->willReturn($this->response);
|
||||||
|
|
||||||
$this->controller->redirectToConversation($this->request);
|
$this->controller->redirectToConversation($this->request);
|
||||||
|
@ -226,18 +253,6 @@ class MessagesControllerTest extends ControllerTest
|
||||||
$this->controller->messagesOfConversation($this->request);
|
$this->controller->messagesOfConversation($this->request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @testdox messagesOfConversation: withMyUserIdGiven -> throwsException
|
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
|
||||||
*/
|
|
||||||
public function testMessagesOfConversationWithMyUserIdGivenThrowsException()
|
|
||||||
{
|
|
||||||
$this->request->attributes->set('user_id', $this->user_a->id);
|
|
||||||
$this->expectException(HttpForbidden::class);
|
|
||||||
|
|
||||||
$this->controller->messagesOfConversation($this->request);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @testdox messagesOfConversation: withUnknownUserIdGiven -> throwsException
|
* @testdox messagesOfConversation: withUnknownUserIdGiven -> throwsException
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
* @covers \Engelsystem\Controllers\MessagesController::messagesOfConversation
|
||||||
|
@ -255,7 +270,7 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testMessagesOfConversationUnderNormalConditionsReturnsCorrectViewAndData()
|
public function testMessagesOfConversationUnderNormalConditionsReturnsCorrectViewAndData()
|
||||||
{
|
{
|
||||||
$this->request->attributes->set('user_id', $this->user_b->id);
|
$this->request->attributes->set('user_id', $this->userB->id);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -277,7 +292,7 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testMessagesOfConversationWithNoMessagesReturnsEmptyMessageList()
|
public function testMessagesOfConversationWithNoMessagesReturnsEmptyMessageList()
|
||||||
{
|
{
|
||||||
$this->request->attributes->set('user_id', $this->user_b->id);
|
$this->request->attributes->set('user_id', $this->userB->id);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -295,18 +310,18 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testMessagesOfConversationWithMessagesMessagesOnlyWithThatUserOrderedByDate()
|
public function testMessagesOfConversationWithMessagesMessagesOnlyWithThatUserOrderedByDate()
|
||||||
{
|
{
|
||||||
$this->request->attributes->set('user_id', $this->user_b->id);
|
$this->request->attributes->set('user_id', $this->userB->id);
|
||||||
|
|
||||||
$user_c = User::factory(['name' => 'c'])->create();
|
$userC = User::factory(['name' => 'c'])->create();
|
||||||
|
|
||||||
// to be listed
|
// to be listed
|
||||||
$this->createMessage($this->user_a, $this->user_b, 'a>b', $this->now);
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a', $this->two_minutes_ago);
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->twoMinutesAgo);
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a2', $this->one_minute_ago);
|
$this->createMessage($this->userB, $this->userA, 'b>a2', $this->oneMinuteAgo);
|
||||||
|
|
||||||
// not to be listed
|
// not to be listed
|
||||||
$this->createMessage($this->user_a, $user_c, 'a>c', $this->now);
|
$this->createMessage($this->userA, $userC, 'a>c', $this->now);
|
||||||
$this->createMessage($user_c, $this->user_b, 'b>c', $this->now);
|
$this->createMessage($userC, $this->userB, 'b>c', $this->now);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -329,8 +344,8 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testMessagesOfConversationWithUnreadMessagesMessagesToMeWillStillBeReturnedAsUnread()
|
public function testMessagesOfConversationWithUnreadMessagesMessagesToMeWillStillBeReturnedAsUnread()
|
||||||
{
|
{
|
||||||
$this->request->attributes->set('user_id', $this->user_b->id);
|
$this->request->attributes->set('user_id', $this->userB->id);
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a', $this->now);
|
$this->createMessage($this->userB, $this->userA, 'b>a', $this->now);
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
|
@ -347,18 +362,43 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testMessagesOfConversationWithUnreadMessagesMessagesToMeWillBeMarkedAsRead()
|
public function testMessagesOfConversationWithUnreadMessagesMessagesToMeWillBeMarkedAsRead()
|
||||||
{
|
{
|
||||||
$this->request->attributes->set('user_id', $this->user_b->id);
|
$this->request->attributes->set('user_id', $this->userB->id);
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('withView')
|
->method('withView')
|
||||||
->willReturnCallback(function (string $view, array $data) {
|
->willReturnCallback(function (string $view, array $data) {
|
||||||
return $this->response;
|
return $this->response;
|
||||||
});
|
});
|
||||||
|
|
||||||
$msg = $this->createMessage($this->user_b, $this->user_a, 'b>a', $this->now);
|
$msg = $this->createMessage($this->userB, $this->userA, 'b>a', $this->now);
|
||||||
$this->controller->messagesOfConversation($this->request);
|
$this->controller->messagesOfConversation($this->request);
|
||||||
$this->assertTrue(Message::whereId($msg->id)->first()->read);
|
$this->assertTrue(Message::whereId($msg->id)->first()->read);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @testdox send: withNoTextGiven -> throwsException
|
* @testdox send: withNoTextGiven -> throwsException
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::send
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
||||||
|
@ -375,37 +415,18 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testSendWithNoUserIdGivenThrowsException()
|
public function testSendWithNoUserIdGivenThrowsException()
|
||||||
{
|
{
|
||||||
$this->request = $this->request->withParsedBody([
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
||||||
'text' => 'a',
|
|
||||||
]);
|
|
||||||
$this->expectException(ModelNotFoundException::class);
|
$this->expectException(ModelNotFoundException::class);
|
||||||
$this->controller->send($this->request);
|
$this->controller->send($this->request);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @testdox send: withMyUserIdGiven -> throwsException
|
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::send
|
|
||||||
*/
|
|
||||||
public function testSendWithMyUserIdGivenThrowsException()
|
|
||||||
{
|
|
||||||
$this->request = $this->request->withParsedBody([
|
|
||||||
'text' => 'a',
|
|
||||||
]);
|
|
||||||
$this->request->attributes->set('user_id', $this->user_a->id);
|
|
||||||
$this->expectException(HttpForbidden::class);
|
|
||||||
|
|
||||||
$this->controller->send($this->request);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @testdox send: withUnknownUserIdGiven -> throwsException
|
* @testdox send: withUnknownUserIdGiven -> throwsException
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::send
|
* @covers \Engelsystem\Controllers\MessagesController::send
|
||||||
*/
|
*/
|
||||||
public function testSendWithUnknownUserIdGivenThrowsException()
|
public function testSendWithUnknownUserIdGivenThrowsException()
|
||||||
{
|
{
|
||||||
$this->request = $this->request->withParsedBody([
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
||||||
'text' => 'a',
|
|
||||||
]);
|
|
||||||
$this->request->attributes->set('user_id', '1234');
|
$this->request->attributes->set('user_id', '1234');
|
||||||
$this->expectException(ModelNotFoundException::class);
|
$this->expectException(ModelNotFoundException::class);
|
||||||
$this->controller->send($this->request);
|
$this->controller->send($this->request);
|
||||||
|
@ -417,24 +438,44 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testSendWithUserAndTextGivenSavesMessage()
|
public function testSendWithUserAndTextGivenSavesMessage()
|
||||||
{
|
{
|
||||||
$this->request = $this->request->withParsedBody([
|
$this->request = $this->request->withParsedBody(['text' => 'a']);
|
||||||
'text' => 'a',
|
$this->request->attributes->set('user_id', $this->userB->id);
|
||||||
]);
|
|
||||||
$this->request->attributes->set('user_id', $this->user_b->id);
|
|
||||||
|
|
||||||
$this->response->expects($this->once())
|
$this->response->expects($this->once())
|
||||||
->method('redirectTo')
|
->method('redirectTo')
|
||||||
->with('http://localhost/messages/' . $this->user_b->id)
|
->with('http://localhost/messages/' . $this->userB->id)
|
||||||
->willReturn($this->response);
|
->willReturn($this->response);
|
||||||
|
|
||||||
$this->controller->send($this->request);
|
$this->controller->send($this->request);
|
||||||
|
|
||||||
$msg = Message::whereText('a')->first();
|
$msg = Message::whereText('a')->first();
|
||||||
$this->assertEquals($this->user_a->id, $msg->user_id);
|
$this->assertEquals($this->userA->id, $msg->user_id);
|
||||||
$this->assertEquals($this->user_b->id, $msg->receiver_id);
|
$this->assertEquals($this->userB->id, $msg->receiver_id);
|
||||||
$this->assertFalse($msg->read);
|
$this->assertFalse($msg->read);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @testdox delete: withNoMsgIdGiven -> throwsException
|
* @testdox delete: withNoMsgIdGiven -> throwsException
|
||||||
* @covers \Engelsystem\Controllers\MessagesController::delete
|
* @covers \Engelsystem\Controllers\MessagesController::delete
|
||||||
|
@ -451,7 +492,7 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testDeleteTryingToDeleteSomeonesMessageThrowsException()
|
public function testDeleteTryingToDeleteSomeonesMessageThrowsException()
|
||||||
{
|
{
|
||||||
$msg = $this->createMessage($this->user_b, $this->user_a, 'a>b', $this->now);
|
$msg = $this->createMessage($this->userB, $this->userA, 'a>b', $this->now);
|
||||||
$this->request->attributes->set('msg_id', $msg->id);
|
$this->request->attributes->set('msg_id', $msg->id);
|
||||||
$this->expectException(HttpForbidden::class);
|
$this->expectException(HttpForbidden::class);
|
||||||
|
|
||||||
|
@ -464,7 +505,7 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testDeleteTryingToDeleteMyMessageDeletesItAndRedirect()
|
public function testDeleteTryingToDeleteMyMessageDeletesItAndRedirect()
|
||||||
{
|
{
|
||||||
$msg = $this->createMessage($this->user_a, $this->user_b, 'a>b', $this->now);
|
$msg = $this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
||||||
$this->request->attributes->set('msg_id', $msg->id);
|
$this->request->attributes->set('msg_id', $msg->id);
|
||||||
$this->request->attributes->set('user_id', '1');
|
$this->request->attributes->set('user_id', '1');
|
||||||
|
|
||||||
|
@ -493,10 +534,10 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testNumberOfUnreadMessagesWithMessagesNotToMeMessagesNotToMeAreIgnored()
|
public function testNumberOfUnreadMessagesWithMessagesNotToMeMessagesNotToMeAreIgnored()
|
||||||
{
|
{
|
||||||
$user_c = User::factory(['name' => 'c'])->create();
|
$userC = User::factory(['name' => 'c'])->create();
|
||||||
|
|
||||||
$this->createMessage($this->user_a, $this->user_b, 'a>b', $this->now);
|
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
|
||||||
$this->createMessage($this->user_b, $user_c, 'b>c', $this->now);
|
$this->createMessage($this->userB, $userC, 'b>c', $this->now);
|
||||||
$this->assertEquals(0, $this->controller->numberOfUnreadMessages());
|
$this->assertEquals(0, $this->controller->numberOfUnreadMessages());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -506,11 +547,11 @@ class MessagesControllerTest extends ControllerTest
|
||||||
*/
|
*/
|
||||||
public function testNumberOfUnreadMessagesWithMessagesReturnsSumOfUnreadMessagesSentToMe()
|
public function testNumberOfUnreadMessagesWithMessagesReturnsSumOfUnreadMessagesSentToMe()
|
||||||
{
|
{
|
||||||
$user_c = User::factory(['name' => 'c'])->create();
|
$userC = User::factory(['name' => 'c'])->create();
|
||||||
|
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a1', $this->now);
|
$this->createMessage($this->userB, $this->userA, 'b>a1', $this->now);
|
||||||
$this->createMessage($this->user_b, $this->user_a, 'b>a2', $this->now);
|
$this->createMessage($this->userB, $this->userA, 'b>a2', $this->now);
|
||||||
$this->createMessage($user_c, $this->user_a, 'c>a', $this->now);
|
$this->createMessage($userC, $this->userA, 'c>a', $this->now);
|
||||||
|
|
||||||
$this->assertEquals(3, $this->controller->numberOfUnreadMessages());
|
$this->assertEquals(3, $this->controller->numberOfUnreadMessages());
|
||||||
}
|
}
|
||||||
|
@ -527,13 +568,13 @@ class MessagesControllerTest extends ControllerTest
|
||||||
|
|
||||||
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
|
$this->app->bind(UrlGeneratorInterface::class, UrlGenerator::class);
|
||||||
|
|
||||||
$this->user_a = User::factory(['name' => 'a'])->create();
|
$this->userA = User::factory(['name' => 'a'])->create();
|
||||||
$this->user_b = User::factory(['name' => 'b'])->create();
|
$this->userB = User::factory(['name' => 'b'])->create();
|
||||||
$this->setExpects($this->auth, 'user', null, $this->user_a, $this->any());
|
$this->setExpects($this->auth, 'user', null, $this->userA, $this->any());
|
||||||
|
|
||||||
$this->now = Carbon::now();
|
$this->now = Carbon::now();
|
||||||
$this->one_minute_ago = Carbon::now()->subMinute();
|
$this->oneMinuteAgo = Carbon::now()->subMinute();
|
||||||
$this->two_minutes_ago = Carbon::now()->subMinutes(2);
|
$this->twoMinutesAgo = Carbon::now()->subMinutes(2);
|
||||||
|
|
||||||
$this->controller = $this->app->get(MessagesController::class);
|
$this->controller = $this->app->get(MessagesController::class);
|
||||||
$this->controller->setValidator(new Validator());
|
$this->controller->setValidator(new Validator());
|
||||||
|
|
|
@ -83,6 +83,7 @@ class MessageTest extends ModelTest
|
||||||
* Tests that the Messages have the correct senders.
|
* Tests that the Messages have the correct senders.
|
||||||
*
|
*
|
||||||
* @covers \Engelsystem\Models\Message::user
|
* @covers \Engelsystem\Models\Message::user
|
||||||
|
* @covers \Engelsystem\Models\Message::sender
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -91,6 +92,10 @@ class MessageTest extends ModelTest
|
||||||
$this->assertSame($this->user1->id, $this->message1->user->id);
|
$this->assertSame($this->user1->id, $this->message1->user->id);
|
||||||
$this->assertSame($this->user1->id, $this->message2->user->id);
|
$this->assertSame($this->user1->id, $this->message2->user->id);
|
||||||
$this->assertSame($this->user2->id, $this->message3->user->id);
|
$this->assertSame($this->user2->id, $this->message3->user->id);
|
||||||
|
|
||||||
|
$this->assertSame($this->user1->id, $this->message1->sender->id);
|
||||||
|
$this->assertSame($this->user1->id, $this->message2->sender->id);
|
||||||
|
$this->assertSame($this->user2->id, $this->message3->sender->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue