forked from musonza/chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationTest.php
More file actions
358 lines (268 loc) · 13.5 KB
/
ConversationTest.php
File metadata and controls
358 lines (268 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<?php
namespace Musonza\Chat\Tests;
use Chat;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Collection;
use Musonza\Chat\Exceptions\DirectMessagingExistsException;
use Musonza\Chat\Exceptions\InvalidDirectMessageNumberOfParticipants;
use Musonza\Chat\Models\Conversation;
use Musonza\Chat\Models\Participation;
use Musonza\Chat\Tests\Helpers\Models\Client;
class ConversationTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function it_creates_a_conversation()
{
Chat::createConversation([$this->alpha, $this->bravo]);
$this->assertDatabaseHas($this->prefix.'conversations', ['id' => 1]);
}
/** @test */
public function it_returns_a_conversation_given_the_id()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$c = Chat::conversations()->getById($conversation->id);
$this->assertEquals($conversation->id, $c->id);
}
/** @test */
public function it_returns_participant_conversations()
{
Chat::createConversation([$this->alpha, $this->bravo]);
Chat::createConversation([$this->alpha, $this->charlie]);
$this->assertEquals(2, $this->alpha->conversations()->count());
}
/** @test */
public function it_can_mark_a_conversation_as_read()
{
/** @var Conversation $conversation */
$conversation = Chat::createConversation([
$this->alpha,
$this->bravo,
])->makeDirect();
Chat::message('Hello there 0')->from($this->bravo)->to($conversation)->send();
Chat::message('Hello there 0')->from($this->bravo)->to($conversation)->send();
Chat::message('Hello there 0')->from($this->bravo)->to($conversation)->send();
Chat::message('Hello there 1')->from($this->alpha)->to($conversation)->send();
Chat::conversation($conversation)->setParticipant($this->alpha)->readAll();
$this->assertEquals(0, $conversation->unReadNotifications($this->alpha)->count());
$this->assertEquals(1, $conversation->unReadNotifications($this->bravo)->count());
}
/** @test */
public function it_can_update_conversation_details()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$data = ['title' => 'PHP Channel', 'description' => 'PHP Channel Description'];
$conversation->update(['data' => $data]);
$this->assertEquals('PHP Channel', $conversation->data['title']);
$this->assertEquals('PHP Channel Description', $conversation->data['description']);
}
/** @test */
public function it_can_clear_a_conversation()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
Chat::message('Hello there 0')->from($this->alpha)->to($conversation)->send();
Chat::message('Hello there 1')->from($this->alpha)->to($conversation)->send();
Chat::message('Hello there 2')->from($this->alpha)->to($conversation)->send();
Chat::conversation($conversation)->setParticipant($this->alpha)->clear();
$messages = Chat::conversation($conversation)->setParticipant($this->alpha)->getMessages();
$this->assertEquals($messages->count(), 0);
}
/** @test */
public function it_can_create_a_conversation_between_two_users()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$this->assertCount(2, $conversation->participants);
}
/** @test */
public function it_can_remove_a_single_participant_from_conversation()
{
$clientModel = factory(Client::class)->create();
$conversation = Chat::createConversation([$this->alpha, $this->bravo, $clientModel]);
$conversation = Chat::conversation($conversation)->removeParticipants($this->alpha);
$this->assertEquals(2, $conversation->fresh()->participants()->count());
$conversation = Chat::conversation($conversation)->removeParticipants($clientModel);
$this->assertEquals(1, $conversation->fresh()->participants()->count());
}
/** @test */
public function it_can_remove_multiple_users_from_conversation()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$conversation = Chat::conversation($conversation)->removeParticipants([$this->alpha, $this->bravo]);
$this->assertEquals(0, $conversation->fresh()->participants->count());
}
/** @test */
public function it_can_add_a_single_user_to_conversation()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$this->assertEquals($conversation->participants->count(), 2);
$userThree = $this->createUsers(1);
Chat::conversation($conversation)->addParticipants([$userThree[0]]);
$this->assertEquals($conversation->fresh()->participants->count(), 3);
}
/** @test */
public function it_can_add_multiple_users_to_conversation()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$this->assertEquals($conversation->participants->count(), 2);
$otherUsers = $this->createUsers(5);
Chat::conversation($conversation)->addParticipants($otherUsers->all());
$this->assertEquals($conversation->fresh()->participants->count(), 7);
}
/** @test */
public function it_can_return_conversation_recent_messsage()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
Chat::message('Hello 1')->from($this->bravo)->to($conversation)->send();
Chat::message('Hello 2')->from($this->alpha)->to($conversation)->send();
$conversation2 = Chat::createConversation([$this->alpha, $this->charlie]);
Chat::message('Hello Man 4')->from($this->alpha)->to($conversation2)->send();
$conversation3 = Chat::createConversation([$this->alpha, $this->delta]);
Chat::message('Hello Man 5')->from($this->delta)->to($conversation3)->send();
Chat::message('Hello Man 6')->from($this->alpha)->to($conversation3)->send();
Chat::message('Hello Man 3')->from($this->charlie)->to($conversation2)->send();
$message7 = Chat::message('Hello Man 10')->from($this->alpha)->to($conversation2)->send();
$this->assertEquals($message7->id, $conversation2->last_message->id);
}
/** @test */
public function it_returns_last_message_as_null_when_the_very_last_message_was_deleted()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$message = Chat::message('Hello & Bye')->from($this->alpha)->to($conversation)->send();
Chat::message($message)->setParticipant($this->alpha)->delete();
$conversations = Chat::conversations()->setParticipant($this->alpha)->get();
$this->assertNull($conversations->first()->last_message);
}
/** @test */
public function it_returns_correct_attributes_in_last_message()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
Chat::message('Hello')->from($this->alpha)->to($conversation)->send();
/** @var Collection $conversations */
$conversations = Chat::conversations()->setParticipant($this->alpha)->get();
$this->assertTrue((bool) $conversations->first()->conversation->last_message->is_seen);
$conversations = Chat::conversations()->setParticipant($this->bravo)->get();
$this->assertFalse((bool) $conversations->first()->conversation->last_message->is_seen);
}
/** @test */
public function it_returns_the_correct_order_of_conversations_when_updated_at_is_duplicated()
{
$auth = $this->alpha;
$conversation = Chat::createConversation([$auth, $this->bravo]);
Chat::message('Hello-'.$conversation->id)->from($auth)->to($conversation)->send();
$conversation = Chat::createConversation([$auth, $this->charlie]);
Chat::message('Hello-'.$conversation->id)->from($auth)->to($conversation)->send();
$conversation = Chat::createConversation([$auth, $this->delta]);
Chat::message('Hello-'.$conversation->id)->from($auth)->to($conversation)->send();
/** @var Collection $conversations */
$conversations = Chat::conversations()->setPaginationParams(['sorting' => 'desc'])->setParticipant($auth)->limit(1)->page(1)->get();
$this->assertEquals('Hello-3', $conversations->first()->conversation->last_message->body);
$conversations = Chat::conversations()->setPaginationParams(['sorting' => 'desc'])->setParticipant($auth)->limit(1)->page(2)->get();
$this->assertEquals('Hello-2', $conversations->first()->conversation->last_message->body);
$conversations = Chat::conversations()->setPaginationParams(['sorting' => 'desc'])->setParticipant($auth)->limit(1)->page(3)->get();
$this->assertEquals('Hello-1', $conversations->first()->conversation->last_message->body);
}
/** @test */
public function it_allows_setting_private_or_public_conversation()
{
/** @var Conversation $conversation */
$conversation = Chat::createConversation([
$this->alpha,
$this->bravo,
])->makePrivate();
$this->assertTrue($conversation->private);
$conversation->makePrivate(false);
$this->assertFalse($conversation->private);
}
/**
* DIRECT MESSAGING.
*
* @test
*/
public function it_creates_direct_messaging()
{
$conversation = Chat::createConversation([$this->alpha, $this->bravo])
->makeDirect();
$this->assertTrue($conversation->direct_message);
}
/** @test */
public function it_does_not_duplicate_direct_messaging()
{
Chat::createConversation([$this->alpha, $this->bravo])
->makeDirect();
$this->expectException(DirectMessagingExistsException::class);
Chat::createConversation([$this->alpha, $this->bravo])
->makeDirect();
}
/** @test */
public function it_prevents_additional_participants_to_direct_conversation()
{
/** @var Conversation $conversation */
$conversation = Chat::createConversation([$this->alpha, $this->bravo])
->makeDirect();
$this->expectException(InvalidDirectMessageNumberOfParticipants::class);
$conversation->addParticipants([$this->charlie]);
}
/** @test */
public function it_can_return_a_conversation_between_users()
{
/** @var Conversation $conversation */
// $conversation = Chat::makeDirect()->createConversation([$this->alpha, $this->bravo]);
$conversation = Chat::createConversation([$this->alpha, $this->bravo])->makeDirect();
Chat::createConversation([$this->alpha, $this->charlie]);
$conversation3 = Chat::createConversation([$this->alpha, $this->delta])->makeDirect();
$c1 = Chat::conversations()->between($this->alpha, $this->bravo);
$this->assertEquals($conversation->id, $c1->id);
$c3 = Chat::conversations()->between($this->alpha, $this->delta);
$this->assertEquals($conversation3->id, $c3->id);
}
/** @test */
public function it_filters_conversations_by_type()
{
Chat::createConversation([$this->alpha, $this->bravo])->makePrivate();
Chat::createConversation([$this->alpha, $this->bravo])->makePrivate(false);
Chat::createConversation([$this->alpha, $this->bravo])->makePrivate();
Chat::createConversation([$this->alpha, $this->charlie])->makeDirect();
$allConversations = Chat::conversations()->setParticipant($this->alpha)->get();
$this->assertCount(4, $allConversations, 'All Conversations');
$privateConversations = Chat::conversations()->setParticipant($this->alpha)->isPrivate()->get();
$this->assertCount(3, $privateConversations, 'Private Conversations');
$publicConversations = Chat::conversations()->setParticipant($this->alpha)->isPrivate(false)->get();
$this->assertCount(1, $publicConversations, 'Public Conversations');
$directConversations = Chat::conversations()->setParticipant($this->alpha)->isDirect()->get();
$this->assertCount(1, $directConversations, 'Direct Conversations');
}
/**
* Conversation Settings.
*
* @test
*/
public function it_can_update_participant_conversation_settings()
{
/** @var Conversation $conversation */
$conversation = Chat::createConversation([$this->alpha]);
$settings = ['mute_mentions' => true];
Chat::conversation($conversation)
->getParticipation($this->alpha)
->update(['settings' => $settings]);
$this->assertEquals(
$settings,
$this->alpha->participation->where('conversation_id', $conversation->id)->first()->settings
);
}
/** @test */
public function it_can_get_participation_info_for_a_model()
{
/** @var Conversation $conversation */
$conversation = Chat::createConversation([$this->alpha]);
$participation = Chat::conversation($conversation)->setParticipant($this->alpha)->getParticipation();
$this->assertInstanceOf(Participation::class, $participation);
}
/** @test */
public function it_specifies_fields_to_return_for_sender()
{
$this->app['config']->set('musonza_chat.sender_fields_whitelist', ['uid', 'email']);
$conversation = Chat::createConversation([$this->alpha, $this->bravo]);
$message = Chat::message('Hello')->from($this->alpha)->to($conversation)->send();
$this->assertSame(['uid', 'email'], array_keys($message->sender));
}
}