]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentTest.php
Comments: Split out page comment reference logic to own component
[bookstack] / tests / Entity / CommentTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Comment;
7 use BookStack\Entities\Models\Page;
8 use Tests\TestCase;
9
10 class CommentTest extends TestCase
11 {
12     public function test_add_comment()
13     {
14         $this->asAdmin();
15         $page = $this->entities->page();
16
17         $comment = Comment::factory()->make(['parent_id' => 2]);
18         $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
19
20         $resp->assertStatus(200);
21         $resp->assertSee($comment->html, false);
22
23         $pageResp = $this->get($page->getUrl());
24         $pageResp->assertSee($comment->html, false);
25
26         $this->assertDatabaseHas('comments', [
27             'local_id'    => 1,
28             'entity_id'   => $page->id,
29             'entity_type' => Page::newModelInstance()->getMorphClass(),
30             'text'        => null,
31             'parent_id'   => 2,
32         ]);
33
34         $this->assertActivityExists(ActivityType::COMMENT_CREATE);
35     }
36     public function test_add_comment_stores_content_reference_only_if_format_valid()
37     {
38         $validityByRefs = [
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,
44         ];
45
46         $page = $this->entities->page();
47
48         foreach ($validityByRefs as $ref => $valid) {
49             $this->asAdmin()->postJson("/comment/$page->id", [
50                 'html' => '<p>My comment</p>',
51                 'parent_id' => null,
52                 'content_ref' => $ref,
53             ]);
54
55             if ($valid) {
56                 $this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
57             } else {
58                 $this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
59             }
60         }
61     }
62
63     public function test_comment_edit()
64     {
65         $this->asAdmin();
66         $page = $this->entities->page();
67
68         $comment = Comment::factory()->make();
69         $this->postJson("/comment/$page->id", $comment->getAttributes());
70
71         $comment = $page->comments()->first();
72         $newHtml = '<p>updated text content</p>';
73         $resp = $this->putJson("/comment/$comment->id", [
74             'html' => $newHtml,
75         ]);
76
77         $resp->assertStatus(200);
78         $resp->assertSee($newHtml, false);
79         $resp->assertDontSee($comment->html, false);
80
81         $this->assertDatabaseHas('comments', [
82             'html'      => $newHtml,
83             'entity_id' => $page->id,
84         ]);
85
86         $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
87     }
88
89     public function test_comment_delete()
90     {
91         $this->asAdmin();
92         $page = $this->entities->page();
93
94         $comment = Comment::factory()->make();
95         $this->postJson("/comment/$page->id", $comment->getAttributes());
96
97         $comment = $page->comments()->first();
98
99         $resp = $this->delete("/comment/$comment->id");
100         $resp->assertStatus(200);
101
102         $this->assertDatabaseMissing('comments', [
103             'id' => $comment->id,
104         ]);
105
106         $this->assertActivityExists(ActivityType::COMMENT_DELETE);
107     }
108
109     public function test_scripts_cannot_be_injected_via_comment_html()
110     {
111         $page = $this->entities->page();
112
113         $script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
114         $this->asAdmin()->postJson("/comment/$page->id", [
115             'html' => $script,
116         ]);
117
118         $pageView = $this->get($page->getUrl());
119         $pageView->assertDontSee($script, false);
120         $pageView->assertSee('<p>My lovely comment</p>', false);
121
122         $comment = $page->comments()->first();
123         $this->putJson("/comment/$comment->id", [
124             'html' => $script . '<p>updated</p>',
125         ]);
126
127         $pageView = $this->get($page->getUrl());
128         $pageView->assertDontSee($script, false);
129         $pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
130     }
131
132     public function test_scripts_are_removed_even_if_already_in_db()
133     {
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
138         ]);
139
140         $resp = $this->asAdmin()->get($page->getUrl());
141         $resp->assertSee('scriptincommentest', false);
142         $resp->assertDontSee('superbadscript', false);
143         $resp->assertDontSee('superbadonclick', false);
144     }
145
146     public function test_comment_html_is_limited()
147     {
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>';
151
152         $resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
153         $resp->assertOk();
154         $this->assertDatabaseHas('comments', [
155            'entity_type' => 'page',
156            'entity_id' => $page->id,
157            'html' => $expected,
158         ]);
159
160         $comment = $page->comments()->first();
161         $resp = $this->put("/comment/{$comment->id}", ['html' => $input]);
162         $resp->assertOk();
163         $this->assertDatabaseHas('comments', [
164             'id'   => $comment->id,
165             'html' => $expected,
166         ]);
167     }
168
169     public function test_reply_comments_are_nested()
170     {
171         $this->asAdmin();
172         $page = $this->entities->page();
173
174         $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
175         $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
176
177         $respHtml = $this->withHtml($this->get($page->getUrl()));
178         $respHtml->assertElementCount('.comment-branch', 3);
179         $respHtml->assertElementNotExists('.comment-branch .comment-branch');
180
181         $comment = $page->comments()->first();
182         $resp = $this->postJson("/comment/$page->id", [
183             'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
184         ]);
185         $resp->assertStatus(200);
186
187         $respHtml = $this->withHtml($this->get($page->getUrl()));
188         $respHtml->assertElementCount('.comment-branch', 4);
189         $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
190     }
191
192     public function test_comments_are_visible_in_the_page_editor()
193     {
194         $page = $this->entities->page();
195
196         $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
197
198         $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
199         $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
200     }
201
202     public function test_comment_creator_name_truncated()
203     {
204         [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
205         $page = $this->entities->page();
206
207         $comment = Comment::factory()->make();
208         $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
209
210         $pageResp = $this->asAdmin()->get($page->getUrl());
211         $pageResp->assertSee('Wolfeschlegels…');
212     }
213
214     public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
215     {
216         $editor = $this->users->editor();
217         $page = $this->entities->page();
218
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);
223
224         $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
225         $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
226
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);
231
232         Comment::factory()->create([
233             'created_by'  => $editor->id,
234             'entity_type' => 'page',
235             'entity_id'   => $page->id,
236         ]);
237
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);
242     }
243
244     public function test_comment_displays_relative_times()
245     {
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();
250         $comment->save();
251
252         $pageResp = $this->asAdmin()->get($page->getUrl());
253         $html = $this->withHtml($pageResp);
254
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');
259     }
260 }