3 namespace Tests\Activity;
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;
15 class WatchTest extends TestCase
17 public function test_watch_action_exists_on_entity_unless_active()
19 $editor = $this->users->editor();
20 $this->actingAs($editor);
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');
28 $watchOptions = new UserEntityWatchOptions($editor, $entity);
29 $watchOptions->updateWatchLevel('comments');
31 $resp = $this->get($entity->getUrl());
32 $this->withHtml($resp)->assertElementNotExists('form[action$="/watching/update"] button.icon-list-item');
36 public function test_watch_action_only_shows_with_permission()
38 $viewer = $this->users->viewer();
39 $this->actingAs($viewer);
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');
48 $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
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');
57 public function test_watch_update()
59 $editor = $this->users->editor();
60 $book = $this->entities->book();
62 $this->actingAs($editor)->get($book->getUrl());
63 $resp = $this->put('/watching/update', [
64 'type' => get_class($book),
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,
78 $resp = $this->put('/watching/update', [
79 'type' => get_class($book),
83 $resp->assertRedirect($book->getUrl());
84 $this->assertDatabaseMissing('watches', [
85 'watchable_id' => $book->id,
86 'watchable_type' => $book->getMorphClass(),
87 'user_id' => $editor->id,
91 public function test_watch_update_fails_for_guest()
93 $this->setSettings(['app-public' => 'true']);
94 $guest = $this->users->guest();
95 $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
96 $book = $this->entities->book();
98 $resp = $this->put('/watching/update', [
99 'type' => get_class($book),
101 'level' => 'comments'
104 $this->assertPermissionError($resp);
107 public function test_watch_detail_display_reflects_state()
109 $editor = $this->users->editor();
110 $book = $this->entities->bookHasChaptersAndPages();
111 $chapter = $book->chapters()->first();
112 $page = $chapter->pages()->first();
114 (new UserEntityWatchOptions($editor, $book))->updateWatchLevel('updates');
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');
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');
124 (new UserEntityWatchOptions($editor, $page))->updateWatchLevel('updates');
125 $this->get($page->getUrl())->assertSee('Watching new pages and updates');
128 public function test_watch_detail_ignore_indicator_cascades()
130 $editor = $this->users->editor();
131 $book = $this->entities->bookHasChaptersAndPages();
132 (new UserEntityWatchOptions($editor, $book))->updateWatchLevel('ignore');
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');
139 public function test_watch_option_menu_shows_current_active_state()
141 $editor = $this->users->editor();
142 $book = $this->entities->book();
143 $options = new UserEntityWatchOptions($editor, $book);
145 $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
146 $respHtml->assertElementNotExists('form[action$="/watching/update"] svg[data-icon="check-circle"]');
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"]');
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"]');
157 public function test_watch_option_menu_limits_options_for_pages()
159 $editor = $this->users->editor();
160 $book = $this->entities->bookHasChaptersAndPages();
161 (new UserEntityWatchOptions($editor, $book))->updateWatchLevel('ignore');
163 $respHtml = $this->withHtml($this->actingAs($editor)->get($book->getUrl()));
164 $respHtml->assertElementExists('form[action$="/watching/update"] button[name="level"][value="new"]');
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"]');
171 public function test_notify_own_page_changes()
173 $editor = $this->users->editor();
174 $entities = $this->entities->createChainBelongingToUser($editor);
175 $prefs = new UserNotificationPreferences($editor);
176 $prefs->updateFromSettingsArray(['own-page-changes' => 'true']);
178 $notifications = Notification::fake();
181 $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
182 $notifications->assertSentTo($editor, PageUpdateNotification::class);
185 public function test_notify_own_page_comments()
187 $editor = $this->users->editor();
188 $entities = $this->entities->createChainBelongingToUser($editor);
189 $prefs = new UserNotificationPreferences($editor);
190 $prefs->updateFromSettingsArray(['own-page-comments' => 'true']);
192 $notifications = Notification::fake();
194 $this->asAdmin()->post("/comment/{$entities['page']->id}", [
195 'text' => 'My new comment'
197 $notifications->assertSentTo($editor, CommentCreationNotification::class);
200 public function test_notify_comment_replies()
202 $editor = $this->users->editor();
203 $entities = $this->entities->createChainBelongingToUser($editor);
204 $prefs = new UserNotificationPreferences($editor);
205 $prefs->updateFromSettingsArray(['comment-replies' => 'true']);
207 $notifications = Notification::fake();
209 $this->actingAs($editor)->post("/comment/{$entities['page']->id}", [
210 'text' => 'My new comment'
212 $comment = $entities['page']->comments()->first();
214 $this->asAdmin()->post("/comment/{$entities['page']->id}", [
215 'text' => 'My new comment response',
216 'parent_id' => $comment->id,
218 $notifications->assertSentTo($editor, CommentCreationNotification::class);
221 public function test_notify_watch_parent_book_ignore()
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]);
230 $notifications = Notification::fake();
232 $this->asAdmin()->post("/comment/{$entities['page']->id}", [
233 'text' => 'My new comment response',
235 $this->entities->updatePage($entities['page'], ['name' => 'My updated page', 'html' => 'Hello']);
236 $notifications->assertNothingSent();
239 public function test_notify_watch_parent_book_comments()
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');
249 $this->actingAs($admin)->post("/comment/{$entities['page']->id}", [
250 'text' => 'My new comment response',
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');
264 public function test_notify_watch_parent_book_updates()
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');
273 $this->actingAs($admin);
274 $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
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');
287 $notifications = Notification::fake();
288 $this->entities->updatePage($entities['page'], ['name' => 'Updated page', 'html' => 'new page content']);
289 $notifications->assertNothingSentTo($editor);
292 public function test_notify_watch_parent_book_new()
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');
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']);
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);