2021-06-26 23:23:15 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Entity;
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2023-07-19 17:09:08 +08:00
|
|
|
use BookStack\Activity\ActivityType;
|
2023-05-18 00:56:55 +08:00
|
|
|
use BookStack\Activity\Models\Comment;
|
2021-06-26 23:23:15 +08:00
|
|
|
use BookStack\Entities\Models\Page;
|
2020-04-04 08:16:05 +08:00
|
|
|
use Tests\TestCase;
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2025-05-12 21:26:09 +08:00
|
|
|
class CommentStoreTest extends TestCase
|
2017-06-13 05:01:17 +08:00
|
|
|
{
|
|
|
|
public function test_add_comment()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2022-09-30 05:11:16 +08:00
|
|
|
$page = $this->entities->page();
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2021-10-31 04:29:59 +08:00
|
|
|
$comment = Comment::factory()->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);
|
2024-02-01 00:35:58 +08:00
|
|
|
$resp->assertSee($comment->html, false);
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$pageResp = $this->get($page->getUrl());
|
2024-02-01 00:35:58 +08:00
|
|
|
$pageResp->assertSee($comment->html, false);
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2017-09-10 01:41:59 +08:00
|
|
|
$this->assertDatabaseHas('comments', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'local_id' => 1,
|
|
|
|
'entity_id' => $page->id,
|
2018-09-25 19:30:50 +08:00
|
|
|
'entity_type' => Page::newModelInstance()->getMorphClass(),
|
2024-01-31 22:22:04 +08:00
|
|
|
'text' => null,
|
2021-06-26 23:23:15 +08:00
|
|
|
'parent_id' => 2,
|
2017-09-10 01:41:59 +08:00
|
|
|
]);
|
2023-07-19 17:09:08 +08:00
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_CREATE);
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|
2025-04-19 04:13:49 +08:00
|
|
|
public function test_add_comment_stores_content_reference_only_if_format_valid()
|
|
|
|
{
|
|
|
|
$validityByRefs = [
|
|
|
|
'bkmrk-my-title:4589284922:4-3' => true,
|
|
|
|
'bkmrk-my-title:4589284922:' => true,
|
|
|
|
'bkmrk-my-title:4589284922:abc' => false,
|
|
|
|
'my-title:4589284922:' => false,
|
|
|
|
'bkmrk-my-title-4589284922:' => false,
|
|
|
|
];
|
|
|
|
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
|
|
|
foreach ($validityByRefs as $ref => $valid) {
|
|
|
|
$this->asAdmin()->postJson("/comment/$page->id", [
|
|
|
|
'html' => '<p>My comment</p>',
|
|
|
|
'parent_id' => null,
|
|
|
|
'content_ref' => $ref,
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($valid) {
|
|
|
|
$this->assertDatabaseHas('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
|
|
|
|
} else {
|
|
|
|
$this->assertDatabaseMissing('comments', ['entity_id' => $page->id, 'content_ref' => $ref]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 05:01:17 +08:00
|
|
|
|
|
|
|
public function test_comment_edit()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2022-09-30 05:11:16 +08:00
|
|
|
$page = $this->entities->page();
|
2017-09-10 01:41:59 +08:00
|
|
|
|
2021-10-31 04:29:59 +08:00
|
|
|
$comment = Comment::factory()->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();
|
2024-01-31 22:22:04 +08:00
|
|
|
$newHtml = '<p>updated text content</p>';
|
2020-07-29 01:19:18 +08:00
|
|
|
$resp = $this->putJson("/comment/$comment->id", [
|
2024-01-31 22:22:04 +08:00
|
|
|
'html' => $newHtml,
|
2017-09-10 01:41:59 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$resp->assertStatus(200);
|
2024-01-31 22:22:04 +08:00
|
|
|
$resp->assertSee($newHtml, false);
|
|
|
|
$resp->assertDontSee($comment->html, false);
|
2017-09-10 01:41:59 +08:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
2024-01-31 22:22:04 +08:00
|
|
|
'html' => $newHtml,
|
2021-06-26 23:23:15 +08:00
|
|
|
'entity_id' => $page->id,
|
2017-09-10 01:41:59 +08:00
|
|
|
]);
|
2023-07-19 17:09:08 +08:00
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_UPDATE);
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_comment_delete()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2022-09-30 05:11:16 +08:00
|
|
|
$page = $this->entities->page();
|
2017-06-13 05:01:17 +08:00
|
|
|
|
2021-10-31 04:29:59 +08:00
|
|
|
$comment = Comment::factory()->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', [
|
2021-06-26 23:23:15 +08:00
|
|
|
'id' => $comment->id,
|
2017-09-10 01:41:59 +08:00
|
|
|
]);
|
2023-07-19 17:09:08 +08:00
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_DELETE);
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|
2020-05-02 06:24:11 +08:00
|
|
|
|
2025-04-28 22:37:09 +08:00
|
|
|
public function test_comment_archive_and_unarchive()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
|
|
|
$comment = Comment::factory()->make();
|
|
|
|
$page->comments()->save($comment);
|
|
|
|
$comment->refresh();
|
|
|
|
|
|
|
|
$this->put("/comment/$comment->id/archive");
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'id' => $comment->id,
|
|
|
|
'archived' => true,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_UPDATE);
|
|
|
|
|
|
|
|
$this->put("/comment/$comment->id/unarchive");
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'id' => $comment->id,
|
|
|
|
'archived' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_UPDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_archive_endpoints_require_delete_or_edit_permissions()
|
|
|
|
{
|
|
|
|
$viewer = $this->users->viewer();
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
|
|
|
$comment = Comment::factory()->make();
|
|
|
|
$page->comments()->save($comment);
|
|
|
|
$comment->refresh();
|
|
|
|
|
|
|
|
$endpoints = ["/comment/$comment->id/archive", "/comment/$comment->id/unarchive"];
|
|
|
|
|
|
|
|
foreach ($endpoints as $endpoint) {
|
|
|
|
$resp = $this->actingAs($viewer)->put($endpoint);
|
|
|
|
$this->assertPermissionError($resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->permissions->grantUserRolePermissions($viewer, ['comment-delete-all']);
|
|
|
|
|
|
|
|
foreach ($endpoints as $endpoint) {
|
|
|
|
$resp = $this->actingAs($viewer)->put($endpoint);
|
|
|
|
$resp->assertOk();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->permissions->removeUserRolePermissions($viewer, ['comment-delete-all']);
|
|
|
|
$this->permissions->grantUserRolePermissions($viewer, ['comment-update-all']);
|
|
|
|
|
|
|
|
foreach ($endpoints as $endpoint) {
|
|
|
|
$resp = $this->actingAs($viewer)->put($endpoint);
|
|
|
|
$resp->assertOk();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-05-12 21:26:09 +08:00
|
|
|
public function test_non_top_level_comments_cant_be_archived_or_unarchived()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
|
|
|
$comment = Comment::factory()->make();
|
|
|
|
$page->comments()->save($comment);
|
|
|
|
$subComment = Comment::factory()->make(['parent_id' => $comment->id]);
|
|
|
|
$page->comments()->save($subComment);
|
|
|
|
$subComment->refresh();
|
|
|
|
|
|
|
|
$resp = $this->putJson("/comment/$subComment->id/archive");
|
|
|
|
$resp->assertStatus(400);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'id' => $subComment->id,
|
|
|
|
'archived' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->putJson("/comment/$subComment->id/unarchive");
|
|
|
|
$resp->assertStatus(400);
|
|
|
|
}
|
|
|
|
|
2024-01-31 22:22:04 +08:00
|
|
|
public function test_scripts_cannot_be_injected_via_comment_html()
|
2020-05-02 06:24:11 +08:00
|
|
|
{
|
2022-09-30 05:11:16 +08:00
|
|
|
$page = $this->entities->page();
|
2020-05-02 06:24:11 +08:00
|
|
|
|
2024-01-31 22:22:04 +08:00
|
|
|
$script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
|
2024-02-01 00:20:22 +08:00
|
|
|
$this->asAdmin()->postJson("/comment/$page->id", [
|
2024-01-31 22:22:04 +08:00
|
|
|
'html' => $script,
|
2020-05-02 06:24:11 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
2021-10-27 05:04:18 +08:00
|
|
|
$pageView->assertDontSee($script, false);
|
2024-01-31 22:22:04 +08:00
|
|
|
$pageView->assertSee('<p>My lovely comment</p>', false);
|
2020-05-02 06:24:11 +08:00
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
2020-07-29 01:19:18 +08:00
|
|
|
$this->putJson("/comment/$comment->id", [
|
2024-01-31 22:22:04 +08:00
|
|
|
'html' => $script . '<p>updated</p>',
|
2020-05-02 06:24:11 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
2021-10-27 05:04:18 +08:00
|
|
|
$pageView->assertDontSee($script, false);
|
2024-01-31 22:22:04 +08:00
|
|
|
$pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
|
2020-05-02 06:24:11 +08:00
|
|
|
}
|
2023-06-09 18:12:39 +08:00
|
|
|
|
2024-02-01 00:20:22 +08:00
|
|
|
public function test_scripts_are_removed_even_if_already_in_db()
|
|
|
|
{
|
|
|
|
$page = $this->entities->page();
|
|
|
|
Comment::factory()->create([
|
|
|
|
'html' => '<script>superbadscript</script><p onclick="superbadonclick">scriptincommentest</p>',
|
|
|
|
'entity_type' => 'page', 'entity_id' => $page
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->get($page->getUrl());
|
|
|
|
$resp->assertSee('scriptincommentest', false);
|
|
|
|
$resp->assertDontSee('superbadscript', false);
|
|
|
|
$resp->assertDontSee('superbadonclick', false);
|
|
|
|
}
|
|
|
|
|
2024-02-01 00:47:58 +08:00
|
|
|
public function test_comment_html_is_limited()
|
|
|
|
{
|
|
|
|
$page = $this->entities->page();
|
|
|
|
$input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" data-a="b">a</a><section>Hello</section></p>';
|
|
|
|
$expected = '<p>Content<a href="#cat">a</a></p>';
|
|
|
|
|
|
|
|
$resp = $this->asAdmin()->post("/comment/{$page->id}", ['html' => $input]);
|
|
|
|
$resp->assertOk();
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'entity_type' => 'page',
|
|
|
|
'entity_id' => $page->id,
|
|
|
|
'html' => $expected,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
|
|
|
$resp = $this->put("/comment/{$comment->id}", ['html' => $input]);
|
|
|
|
$resp->assertOk();
|
|
|
|
$this->assertDatabaseHas('comments', [
|
|
|
|
'id' => $comment->id,
|
|
|
|
'html' => $expected,
|
|
|
|
]);
|
|
|
|
}
|
2017-06-13 05:01:17 +08:00
|
|
|
}
|