]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentDisplayTest.php
Merge branch 'development' of github.com:BookStackApp/BookStack into development
[bookstack] / tests / Entity / CommentDisplayTest.php
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 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.