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);
37 public function test_comment_edit()
40 $page = $this->entities->page();
42 $comment = Comment::factory()->make();
43 $this->postJson("/comment/$page->id", $comment->getAttributes());
45 $comment = $page->comments()->first();
46 $newHtml = '<p>updated text content</p>';
47 $resp = $this->putJson("/comment/$comment->id", [
51 $resp->assertStatus(200);
52 $resp->assertSee($newHtml, false);
53 $resp->assertDontSee($comment->html, false);
55 $this->assertDatabaseHas('comments', [
57 'entity_id' => $page->id,
60 $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
63 public function test_comment_delete()
66 $page = $this->entities->page();
68 $comment = Comment::factory()->make();
69 $this->postJson("/comment/$page->id", $comment->getAttributes());
71 $comment = $page->comments()->first();
73 $resp = $this->delete("/comment/$comment->id");
74 $resp->assertStatus(200);
76 $this->assertDatabaseMissing('comments', [
80 $this->assertActivityExists(ActivityType::COMMENT_DELETE);
83 public function test_scripts_cannot_be_injected_via_comment_html()
85 $page = $this->entities->page();
87 $script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
88 $this->asAdmin()->postJson("/comment/$page->id", [
92 $pageView = $this->get($page->getUrl());
93 $pageView->assertDontSee($script, false);
94 $pageView->assertSee('<p>My lovely comment</p>', false);
96 $comment = $page->comments()->first();
97 $this->putJson("/comment/$comment->id", [
98 'html' => $script . '<p>updated</p>',
101 $pageView = $this->get($page->getUrl());
102 $pageView->assertDontSee($script, false);
103 $pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
106 public function test_scripts_are_removed_even_if_already_in_db()
108 $page = $this->entities->page();
109 Comment::factory()->create([
110 'html' => '<script>superbadscript</script><p onclick="superbadonclick">scriptincommentest</p>',
111 'entity_type' => 'page', 'entity_id' => $page
114 $resp = $this->asAdmin()->get($page->getUrl());
115 $resp->assertSee('scriptincommentest', false);
116 $resp->assertDontSee('superbadscript', false);
117 $resp->assertDontSee('superbadonclick', false);
120 public function test_comment_html_is_limited()
122 $page = $this->entities->page();
123 $input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" data-a="b">a</a><section>Hello</section></p>';
124 $expected = '<p>Content<a href="#cat">a</a></p>';
126 $resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
128 $this->assertDatabaseHas('comments', [
129 'entity_type' => 'page',
130 'entity_id' => $page->id,
134 $comment = $page->comments()->first();
135 $resp = $this->put("/comment/{$comment->id}", ['html' => $input]);
137 $this->assertDatabaseHas('comments', [
138 'id' => $comment->id,
143 public function test_reply_comments_are_nested()
146 $page = $this->entities->page();
148 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
149 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
151 $respHtml = $this->withHtml($this->get($page->getUrl()));
152 $respHtml->assertElementCount('.comment-branch', 3);
153 $respHtml->assertElementNotExists('.comment-branch .comment-branch');
155 $comment = $page->comments()->first();
156 $resp = $this->postJson("/comment/$page->id", [
157 'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
159 $resp->assertStatus(200);
161 $respHtml = $this->withHtml($this->get($page->getUrl()));
162 $respHtml->assertElementCount('.comment-branch', 4);
163 $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
166 public function test_comments_are_visible_in_the_page_editor()
168 $page = $this->entities->page();
170 $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
172 $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
173 $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
176 public function test_comment_creator_name_truncated()
178 [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
179 $page = $this->entities->page();
181 $comment = Comment::factory()->make();
182 $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
184 $pageResp = $this->asAdmin()->get($page->getUrl());
185 $pageResp->assertSee('Wolfeschlegels…');
188 public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
190 $editor = $this->users->editor();
191 $page = $this->entities->page();
193 $resp = $this->actingAs($editor)->get($page->getUrl());
194 $resp->assertSee('tinymce.min.js?', false);
195 $resp->assertSee('window.editor_translations', false);
196 $resp->assertSee('component="entity-selector"', false);
198 $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
199 $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
201 $resp = $this->actingAs($editor)->get($page->getUrl());
202 $resp->assertDontSee('tinymce.min.js?', false);
203 $resp->assertDontSee('window.editor_translations', false);
204 $resp->assertDontSee('component="entity-selector"', false);
206 Comment::factory()->create([
207 'created_by' => $editor->id,
208 'entity_type' => 'page',
209 'entity_id' => $page->id,
212 $resp = $this->actingAs($editor)->get($page->getUrl());
213 $resp->assertSee('tinymce.min.js?', false);
214 $resp->assertSee('window.editor_translations', false);
215 $resp->assertSee('component="entity-selector"', false);
218 public function test_comment_displays_relative_times()
220 $page = $this->entities->page();
221 $comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
222 $comment->created_at = now()->subWeek();
223 $comment->updated_at = now()->subDay();
226 $pageResp = $this->asAdmin()->get($page->getUrl());
227 $html = $this->withHtml($pageResp);
229 // Create date shows relative time as text to user
230 $html->assertElementContains('.comment-box', 'commented 1 week ago');
231 // Updated indicator has full time as title
232 $html->assertElementContains('.comment-box span[title^="Updated ' . $comment->updated_at->format('Y-m-d') . '"]', 'Updated');