3 namespace Tests\Activity;
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;
20 class WatchTest extends TestCase
22 public function test_watch_action_exists_on_entity_unless_active()
24 $editor = $this->users->editor();
25 $this->actingAs($editor);
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');
33 $watchOptions = new UserEntityWatchOptions($editor, $entity);
34 $watchOptions->updateLevelByValue(WatchLevels::COMMENTS);
36 $resp = $this->get($entity->getUrl());
37 $this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
41 public function test_watch_action_only_shows_with_permission()
43 $viewer = $this->users->viewer();
44 $this->actingAs($viewer);
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');
53 $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
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');
62 public function test_watch_update()
64 $editor = $this->users->editor();
65 $book = $this->entities->book();
67 $this->actingAs($editor)->get($book->getUrl());
68 $resp = $this->put('/watching/update', [
69 'type' => $book->getMorphClass(),
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,
83 $resp = $this->put('/watching/update', [
84 'type' => $book->getMorphClass(),
88 $resp->assertRedirect($book->getUrl());
89 $this->assertDatabaseMissing('watches', [
90 'watchable_id' => $book->id,
91 'watchable_type' => $book->getMorphClass(),
92 'user_id' => $editor->id,
96 public function test_watch_update_fails_for_guest()
98 $this->setSettings(['app-public' => 'true']);
99 $guest = $this->users->guest();
100 $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
101 $book = $this->entities->book();
103 $resp = $this->put('/watching/update', [
104 'type' => $book->getMorphClass(),
106 'level' => 'comments'
109 $this->assertPermissionError($resp);
110 $guest->unsetRelations();
113 public function test_watch_detail_display_reflects_state()
115 $editor = $this->users->editor();
116 $book = $this->entities->bookHasChaptersAndPages();
117 $chapter = $book->chapters()->first();
118 $page = $chapter->pages()->first();
120 (new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::UPDATES);
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');
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');
130 (new UserEntityWatchOptions($editor, $page))->updateLevelByValue(WatchLevels::UPDATES);
131 $this->get($page->getUrl())->assertSee('Watching new pages and updates');
134 public function test_watch_detail_ignore_indicator_cascades()
136 $editor = $this->users->editor();
137 $book = $this->entities->bookHasChaptersAndPages();
138 (new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::IGNORE);
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');
145 public function test_watch_option_menu_shows_current_active_state()
147 $editor = $this->users->editor();
148 $book = $this->entities->book();
149 $options = new UserEntityWatchOptions($editor, $book);
151 $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
152 $respHtml->assertElementNotExists('form[action$="/watching/update"] svg[data-icon="check-circle"]');
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"]');
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"]');
163 public function test_watch_option_menu_limits_options_for_pages()
165 $editor = $this->users->editor();
166 $book = $this->entities->bookHasChaptersAndPages();
167 (new UserEntityWatchOptions($editor, $book))->updateLevelByValue(WatchLevels::IGNORE);
169 $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
170 $respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="new"]');
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"]');
177 public function test_notify_own_page_changes()
179 $editor = $this->users->editor();
180 $entities = $this->entities->createChainBelongingToUser($editor);
181 $prefs = new UserNotificationPreferences($editor);
182 $prefs->updateFromSettingsArray(['own-page-changes' => 'true']);
184 $notifications = Notification::fake();
187 $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
188 $notifications->assertSentTo($editor, PageUpdateNotification::class);
191 public function test_notify_own_page_comments()
193 $editor = $this->users->editor();
194 $entities = $this->entities->createChainBelongingToUser($editor);
195 $prefs = new UserNotificationPreferences($editor);
196 $prefs->updateFromSettingsArray(['own-page-comments' => 'true']);
198 $notifications = Notification::fake();
200 $this->asAdmin()->post("/comment/{$entities['page']->id}", [
201 'text' => 'My new comment'
203 $notifications->assertSentTo($editor, CommentCreationNotification::class);
206 public function test_notify_comment_replies()
208 $editor = $this->users->editor();
209 $entities = $this->entities->createChainBelongingToUser($editor);
210 $prefs = new UserNotificationPreferences($editor);
211 $prefs->updateFromSettingsArray(['comment-replies' => 'true']);
213 $notifications = Notification::fake();
215 $this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
216 'text' => 'My new comment'
218 $comment = $entities['page']->comments()->first();
220 $this->asAdmin()->post("/comment/{$entities['page']->id}", [
221 'text' => 'My new comment response',
222 'parent_id' => $comment->id,
224 $notifications->assertSentTo($editor, CommentCreationNotification::class);
227 public function test_notify_watch_parent_book_ignore()
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]);
236 $notifications = Notification::fake();
238 $this->asAdmin()->post("/comment/{$entities['page']->id}", [
239 'text' => 'My new comment response',
241 $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
242 $notifications->assertNothingSent();
245 public function test_notify_watch_parent_book_comments()
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);
255 $this->actingAs($admin)->post("/comment/{$entities['page']->id}", [
256 'text' => 'My new comment response',
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');
270 public function test_notify_watch_parent_book_updates()
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);
279 $this->actingAs($admin);
280 $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
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');
293 $notifications = Notification::fake();
294 $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
295 $notifications->assertNothingSentTo($editor);
298 public function test_notify_watch_parent_book_new()
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);
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']);
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);
321 public function test_notifications_sent_in_right_language()
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);
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()]),
336 $notifications = Notification::fake();
337 $logger = app()->make(ActivityLogger::class);
338 $this->actingAs($admin);
340 foreach ($activities as $activityType => $detail) {
341 $logger->add($activityType, $detail);
344 $sent = $notifications->sentNotifications()[get_class($editor)][$editor->id];
345 $this->assertCount(3, $sent);
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);
358 public function test_notifications_not_sent_if_lacking_view_permission_for_related_item()
360 $notifications = Notification::fake();
361 $editor = $this->users->editor();
362 $page = $this->entities->page();
364 $watches = new UserEntityWatchOptions($editor, $page);
365 $watches->updateLevelByValue(WatchLevels::COMMENTS);
366 $this->permissions->disableEntityInheritedPermissions($page);
368 $this->asAdmin()->post("/comment/{$page->id}", [
369 'text' => 'My new comment response',
372 $notifications->assertNothingSentTo($editor);
375 public function test_watches_deleted_on_user_delete()
377 $editor = $this->users->editor();
378 $page = $this->entities->page();
380 $watches = new UserEntityWatchOptions($editor, $page);
381 $watches->updateLevelByValue(WatchLevels::COMMENTS);
382 $this->assertDatabaseHas('watches', ['user_id' => $editor->id]);
384 $this->asAdmin()->delete($editor->getEditUrl());
386 $this->assertDatabaseMissing('watches', ['user_id' => $editor->id]);
389 public function test_watches_deleted_on_item_delete()
391 $editor = $this->users->editor();
392 $page = $this->entities->page();
394 $watches = new UserEntityWatchOptions($editor, $page);
395 $watches->updateLevelByValue(WatchLevels::COMMENTS);
396 $this->assertDatabaseHas('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);
398 $this->entities->destroy($page);
400 $this->assertDatabaseMissing('watches', ['watchable_type' => 'page', 'watchable_id' => $page->id]);