]> BookStack Code Mirror - bookstack/blob - tests/Activity/WatchTest.php
Cleanup Command: Allowed running non-interactively
[bookstack] / tests / Activity / WatchTest.php
1 <?php
2
3 namespace Tests\Activity;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Comment;
7 use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
8 use BookStack\Activity\Notifications\Messages\CommentCreationNotification;
9 use BookStack\Activity\Notifications\Messages\PageCreationNotification;
10 use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
11 use BookStack\Activity\Tools\ActivityLogger;
12 use BookStack\Activity\Tools\UserEntityWatchOptions;
13 use BookStack\Activity\WatchLevels;
14 use BookStack\Entities\Models\Entity;
15 use BookStack\Entities\Tools\TrashCan;
16 use BookStack\Settings\UserNotificationPreferences;
17 use Illuminate\Support\Facades\Notification;
18 use Tests\TestCase;
19
20 class WatchTest extends TestCase
21 {
22     public function test_watch_action_exists_on_entity_unless_active()
23     {
24         $editor = $this->users->editor();
25         $this->actingAs($editor);
26
27         $entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->page()];
28         /** @var Entity $entity */
29         foreach ($entities as $entity) {
30             $resp = $this->get($entity->getUrl());
31             $this->withHtml($resp)->assertElementContains('form[action$="/watching/update"] button.icon-list-item', 'Watch');
32
33             $watchOptions = new UserEntityWatchOptions($editor, $entity);
34             $watchOptions->updateLevelByValue(WatchLevels::COMMENTS);
35
36             $resp = $this->get($entity->getUrl());
37             $this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
38         }
39     }
40
41     public function test_watch_action_only_shows_with_permission()
42     {
43         $viewer = $this->users->viewer();
44         $this->actingAs($viewer);
45
46         $entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->page()];
47         /** @var Entity $entity */
48         foreach ($entities as $entity) {
49             $resp = $this->get($entity->getUrl());
50             $this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
51         }
52
53         $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
54
55         /** @var Entity $entity */
56         foreach ($entities as $entity) {
57             $resp = $this->get($entity->getUrl());
58             $this->withHtml($resp)->assertElementExists('form[action$="/watching/update"] button.icon-list-item');
59         }
60     }
61
62     public function test_watch_update()
63     {
64         $editor = $this->users->editor();
65         $book = $this->entities->book();
66
67         $this->actingAs($editor)->get($book->getUrl());
68         $resp = $this->put('/watching/update', [
69             'type' => $book->getMorphClass(),
70             'id' => $book->id,
71             'level' => 'comments'
72         ]);
73
74         $resp->assertRedirect($book->getUrl());
75         $this->assertSessionHas('success');
76         $this->assertDatabaseHas('watches', [
77             'watchable_id' => $book->id,
78             'watchable_type' => $book->getMorphClass(),
79             'user_id' => $editor->id,
80             'level' => WatchLevels::COMMENTS,
81         ]);
82
83         $resp = $this->put('/watching/update', [
84             'type' => $book->getMorphClass(),
85             'id' => $book->id,
86             'level' => 'default'
87         ]);
88         $resp->assertRedirect($book->getUrl());
89         $this->assertDatabaseMissing('watches', [
90             'watchable_id' => $book->id,
91             'watchable_type' => $book->getMorphClass(),
92             'user_id' => $editor->id,
93         ]);
94     }
95
96     public function test_watch_update_fails_for_guest()
97     {
98         $this->setSettings(['app-public' => 'true']);
99         $guest = $this->users->guest();
100         $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
101         $book = $this->entities->book();
102
103         $resp = $this->put('/watching/update', [
104             'type' => $book->getMorphClass(),
105             'id' => $book->id,
106             'level' => 'comments'
107         ]);
108
109         $this->assertPermissionError($resp);
110         $guest->unsetRelations();
111     }
112
113     public function test_watch_detail_display_reflects_state()
114     {
115         $editor = $this->users->editor();
116         $book = $this->entities->bookHasChaptersAndPages();
117         $chapter = $book->chapters()->first();
118         $page = $chapter->pages()->first();
119
120         (new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::UPDATES);
121
122         $this->actingAs($editor)->get($book->getUrl())->assertSee('Watching new pages and updates');
123         $this->get($chapter->getUrl())->assertSee('Watching via parent book');
124         $this->get($page->getUrl())->assertSee('Watching via parent book');
125
126         (new UserEntityWatchOptions($editor, $chapter))->updateLevelByValue(WatchLevels::COMMENTS);
127         $this->get($chapter->getUrl())->assertSee('Watching new pages, updates & comments');
128         $this->get($page->getUrl())->assertSee('Watching via parent chapter');
129
130         (new UserEntityWatchOptions($editor, $page))->updateLevelByValue(WatchLevels::UPDATES);
131         $this->get($page->getUrl())->assertSee('Watching new pages and updates');
132     }
133
134     public function test_watch_detail_ignore_indicator_cascades()
135     {
136         $editor = $this->users->editor();
137         $book = $this->entities->bookHasChaptersAndPages();
138         (new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::IGNORE);
139
140         $this->actingAs($editor)->get($book->getUrl())->assertSee('Ignoring notifications');
141         $this->get($book->chapters()->first()->getUrl())->assertSee('Ignoring via parent book');
142         $this->get($book->pages()->first()->getUrl())->assertSee('Ignoring via parent book');
143     }
144
145     public function test_watch_option_menu_shows_current_active_state()
146     {
147         $editor = $this->users->editor();
148         $book = $this->entities->book();
149         $options = new UserEntityWatchOptions($editor, $book);
150
151         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
152         $respHtml->assertElementNotExists('form[action$="/watching/update"] svg[data-icon="check-circle"]');
153
154         $options->updateLevelByValue(WatchLevels::COMMENTS);
155         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
156         $respHtml->assertElementExists('form[action$="/watching/update"] button[value="comments"] svg[data-icon="check-circle"]');
157
158         $options->updateLevelByValue(WatchLevels::IGNORE);
159         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
160         $respHtml->assertElementExists('form[action$="/watching/update"] button[value="ignore"] svg[data-icon="check-circle"]');
161     }
162
163     public function test_watch_option_menu_limits_options_for_pages()
164     {
165         $editor = $this->users->editor();
166         $book = $this->entities->bookHasChaptersAndPages();
167         (new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::IGNORE);
168
169         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
170         $respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="new"]');
171
172         $respHtml = $this->withHtml($this->get($book->pages()->first()->getUrl()));
173         $respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="updates"]');
174         $respHtml->assertElementNotExists('form[action$="/watching/update"] button[name="level"][value="new"]');
175     }
176
177     public function test_notify_own_page_changes()
178     {
179         $editor = $this->users->editor();
180         $entities = $this->entities->createChainBelongingToUser($editor);
181         $prefs = new UserNotificationPreferences($editor);
182         $prefs->updateFromSettingsArray(['own-page-changes' => 'true']);
183
184         $notifications = Notification::fake();
185
186         $this->asAdmin();
187         $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
188         $notifications->assertSentTo($editor, PageUpdateNotification::class);
189     }
190
191     public function test_notify_own_page_comments()
192     {
193         $editor = $this->users->editor();
194         $entities = $this->entities->createChainBelongingToUser($editor);
195         $prefs = new UserNotificationPreferences($editor);
196         $prefs->updateFromSettingsArray(['own-page-comments' => 'true']);
197
198         $notifications = Notification::fake();
199
200         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
201             'text' => 'My new comment'
202         ]);
203         $notifications->assertSentTo($editor, CommentCreationNotification::class);
204     }
205
206     public function test_notify_comment_replies()
207     {
208         $editor = $this->users->editor();
209         $entities = $this->entities->createChainBelongingToUser($editor);
210         $prefs = new UserNotificationPreferences($editor);
211         $prefs->updateFromSettingsArray(['comment-replies' => 'true']);
212
213         $notifications = Notification::fake();
214
215         $this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
216             'text' => 'My new comment'
217         ]);
218         $comment = $entities['page']->comments()->first();
219
220         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
221             'text' => 'My new comment response',
222             'parent_id' => $comment->id,
223         ]);
224         $notifications->assertSentTo($editor, CommentCreationNotification::class);
225     }
226
227     public function test_notify_watch_parent_book_ignore()
228     {
229         $editor = $this->users->editor();
230         $entities = $this->entities->createChainBelongingToUser($editor);
231         $watches = new UserEntityWatchOptions($editor, $entities['book']);
232         $prefs = new UserNotificationPreferences($editor);
233         $watches->updateLevelByValue(WatchLevels::IGNORE);
234         $prefs->updateFromSettingsArray(['own-page-changes' => 'true', 'own-page-comments' => true]);
235
236         $notifications = Notification::fake();
237
238         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
239             'text' => 'My new comment response',
240         ]);
241         $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
242         $notifications->assertNothingSent();
243     }
244
245     public function test_notify_watch_parent_book_comments()
246     {
247         $notifications = Notification::fake();
248         $editor = $this->users->editor();
249         $admin = $this->users->admin();
250         $entities = $this->entities->createChainBelongingToUser($editor);
251         $watches = new UserEntityWatchOptions($editor, $entities['book']);
252         $watches->updateLevelByValue(WatchLevels::COMMENTS);
253
254         // Comment post
255         $this->actingAs($admin)->post("/comment/{$entities['page']->id}", [
256             'text' => 'My new comment response',
257         ]);
258
259         $notifications->assertSentTo($editor, function (CommentCreationNotification $notification) use ($editor, $admin, $entities) {
260             $mail = $notification->toMail($editor);
261             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
262             return $mail->subject === 'New comment on page: ' . $entities['page']->getShortName()
263                 && str_contains($mailContent, 'View Comment')
264                 && str_contains($mailContent, 'Page Name: ' . $entities['page']->name)
265                 && str_contains($mailContent, 'Commenter: ' . $admin->name)
266                 && str_contains($mailContent, 'Comment: My new comment response');
267         });
268     }
269
270     public function test_notify_watch_parent_book_updates()
271     {
272         $notifications = Notification::fake();
273         $editor = $this->users->editor();
274         $admin = $this->users->admin();
275         $entities = $this->entities->createChainBelongingToUser($editor);
276         $watches = new UserEntityWatchOptions($editor, $entities['book']);
277         $watches->updateLevelByValue(WatchLevels::UPDATES);
278
279         $this->actingAs($admin);
280         $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
281
282         $notifications->assertSentTo($editor, function (PageUpdateNotification $notification) use ($editor, $admin) {
283             $mail = $notification->toMail($editor);
284             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
285             return $mail->subject === 'Updated page: Updated page'
286                 && str_contains($mailContent, 'View Page')
287                 && str_contains($mailContent, 'Page Name: Updated page')
288                 && str_contains($mailContent, 'Updated By: ' . $admin->name)
289                 && str_contains($mailContent, 'you won\'t be sent notifications for further edits to this page by the same editor');
290         });
291
292         // Test debounce
293         $notifications = Notification::fake();
294         $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
295         $notifications->assertNothingSentTo($editor);
296     }
297
298     public function test_notify_watch_parent_book_new()
299     {
300         $notifications = Notification::fake();
301         $editor = $this->users->editor();
302         $admin = $this->users->admin();
303         $entities = $this->entities->createChainBelongingToUser($editor);
304         $watches = new UserEntityWatchOptions($editor, $entities['book']);
305         $watches->updateLevelByValue(WatchLevels::NEW);
306
307         $this->actingAs($admin)->get($entities['chapter']->getUrl('/create-page'));
308         $page = $entities['chapter']->pages()->where('draft', '=', true)->first();
309         $this->post($page->getUrl(), ['name' => 'My new page', 'html' => 'My new page content']);
310
311         $notifications->assertSentTo($editor, function (PageCreationNotification $notification) use ($editor, $admin) {
312             $mail = $notification->toMail($editor);
313             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
314             return $mail->subject === 'New page: My new page'
315                 && str_contains($mailContent, 'View Page')
316                 && str_contains($mailContent, 'Page Name: My new page')
317                 && str_contains($mailContent, 'Created By: ' . $admin->name);
318         });
319     }
320
321     public function test_notifications_sent_in_right_language()
322     {
323         $editor = $this->users->editor();
324         $admin = $this->users->admin();
325         setting()->putUser($editor, 'language', 'de');
326         $entities = $this->entities->createChainBelongingToUser($editor);
327         $watches = new UserEntityWatchOptions($editor, $entities['book']);
328         $watches->updateLevelByValue(WatchLevels::COMMENTS);
329
330         $activities = [
331             ActivityType::PAGE_CREATE => $entities['page'],
332             ActivityType::PAGE_UPDATE => $entities['page'],
333             ActivityType::COMMENT_CREATE => (new Comment([]))->forceFill(['entity_id' => $entities['page']->id, 'entity_type' => $entities['page']->getMorphClass()]),
334         ];
335
336         $notifications = Notification::fake();
337         $logger = app()->make(ActivityLogger::class);
338         $this->actingAs($admin);
339
340         foreach ($activities as $activityType => $detail) {
341             $logger->add($activityType, $detail);
342         }
343
344         $sent = $notifications->sentNotifications()[get_class($editor)][$editor->id];
345         $this->assertCount(3, $sent);
346
347         foreach ($sent as $notificationInfo) {
348             $notification = $notificationInfo[0]['notification'];
349             $this->assertInstanceOf(BaseActivityNotification::class, $notification);
350             $mail = $notification->toMail($editor);
351             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
352             $this->assertStringContainsString('Name der Seite:', $mailContent);
353             $this->assertStringContainsString('Diese Benachrichtigung wurde', $mailContent);
354             $this->assertStringContainsString('Sollte es beim Anklicken der Schaltfläche', $mailContent);
355         }
356     }
357
358     public function test_notifications_not_sent_if_lacking_view_permission_for_related_item()
359     {
360         $notifications = Notification::fake();
361         $editor = $this->users->editor();
362         $page = $this->entities->page();
363
364         $watches = new UserEntityWatchOptions($editor, $page);
365         $watches->updateLevelByValue(WatchLevels::COMMENTS);
366         $this->permissions->disableEntityInheritedPermissions($page);
367
368         $this->asAdmin()->post("/comment/{$page->id}", [
369             'text' => 'My new comment response',
370         ])->assertOk();
371
372         $notifications->assertNothingSentTo($editor);
373     }
374
375     public function test_watches_deleted_on_user_delete()
376     {
377         $editor = $this->users->editor();
378         $page = $this->entities->page();
379
380         $watches = new UserEntityWatchOptions($editor, $page);
381         $watches->updateLevelByValue(WatchLevels::COMMENTS);
382         $this->assertDatabaseHas('watches', ['user_id' => $editor->id]);
383
384         $this->asAdmin()->delete($editor->getEditUrl());
385
386         $this->assertDatabaseMissing('watches', ['user_id' => $editor->id]);
387     }
388
389     public function test_watches_deleted_on_item_delete()
390     {
391         $editor = $this->users->editor();
392         $page = $this->entities->page();
393
394         $watches = new UserEntityWatchOptions($editor, $page);
395         $watches->updateLevelByValue(WatchLevels::COMMENTS);
396         $this->assertDatabaseHas('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
397
398         $this->entities->destroy($page);
399
400         $this->assertDatabaseMissing('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
401     }
402 }