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('tinymce.min.js?', false);
64 $resp->assertSee('window.editor_translations', false);
65 $resp->assertSee('component="entity-selector"', false);
67 $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
68 $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
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);
75 Comment::factory()->create([
76 'created_by' => $editor->id,
77 'entity_type' => 'page',
78 'entity_id' => $page->id,
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);
87 public function test_comment_displays_relative_times()
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();
95 $pageResp = $this->asAdmin()->get($page->getUrl());
96 $html = $this->withHtml($pageResp);
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');
104 public function test_comment_displays_reference_if_set()
106 $page = $this->entities->page();
107 $comment = Comment::factory()->make([
108 'content_ref' => 'bkmrk-a:abc:4-1',
111 $page->comments()->save($comment);
113 $html = $this->withHtml($this->asEditor()->get($page->getUrl()));
114 $html->assertElementExists('#comment10 .comment-reference-indicator-wrap a');
117 public function test_archived_comments_are_shown_in_their_own_container()
119 $page = $this->entities->page();
120 $comment = Comment::factory()->make(['local_id' => 44]);
121 $page->comments()->save($comment);
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');
127 $comment->archived = true;
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');