+ public function test_comment_create_permission()
+ {
+ $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
+
+ $this->actingAs($this->user)
+ ->addComment($ownPage)
+ ->assertStatus(403);
+
+ $this->giveUserPermissions($this->user, ['comment-create-all']);
+
+ $this->actingAs($this->user)
+ ->addComment($ownPage)
+ ->assertOk();
+ }
+
+ public function test_comment_update_own_permission()
+ {
+ $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
+ $this->giveUserPermissions($this->user, ['comment-create-all']);
+ $this->actingAs($this->user)->addComment($ownPage);
+ /** @var Comment $comment */
+ $comment = $ownPage->comments()->latest()->first();
+
+ // no comment-update-own
+ $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
+
+ $this->giveUserPermissions($this->user, ['comment-update-own']);
+
+ // now has comment-update-own
+ $this->actingAs($this->user)->updateComment($comment)->assertOk();
+ }
+
+ public function test_comment_update_all_permission()
+ {
+ /** @var Page $ownPage */
+ $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
+ $this->asAdmin()->addComment($ownPage);
+ /** @var Comment $comment */
+ $comment = $ownPage->comments()->latest()->first();
+
+ // no comment-update-all
+ $this->actingAs($this->user)->updateComment($comment)->assertStatus(403);
+
+ $this->giveUserPermissions($this->user, ['comment-update-all']);
+
+ // now has comment-update-all
+ $this->actingAs($this->user)->updateComment($comment)->assertOk();
+ }
+
+ public function test_comment_delete_own_permission()
+ {
+ /** @var Page $ownPage */
+ $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
+ $this->giveUserPermissions($this->user, ['comment-create-all']);
+ $this->actingAs($this->user)->addComment($ownPage);
+
+ /** @var Comment $comment */
+ $comment = $ownPage->comments()->latest()->first();
+
+ // no comment-delete-own
+ $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
+
+ $this->giveUserPermissions($this->user, ['comment-delete-own']);
+
+ // now has comment-update-own
+ $this->actingAs($this->user)->deleteComment($comment)->assertOk();
+ }
+
+ public function test_comment_delete_all_permission()
+ {
+ /** @var Page $ownPage */
+ $ownPage = $this->createEntityChainBelongingToUser($this->user)['page'];
+ $this->asAdmin()->addComment($ownPage);
+ /** @var Comment $comment */
+ $comment = $ownPage->comments()->latest()->first();
+
+ // no comment-delete-all
+ $this->actingAs($this->user)->deleteComment($comment)->assertStatus(403);
+
+ $this->giveUserPermissions($this->user, ['comment-delete-all']);
+
+ // now has comment-delete-all
+ $this->actingAs($this->user)->deleteComment($comment)->assertOk();
+ }
+
+ private function addComment(Page $page): TestResponse
+ {
+ $comment = Comment::factory()->make();
+
+ return $this->postJson("/comment/$page->id", $comment->only('text', 'html'));
+ }
+
+ private function updateComment(Comment $comment): TestResponse
+ {
+ $commentData = Comment::factory()->make();
+
+ return $this->putJson("/comment/{$comment->id}", $commentData->only('text', 'html'));
+ }
+
+ private function deleteComment(Comment $comment): TestResponse
+ {
+ return $this->json('DELETE', '/comment/' . $comment->id);
+ }