2020-04-04 08:16:05 +08:00
|
|
|
<?php namespace Tests\Entity;
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2018-09-25 19:30:50 +08:00
|
|
|
use BookStack\Entities\Page;
|
|
|
|
use BookStack\Actions\Comment;
|
2020-04-04 08:16:05 +08:00
|
|
|
use Tests\TestCase;
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
class CommentTest extends TestCase
|
2017-06-13 05:01:17 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public function test_add_comment()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2017-09-10 01:41:59 +08:00
|
|
|
$page = Page::first();
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$comment = factory(Comment::class)->make(['parent_id' => 2]);
|
2020-07-29 01:19:18 +08:00
|
|
|
$resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSee($comment->text);
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$pageResp = $this->get($page->getUrl());
|
|
|
|
$pageResp->assertSee($comment->text);
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'local_id' => 1,
|
|
|
|
'entity_id' => $page->id,
|
2018-09-25 19:30:50 +08:00
|
|
|
'entity_type' => Page::newModelInstance()->getMorphClass(),
|
2017-09-10 01:41:59 +08:00
|
|
|
'text' => $comment->text,
|
|
|
|
'parent_id' => 2
|
|
|
|
]);
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_comment_edit()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2017-09-10 01:41:59 +08:00
|
|
|
$page = Page::first();
|
|
|
|
|
|
|
|
$comment = factory(Comment::class)->make();
|
2020-07-29 01:19:18 +08:00
|
|
|
$this->postJson("/comment/$page->id", $comment->getAttributes());
|
2017-09-10 01:41:59 +08:00
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
|
|
|
$newText = 'updated text content';
|
2020-07-29 01:19:18 +08:00
|
|
|
$resp = $this->putJson("/comment/$comment->id", [
|
2017-09-10 01:41:59 +08:00
|
|
|
'text' => $newText,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
$resp->assertSee($newText);
|
|
|
|
$resp->assertDontSee($comment->text);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'text' => $newText,
|
|
|
|
'entity_id' => $page->id
|
|
|
|
]);
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_comment_delete()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
|
|
|
$page = Page::first();
|
|
|
|
|
|
|
|
$comment = factory(Comment::class)->make();
|
2020-07-29 01:19:18 +08:00
|
|
|
$this->postJson("/comment/$page->id", $comment->getAttributes());
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$comment = $page->comments()->first();
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2020-07-29 01:19:18 +08:00
|
|
|
$resp = $this->delete("/comment/$comment->id");
|
2017-09-10 01:41:59 +08:00
|
|
|
$resp->assertStatus(200);
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$this->assertDatabaseMissing('comments', [
|
|
|
|
'id' => $comment->id
|
|
|
|
]);
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|
2020-05-02 06:24:11 +08:00
|
|
|
|
|
|
|
public function test_comments_converts_markdown_input_to_html()
|
|
|
|
{
|
|
|
|
$page = Page::first();
|
2020-07-29 01:19:18 +08:00
|
|
|
$this->asAdmin()->postJson("/comment/$page->id", [
|
2020-05-02 06:24:11 +08:00
|
|
|
'text' => '# My Title',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'entity_id' => $page->id,
|
|
|
|
'entity_type' => $page->getMorphClass(),
|
|
|
|
'text' => '# My Title',
|
|
|
|
'html' => "<h1>My Title</h1>\n",
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
|
|
|
$pageView->assertSee('<h1>My Title</h1>');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_html_cannot_be_injected_via_comment_content()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
|
|
|
$page = Page::first();
|
|
|
|
|
|
|
|
$script = '<script>const a = "script";</script>\n\n# sometextinthecomment';
|
2020-07-29 01:19:18 +08:00
|
|
|
$this->postJson("/comment/$page->id", [
|
2020-05-02 06:24:11 +08:00
|
|
|
'text' => $script,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
|
|
|
$pageView->assertDontSee($script);
|
|
|
|
$pageView->assertSee('sometextinthecomment');
|
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
2020-07-29 01:19:18 +08:00
|
|
|
$this->putJson("/comment/$comment->id", [
|
2020-05-02 06:24:11 +08:00
|
|
|
'text' => $script . 'updated',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
|
|
|
$pageView->assertDontSee($script);
|
|
|
|
$pageView->assertSee('sometextinthecommentupdated');
|
|
|
|
}
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|