-<?php namespace Tests;
+<?php namespace Tests\Entity;
-use BookStack\Page;
-use BookStack\Comment;
+use BookStack\Entities\Page;
+use BookStack\Actions\Comment;
+use Tests\TestCase;
-class CommentTest extends BrowserKitTest
+class CommentTest extends TestCase
{
public function test_add_comment()
{
$this->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' => '<p>'.$newText.'</p>',
+ ]);
+
+ $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
+ ]);
}
}