3 namespace Tests\Entity;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Comment;
7 use BookStack\Entities\Models\Page;
10 class CommentTest extends TestCase
12 public function test_add_comment()
15 $page = $this->entities->page();
17 $comment = Comment::factory()->make(['parent_id' => 2]);
18 $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
20 $resp->assertStatus(200);
21 $resp->assertSee($comment->html, false);
23 $pageResp = $this->get($page->getUrl());
24 $pageResp->assertSee($comment->html, false);
26 $this->assertDatabaseHas('comments', [
28 'entity_id' => $page->id,
29 'entity_type' => Page::newModelInstance()->getMorphClass(),
34 $this->assertActivityExists(ActivityType::COMMENT_CREATE);
36 public function test_add_comment_stores_content_reference_only_if_format_valid()
39 'bkmrk-my-title:4589284922:4-3' => true,
40 'bkmrk-my-title:4589284922:' => true,
41 'bkmrk-my-title:4589284922:abc' => false,
42 'my-title:4589284922:' => false,
43 'bkmrk-my-title-4589284922:' => false,
46 $page = $this->entities->page();
48 foreach ($validityByRefs as $ref => $valid) {
49 $this->asAdmin()->postJson("/comment/$page->id", [
50 'html' => '<p>My comment</p>',
52 'content_ref' => $ref,
56 $this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
58 $this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
63 public function test_comment_edit()
66 $page = $this->entities->page();
68 $comment = Comment::factory()->make();
69 $this->postJson("/comment/$page->id", $comment->getAttributes());
71 $comment = $page->comments()->first();
72 $newHtml = '<p>updated text content</p>';
73 $resp = $this->putJson("/comment/$comment->id", [
77 $resp->assertStatus(200);
78 $resp->assertSee($newHtml, false);
79 $resp->assertDontSee($comment->html, false);
81 $this->assertDatabaseHas('comments', [
83 'entity_id' => $page->id,
86 $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
89 public function test_comment_delete()
92 $page = $this->entities->page();
94 $comment = Comment::factory()->make();
95 $this->postJson("/comment/$page->id", $comment->getAttributes());
97 $comment = $page->comments()->first();
99 $resp = $this->delete("/comment/$comment->id");
100 $resp->assertStatus(200);
102 $this->assertDatabaseMissing('comments', [
103 'id' => $comment->id,
106 $this->assertActivityExists(ActivityType::COMMENT_DELETE);
109 public function test_comment_archive_and_unarchive()
112 $page = $this->entities->page();
114 $comment = Comment::factory()->make();
115 $page->comments()->save($comment);
118 $this->put("/comment/$comment->id/archive");
120 $this->assertDatabaseHas('comments', [
121 'id' => $comment->id,
125 $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
127 $this->put("/comment/$comment->id/unarchive");
129 $this->assertDatabaseHas('comments', [
130 'id' => $comment->id,
134 $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
137 public function test_archive_endpoints_require_delete_or_edit_permissions()
139 $viewer = $this->users->viewer();
140 $page = $this->entities->page();
142 $comment = Comment::factory()->make();
143 $page->comments()->save($comment);
146 $endpoints = ["/comment/$comment->id/archive", "/comment/$comment->id/unarchive"];
148 foreach ($endpoints as $endpoint) {
149 $resp = $this->actingAs($viewer)->put($endpoint);
150 $this->assertPermissionError($resp);
153 $this->permissions->grantUserRolePermissions($viewer, ['comment-delete-all']);
155 foreach ($endpoints as $endpoint) {
156 $resp = $this->actingAs($viewer)->put($endpoint);
160 $this->permissions->removeUserRolePermissions($viewer, ['comment-delete-all']);
161 $this->permissions->grantUserRolePermissions($viewer, ['comment-update-all']);
163 foreach ($endpoints as $endpoint) {
164 $resp = $this->actingAs($viewer)->put($endpoint);
169 public function test_scripts_cannot_be_injected_via_comment_html()
171 $page = $this->entities->page();
173 $script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
174 $this->asAdmin()->postJson("/comment/$page->id", [
178 $pageView = $this->get($page->getUrl());
179 $pageView->assertDontSee($script, false);
180 $pageView->assertSee('<p>My lovely comment</p>', false);
182 $comment = $page->comments()->first();
183 $this->putJson("/comment/$comment->id", [
184 'html' => $script . '<p>updated</p>',
187 $pageView = $this->get($page->getUrl());
188 $pageView->assertDontSee($script, false);
189 $pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
192 public function test_scripts_are_removed_even_if_already_in_db()
194 $page = $this->entities->page();
195 Comment::factory()->create([
196 'html' => '<script>superbadscript</script><p onclick="superbadonclick">scriptincommentest</p>',
197 'entity_type' => 'page', 'entity_id' => $page
200 $resp = $this->asAdmin()->get($page->getUrl());
201 $resp->assertSee('scriptincommentest', false);
202 $resp->assertDontSee('superbadscript', false);
203 $resp->assertDontSee('superbadonclick', false);
206 public function test_comment_html_is_limited()
208 $page = $this->entities->page();
209 $input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" data-a="b">a</a><section>Hello</section></p>';
210 $expected = '<p>Content<a href="#cat">a</a></p>';
212 $resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
214 $this->assertDatabaseHas('comments', [
215 'entity_type' => 'page',
216 'entity_id' => $page->id,
220 $comment = $page->comments()->first();
221 $resp = $this->put("/comment/{$comment->id}", ['html' => $input]);
223 $this->assertDatabaseHas('comments', [
224 'id' => $comment->id,
229 public function test_reply_comments_are_nested()
232 $page = $this->entities->page();
234 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
235 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
237 $respHtml = $this->withHtml($this->get($page->getUrl()));
238 $respHtml->assertElementCount('.comment-branch', 3);
239 $respHtml->assertElementNotExists('.comment-branch .comment-branch');
241 $comment = $page->comments()->first();
242 $resp = $this->postJson("/comment/$page->id", [
243 'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
245 $resp->assertStatus(200);
247 $respHtml = $this->withHtml($this->get($page->getUrl()));
248 $respHtml->assertElementCount('.comment-branch', 4);
249 $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
252 public function test_comments_are_visible_in_the_page_editor()
254 $page = $this->entities->page();
256 $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
258 $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
259 $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
262 public function test_comment_creator_name_truncated()
264 [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
265 $page = $this->entities->page();
267 $comment = Comment::factory()->make();
268 $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
270 $pageResp = $this->asAdmin()->get($page->getUrl());
271 $pageResp->assertSee('Wolfeschlegels…');
274 public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
276 $editor = $this->users->editor();
277 $page = $this->entities->page();
279 $resp = $this->actingAs($editor)->get($page->getUrl());
280 $resp->assertSee('tinymce.min.js?', false);
281 $resp->assertSee('window.editor_translations', false);
282 $resp->assertSee('component="entity-selector"', false);
284 $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
285 $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
287 $resp = $this->actingAs($editor)->get($page->getUrl());
288 $resp->assertDontSee('tinymce.min.js?', false);
289 $resp->assertDontSee('window.editor_translations', false);
290 $resp->assertDontSee('component="entity-selector"', false);
292 Comment::factory()->create([
293 'created_by' => $editor->id,
294 'entity_type' => 'page',
295 'entity_id' => $page->id,
298 $resp = $this->actingAs($editor)->get($page->getUrl());
299 $resp->assertSee('tinymce.min.js?', false);
300 $resp->assertSee('window.editor_translations', false);
301 $resp->assertSee('component="entity-selector"', false);
304 public function test_comment_displays_relative_times()
306 $page = $this->entities->page();
307 $comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
308 $comment->created_at = now()->subWeek();
309 $comment->updated_at = now()->subDay();
312 $pageResp = $this->asAdmin()->get($page->getUrl());
313 $html = $this->withHtml($pageResp);
315 // Create date shows relative time as text to user
316 $html->assertElementContains('.comment-box', 'commented 1 week ago');
317 // Updated indicator has full time as title
318 $html->assertElementContains('.comment-box span[title^="Updated ' . $comment->updated_at->format('Y-m-d') . '"]', 'Updated');