]> BookStack Code Mirror - bookstack/blob - tests/Activity/WatchTest.php
WYSIWYG: Updated TinyMCE from 6.5.1 to 6.7.2
[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         // Create some existing comments to pad IDs to help potentially error
214         // on mis-identification of parent via ids used.
215         Comment::factory()->count(5)
216             ->for($entities['page'], 'entity')
217             ->create(['created_by' => $this->users->admin()->id]);
218
219         $notifications = Notification::fake();
220
221         $this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
222             'text' => 'My new comment'
223         ]);
224         $comment = $entities['page']->comments()->orderBy('id', 'desc')->first();
225
226         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
227             'text' => 'My new comment response',
228             'parent_id' => $comment->local_id,
229         ]);
230         $notifications->assertSentTo($editor, CommentCreationNotification::class);
231     }
232
233     public function test_notify_watch_parent_book_ignore()
234     {
235         $editor = $this->users->editor();
236         $entities = $this->entities->createChainBelongingToUser($editor);
237         $watches = new UserEntityWatchOptions($editor, $entities['book']);
238         $prefs = new UserNotificationPreferences($editor);
239         $watches->updateLevelByValue(WatchLevels::IGNORE);
240         $prefs->updateFromSettingsArray(['own-page-changes' => 'true', 'own-page-comments' => true]);
241
242         $notifications = Notification::fake();
243
244         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
245             'text' => 'My new comment response',
246         ]);
247         $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
248         $notifications->assertNothingSent();
249     }
250
251     public function test_notify_watch_parent_book_comments()
252     {
253         $notifications = Notification::fake();
254         $editor = $this->users->editor();
255         $admin = $this->users->admin();
256         $entities = $this->entities->createChainBelongingToUser($editor);
257         $watches = new UserEntityWatchOptions($editor, $entities['book']);
258         $watches->updateLevelByValue(WatchLevels::COMMENTS);
259
260         // Comment post
261         $this->actingAs($admin)->post("/comment/{$entities['page']->id}", [
262             'text' => 'My new comment response',
263         ]);
264
265         $notifications->assertSentTo($editor, function (CommentCreationNotification $notification) use ($editor, $admin, $entities) {
266             $mail = $notification->toMail($editor);
267             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
268             return $mail->subject === 'New comment on page: ' . $entities['page']->getShortName()
269                 && str_contains($mailContent, 'View Comment')
270                 && str_contains($mailContent, 'Page Name: ' . $entities['page']->name)
271                 && str_contains($mailContent, 'Commenter: ' . $admin->name)
272                 && str_contains($mailContent, 'Comment: My new comment response');
273         });
274     }
275
276     public function test_notify_watch_parent_book_updates()
277     {
278         $notifications = Notification::fake();
279         $editor = $this->users->editor();
280         $admin = $this->users->admin();
281         $entities = $this->entities->createChainBelongingToUser($editor);
282         $watches = new UserEntityWatchOptions($editor, $entities['book']);
283         $watches->updateLevelByValue(WatchLevels::UPDATES);
284
285         $this->actingAs($admin);
286         $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
287
288         $notifications->assertSentTo($editor, function (PageUpdateNotification $notification) use ($editor, $admin) {
289             $mail = $notification->toMail($editor);
290             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
291             return $mail->subject === 'Updated page: Updated page'
292                 && str_contains($mailContent, 'View Page')
293                 && str_contains($mailContent, 'Page Name: Updated page')
294                 && str_contains($mailContent, 'Updated By: ' . $admin->name)
295                 && str_contains($mailContent, 'you won\'t be sent notifications for further edits to this page by the same editor');
296         });
297
298         // Test debounce
299         $notifications = Notification::fake();
300         $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
301         $notifications->assertNothingSentTo($editor);
302     }
303
304     public function test_notify_watch_parent_book_new()
305     {
306         $notifications = Notification::fake();
307         $editor = $this->users->editor();
308         $admin = $this->users->admin();
309         $entities = $this->entities->createChainBelongingToUser($editor);
310         $watches = new UserEntityWatchOptions($editor, $entities['book']);
311         $watches->updateLevelByValue(WatchLevels::NEW);
312
313         $this->actingAs($admin)->get($entities['chapter']->getUrl('/create-page'));
314         $page = $entities['chapter']->pages()->where('draft', '=', true)->first();
315         $this->post($page->getUrl(), ['name' => 'My new page', 'html' => 'My new page content']);
316
317         $notifications->assertSentTo($editor, function (PageCreationNotification $notification) use ($editor, $admin) {
318             $mail = $notification->toMail($editor);
319             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
320             return $mail->subject === 'New page: My new page'
321                 && str_contains($mailContent, 'View Page')
322                 && str_contains($mailContent, 'Page Name: My new page')
323                 && str_contains($mailContent, 'Created By: ' . $admin->name);
324         });
325     }
326
327     public function test_notifications_sent_in_right_language()
328     {
329         $editor = $this->users->editor();
330         $admin = $this->users->admin();
331         setting()->putUser($editor, 'language', 'de');
332         $entities = $this->entities->createChainBelongingToUser($editor);
333         $watches = new UserEntityWatchOptions($editor, $entities['book']);
334         $watches->updateLevelByValue(WatchLevels::COMMENTS);
335
336         $activities = [
337             ActivityType::PAGE_CREATE => $entities['page'],
338             ActivityType::PAGE_UPDATE => $entities['page'],
339             ActivityType::COMMENT_CREATE => Comment::factory()->make([
340                 'entity_id' => $entities['page']->id,
341                 'entity_type' => $entities['page']->getMorphClass(),
342             ]),
343         ];
344
345         $notifications = Notification::fake();
346         $logger = app()->make(ActivityLogger::class);
347         $this->actingAs($admin);
348
349         foreach ($activities as $activityType => $detail) {
350             $logger->add($activityType, $detail);
351         }
352
353         $sent = $notifications->sentNotifications()[get_class($editor)][$editor->id];
354         $this->assertCount(3, $sent);
355
356         foreach ($sent as $notificationInfo) {
357             $notification = $notificationInfo[0]['notification'];
358             $this->assertInstanceOf(BaseActivityNotification::class, $notification);
359             $mail = $notification->toMail($editor);
360             $mailContent = html_entity_decode(strip_tags($mail->render()), ENT_QUOTES);
361             $this->assertStringContainsString('Name der Seite:', $mailContent);
362             $this->assertStringContainsString('Diese Benachrichtigung wurde', $mailContent);
363             $this->assertStringContainsString('Sollte es beim Anklicken der Schaltfläche', $mailContent);
364         }
365     }
366
367     public function test_notifications_not_sent_if_lacking_view_permission_for_related_item()
368     {
369         $notifications = Notification::fake();
370         $editor = $this->users->editor();
371         $page = $this->entities->page();
372
373         $watches = new UserEntityWatchOptions($editor, $page);
374         $watches->updateLevelByValue(WatchLevels::COMMENTS);
375         $this->permissions->disableEntityInheritedPermissions($page);
376
377         $this->asAdmin()->post("/comment/{$page->id}", [
378             'text' => 'My new comment response',
379         ])->assertOk();
380
381         $notifications->assertNothingSentTo($editor);
382     }
383
384     public function test_watches_deleted_on_user_delete()
385     {
386         $editor = $this->users->editor();
387         $page = $this->entities->page();
388
389         $watches = new UserEntityWatchOptions($editor, $page);
390         $watches->updateLevelByValue(WatchLevels::COMMENTS);
391         $this->assertDatabaseHas('watches', ['user_id' => $editor->id]);
392
393         $this->asAdmin()->delete($editor->getEditUrl());
394
395         $this->assertDatabaseMissing('watches', ['user_id' => $editor->id]);
396     }
397
398     public function test_watches_deleted_on_item_delete()
399     {
400         $editor = $this->users->editor();
401         $page = $this->entities->page();
402
403         $watches = new UserEntityWatchOptions($editor, $page);
404         $watches->updateLevelByValue(WatchLevels::COMMENTS);
405         $this->assertDatabaseHas('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
406
407         $this->entities->destroy($page);
408
409         $this->assertDatabaseMissing('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
410     }
411 }