1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Models\Page;
4 use BookStack\Actions\Comment;
7 class CommentTest extends TestCase
10 public function test_add_comment()
13 $page = Page::first();
15 $comment = factory(Comment::class)->make(['parent_id' => 2]);
16 $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
18 $resp->assertStatus(200);
19 $resp->assertSee($comment->text);
21 $pageResp = $this->get($page->getUrl());
22 $pageResp->assertSee($comment->text);
24 $this->assertDatabaseHas('comments', [
26 'entity_id' => $page->id,
27 'entity_type' => Page::newModelInstance()->getMorphClass(),
28 'text' => $comment->text,
33 public function test_comment_edit()
36 $page = Page::first();
38 $comment = factory(Comment::class)->make();
39 $this->postJson("/comment/$page->id", $comment->getAttributes());
41 $comment = $page->comments()->first();
42 $newText = 'updated text content';
43 $resp = $this->putJson("/comment/$comment->id", [
47 $resp->assertStatus(200);
48 $resp->assertSee($newText);
49 $resp->assertDontSee($comment->text);
51 $this->assertDatabaseHas('comments', [
53 'entity_id' => $page->id
57 public function test_comment_delete()
60 $page = Page::first();
62 $comment = factory(Comment::class)->make();
63 $this->postJson("/comment/$page->id", $comment->getAttributes());
65 $comment = $page->comments()->first();
67 $resp = $this->delete("/comment/$comment->id");
68 $resp->assertStatus(200);
70 $this->assertDatabaseMissing('comments', [
75 public function test_comments_converts_markdown_input_to_html()
77 $page = Page::first();
78 $this->asAdmin()->postJson("/comment/$page->id", [
79 'text' => '# My Title',
82 $this->assertDatabaseHas('comments', [
83 'entity_id' => $page->id,
84 'entity_type' => $page->getMorphClass(),
85 'text' => '# My Title',
86 'html' => "<h1>My Title</h1>\n",
89 $pageView = $this->get($page->getUrl());
90 $pageView->assertSee('<h1>My Title</h1>');
93 public function test_html_cannot_be_injected_via_comment_content()
96 $page = Page::first();
98 $script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
99 $this->postJson("/comment/$page->id", [
103 $pageView = $this->get($page->getUrl());
104 $pageView->assertDontSee($script);
105 $pageView->assertSee('sometextinthecomment');
107 $comment = $page->comments()->first();
108 $this->putJson("/comment/$comment->id", [
109 'text' => $script . 'updated',
112 $pageView = $this->get($page->getUrl());
113 $pageView->assertDontSee($script);
114 $pageView->assertSee('sometextinthecommentupdated');