3 namespace Tests\Entity;
5 use BookStack\Actions\Comment;
6 use BookStack\Entities\Models\Page;
9 class CommentTest extends TestCase
11 public function test_add_comment()
14 $page = Page::first();
16 $comment = Comment::factory()->make(['parent_id' => 2]);
17 $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
19 $resp->assertStatus(200);
20 $resp->assertSee($comment->text);
22 $pageResp = $this->get($page->getUrl());
23 $pageResp->assertSee($comment->text);
25 $this->assertDatabaseHas('comments', [
27 'entity_id' => $page->id,
28 'entity_type' => Page::newModelInstance()->getMorphClass(),
29 'text' => $comment->text,
34 public function test_comment_edit()
37 $page = Page::first();
39 $comment = Comment::factory()->make();
40 $this->postJson("/comment/$page->id", $comment->getAttributes());
42 $comment = $page->comments()->first();
43 $newText = 'updated text content';
44 $resp = $this->putJson("/comment/$comment->id", [
48 $resp->assertStatus(200);
49 $resp->assertSee($newText);
50 $resp->assertDontSee($comment->text);
52 $this->assertDatabaseHas('comments', [
54 'entity_id' => $page->id,
58 public function test_comment_delete()
61 $page = Page::first();
63 $comment = Comment::factory()->make();
64 $this->postJson("/comment/$page->id", $comment->getAttributes());
66 $comment = $page->comments()->first();
68 $resp = $this->delete("/comment/$comment->id");
69 $resp->assertStatus(200);
71 $this->assertDatabaseMissing('comments', [
76 public function test_comments_converts_markdown_input_to_html()
78 $page = Page::first();
79 $this->asAdmin()->postJson("/comment/$page->id", [
80 'text' => '# My Title',
83 $this->assertDatabaseHas('comments', [
84 'entity_id' => $page->id,
85 'entity_type' => $page->getMorphClass(),
86 'text' => '# My Title',
87 'html' => "<h1>My Title</h1>\n",
90 $pageView = $this->get($page->getUrl());
91 $pageView->assertSee('<h1>My Title</h1>', false);
94 public function test_html_cannot_be_injected_via_comment_content()
97 $page = Page::first();
99 $script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
100 $this->postJson("/comment/$page->id", [
104 $pageView = $this->get($page->getUrl());
105 $pageView->assertDontSee($script, false);
106 $pageView->assertSee('sometextinthecomment');
108 $comment = $page->comments()->first();
109 $this->putJson("/comment/$comment->id", [
110 'text' => $script . 'updated',
113 $pageView = $this->get($page->getUrl());
114 $pageView->assertDontSee($script, false);
115 $pageView->assertSee('sometextinthecommentupdated');