Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Xety committed Mar 12, 2018
1 parent 9e3074f commit 6b7848f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/Http/Controllers/Discuss/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 !');
Expand Down Expand Up @@ -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()) {
Expand Down
103 changes: 103 additions & 0 deletions tests/Http/Controllers/Discuss/PostControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
namespace Tests\Http\Controllers\Discuss;

use Tests\TestCase;
use Xetaravel\Models\DiscussPost;
use Xetaravel\Models\User;

class PostControllerTest extends TestCase
{
/**
* Triggered before each test.
*
* @return void
*/
public function setUp()
{
parent::setUp();

$user = User::find(1);
$this->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');
}
}

0 comments on commit 6b7848f

Please sign in to comment.