2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Entity;
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2023-07-19 05:09:08 -04:00
|
|
|
use BookStack\Activity\ActivityType;
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Activity\Models\Comment;
|
2021-06-26 11:23:15 -04:00
|
|
|
use BookStack\Entities\Models\Page;
|
2020-04-03 20:16:05 -04:00
|
|
|
use Tests\TestCase;
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2017-09-09 13:41:59 -04:00
|
|
|
class CommentTest extends TestCase
|
2017-06-12 17:01:17 -04:00
|
|
|
{
|
|
|
|
public function test_add_comment()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2021-10-30 16:29:59 -04:00
|
|
|
$comment = Comment::factory()->make(['parent_id' => 2]);
|
2020-07-28 13:19:18 -04:00
|
|
|
$resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2017-09-09 13:41:59 -04:00
|
|
|
$resp->assertStatus(200);
|
2024-01-31 11:35:58 -05:00
|
|
|
$resp->assertSee($comment->html, false);
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2017-09-09 13:41:59 -04:00
|
|
|
$pageResp = $this->get($page->getUrl());
|
2024-01-31 11:35:58 -05:00
|
|
|
$pageResp->assertSee($comment->html, false);
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2017-09-09 13:41:59 -04:00
|
|
|
$this->assertDatabaseHas('comments', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'local_id' => 1,
|
|
|
|
'entity_id' => $page->id,
|
2018-09-25 07:30:50 -04:00
|
|
|
'entity_type' => Page::newModelInstance()->getMorphClass(),
|
2024-01-31 09:22:04 -05:00
|
|
|
'text' => null,
|
2021-06-26 11:23:15 -04:00
|
|
|
'parent_id' => 2,
|
2017-09-09 13:41:59 -04:00
|
|
|
]);
|
2023-07-19 05:09:08 -04:00
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_CREATE);
|
2017-06-12 17:01:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_comment_edit()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2017-09-09 13:41:59 -04:00
|
|
|
|
2021-10-30 16:29:59 -04:00
|
|
|
$comment = Comment::factory()->make();
|
2020-07-28 13:19:18 -04:00
|
|
|
$this->postJson("/comment/$page->id", $comment->getAttributes());
|
2017-09-09 13:41:59 -04:00
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
2024-01-31 09:22:04 -05:00
|
|
|
$newHtml = '<p>updated text content</p>';
|
2020-07-28 13:19:18 -04:00
|
|
|
$resp = $this->putJson("/comment/$comment->id", [
|
2024-01-31 09:22:04 -05:00
|
|
|
'html' => $newHtml,
|
2017-09-09 13:41:59 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$resp->assertStatus(200);
|
2024-01-31 09:22:04 -05:00
|
|
|
$resp->assertSee($newHtml, false);
|
|
|
|
$resp->assertDontSee($comment->html, false);
|
2017-09-09 13:41:59 -04:00
|
|
|
|
|
|
|
$this->assertDatabaseHas('comments', [
|
2024-01-31 09:22:04 -05:00
|
|
|
'html' => $newHtml,
|
2021-06-26 11:23:15 -04:00
|
|
|
'entity_id' => $page->id,
|
2017-09-09 13:41:59 -04:00
|
|
|
]);
|
2023-07-19 05:09:08 -04:00
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_UPDATE);
|
2017-06-12 17:01:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_comment_delete()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2021-10-30 16:29:59 -04:00
|
|
|
$comment = Comment::factory()->make();
|
2020-07-28 13:19:18 -04:00
|
|
|
$this->postJson("/comment/$page->id", $comment->getAttributes());
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2017-09-09 13:41:59 -04:00
|
|
|
$comment = $page->comments()->first();
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2020-07-28 13:19:18 -04:00
|
|
|
$resp = $this->delete("/comment/$comment->id");
|
2017-09-09 13:41:59 -04:00
|
|
|
$resp->assertStatus(200);
|
2017-06-12 17:01:17 -04:00
|
|
|
|
2017-09-09 13:41:59 -04:00
|
|
|
$this->assertDatabaseMissing('comments', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'id' => $comment->id,
|
2017-09-09 13:41:59 -04:00
|
|
|
]);
|
2023-07-19 05:09:08 -04:00
|
|
|
|
|
|
|
$this->assertActivityExists(ActivityType::COMMENT_DELETE);
|
2017-06-12 17:01:17 -04:00
|
|
|
}
|
2020-05-01 18:24:11 -04:00
|
|
|
|
2024-01-31 09:22:04 -05:00
|
|
|
public function test_scripts_cannot_be_injected_via_comment_html()
|
2020-05-01 18:24:11 -04:00
|
|
|
{
|
2022-09-29 17:11:16 -04:00
|
|
|
$page = $this->entities->page();
|
2020-05-01 18:24:11 -04:00
|
|
|
|
2024-01-31 09:22:04 -05:00
|
|
|
$script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
|
2024-01-31 11:20:22 -05:00
|
|
|
$this->asAdmin()->postJson("/comment/$page->id", [
|
2024-01-31 09:22:04 -05:00
|
|
|
'html' => $script,
|
2020-05-01 18:24:11 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
2021-10-26 17:04:18 -04:00
|
|
|
$pageView->assertDontSee($script, false);
|
2024-01-31 09:22:04 -05:00
|
|
|
$pageView->assertSee('<p>My lovely comment</p>', false);
|
2020-05-01 18:24:11 -04:00
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
2020-07-28 13:19:18 -04:00
|
|
|
$this->putJson("/comment/$comment->id", [
|
2024-01-31 09:22:04 -05:00
|
|
|
'html' => $script . '<p>updated</p>',
|
2020-05-01 18:24:11 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pageView = $this->get($page->getUrl());
|
2021-10-26 17:04:18 -04:00
|
|
|
$pageView->assertDontSee($script, false);
|
2024-01-31 09:22:04 -05:00
|
|
|
$pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
|
2020-05-01 18:24:11 -04:00
|
|
|
}
|
2023-06-09 06:12:39 -04:00
|
|
|
|
2024-01-31 11:20:22 -05: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-01-31 11:47:58 -05: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,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-06-09 06:12:39 -04:00
|
|
|
public function test_reply_comments_are_nested()
|
|
|
|
{
|
|
|
|
$this->asAdmin();
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
2024-01-31 09:22:04 -05:00
|
|
|
$this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
|
|
|
|
$this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
|
2023-06-09 06:12:39 -04:00
|
|
|
|
|
|
|
$respHtml = $this->withHtml($this->get($page->getUrl()));
|
|
|
|
$respHtml->assertElementCount('.comment-branch', 3);
|
|
|
|
$respHtml->assertElementNotExists('.comment-branch .comment-branch');
|
|
|
|
|
|
|
|
$comment = $page->comments()->first();
|
2024-01-31 09:22:04 -05:00
|
|
|
$resp = $this->postJson("/comment/$page->id", [
|
|
|
|
'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
|
|
|
|
]);
|
2023-06-09 06:12:39 -04:00
|
|
|
$resp->assertStatus(200);
|
|
|
|
|
|
|
|
$respHtml = $this->withHtml($this->get($page->getUrl()));
|
|
|
|
$respHtml->assertElementCount('.comment-branch', 4);
|
|
|
|
$respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
|
|
|
|
}
|
2023-06-16 08:08:04 -04:00
|
|
|
|
|
|
|
public function test_comments_are_visible_in_the_page_editor()
|
|
|
|
{
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
2024-01-31 09:22:04 -05:00
|
|
|
$this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
|
2023-06-16 08:08:04 -04:00
|
|
|
|
|
|
|
$respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
|
|
|
|
$respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
|
|
|
|
}
|
2023-09-12 15:00:00 -04:00
|
|
|
|
|
|
|
public function test_comment_creator_name_truncated()
|
2023-09-13 05:09:33 -04:00
|
|
|
{
|
|
|
|
[$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
|
2023-09-12 15:00:00 -04:00
|
|
|
$page = $this->entities->page();
|
|
|
|
|
|
|
|
$comment = Comment::factory()->make();
|
2023-09-13 05:09:33 -04:00
|
|
|
$this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
|
2023-09-12 15:00:00 -04:00
|
|
|
|
2023-09-13 05:09:33 -04:00
|
|
|
$pageResp = $this->asAdmin()->get($page->getUrl());
|
|
|
|
$pageResp->assertSee('Wolfeschlegels…');
|
2023-09-12 15:00:00 -04:00
|
|
|
}
|
2024-01-31 09:22:04 -05:00
|
|
|
|
|
|
|
public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
|
|
|
|
{
|
|
|
|
$editor = $this->users->editor();
|
|
|
|
$page = $this->entities->page();
|
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get($page->getUrl());
|
|
|
|
$resp->assertSee('tinymce.min.js?', false);
|
|
|
|
$resp->assertSee('window.editor_translations', false);
|
|
|
|
$resp->assertSee('component="entity-selector"', false);
|
|
|
|
|
|
|
|
$this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
|
|
|
|
$this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
|
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get($page->getUrl());
|
|
|
|
$resp->assertDontSee('tinymce.min.js?', false);
|
|
|
|
$resp->assertDontSee('window.editor_translations', false);
|
|
|
|
$resp->assertDontSee('component="entity-selector"', false);
|
|
|
|
|
|
|
|
Comment::factory()->create([
|
|
|
|
'created_by' => $editor->id,
|
|
|
|
'entity_type' => 'page',
|
|
|
|
'entity_id' => $page->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$resp = $this->actingAs($editor)->get($page->getUrl());
|
|
|
|
$resp->assertSee('tinymce.min.js?', false);
|
|
|
|
$resp->assertSee('window.editor_translations', false);
|
|
|
|
$resp->assertSee('component="entity-selector"', false);
|
|
|
|
}
|
2017-06-12 17:01:17 -04:00
|
|
|
}
|