]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentDisplayTest.php
Tests: Updated comment test to account for new editor usage
[bookstack] / tests / Entity / CommentDisplayTest.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 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('window.editor_translations', false);
64         $resp->assertSee('component="entity-selector"', false);
65
66         $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
67         $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
68
69         $resp = $this->actingAs($editor)->get($page->getUrl());
70         $resp->assertDontSee('window.editor_translations', false);
71         $resp->assertDontSee('component="entity-selector"', false);
72
73         Comment::factory()->create([
74             'created_by'  => $editor->id,
75             'entity_type' => 'page',
76             'entity_id'   => $page->id,
77         ]);
78
79         $resp = $this->actingAs($editor)->get($page->getUrl());
80         $resp->assertSee('window.editor_translations', false);
81         $resp->assertSee('component="entity-selector"', false);
82     }
83
84     public function test_comment_displays_relative_times()
85     {
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();
90         $comment->save();
91
92         $pageResp = $this->asAdmin()->get($page->getUrl());
93         $html = $this->withHtml($pageResp);
94
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');
99     }
100
101     public function test_comment_displays_reference_if_set()
102     {
103         $page = $this->entities->page();
104         $comment = Comment::factory()->make([
105             'content_ref' => 'bkmrk-a:abc:4-1',
106             'local_id'   =>  10,
107         ]);
108         $page->comments()->save($comment);
109
110         $html = $this->withHtml($this->asEditor()->get($page->getUrl()));
111         $html->assertElementExists('#comment10 .comment-reference-indicator-wrap a');
112     }
113
114     public function test_archived_comments_are_shown_in_their_own_container()
115     {
116         $page = $this->entities->page();
117         $comment = Comment::factory()->make(['local_id' => 44]);
118         $page->comments()->save($comment);
119
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');
123
124         $comment->archived = true;
125         $comment->save();
126
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');
130     }
131 }