]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentTest.php
Merge branch 'Bajszi97/development' into development
[bookstack] / tests / Entity / CommentTest.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 CommentTest extends TestCase
11 {
12     public function test_add_comment()
13     {
14         $this->asAdmin();
15         $page = $this->entities->page();
16
17         $comment = Comment::factory()->make(['parent_id' => 2]);
18         $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
19
20         $resp->assertStatus(200);
21         $resp->assertSee($comment->text);
22
23         $pageResp = $this->get($page->getUrl());
24         $pageResp->assertSee($comment->text);
25
26         $this->assertDatabaseHas('comments', [
27             'local_id'    => 1,
28             'entity_id'   => $page->id,
29             'entity_type' => Page::newModelInstance()->getMorphClass(),
30             'text'        => $comment->text,
31             'parent_id'   => 2,
32         ]);
33
34         $this->assertActivityExists(ActivityType::COMMENT_CREATE);
35     }
36
37     public function test_comment_edit()
38     {
39         $this->asAdmin();
40         $page = $this->entities->page();
41
42         $comment = Comment::factory()->make();
43         $this->postJson("/comment/$page->id", $comment->getAttributes());
44
45         $comment = $page->comments()->first();
46         $newText = 'updated text content';
47         $resp = $this->putJson("/comment/$comment->id", [
48             'text' => $newText,
49         ]);
50
51         $resp->assertStatus(200);
52         $resp->assertSee($newText);
53         $resp->assertDontSee($comment->text);
54
55         $this->assertDatabaseHas('comments', [
56             'text'      => $newText,
57             'entity_id' => $page->id,
58         ]);
59
60         $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
61     }
62
63     public function test_comment_delete()
64     {
65         $this->asAdmin();
66         $page = $this->entities->page();
67
68         $comment = Comment::factory()->make();
69         $this->postJson("/comment/$page->id", $comment->getAttributes());
70
71         $comment = $page->comments()->first();
72
73         $resp = $this->delete("/comment/$comment->id");
74         $resp->assertStatus(200);
75
76         $this->assertDatabaseMissing('comments', [
77             'id' => $comment->id,
78         ]);
79
80         $this->assertActivityExists(ActivityType::COMMENT_DELETE);
81     }
82
83     public function test_comments_converts_markdown_input_to_html()
84     {
85         $page = $this->entities->page();
86         $this->asAdmin()->postJson("/comment/$page->id", [
87             'text' => '# My Title',
88         ]);
89
90         $this->assertDatabaseHas('comments', [
91             'entity_id'   => $page->id,
92             'entity_type' => $page->getMorphClass(),
93             'text'        => '# My Title',
94             'html'        => "<h1>My Title</h1>\n",
95         ]);
96
97         $pageView = $this->get($page->getUrl());
98         $pageView->assertSee('<h1>My Title</h1>', false);
99     }
100
101     public function test_html_cannot_be_injected_via_comment_content()
102     {
103         $this->asAdmin();
104         $page = $this->entities->page();
105
106         $script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
107         $this->postJson("/comment/$page->id", [
108             'text' => $script,
109         ]);
110
111         $pageView = $this->get($page->getUrl());
112         $pageView->assertDontSee($script, false);
113         $pageView->assertSee('sometextinthecomment');
114
115         $comment = $page->comments()->first();
116         $this->putJson("/comment/$comment->id", [
117             'text' => $script . 'updated',
118         ]);
119
120         $pageView = $this->get($page->getUrl());
121         $pageView->assertDontSee($script, false);
122         $pageView->assertSee('sometextinthecommentupdated');
123     }
124
125     public function test_reply_comments_are_nested()
126     {
127         $this->asAdmin();
128         $page = $this->entities->page();
129
130         $this->postJson("/comment/$page->id", ['text' => 'My new comment']);
131         $this->postJson("/comment/$page->id", ['text' => 'My new comment']);
132
133         $respHtml = $this->withHtml($this->get($page->getUrl()));
134         $respHtml->assertElementCount('.comment-branch', 3);
135         $respHtml->assertElementNotExists('.comment-branch .comment-branch');
136
137         $comment = $page->comments()->first();
138         $resp = $this->postJson("/comment/$page->id", ['text' => 'My nested comment', 'parent_id' => $comment->local_id]);
139         $resp->assertStatus(200);
140
141         $respHtml = $this->withHtml($this->get($page->getUrl()));
142         $respHtml->assertElementCount('.comment-branch', 4);
143         $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
144     }
145
146     public function test_comments_are_visible_in_the_page_editor()
147     {
148         $page = $this->entities->page();
149
150         $this->asAdmin()->postJson("/comment/$page->id", ['text' => 'My great comment to see in the editor']);
151
152         $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
153         $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
154     }
155
156     public function test_comment_creator_name_truncated()
157     {
158         [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
159         $page = $this->entities->page();
160
161         $comment = Comment::factory()->make();
162         $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
163
164         $pageResp = $this->asAdmin()->get($page->getUrl());
165         $pageResp->assertSee('Wolfeschlegels…');
166     }
167 }