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