]> BookStack Code Mirror - bookstack/blob - tests/Entity/CommentTest.php
#47 - Fixes the issues with the test case.
[bookstack] / tests / Entity / CommentTest.php
1 <?php namespace Tests;
2
3 use BookStack\Page;
4 use BookStack\Comment;
5
6 class CommentTest extends BrowserKitTest
7 {
8
9     public function test_add_comment()
10     {
11         $this->asAdmin();
12         $page = $this->getPage();
13
14         $this->addComment($page);
15     }
16
17     public function test_comment_reply()
18     {
19         $this->asAdmin();
20         $page = $this->getPage();
21
22         // add a normal comment
23         $createdComment = $this->addComment($page);
24
25         // reply to the added comment
26         $this->addComment($page, $createdComment['id']);
27     }
28
29     public function test_comment_edit()
30     {
31         $this->asAdmin();
32         $page = $this->getPage();
33
34         $createdComment = $this->addComment($page);
35         $comment = [
36             'id' => $createdComment['id'],
37             'page_id' => $createdComment['page_id']
38         ];
39         $this->updateComment($comment);
40     }
41
42     public function test_comment_delete()
43     {
44         $this->asAdmin();
45         $page = $this->getPage();
46
47         $createdComment = $this->addComment($page);
48
49         $this->deleteComment($createdComment['id']);
50     }
51
52     private function getPage() {
53         $page = Page::first();
54         return $page;
55     }
56
57
58     private function addComment($page, $parentCommentId = null) {
59         $comment = factory(Comment::class)->make();
60         $url = "/ajax/page/$page->id/comment/";
61         $request = [
62             'text' => $comment->text,
63             'html' => $comment->html
64         ];
65         if (!empty($parentCommentId)) {
66             $request['parent_id'] = $parentCommentId;
67         }
68         $this->call('POST', $url, $request);
69
70         $createdComment = $this->checkResponse();
71         return $createdComment;
72     }
73
74     private function updateComment($comment) {
75         $tmpComment = factory(Comment::class)->make();
76         $url = '/ajax/page/' . $comment['page_id'] . '/comment/ ' . $comment['id'];
77          $request = [
78             'text' => $tmpComment->text,
79             'html' => $tmpComment->html
80         ];
81
82         $this->call('PUT', $url, $request);
83
84         $updatedComment = $this->checkResponse();
85         return $updatedComment;
86     }
87
88     private function deleteComment($commentId) {
89         //  Route::delete('/ajax/comment/{id}', 'CommentController@destroy');
90         $url = '/ajax/comment/' . $commentId;
91         $this->call('DELETE', $url);
92
93         $deletedComment = $this->checkResponse();
94         return $deletedComment;
95     }
96
97     private function checkResponse() {
98         $expectedResp = [
99             'status' => 'success'
100         ];
101
102         $this->assertResponseOk();
103         $this->seeJsonContains($expectedResp);
104
105         $resp = $this->decodeResponseJson();
106         $createdComment = $resp['comment'];
107         $this->assertArrayHasKey('id', $createdComment);
108
109         return $createdComment;
110     }
111 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.