3 namespace Tests\Entity;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Comment;
7 use BookStack\Entities\Models\Page;
10 class CommentDisplayTest extends TestCase
12 public function test_reply_comments_are_nested()
15 $page = $this->entities->page();
17 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
18 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
20 $respHtml = $this->withHtml($this->get($page->getUrl()));
21 $respHtml->assertElementCount('.comment-branch', 3);
22 $respHtml->assertElementNotExists('.comment-branch .comment-branch');
24 $comment = $page->comments()->first();
25 $resp = $this->postJson("/comment/$page->id", [
26 'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
28 $resp->assertStatus(200);
30 $respHtml = $this->withHtml($this->get($page->getUrl()));
31 $respHtml->assertElementCount('.comment-branch', 4);
32 $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
35 public function test_comments_are_visible_in_the_page_editor()
37 $page = $this->entities->page();
39 $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
41 $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
42 $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
45 public function test_comment_creator_name_truncated()
47 [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
48 $page = $this->entities->page();
50 $comment = Comment::factory()->make();
51 $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
53 $pageResp = $this->asAdmin()->get($page->getUrl());
54 $pageResp->assertSee('Wolfeschlegels…');
57 public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
59 $editor = $this->users->editor();
60 $page = $this->entities->page();
62 $resp = $this->actingAs($editor)->get($page->getUrl());
63 $resp->assertSee('window.editor_translations', false);
64 $resp->assertSee('component="entity-selector"', false);
66 $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
67 $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
69 $resp = $this->actingAs($editor)->get($page->getUrl());
70 $resp->assertDontSee('window.editor_translations', false);
71 $resp->assertDontSee('component="entity-selector"', false);
73 Comment::factory()->create([
74 'created_by' => $editor->id,
75 'entity_type' => 'page',
76 'entity_id' => $page->id,
79 $resp = $this->actingAs($editor)->get($page->getUrl());
80 $resp->assertSee('window.editor_translations', false);
81 $resp->assertSee('component="entity-selector"', false);
84 public function test_comment_displays_relative_times()
86 $page = $this->entities->page();
87 $comment = Comment::factory()->create(['entity_id' => $page->id, 'entity_type' => $page->getMorphClass()]);
88 $comment->created_at = now()->subWeek();
89 $comment->updated_at = now()->subDay();
92 $pageResp = $this->asAdmin()->get($page->getUrl());
93 $html = $this->withHtml($pageResp);
95 // Create date shows relative time as text to user
96 $html->assertElementContains('.comment-box', 'commented 1 week ago');
97 // Updated indicator has full time as title
98 $html->assertElementContains('.comment-box span[title^="Updated ' . $comment->updated_at->format('Y-m-d') . '"]', 'Updated');
101 public function test_comment_displays_reference_if_set()
103 $page = $this->entities->page();
104 $comment = Comment::factory()->make([
105 'content_ref' => 'bkmrk-a:abc:4-1',
108 $page->comments()->save($comment);
110 $html = $this->withHtml($this->asEditor()->get($page->getUrl()));
111 $html->assertElementExists('#comment10 .comment-reference-indicator-wrap a');
114 public function test_archived_comments_are_shown_in_their_own_container()
116 $page = $this->entities->page();
117 $comment = Comment::factory()->make(['local_id' => 44]);
118 $page->comments()->save($comment);
120 $html = $this->withHtml($this->asEditor()->get($page->getUrl()));
121 $html->assertElementExists('#comment-tab-panel-active #comment44');
122 $html->assertElementNotExists('#comment-tab-panel-archived .comment-box');
124 $comment->archived = true;
127 $html = $this->withHtml($this->asEditor()->get($page->getUrl()));
128 $html->assertElementExists('#comment-tab-panel-archived #comment44.comment-box');
129 $html->assertElementNotExists('#comment-tab-panel-active #comment44');