]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentTest.php
Comments: Checked content/arhived comment styles in dark mode
[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_comment_archive_and_unarchive()
110     {
111         $this->asAdmin();
112         $page = $this->entities->page();
113
114         $comment = Comment::factory()->make();
115         $page->comments()->save($comment);
116         $comment->refresh();
117
118         $this->put("/comment/$comment->id/archive");
119
120         $this->assertDatabaseHas('comments', [
121             'id' => $comment->id,
122             'archived' => true,
123         ]);
124
125         $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
126
127         $this->put("/comment/$comment->id/unarchive");
128
129         $this->assertDatabaseHas('comments', [
130             'id' => $comment->id,
131             'archived' => false,
132         ]);
133
134         $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
135     }
136
137     public function test_archive_endpoints_require_delete_or_edit_permissions()
138     {
139         $viewer = $this->users->viewer();
140         $page = $this->entities->page();
141
142         $comment = Comment::factory()->make();
143         $page->comments()->save($comment);
144         $comment->refresh();
145
146         $endpoints = ["/comment/$comment->id/archive", "/comment/$comment->id/unarchive"];
147
148         foreach ($endpoints as $endpoint) {
149             $resp = $this->actingAs($viewer)->put($endpoint);
150             $this->assertPermissionError($resp);
151         }
152
153         $this->permissions->grantUserRolePermissions($viewer, ['comment-delete-all']);
154
155         foreach ($endpoints as $endpoint) {
156             $resp = $this->actingAs($viewer)->put($endpoint);
157             $resp->assertOk();
158         }
159
160         $this->permissions->removeUserRolePermissions($viewer, ['comment-delete-all']);
161         $this->permissions->grantUserRolePermissions($viewer, ['comment-update-all']);
162
163         foreach ($endpoints as $endpoint) {
164             $resp = $this->actingAs($viewer)->put($endpoint);
165             $resp->assertOk();
166         }
167     }
168
169     public function test_scripts_cannot_be_injected_via_comment_html()
170     {
171         $page = $this->entities->page();
172
173         $script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
174         $this->asAdmin()->postJson("/comment/$page->id", [
175             'html' => $script,
176         ]);
177
178         $pageView = $this->get($page->getUrl());
179         $pageView->assertDontSee($script, false);
180         $pageView->assertSee('<p>My lovely comment</p>', false);
181
182         $comment = $page->comments()->first();
183         $this->putJson("/comment/$comment->id", [
184             'html' => $script . '<p>updated</p>',
185         ]);
186
187         $pageView = $this->get($page->getUrl());
188         $pageView->assertDontSee($script, false);
189         $pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
190     }
191
192     public function test_scripts_are_removed_even_if_already_in_db()
193     {
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
198         ]);
199
200         $resp = $this->asAdmin()->get($page->getUrl());
201         $resp->assertSee('scriptincommentest', false);
202         $resp->assertDontSee('superbadscript', false);
203         $resp->assertDontSee('superbadonclick', false);
204     }
205
206     public function test_comment_html_is_limited()
207     {
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>';
211
212         $resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
213         $resp->assertOk();
214         $this->assertDatabaseHas('comments', [
215            'entity_type' => 'page',
216            'entity_id' => $page->id,
217            'html' => $expected,
218         ]);
219
220         $comment = $page->comments()->first();
221         $resp = $this->put("/comment/{$comment->id}", ['html' => $input]);
222         $resp->assertOk();
223         $this->assertDatabaseHas('comments', [
224             'id'   => $comment->id,
225             'html' => $expected,
226         ]);
227     }
228
229     public function test_reply_comments_are_nested()
230     {
231         $this->asAdmin();
232         $page = $this->entities->page();
233
234         $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
235         $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
236
237         $respHtml = $this->withHtml($this->get($page->getUrl()));
238         $respHtml->assertElementCount('.comment-branch', 3);
239         $respHtml->assertElementNotExists('.comment-branch .comment-branch');
240
241         $comment = $page->comments()->first();
242         $resp = $this->postJson("/comment/$page->id", [
243             'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
244         ]);
245         $resp->assertStatus(200);
246
247         $respHtml = $this->withHtml($this->get($page->getUrl()));
248         $respHtml->assertElementCount('.comment-branch', 4);
249         $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
250     }
251
252     public function test_comments_are_visible_in_the_page_editor()
253     {
254         $page = $this->entities->page();
255
256         $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
257
258         $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
259         $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
260     }
261
262     public function test_comment_creator_name_truncated()
263     {
264         [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
265         $page = $this->entities->page();
266
267         $comment = Comment::factory()->make();
268         $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
269
270         $pageResp = $this->asAdmin()->get($page->getUrl());
271         $pageResp->assertSee('Wolfeschlegels…');
272     }
273
274     public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
275     {
276         $editor = $this->users->editor();
277         $page = $this->entities->page();
278
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);
283
284         $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
285         $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
286
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);
291
292         Comment::factory()->create([
293             'created_by'  => $editor->id,
294             'entity_type' => 'page',
295             'entity_id'   => $page->id,
296         ]);
297
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);
302     }
303
304     public function test_comment_displays_relative_times()
305     {
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();
310         $comment->save();
311
312         $pageResp = $this->asAdmin()->get($page->getUrl());
313         $html = $this->withHtml($pageResp);
314
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');
319     }
320 }