From 6b7848ff6cef46578cb6c44a3e5f7d1837552c70 Mon Sep 17 00:00:00 2001 From: Xety Date: Tue, 13 Mar 2018 00:29:01 +0100 Subject: [PATCH] Add tests --- .../Controllers/Discuss/PostController.php | 5 +- .../Discuss/PostControllerTest.php | 103 ++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/Http/Controllers/Discuss/PostControllerTest.php diff --git a/app/Http/Controllers/Discuss/PostController.php b/app/Http/Controllers/Discuss/PostController.php index 8ba4f697..7dd10596 100644 --- a/app/Http/Controllers/Discuss/PostController.php +++ b/app/Http/Controllers/Discuss/PostController.php @@ -26,7 +26,8 @@ public function create(Request $request): RedirectResponse { $conversation = DiscussConversation::findOrFail($request->conversation_id); - if (DiscussPost::isFlooding('xetaravel.flood.discuss.post')) { + // Use that have the permission "manage.discuss" can bypass this rule. (Default to Administrator) + if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->hasPermission('manage.discuss')) { return back() ->withInput() ->with('danger', 'Wow, keep calm bro, and try to not flood !'); @@ -108,7 +109,7 @@ public function delete(int $id): RedirectResponse if ($conversation->last_post_id == $post->getKey()) { $previousPost = DiscussPostRepository::findPreviousPost($post); - $conversation->last_post_id = $previousPost->getKey(); + $conversation->last_post_id = !is_null($previousPost) ? $previousPost->getKey() : null; } if ($conversation->solved_post_id == $post->getKey()) { diff --git a/tests/Http/Controllers/Discuss/PostControllerTest.php b/tests/Http/Controllers/Discuss/PostControllerTest.php new file mode 100644 index 00000000..121c0965 --- /dev/null +++ b/tests/Http/Controllers/Discuss/PostControllerTest.php @@ -0,0 +1,103 @@ +be($user); + } + + /** + * testCreateSuccess method + * + * @return void + */ + public function testCreateSuccess() + { + $data = [ + 'conversation_id' => 1, + 'content' => '**This** is an awesome text.' + ]; + + $response = $this->post('/discuss/post/create', $data); + $response->assertSessionHas('success'); + $response->assertStatus(302); + } + + /** + * testShowSuccess method + * + * @return void + */ + public function testShowSuccess() + { + $response = $this->get('/discuss/post/show/2'); + $response->assertStatus(302); + $response->assertRedirect('/discuss/conversation/this-is-an-announcement.1?page=1&#post-2'); + } + + /** + * testDeleteSuccess method + * + * @return void + */ + public function testDeleteSuccess() + { + $response = $this->delete('/discuss/post/delete/2'); + $response->assertStatus(302); + $response->assertSessionHas('success'); + + $this->assertNull(DiscussPost::find(2)); + } + + /** + * testDeleteFirstPostFailed method + * + * @return void + */ + public function testDeleteFirstPostFailed() + { + $response = $this->delete('/discuss/post/delete/1'); + $response->assertStatus(302); + $response->assertSessionHas('danger'); + } + + /** + * testSolvedSuccess method + * + * @return void + */ + public function testSolvedSuccess() + { + $response = $this->get('/discuss/post/solved/2'); + $response->assertStatus(302); + $response->assertSessionHas('success'); + } + + /** + * testAlreadySolvedFailed method + * + * @return void + */ + public function testAlreadySolvedFailed() + { + $response = $this->get('/discuss/post/solved/2'); + + $response = $this->get('/discuss/post/solved/2'); + $response->assertStatus(302); + $response->assertSessionHas('danger'); + } +}