X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/79cfd39fde89ee4889c44954ddad9afd84ee9f1d..refs/pull/2022/head:/tests/Entity/CommentTest.php diff --git a/tests/Entity/CommentTest.php b/tests/Entity/CommentTest.php index 86eb31213..a2126407b 100644 --- a/tests/Entity/CommentTest.php +++ b/tests/Entity/CommentTest.php @@ -1,111 +1,75 @@ -asAdmin(); - $page = $this->getPage(); + $page = Page::first(); - $this->addComment($page); - } + $comment = factory(Comment::class)->make(['parent_id' => 2]); + $resp = $this->postJson("/ajax/page/$page->id/comment", $comment->getAttributes()); - public function test_comment_reply() - { - $this->asAdmin(); - $page = $this->getPage(); + $resp->assertStatus(200); + $resp->assertSee($comment->text); - // add a normal comment - $createdComment = $this->addComment($page); + $pageResp = $this->get($page->getUrl()); + $pageResp->assertSee($comment->text); - // reply to the added comment - $this->addComment($page, $createdComment['id']); + $this->assertDatabaseHas('comments', [ + 'local_id' => 1, + 'entity_id' => $page->id, + 'entity_type' => Page::newModelInstance()->getMorphClass(), + 'text' => $comment->text, + 'parent_id' => 2 + ]); } public function test_comment_edit() { $this->asAdmin(); - $page = $this->getPage(); - - $createdComment = $this->addComment($page); - $comment = [ - 'id' => $createdComment['id'], - 'page_id' => $createdComment['page_id'] - ]; - $this->updateComment($comment); + $page = Page::first(); + + $comment = factory(Comment::class)->make(); + $this->postJson("/ajax/page/$page->id/comment", $comment->getAttributes()); + + $comment = $page->comments()->first(); + $newText = 'updated text content'; + $resp = $this->putJson("/ajax/comment/$comment->id", [ + 'text' => $newText, + 'html' => '
'.$newText.'
', + ]); + + $resp->assertStatus(200); + $resp->assertSee($newText); + $resp->assertDontSee($comment->text); + + $this->assertDatabaseHas('comments', [ + 'text' => $newText, + 'entity_id' => $page->id + ]); } public function test_comment_delete() { $this->asAdmin(); - $page = $this->getPage(); - - $createdComment = $this->addComment($page); - - $this->deleteComment($createdComment['id']); - } - - private function getPage() { $page = Page::first(); - return $page; - } - - private function addComment($page, $parentCommentId = null) { $comment = factory(Comment::class)->make(); - $url = "/ajax/page/$page->id/comment/"; - $request = [ - 'text' => $comment->text, - 'html' => $comment->html - ]; - if (!empty($parentCommentId)) { - $request['parent_id'] = $parentCommentId; - } - $this->call('POST', $url, $request); - - $createdComment = $this->checkResponse(); - return $createdComment; - } - - private function updateComment($comment) { - $tmpComment = factory(Comment::class)->make(); - $url = '/ajax/page/' . $comment['page_id'] . '/comment/ ' . $comment['id']; - $request = [ - 'text' => $tmpComment->text, - 'html' => $tmpComment->html - ]; - - $this->call('PUT', $url, $request); - - $updatedComment = $this->checkResponse(); - return $updatedComment; - } - - private function deleteComment($commentId) { - // Route::delete('/ajax/comment/{id}', 'CommentController@destroy'); - $url = '/ajax/comment/' . $commentId; - $this->call('DELETE', $url); - - $deletedComment = $this->checkResponse(); - return $deletedComment; - } - - private function checkResponse() { - $expectedResp = [ - 'status' => 'success' - ]; + $this->postJson("/ajax/page/$page->id/comment", $comment->getAttributes()); - $this->assertResponseOk(); - $this->seeJsonContains($expectedResp); + $comment = $page->comments()->first(); - $resp = $this->decodeResponseJson(); - $createdComment = $resp['comment']; - $this->assertArrayHasKey('id', $createdComment); + $resp = $this->delete("/ajax/comment/$comment->id"); + $resp->assertStatus(200); - return $createdComment; + $this->assertDatabaseMissing('comments', [ + 'id' => $comment->id + ]); } }