-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathWatchTest.php
More file actions
483 lines (392 loc) · 21.1 KB
/
WatchTest.php
File metadata and controls
483 lines (392 loc) · 21.1 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
<?php
namespace Tests\Activity;
use BookStack\Activity\ActivityType;
use BookStack\Activity\Models\Comment;
use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
use BookStack\Activity\Notifications\Messages\CommentCreationNotification;
use BookStack\Activity\Notifications\Messages\PageCreationNotification;
use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
use BookStack\Activity\Tools\ActivityLogger;
use BookStack\Activity\Tools\UserEntityWatchOptions;
use BookStack\Activity\WatchLevels;
use BookStack\Entities\Models\Entity;
use BookStack\Settings\UserNotificationPreferences;
use Illuminate\Contracts\Notifications\Dispatcher;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class WatchTest extends TestCase
{
public function test_watch_action_exists_on_entity_unless_active()
{
$editor = $this->users->editor();
$this->actingAs($editor);
$entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->page()];
/** @var Entity $entity */
foreach ($entities as $entity) {
$resp = $this->get($entity->getUrl());
$this->withHtml($resp)->assertElementContains('form[action$="/watching/update"] button.icon-list-item', 'Watch');
$watchOptions = new UserEntityWatchOptions($editor, $entity);
$watchOptions->updateLevelByValue(WatchLevels::COMMENTS);
$resp = $this->get($entity->getUrl());
$this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
}
}
public function test_watch_action_only_shows_with_permission()
{
$viewer = $this->users->viewer();
$this->actingAs($viewer);
$entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->page()];
/** @var Entity $entity */
foreach ($entities as $entity) {
$resp = $this->get($entity->getUrl());
$this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
}
$this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
/** @var Entity $entity */
foreach ($entities as $entity) {
$resp = $this->get($entity->getUrl());
$this->withHtml($resp)->assertElementExists('form[action$="/watching/update"] button.icon-list-item');
}
}
public function test_watch_update()
{
$editor = $this->users->editor();
$book = $this->entities->book();
$resp = $this->actingAs($editor)->put('/watching/update', [
'type' => $book->getMorphClass(),
'id' => $book->id,
'level' => 'comments'
]);
$resp->assertRedirect($book->getUrl());
$this->assertSessionHas('success');
$this->assertDatabaseHas('watches', [
'watchable_id' => $book->id,
'watchable_type' => $book->getMorphClass(),
'user_id' => $editor->id,
'level' => WatchLevels::COMMENTS,
]);
$resp = $this->put('/watching/update', [
'type' => $book->getMorphClass(),
'id' => $book->id,
'level' => 'default'
]);
$resp->assertRedirect($book->getUrl());
$this->assertDatabaseMissing('watches', [
'watchable_id' => $book->id,
'watchable_type' => $book->getMorphClass(),
'user_id' => $editor->id,
]);
}
public function test_watch_update_fails_for_guest()
{
$this->setSettings(['app-public' => 'true']);
$guest = $this->users->guest();
$this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
$book = $this->entities->book();
$resp = $this->put('/watching/update', [
'type' => $book->getMorphClass(),
'id' => $book->id,
'level' => 'comments'
]);
$this->assertPermissionError($resp);
$guest->unsetRelations();
}
public function test_watch_detail_display_reflects_state()
{
$editor = $this->users->editor();
$book = $this->entities->bookHasChaptersAndPages();
$chapter = $book->chapters()->first();
$page = $chapter->pages()->first();
(new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::UPDATES);
$this->actingAs($editor)->get($book->getUrl())->assertSee('Watching new pages and updates');
$this->get($chapter->getUrl())->assertSee('Watching via parent book');
$this->get($page->getUrl())->assertSee('Watching via parent book');
(new UserEntityWatchOptions($editor, $chapter))->updateLevelByValue(WatchLevels::COMMENTS);
$this->get($chapter->getUrl())->assertSee('Watching new pages, updates & comments');
$this->get($page->getUrl())->assertSee('Watching via parent chapter');
(new UserEntityWatchOptions($editor, $page))->updateLevelByValue(WatchLevels::UPDATES);
$this->get($page->getUrl())->assertSee('Watching new pages and updates');
}
public function test_watch_detail_ignore_indicator_cascades()
{
$editor = $this->users->editor();
$book = $this->entities->bookHasChaptersAndPages();
(new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::IGNORE);
$this->actingAs($editor)->get($book->getUrl())->assertSee('Ignoring notifications');
$this->get($book->chapters()->first()->getUrl())->assertSee('Ignoring via parent book');
$this->get($book->pages()->first()->getUrl())->assertSee('Ignoring via parent book');
}
public function test_watch_option_menu_shows_current_active_state()
{
$editor = $this->users->editor();
$book = $this->entities->book();
$options = new UserEntityWatchOptions($editor, $book);
$respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
$respHtml->assertElementNotExists('form[action$="/watching/update"] svg[data-icon="check-circle"]');
$options->updateLevelByValue(WatchLevels::COMMENTS);
$respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
$respHtml->assertElementExists('form[action$="/watching/update"] button[value="comments"] svg[data-icon="check-circle"]');
$options->updateLevelByValue(WatchLevels::IGNORE);
$respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
$respHtml->assertElementExists('form[action$="/watching/update"] button[value="ignore"] svg[data-icon="check-circle"]');
}
public function test_watch_option_menu_limits_options_for_pages()
{
$editor = $this->users->editor();
$book = $this->entities->bookHasChaptersAndPages();
(new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::IGNORE);
$respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
$respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="new"]');
$respHtml = $this->withHtml($this->get($book->pages()->first()->getUrl()));
$respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="updates"]');
$respHtml->assertElementNotExists('form[action$="/watching/update"] button[name="level"][value="new"]');
}
public function test_notify_own_page_changes()
{
$editor = $this->users->editor();
$entities = $this->entities->createChainBelongingToUser($editor);
$prefs = new UserNotificationPreferences($editor);
$prefs->updateFromSettingsArray(['own-page-changes' => 'true']);
$notifications = Notification::fake();
$this->asAdmin();
$this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
$notifications->assertSentTo($editor, PageUpdateNotification::class);
}
public function test_notify_own_page_comments()
{
$editor = $this->users->editor();
$entities = $this->entities->createChainBelongingToUser($editor);
$prefs = new UserNotificationPreferences($editor);
$prefs->updateFromSettingsArray(['own-page-comments' => 'true']);
$notifications = Notification::fake();
$this->asAdmin()->post("/comment/{$entities['page']->id}", [
'html' => '<p>My new comment</p>'
]);
$notifications->assertSentTo($editor, CommentCreationNotification::class);
}
public function test_notify_comment_replies()
{
$editor = $this->users->editor();
$entities = $this->entities->createChainBelongingToUser($editor);
$prefs = new UserNotificationPreferences($editor);
$prefs->updateFromSettingsArray(['comment-replies' => 'true']);
// Create some existing comments to pad IDs to help potentially error
// on mis-identification of parent via ids used.
Comment::factory()->count(5)
->for($entities['page'], 'entity')
->create(['created_by' => $this->users->admin()->id]);
$notifications = Notification::fake();
$this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
'html' => '<p>My new comment</p>'
]);
$comment = $entities['page']->comments()->orderBy('id', 'desc')->first();
$this->asAdmin()->post("/comment/{$entities['page']->id}", [
'html' => '<p>My new comment response</p>',
'parent_id' => $comment->local_id,
]);
$notifications->assertSentTo($editor, CommentCreationNotification::class);
}
public function test_notify_watch_parent_book_ignore()
{
$editor = $this->users->editor();
$entities = $this->entities->createChainBelongingToUser($editor);
$watches = new UserEntityWatchOptions($editor, $entities['book']);
$prefs = new UserNotificationPreferences($editor);
$watches->updateLevelByValue(WatchLevels::IGNORE);
$prefs->updateFromSettingsArray(['own-page-changes' => 'true', 'own-page-comments' => true]);
$notifications = Notification::fake();
$this->asAdmin()->post("/comment/{$entities['page']->id}", [
'text' => 'My new comment response',
]);
$this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
$notifications->assertNothingSent();
}
public function test_notify_watch_parent_book_comments()
{
$notifications = Notification::fake();
$editor = $this->users->editor();
$admin = $this->users->admin();
$entities = $this->entities->createChainBelongingToUser($editor);
$watches = new UserEntityWatchOptions($editor, $entities['book']);
$watches->updateLevelByValue(WatchLevels::COMMENTS);
// Comment post
$this->actingAs($admin)->post("/comment/{$entities['page']->id}", [
'html' => '<p>My new comment response</p>',
]);
$notifications->assertSentTo($editor, function (CommentCreationNotification $notification) use ($editor, $admin, $entities) {
$mail = $notification->toMail($editor);
$mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
return $mail->subject === 'New comment on page: ' . $entities['page']->getShortName()
&& str_contains($mailContent, 'View Comment')
&& str_contains($mailContent, 'Page Name: ' . $entities['page']->name)
&& str_contains($mailContent, 'Page Path: ' . $entities['book']->getShortName(24) . ' > ' . $entities['chapter']->getShortName(24))
&& str_contains($mailContent, 'Commenter: ' . $admin->name)
&& str_contains($mailContent, 'Comment: My new comment response');
});
}
public function test_notify_watch_parent_book_updates()
{
$notifications = Notification::fake();
$editor = $this->users->editor();
$admin = $this->users->admin();
$entities = $this->entities->createChainBelongingToUser($editor);
$watches = new UserEntityWatchOptions($editor, $entities['book']);
$watches->updateLevelByValue(WatchLevels::UPDATES);
$this->actingAs($admin);
$this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
$notifications->assertSentTo($editor, function (PageUpdateNotification $notification) use ($editor, $admin, $entities) {
$mail = $notification->toMail($editor);
$mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
return $mail->subject === 'Updated page: Updated page'
&& str_contains($mailContent, 'View Page')
&& str_contains($mailContent, 'Page Name: Updated page')
&& str_contains($mailContent, 'Page Path: ' . $entities['book']->getShortName(24) . ' > ' . $entities['chapter']->getShortName(24))
&& str_contains($mailContent, 'Updated By: ' . $admin->name)
&& str_contains($mailContent, 'you won\'t be sent notifications for further edits to this page by the same editor');
});
// Test debounce
$notifications = Notification::fake();
$this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
$notifications->assertNothingSentTo($editor);
}
public function test_notify_watch_parent_book_new()
{
$notifications = Notification::fake();
$editor = $this->users->editor();
$admin = $this->users->admin();
$entities = $this->entities->createChainBelongingToUser($editor);
$watches = new UserEntityWatchOptions($editor, $entities['book']);
$watches->updateLevelByValue(WatchLevels::NEW);
$this->actingAs($admin)->get($entities['chapter']->getUrl('/create-page'));
$page = $entities['chapter']->pages()->where('draft', '=', true)->first();
$this->post($page->getUrl(), ['name' => 'My new page', 'html' => 'My new page content']);
$notifications->assertSentTo($editor, function (PageCreationNotification $notification) use ($editor, $admin, $entities) {
$mail = $notification->toMail($editor);
$mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
return $mail->subject === 'New page: My new page'
&& str_contains($mailContent, 'View Page')
&& str_contains($mailContent, 'Page Name: My new page')
&& str_contains($mailContent, 'Page Path: ' . $entities['book']->getShortName(24) . ' > ' . $entities['chapter']->getShortName(24))
&& str_contains($mailContent, 'Created By: ' . $admin->name);
});
}
public function test_notify_watch_page_ignore_when_no_page_owner()
{
$editor = $this->users->editor();
$entities = $this->entities->createChainBelongingToUser($editor);
$entities['page']->owned_by = null;
$entities['page']->save();
$watches = new UserEntityWatchOptions($editor, $entities['page']);
$watches->updateLevelByValue(WatchLevels::IGNORE);
$notifications = Notification::fake();
$this->asAdmin();
$this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
$notifications->assertNothingSent();
}
public function test_notifications_sent_in_right_language()
{
$editor = $this->users->editor();
$admin = $this->users->admin();
setting()->putUser($editor, 'language', 'de');
$entities = $this->entities->createChainBelongingToUser($editor);
$watches = new UserEntityWatchOptions($editor, $entities['book']);
$watches->updateLevelByValue(WatchLevels::COMMENTS);
$activities = [
ActivityType::PAGE_CREATE => $entities['page'],
ActivityType::PAGE_UPDATE => $entities['page'],
ActivityType::COMMENT_CREATE => Comment::factory()->make([
'commentable_id' => $entities['page']->id,
'commentable_type' => $entities['page']->getMorphClass(),
]),
];
$notifications = Notification::fake();
$logger = app()->make(ActivityLogger::class);
$this->actingAs($admin);
foreach ($activities as $activityType => $detail) {
$logger->add($activityType, $detail);
}
$sent = $notifications->sentNotifications()[get_class($editor)][$editor->id];
$this->assertCount(3, $sent);
foreach ($sent as $notificationInfo) {
$notification = $notificationInfo[0]['notification'];
$this->assertInstanceOf(BaseActivityNotification::class, $notification);
$mail = $notification->toMail($editor);
$mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
$this->assertStringContainsString('Name der Seite:', $mailContent);
$this->assertStringContainsString('Diese Benachrichtigung wurde', $mailContent);
$this->assertStringContainsString('Sollte es beim Anklicken der Schaltfläche', $mailContent);
}
}
public function test_failed_notifications_dont_block_and_log_errors()
{
$logger = $this->withTestLogger();
$editor = $this->users->editor();
$admin = $this->users->admin();
$page = $this->entities->page();
$book = $page->book;
$activityLogger = app()->make(ActivityLogger::class);
$watches = new UserEntityWatchOptions($editor, $book);
$watches->updateLevelByValue(WatchLevels::UPDATES);
$mockDispatcher = $this->mock(Dispatcher::class);
$mockDispatcher->shouldReceive('send')->once()
->andThrow(\Exception::class, 'Failed to connect to mail server');
$this->actingAs($admin);
$activityLogger->add(ActivityType::PAGE_UPDATE, $page);
$this->assertTrue($logger->hasErrorThatContains("Failed to send email notification to user [id:{$editor->id}] with error: Failed to connect to mail server"));
}
public function test_notifications_not_sent_if_lacking_view_permission_for_related_item()
{
$notifications = Notification::fake();
$editor = $this->users->editor();
$page = $this->entities->page();
$watches = new UserEntityWatchOptions($editor, $page);
$watches->updateLevelByValue(WatchLevels::COMMENTS);
$this->permissions->disableEntityInheritedPermissions($page);
$this->asAdmin()->post("/comment/{$page->id}", [
'html' => '<p>My new comment response</p>',
])->assertOk();
$notifications->assertNothingSentTo($editor);
}
public function test_watches_deleted_on_user_delete()
{
$editor = $this->users->editor();
$page = $this->entities->page();
$watches = new UserEntityWatchOptions($editor, $page);
$watches->updateLevelByValue(WatchLevels::COMMENTS);
$this->assertDatabaseHas('watches', ['user_id' => $editor->id]);
$this->asAdmin()->delete($editor->getEditUrl());
$this->assertDatabaseMissing('watches', ['user_id' => $editor->id]);
}
public function test_watches_deleted_on_item_delete()
{
$editor = $this->users->editor();
$page = $this->entities->page();
$watches = new UserEntityWatchOptions($editor, $page);
$watches->updateLevelByValue(WatchLevels::COMMENTS);
$this->assertDatabaseHas('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
$this->entities->destroy($page);
$this->assertDatabaseMissing('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
}
public function test_page_path_in_notifications_limited_by_permissions()
{
$chapter = $this->entities->chapterHasPages();
$page = $chapter->pages()->first();
$book = $chapter->book;
$notification = new PageCreationNotification($page, $this->users->editor());
$viewer = $this->users->viewer();
$viewerRole = $viewer->roles()->first();
$content = html_entity_decode(strip_tags($notification->toMail($viewer)->render()), ENT_QUOTES);
$this->assertStringContainsString('Page Path: ' . $book->getShortName(24) . ' > ' . $chapter->getShortName(24), $content);
$this->permissions->setEntityPermissions($page, ['view'], [$viewerRole]);
$this->permissions->setEntityPermissions($chapter, [], [$viewerRole]);
$content = html_entity_decode(strip_tags($notification->toMail($viewer)->render()), ENT_QUOTES);
$this->assertStringContainsString('Page Path: ' . $book->getShortName(24), $content);
$this->assertStringNotContainsString(' > ' . $chapter->getShortName(24), $content);
$this->permissions->setEntityPermissions($book, [], [$viewerRole]);
$content = html_entity_decode(strip_tags($notification->toMail($viewer)->render()), ENT_QUOTES);
$this->assertStringNotContainsString('Page Path:', $content);
$this->assertStringNotContainsString($book->getShortName(24), $content);
$this->assertStringNotContainsString($chapter->getShortName(24), $content);
}
}