3 namespace Tests\Entity;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Comment;
7 use BookStack\Entities\Models\Page;
10 class CommentTest extends TestCase
12 public function test_add_comment()
15 $page = $this->entities->page();
17 $comment = Comment::factory()->make(['parent_id' => 2]);
18 $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
20 $resp->assertStatus(200);
21 $resp->assertSee($comment->text);
23 $pageResp = $this->get($page->getUrl());
24 $pageResp->assertSee($comment->text);
26 $this->assertDatabaseHas('comments', [
28 'entity_id' => $page->id,
29 'entity_type' => Page::newModelInstance()->getMorphClass(),
30 'text' => $comment->text,
34 $this->assertActivityExists(ActivityType::COMMENT_CREATE);
37 public function test_comment_edit()
40 $page = $this->entities->page();
42 $comment = Comment::factory()->make();
43 $this->postJson("/comment/$page->id", $comment->getAttributes());
45 $comment = $page->comments()->first();
46 $newText = 'updated text content';
47 $resp = $this->putJson("/comment/$comment->id", [
51 $resp->assertStatus(200);
52 $resp->assertSee($newText);
53 $resp->assertDontSee($comment->text);
55 $this->assertDatabaseHas('comments', [
57 'entity_id' => $page->id,
60 $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
63 public function test_comment_delete()
66 $page = $this->entities->page();
68 $comment = Comment::factory()->make();
69 $this->postJson("/comment/$page->id", $comment->getAttributes());
71 $comment = $page->comments()->first();
73 $resp = $this->delete("/comment/$comment->id");
74 $resp->assertStatus(200);
76 $this->assertDatabaseMissing('comments', [
80 $this->assertActivityExists(ActivityType::COMMENT_DELETE);
83 public function test_comments_converts_markdown_input_to_html()
85 $page = $this->entities->page();
86 $this->asAdmin()->postJson("/comment/$page->id", [
87 'text' => '# My Title',
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",
97 $pageView = $this->get($page->getUrl());
98 $pageView->assertSee('<h1>My Title</h1>', false);
101 public function test_html_cannot_be_injected_via_comment_content()
104 $page = $this->entities->page();
106 $script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
107 $this->postJson("/comment/$page->id", [
111 $pageView = $this->get($page->getUrl());
112 $pageView->assertDontSee($script, false);
113 $pageView->assertSee('sometextinthecomment');
115 $comment = $page->comments()->first();
116 $this->putJson("/comment/$comment->id", [
117 'text' => $script . 'updated',
120 $pageView = $this->get($page->getUrl());
121 $pageView->assertDontSee($script, false);
122 $pageView->assertSee('sometextinthecommentupdated');
125 public function test_reply_comments_are_nested()
128 $page = $this->entities->page();
130 $this->postJson("/comment/$page->id", ['text' => 'My new comment']);
131 $this->postJson("/comment/$page->id", ['text' => 'My new comment']);
133 $respHtml = $this->withHtml($this->get($page->getUrl()));
134 $respHtml->assertElementCount('.comment-branch', 3);
135 $respHtml->assertElementNotExists('.comment-branch .comment-branch');
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);
141 $respHtml = $this->withHtml($this->get($page->getUrl()));
142 $respHtml->assertElementCount('.comment-branch', 4);
143 $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
146 public function test_comments_are_visible_in_the_page_editor()
148 $page = $this->entities->page();
150 $this->asAdmin()->postJson("/comment/$page->id", ['text' => 'My great comment to see in the editor']);
152 $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
153 $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
156 public function test_comment_creator_name_truncated()
158 [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
159 $page = $this->entities->page();
161 $comment = Comment::factory()->make();
162 $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
164 $pageResp = $this->asAdmin()->get($page->getUrl());
165 $pageResp->assertSee('Wolfeschlegels…');