3 namespace Tests\Entity;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Comment;
7 use BookStack\Entities\Models\Page;
10 class CommentTest extends TestCase
12 public function test_add_comment()
15 $page = $this->entities->page();
17 $comment = Comment::factory()->make(['parent_id' => 2]);
18 $resp = $this->postJson("/comment/$page->id", $comment->getAttributes());
20 $resp->assertStatus(200);
21 $resp->assertSee($comment->text);
23 $pageResp = $this->get($page->getUrl());
24 $pageResp->assertSee($comment->text);
26 $this->assertDatabaseHas('comments', [
28 'entity_id' => $page->id,
29 'entity_type' => Page::newModelInstance()->getMorphClass(),
34 $this->assertActivityExists(ActivityType::COMMENT_CREATE);
37 public function test_comment_edit()
40 $page = $this->entities->page();
42 $comment = Comment::factory()->make();
43 $this->postJson("/comment/$page->id", $comment->getAttributes());
45 $comment = $page->comments()->first();
46 $newHtml = '<p>updated text content</p>';
47 $resp = $this->putJson("/comment/$comment->id", [
51 $resp->assertStatus(200);
52 $resp->assertSee($newHtml, false);
53 $resp->assertDontSee($comment->html, false);
55 $this->assertDatabaseHas('comments', [
57 'entity_id' => $page->id,
60 $this->assertActivityExists(ActivityType::COMMENT_UPDATE);
63 public function test_comment_delete()
66 $page = $this->entities->page();
68 $comment = Comment::factory()->make();
69 $this->postJson("/comment/$page->id", $comment->getAttributes());
71 $comment = $page->comments()->first();
73 $resp = $this->delete("/comment/$comment->id");
74 $resp->assertStatus(200);
76 $this->assertDatabaseMissing('comments', [
80 $this->assertActivityExists(ActivityType::COMMENT_DELETE);
83 public function test_scripts_cannot_be_injected_via_comment_html()
86 $page = $this->entities->page();
88 $script = '<script>const a = "script";</script><p onclick="1">My lovely comment</p>';
89 $this->postJson("/comment/$page->id", [
93 $pageView = $this->get($page->getUrl());
94 $pageView->assertDontSee($script, false);
95 $pageView->assertSee('<p>My lovely comment</p>', false);
97 $comment = $page->comments()->first();
98 $this->putJson("/comment/$comment->id", [
99 'html' => $script . '<p>updated</p>',
102 $pageView = $this->get($page->getUrl());
103 $pageView->assertDontSee($script, false);
104 $pageView->assertSee('<p>My lovely comment</p><p>updated</p>');
107 public function test_reply_comments_are_nested()
110 $page = $this->entities->page();
112 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
113 $this->postJson("/comment/$page->id", ['html' => '<p>My new comment</p>']);
115 $respHtml = $this->withHtml($this->get($page->getUrl()));
116 $respHtml->assertElementCount('.comment-branch', 3);
117 $respHtml->assertElementNotExists('.comment-branch .comment-branch');
119 $comment = $page->comments()->first();
120 $resp = $this->postJson("/comment/$page->id", [
121 'html' => '<p>My nested comment</p>', 'parent_id' => $comment->local_id
123 $resp->assertStatus(200);
125 $respHtml = $this->withHtml($this->get($page->getUrl()));
126 $respHtml->assertElementCount('.comment-branch', 4);
127 $respHtml->assertElementContains('.comment-branch .comment-branch', 'My nested comment');
130 public function test_comments_are_visible_in_the_page_editor()
132 $page = $this->entities->page();
134 $this->asAdmin()->postJson("/comment/$page->id", ['html' => '<p>My great comment to see in the editor</p>']);
136 $respHtml = $this->withHtml($this->get($page->getUrl('/edit')));
137 $respHtml->assertElementContains('.comment-box .content', 'My great comment to see in the editor');
140 public function test_comment_creator_name_truncated()
142 [$longNamedUser] = $this->users->newUserWithRole(['name' => 'Wolfeschlegelsteinhausenbergerdorff'], ['comment-create-all', 'page-view-all']);
143 $page = $this->entities->page();
145 $comment = Comment::factory()->make();
146 $this->actingAs($longNamedUser)->postJson("/comment/$page->id", $comment->getAttributes());
148 $pageResp = $this->asAdmin()->get($page->getUrl());
149 $pageResp->assertSee('Wolfeschlegels…');
152 public function test_comment_editor_js_loaded_with_create_or_edit_permissions()
154 $editor = $this->users->editor();
155 $page = $this->entities->page();
157 $resp = $this->actingAs($editor)->get($page->getUrl());
158 $resp->assertSee('tinymce.min.js?', false);
159 $resp->assertSee('window.editor_translations', false);
160 $resp->assertSee('component="entity-selector"', false);
162 $this->permissions->removeUserRolePermissions($editor, ['comment-create-all']);
163 $this->permissions->grantUserRolePermissions($editor, ['comment-update-own']);
165 $resp = $this->actingAs($editor)->get($page->getUrl());
166 $resp->assertDontSee('tinymce.min.js?', false);
167 $resp->assertDontSee('window.editor_translations', false);
168 $resp->assertDontSee('component="entity-selector"', false);
170 Comment::factory()->create([
171 'created_by' => $editor->id,
172 'entity_type' => 'page',
173 'entity_id' => $page->id,
176 $resp = $this->actingAs($editor)->get($page->getUrl());
177 $resp->assertSee('tinymce.min.js?', false);
178 $resp->assertSee('window.editor_translations', false);
179 $resp->assertSee('component="entity-selector"', false);