Skip to content

Commit 62f78f1

Browse files
committed
Comments: Split tests, added extra archive/reference tests
1 parent f8c0aaf commit 62f78f1

File tree

3 files changed

+160
-95
lines changed

3 files changed

+160
-95
lines changed

app/Activity/CommentRepo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function update(Comment $comment, string $html): Comment
6262
public function archive(Comment $comment): Comment
6363
{
6464
if ($comment->parent_id) {
65-
throw new NotifyException('Only top-level comments can be archived.');
65+
throw new NotifyException('Only top-level comments can be archived.', '/', 400);
6666
}
6767

6868
$comment->archived = true;
@@ -79,7 +79,7 @@ public function archive(Comment $comment): Comment
7979
public function unarchive(Comment $comment): Comment
8080
{
8181
if ($comment->parent_id) {
82-
throw new NotifyException('Only top-level comments can be un-archived.');
82+
throw new NotifyException('Only top-level comments can be un-archived.', '/', 400);
8383
}
8484

8585
$comment->archived = false;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace 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 CommentDisplayTest extends TestCase
11+
{
12+
public function test_reply_comments_are_nested()
13+
{
14+
$this->asAdmin();
15+
$page = $this->entities->page();
16+
17+
$this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
18+
$this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
19+
20+
$respHtml = $this->withHtml($this->get($page->getUrl()));
21+
$respHtml->assertElementCount('.comment-branch', 3);
22+
$respHtml->assertElementNotExists('.comment-branch .comment-branch');
23+
24+
$comment = $page->comments()->first();
25+
$resp = $this->postJson("/comment/$page->id", [
26+
'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
27+
]);
28+
$resp->assertStatus(200);
29+
30+
$respHtml = $this->withHtml($this->get($page->getUrl()));
31+
$respHtml->assertElementCount('.comment-branch', 4);
32+
$respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
33+
}
34+
35+
public function test_comments_are_visible_in_the_page_editor()
36+
{
37+
$page = $this->entities->page();
38+
39+
$this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
40+
41+
$respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
42+
$respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
43+
}
44+
45+
public function test_comment_creator_name_truncated()
46+
{
47+
[$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
48+
$page = $this->entities->page();
49+
50+
$comment = Comment::factory()->make();
51+
$this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
52+
53+
$pageResp = $this->asAdmin()->get($page->getUrl());
54+
$pageResp->assertSee('Wolfeschlegels…');
55+
}
56+
57+
public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
58+
{
59+
$editor = $this->users->editor();
60+
$page = $this->entities->page();
61+
62+
$resp = $this->actingAs($editor)->get($page->getUrl());
63+
$resp->assertSee('tinymce.min.js?', false);
64+
$resp->assertSee('window.editor_translations', false);
65+
$resp->assertSee('component="entity-selector"', false);
66+
67+
$this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
68+
$this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
69+
70+
$resp = $this->actingAs($editor)->get($page->getUrl());
71+
$resp->assertDontSee('tinymce.min.js?', false);
72+
$resp->assertDontSee('window.editor_translations', false);
73+
$resp->assertDontSee('component="entity-selector"', false);
74+
75+
Comment::factory()->create([
76+
'created_by' => $editor->id,
77+
'entity_type' => 'page',
78+
'entity_id' => $page->id,
79+
]);
80+
81+
$resp = $this->actingAs($editor)->get($page->getUrl());
82+
$resp->assertSee('tinymce.min.js?', false);
83+
$resp->assertSee('window.editor_translations', false);
84+
$resp->assertSee('component="entity-selector"', false);
85+
}
86+
87+
public function test_comment_displays_relative_times()
88+
{
89+
$page = $this->entities->page();
90+
$comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
91+
$comment->created_at = now()->subWeek();
92+
$comment->updated_at = now()->subDay();
93+
$comment->save();
94+
95+
$pageResp = $this->asAdmin()->get($page->getUrl());
96+
$html = $this->withHtml($pageResp);
97+
98+
// Create date shows relative time as text to user
99+
$html->assertElementContains('.comment-box', 'commented 1 week ago');
100+
// Updated indicator has full time as title
101+
$html->assertElementContains('.comment-box span[title^="Updated ' . $comment->updated_at->format('Y-m-d') . '"]', 'Updated');
102+
}
103+
104+
public function test_comment_displays_reference_if_set()
105+
{
106+
$page = $this->entities->page();
107+
$comment = Comment::factory()->make([
108+
'content_ref' => 'bkmrk-a:abc:4-1',
109+
'local_id' => 10,
110+
]);
111+
$page->comments()->save($comment);
112+
113+
$html = $this->withHtml($this->asEditor()->get($page->getUrl()));
114+
$html->assertElementExists('#comment10 .comment-reference-indicator-wrap a');
115+
}
116+
117+
public function test_archived_comments_are_shown_in_their_own_container()
118+
{
119+
$page = $this->entities->page();
120+
$comment = Comment::factory()->make(['local_id' => 44]);
121+
$page->comments()->save($comment);
122+
123+
$html = $this->withHtml($this->asEditor()->get($page->getUrl()));
124+
$html->assertElementExists('#comment-tab-panel-active #comment44');
125+
$html->assertElementNotExists('#comment-tab-panel-archived .comment-box');
126+
127+
$comment->archived = true;
128+
$comment->save();
129+
130+
$html = $this->withHtml($this->asEditor()->get($page->getUrl()));
131+
$html->assertElementExists('#comment-tab-panel-archived #comment44.comment-box');
132+
$html->assertElementNotExists('#comment-tab-panel-active #comment44');
133+
}
134+
}
Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use BookStack\Entities\Models\Page;
88
use Tests\TestCase;
99

10-
class CommentTest extends TestCase
10+
class CommentStoreTest extends TestCase
1111
{
1212
public function test_add_comment()
1313
{
@@ -166,6 +166,29 @@ public function test_archive_endpoints_require_delete_or_edit_permissions()
166166
}
167167
}
168168

169+
public function test_non_top_level_comments_cant_be_archived_or_unarchived()
170+
{
171+
$this->asAdmin();
172+
$page = $this->entities->page();
173+
174+
$comment = Comment::factory()->make();
175+
$page->comments()->save($comment);
176+
$subComment = Comment::factory()->make(['parent_id' => $comment->id]);
177+
$page->comments()->save($subComment);
178+
$subComment->refresh();
179+
180+
$resp = $this->putJson("/comment/$subComment->id/archive");
181+
$resp->assertStatus(400);
182+
183+
$this->assertDatabaseHas('comments', [
184+
'id' => $subComment->id,
185+
'archived' => false,
186+
]);
187+
188+
$resp = $this->putJson("/comment/$subComment->id/unarchive");
189+
$resp->assertStatus(400);
190+
}
191+
169192
public function test_scripts_cannot_be_injected_via_comment_html()
170193
{
171194
$page = $this->entities->page();
@@ -225,96 +248,4 @@ public function test_comment_html_is_limited()
225248
'html' => $expected,
226249
]);
227250
}
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-
}
320251
}

0 commit comments

Comments
 (0)