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_scripts_cannot_be_injected_via_comment_html()
111 $page = $this->entities->page();
113 $script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
114 $this->asAdmin()->postJson("/comment/$page->id", [
118 $pageView = $this->get($page->getUrl());
119 $pageView->assertDontSee($script, false);
120 $pageView->assertSee('<p>My lovely comment</p>', false);
122 $comment = $page->comments()->first();
123 $this->putJson("/comment/$comment->id", [
124 'html' => $script . '<p>updated</p>',
127 $pageView = $this->get($page->getUrl());
128 $pageView->assertDontSee($script, false);
129 $pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
132 public function test_scripts_are_removed_even_if_already_in_db()
134 $page = $this->entities->page();
135 Comment::factory()->create([
136 'html' => '<script>superbadscript</script><p onclick="superbadonclick">scriptincommentest</p>',
137 'entity_type' => 'page', 'entity_id' => $page
140 $resp = $this->asAdmin()->get($page->getUrl());
141 $resp->assertSee('scriptincommentest', false);
142 $resp->assertDontSee('superbadscript', false);
143 $resp->assertDontSee('superbadonclick', false);
146 public function test_comment_html_is_limited()
148 $page = $this->entities->page();
149 $input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" data-a="b">a</a><section>Hello</section></p>';
150 $expected = '<p>Content<a href="#cat">a</a></p>';
152 $resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
154 $this->assertDatabaseHas('comments', [
155 'entity_type' => 'page',
156 'entity_id' => $page->id,
160 $comment = $page->comments()->first();
161 $resp = $this->put("/comment/{$comment->id}", ['html' => $input]);
163 $this->assertDatabaseHas('comments', [
164 'id' => $comment->id,
169 public function test_reply_comments_are_nested()
172 $page = $this->entities->page();
174 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
175 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
177 $respHtml = $this->withHtml($this->get($page->getUrl()));
178 $respHtml->assertElementCount('.comment-branch', 3);
179 $respHtml->assertElementNotExists('.comment-branch .comment-branch');
181 $comment = $page->comments()->first();
182 $resp = $this->postJson("/comment/$page->id", [
183 'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
185 $resp->assertStatus(200);
187 $respHtml = $this->withHtml($this->get($page->getUrl()));
188 $respHtml->assertElementCount('.comment-branch', 4);
189 $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
192 public function test_comments_are_visible_in_the_page_editor()
194 $page = $this->entities->page();
196 $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
198 $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
199 $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
202 public function test_comment_creator_name_truncated()
204 [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
205 $page = $this->entities->page();
207 $comment = Comment::factory()->make();
208 $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
210 $pageResp = $this->asAdmin()->get($page->getUrl());
211 $pageResp->assertSee('Wolfeschlegels…');
214 public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
216 $editor = $this->users->editor();
217 $page = $this->entities->page();
219 $resp = $this->actingAs($editor)->get($page->getUrl());
220 $resp->assertSee('tinymce.min.js?', false);
221 $resp->assertSee('window.editor_translations', false);
222 $resp->assertSee('component="entity-selector"', false);
224 $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
225 $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
227 $resp = $this->actingAs($editor)->get($page->getUrl());
228 $resp->assertDontSee('tinymce.min.js?', false);
229 $resp->assertDontSee('window.editor_translations', false);
230 $resp->assertDontSee('component="entity-selector"', false);
232 Comment::factory()->create([
233 'created_by' => $editor->id,
234 'entity_type' => 'page',
235 'entity_id' => $page->id,
238 $resp = $this->actingAs($editor)->get($page->getUrl());
239 $resp->assertSee('tinymce.min.js?', false);
240 $resp->assertSee('window.editor_translations', false);
241 $resp->assertSee('component="entity-selector"', false);
244 public function test_comment_displays_relative_times()
246 $page = $this->entities->page();
247 $comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
248 $comment->created_at = now()->subWeek();
249 $comment->updated_at = now()->subDay();
252 $pageResp = $this->asAdmin()->get($page->getUrl());
253 $html = $this->withHtml($pageResp);
255 // Create date shows relative time as text to user
256 $html->assertElementContains('.comment-box', 'commented 1 week ago');
257 // Updated indicator has full time as title
258 $html->assertElementContains('.comment-box span[title^="Updated ' . $comment->updated_at->format('Y-m-d') . '"]', 'Updated');