]> BookStack Code Mirror - bookstack/blob - tests/Activity/WatchTest.php
Notifications: Add phpunit test for notification sending
[bookstack] / tests / Activity / WatchTest.php
1 <?php
2
3 namespace Tests\Activity;
4
5 use BookStack\Activity\Notifications\Messages\CommentCreationNotification;
6 use BookStack\Activity\Notifications\Messages\PageCreationNotification;
7 use BookStack\Activity\Notifications\Messages\PageUpdateNotification;
8 use BookStack\Activity\Tools\UserEntityWatchOptions;
9 use BookStack\Activity\WatchLevels;
10 use BookStack\Entities\Models\Entity;
11 use BookStack\Settings\UserNotificationPreferences;
12 use Illuminate\Support\Facades\Notification;
13 use Tests\TestCase;
14
15 class WatchTest extends TestCase
16 {
17     public function test_watch_action_exists_on_entity_unless_active()
18     {
19         $editor = $this->users->editor();
20         $this->actingAs($editor);
21
22         $entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->page()];
23         /** @var Entity $entity */
24         foreach ($entities as $entity) {
25             $resp = $this->get($entity->getUrl());
26             $this->withHtml($resp)->assertElementContains('form[action$="/watching/update"] button.icon-list-item', 'Watch');
27
28             $watchOptions = new UserEntityWatchOptions($editor, $entity);
29             $watchOptions->updateWatchLevel('comments');
30
31             $resp = $this->get($entity->getUrl());
32             $this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
33         }
34     }
35
36     public function test_watch_action_only_shows_with_permission()
37     {
38         $viewer = $this->users->viewer();
39         $this->actingAs($viewer);
40
41         $entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->page()];
42         /** @var Entity $entity */
43         foreach ($entities as $entity) {
44             $resp = $this->get($entity->getUrl());
45             $this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
46         }
47
48         $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
49
50         /** @var Entity $entity */
51         foreach ($entities as $entity) {
52             $resp = $this->get($entity->getUrl());
53             $this->withHtml($resp)->assertElementExists('form[action$="/watching/update"] button.icon-list-item');
54         }
55     }
56
57     public function test_watch_update()
58     {
59         $editor = $this->users->editor();
60         $book = $this->entities->book();
61
62         $this->actingAs($editor)->get($book->getUrl());
63         $resp = $this->put('/watching/update', [
64             'type' => get_class($book),
65             'id' => $book->id,
66             'level' => 'comments'
67         ]);
68
69         $resp->assertRedirect($book->getUrl());
70         $this->assertSessionHas('success');
71         $this->assertDatabaseHas('watches', [
72             'watchable_id' => $book->id,
73             'watchable_type' => $book->getMorphClass(),
74             'user_id' => $editor->id,
75             'level' => WatchLevels::COMMENTS,
76         ]);
77
78         $resp = $this->put('/watching/update', [
79             'type' => get_class($book),
80             'id' => $book->id,
81             'level' => 'default'
82         ]);
83         $resp->assertRedirect($book->getUrl());
84         $this->assertDatabaseMissing('watches', [
85             'watchable_id' => $book->id,
86             'watchable_type' => $book->getMorphClass(),
87             'user_id' => $editor->id,
88         ]);
89     }
90
91     public function test_watch_update_fails_for_guest()
92     {
93         $this->setSettings(['app-public' => 'true']);
94         $guest = $this->users->guest();
95         $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
96         $book = $this->entities->book();
97
98         $resp = $this->put('/watching/update', [
99             'type' => get_class($book),
100             'id' => $book->id,
101             'level' => 'comments'
102         ]);
103
104         $this->assertPermissionError($resp);
105     }
106
107     public function test_watch_detail_display_reflects_state()
108     {
109         $editor = $this->users->editor();
110         $book = $this->entities->bookHasChaptersAndPages();
111         $chapter = $book->chapters()->first();
112         $page = $chapter->pages()->first();
113
114         (new UserEntityWatchOptions($editor, $book))->updateWatchLevel('updates');
115
116         $this->actingAs($editor)->get($book->getUrl())->assertSee('Watching new pages and updates');
117         $this->get($chapter->getUrl())->assertSee('Watching via parent book');
118         $this->get($page->getUrl())->assertSee('Watching via parent book');
119
120         (new UserEntityWatchOptions($editor, $chapter))->updateWatchLevel('comments');
121         $this->get($chapter->getUrl())->assertSee('Watching new pages, updates & comments');
122         $this->get($page->getUrl())->assertSee('Watching via parent chapter');
123
124         (new UserEntityWatchOptions($editor, $page))->updateWatchLevel('updates');
125         $this->get($page->getUrl())->assertSee('Watching new pages and updates');
126     }
127
128     public function test_watch_detail_ignore_indicator_cascades()
129     {
130         $editor = $this->users->editor();
131         $book = $this->entities->bookHasChaptersAndPages();
132         (new UserEntityWatchOptions($editor, $book))->updateWatchLevel('ignore');
133
134         $this->actingAs($editor)->get($book->getUrl())->assertSee('Ignoring notifications');
135         $this->get($book->chapters()->first()->getUrl())->assertSee('Ignoring via parent book');
136         $this->get($book->pages()->first()->getUrl())->assertSee('Ignoring via parent book');
137     }
138
139     public function test_watch_option_menu_shows_current_active_state()
140     {
141         $editor = $this->users->editor();
142         $book = $this->entities->book();
143         $options = new UserEntityWatchOptions($editor, $book);
144
145         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
146         $respHtml->assertElementNotExists('form[action$="/watching/update"] svg[data-icon="check-circle"]');
147
148         $options->updateWatchLevel('comments');
149         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
150         $respHtml->assertElementExists('form[action$="/watching/update"] button[value="comments"] svg[data-icon="check-circle"]');
151
152         $options->updateWatchLevel('ignore');
153         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
154         $respHtml->assertElementExists('form[action$="/watching/update"] button[value="ignore"] svg[data-icon="check-circle"]');
155     }
156
157     public function test_watch_option_menu_limits_options_for_pages()
158     {
159         $editor = $this->users->editor();
160         $book = $this->entities->bookHasChaptersAndPages();
161         (new UserEntityWatchOptions($editor, $book))->updateWatchLevel('ignore');
162
163         $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
164         $respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="new"]');
165
166         $respHtml = $this->withHtml($this->get($book->pages()->first()->getUrl()));
167         $respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="updates"]');
168         $respHtml->assertElementNotExists('form[action$="/watching/update"] button[name="level"][value="new"]');
169     }
170
171     public function test_notify_own_page_changes()
172     {
173         $editor = $this->users->editor();
174         $entities = $this->entities->createChainBelongingToUser($editor);
175         $prefs = new UserNotificationPreferences($editor);
176         $prefs->updateFromSettingsArray(['own-page-changes' => 'true']);
177
178         $notifications = Notification::fake();
179
180         $this->asAdmin();
181         $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
182         $notifications->assertSentTo($editor, PageUpdateNotification::class);
183     }
184
185     public function test_notify_own_page_comments()
186     {
187         $editor = $this->users->editor();
188         $entities = $this->entities->createChainBelongingToUser($editor);
189         $prefs = new UserNotificationPreferences($editor);
190         $prefs->updateFromSettingsArray(['own-page-comments' => 'true']);
191
192         $notifications = Notification::fake();
193
194         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
195             'text' => 'My new comment'
196         ]);
197         $notifications->assertSentTo($editor, CommentCreationNotification::class);
198     }
199
200     public function test_notify_comment_replies()
201     {
202         $editor = $this->users->editor();
203         $entities = $this->entities->createChainBelongingToUser($editor);
204         $prefs = new UserNotificationPreferences($editor);
205         $prefs->updateFromSettingsArray(['comment-replies' => 'true']);
206
207         $notifications = Notification::fake();
208
209         $this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
210             'text' => 'My new comment'
211         ]);
212         $comment = $entities['page']->comments()->first();
213
214         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
215             'text' => 'My new comment response',
216             'parent_id' => $comment->id,
217         ]);
218         $notifications->assertSentTo($editor, CommentCreationNotification::class);
219     }
220
221     public function test_notify_watch_parent_book_ignore()
222     {
223         $editor = $this->users->editor();
224         $entities = $this->entities->createChainBelongingToUser($editor);
225         $watches = new UserEntityWatchOptions($editor, $entities['book']);
226         $prefs = new UserNotificationPreferences($editor);
227         $watches->updateWatchLevel('ignore');
228         $prefs->updateFromSettingsArray(['own-page-changes' => 'true', 'own-page-comments' => true]);
229
230         $notifications = Notification::fake();
231
232         $this->asAdmin()->post("/comment/{$entities['page']->id}", [
233             'text' => 'My new comment response',
234         ]);
235         $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
236         $notifications->assertNothingSent();
237     }
238
239     public function test_notify_watch_parent_book_comments()
240     {
241         $notifications = Notification::fake();
242         $editor = $this->users->editor();
243         $admin = $this->users->admin();
244         $entities = $this->entities->createChainBelongingToUser($editor);
245         $watches = new UserEntityWatchOptions($editor, $entities['book']);
246         $watches->updateWatchLevel('comments');
247
248         // Comment post
249         $this->actingAs($admin)->post("/comment/{$entities['page']->id}", [
250             'text' => 'My new comment response',
251         ]);
252
253         $notifications->assertSentTo($editor, function (CommentCreationNotification $notification) use ($editor, $admin, $entities) {
254             $mail = $notification->toMail($editor);
255             $mailContent = html_entity_decode(strip_tags($mail->render()));
256             return $mail->subject === 'New comment on page: ' . $entities['page']->getShortName()
257                 && str_contains($mailContent, 'View Comment')
258                 && str_contains($mailContent, 'Page Name: ' . $entities['page']->name)
259                 && str_contains($mailContent, 'Commenter: ' . $admin->name)
260                 && str_contains($mailContent, 'Comment: My new comment response');
261         });
262     }
263
264     public function test_notify_watch_parent_book_updates()
265     {
266         $notifications = Notification::fake();
267         $editor = $this->users->editor();
268         $admin = $this->users->admin();
269         $entities = $this->entities->createChainBelongingToUser($editor);
270         $watches = new UserEntityWatchOptions($editor, $entities['book']);
271         $watches->updateWatchLevel('updates');
272
273         $this->actingAs($admin);
274         $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
275
276         $notifications->assertSentTo($editor, function (PageUpdateNotification $notification) use ($editor, $admin) {
277             $mail = $notification->toMail($editor);
278             $mailContent = html_entity_decode(strip_tags($mail->render()));
279             return $mail->subject === 'Updated page: Updated page'
280                 && str_contains($mailContent, 'View Page')
281                 && str_contains($mailContent, 'Page Name: Updated page')
282                 && str_contains($mailContent, 'Updated By: ' . $admin->name)
283                 && str_contains($mailContent, 'you won\'t be sent notifications for further edits to this page by the same editor');
284         });
285
286         // Test debounce
287         $notifications = Notification::fake();
288         $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
289         $notifications->assertNothingSentTo($editor);
290     }
291
292     public function test_notify_watch_parent_book_new()
293     {
294         $notifications = Notification::fake();
295         $editor = $this->users->editor();
296         $admin = $this->users->admin();
297         $entities = $this->entities->createChainBelongingToUser($editor);
298         $watches = new UserEntityWatchOptions($editor, $entities['book']);
299         $watches->updateWatchLevel('new');
300
301         $this->actingAs($admin)->get($entities['chapter']->getUrl('/create-page'));
302         $page = $entities['chapter']->pages()->where('draft', '=', true)->first();
303         $this->post($page->getUrl(), ['name' => 'My new page', 'html' => 'My new page content']);
304
305         $notifications->assertSentTo($editor, function (PageCreationNotification $notification) use ($editor, $admin) {
306             $mail = $notification->toMail($editor);
307             $mailContent = html_entity_decode(strip_tags($mail->render()));
308             return $mail->subject === 'New page: My new page'
309                 && str_contains($mailContent, 'View Page')
310                 && str_contains($mailContent, 'Page Name: My new page')
311                 && str_contains($mailContent, 'Created By: ' . $admin->name);
312         });
313     }
314 }