]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentTest.php
Skip intermediate login page with single provider
[bookstack] / tests / Entity / CommentTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Actions\Comment;
6 use BookStack\Entities\Models\Page;
7 use Tests\TestCase;
8
9 class CommentTest extends TestCase
10 {
11     public function test_add_comment()
12     {
13         $this->asAdmin();
14         $page = Page::first();
15
16         $comment = Comment::factory()->make(['parent_id' => 2]);
17         $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
18
19         $resp->assertStatus(200);
20         $resp->assertSee($comment->text);
21
22         $pageResp = $this->get($page->getUrl());
23         $pageResp->assertSee($comment->text);
24
25         $this->assertDatabaseHas('comments', [
26             'local_id'    => 1,
27             'entity_id'   => $page->id,
28             'entity_type' => Page::newModelInstance()->getMorphClass(),
29             'text'        => $comment->text,
30             'parent_id'   => 2,
31         ]);
32     }
33
34     public function test_comment_edit()
35     {
36         $this->asAdmin();
37         $page = Page::first();
38
39         $comment = Comment::factory()->make();
40         $this->postJson("/comment/$page->id", $comment->getAttributes());
41
42         $comment = $page->comments()->first();
43         $newText = 'updated text content';
44         $resp = $this->putJson("/comment/$comment->id", [
45             'text' => $newText,
46         ]);
47
48         $resp->assertStatus(200);
49         $resp->assertSee($newText);
50         $resp->assertDontSee($comment->text);
51
52         $this->assertDatabaseHas('comments', [
53             'text'      => $newText,
54             'entity_id' => $page->id,
55         ]);
56     }
57
58     public function test_comment_delete()
59     {
60         $this->asAdmin();
61         $page = Page::first();
62
63         $comment = Comment::factory()->make();
64         $this->postJson("/comment/$page->id", $comment->getAttributes());
65
66         $comment = $page->comments()->first();
67
68         $resp = $this->delete("/comment/$comment->id");
69         $resp->assertStatus(200);
70
71         $this->assertDatabaseMissing('comments', [
72             'id' => $comment->id,
73         ]);
74     }
75
76     public function test_comments_converts_markdown_input_to_html()
77     {
78         $page = Page::first();
79         $this->asAdmin()->postJson("/comment/$page->id", [
80             'text' => '# My Title',
81         ]);
82
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",
88         ]);
89
90         $pageView = $this->get($page->getUrl());
91         $pageView->assertSee('<h1>My Title</h1>', false);
92     }
93
94     public function test_html_cannot_be_injected_via_comment_content()
95     {
96         $this->asAdmin();
97         $page = Page::first();
98
99         $script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
100         $this->postJson("/comment/$page->id", [
101             'text' => $script,
102         ]);
103
104         $pageView = $this->get($page->getUrl());
105         $pageView->assertDontSee($script, false);
106         $pageView->assertSee('sometextinthecomment');
107
108         $comment = $page->comments()->first();
109         $this->putJson("/comment/$comment->id", [
110             'text' => $script . 'updated',
111         ]);
112
113         $pageView = $this->get($page->getUrl());
114         $pageView->assertDontSee($script, false);
115         $pageView->assertSee('sometextinthecommentupdated');
116     }
117 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.