Skip to content

Commit

Permalink
adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
levijackson committed Jun 30, 2021
1 parent 1baa676 commit 2e9dc1d
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/Providers/RouteServiceProvider.php
Expand Up @@ -17,7 +17,7 @@ class RouteServiceProvider extends ServiceProvider
*
* @var string
*/
public const HOME = '/home';
public const HOME = '/';

/**
* The controller namespace for the application.
Expand Down
40 changes: 40 additions & 0 deletions tests/Feature/CommentControllerTest.php
@@ -0,0 +1,40 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use Database\Seeders\PostSeeder;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class CommentControllerTest extends TestCase
{
use DatabaseTransactions;

/**
* Anonymous users can not comment on a post
*
* @return void
*/
public function testAnonymousUsersCanNotComment(): void
{
$this->seed(PostSeeder::class);

$response = $this->post('/admin/blog/comment', ['post_id' => '1', 'comment' => 'test comment']);
$response->assertStatus(403);
}
/**
* Anonymous users can not delete a comment on a post
*
* @return void
*/
public function testAnonymousUsersCanNotDeleteComment(): void
{
$this->seed(PostSeeder::class);

$response = $this->delete('/admin/blog/comment/1');
$response->assertStatus(403);
}
}
97 changes: 64 additions & 33 deletions tests/Feature/PostControllerTest.php
Expand Up @@ -68,15 +68,14 @@ public function testDraftBlogPostDoesLoadAuthor(): void
}

/**
* New blog post URLs should not be loadable by an
* anonymous user
* Anaonymous users can not load new blog post page
*
* @return void
*/
public function testNewBlogPostDoesNotLoadAnonymousUser(): void
{
$response = $this->get('/admin/blog/post');
$response->assertStatus(500);
$response->assertStatus(302);
}

/**
Expand All @@ -91,12 +90,36 @@ public function testNewBlogPostDoesLoadAuthor(): void

$response = $this->actingAs($user)
->get('/admin/blog/post');
$response->assertStatus(200);
$response->assertStatus(200);
}

/**
* Authors can save a new blog post
*
* @return void
*/
public function testNewBlogPostCreatedByAuthor(): void
{
$user = User::factory()->create();
$user->role = 'author';
$user->id = '1';

$data = [
'title' => 'Test new post by author',
'metaTitle' => '',
'body' => 'a blog post',
'metaDescription' => '',
'slug' => 'test-new-post-by-author'
];

$response = $this->actingAs($user)
->post('/admin/blog/post', $data);
$response->assertStatus(302);
$this->assertGreaterThan(0, strpos($response->getTargetUrl(), $data['slug']));
}

/**
* Edit blog post URLs should not be loadable by an
* anonymous user
* Anonymous users can not load a blog post edit page
*
* @return void
*/
Expand All @@ -105,11 +128,11 @@ public function testEditBlogPostDoesNotLoadAnonymousUser(): void
$this->seed(PostSeeder::class);

$response = $this->get('/admin/blog/post/laravel-test-post-1');
$response->assertStatus(500);
$response->assertStatus(302);
}

/**
* Authors can load the new blog post page
* Authors can load the edit blog post page
*
* @return void
*/
Expand All @@ -123,52 +146,60 @@ public function testEditBlogPostDoesLoadAuthor(): void

$response = $this->actingAs($user)
->get('/admin/blog/post/laravel-test-post-1');
$response->assertStatus(200);
$response->assertStatus(200);
}

/**
* Authors can load the draft blog post page
* Authors can save an edit to a blog post
*
* @return void
*/
public function testEditDraftBlogPostDoesLoadAuthor(): void
public function testEditBlogPostSavedByAuthor(): void
{
$this->seed(PostSeeder::class);

$user = User::factory()->create();
$user->role = 'author';
$user->id = '2';
$user->id = '1';

$data = [
'title' => 'Test new post by author',
'metaTitle' => '',
'body' => 'a blog post',
'metaDescription' => '',
'slug' => 'test-edit-post-by-author'
];

$response = $this->actingAs($user)
->get('/admin/blog/post/laravel-test-post-2');
$response->assertStatus(200);
->put('/admin/blog/post/laravel-test-post-1', $data);
$response->assertStatus(302);
$this->assertGreaterThan(0, strpos($response->getTargetUrl(), $data['slug']));
}


// TODO
/**
* - Anonymyous users can not comment on a blog post
- Anonymous users can not delete blog posts
- Admins can add a new post
- Admins can edit a post
- Admins can delete a post
- Admins can view a draft blog post
- Authors can add a new post
- Authors can edit a post
- Authors can delete a post
*/


/**
* Anonymous users can not comment on a post
* Authors can delete posts
*
* @return void
*/
public function testAnonymousUsersCanNotComment(): void
public function testAuthorCanDeletePost(): void
{
$this->seed(PostSeeder::class);

$response = $this->post('/admin/blog/comment');
$response->assertStatus(200);
$user = User::factory()->create();
$user->role = 'author';
$user->id = '1';

$data = [
'title' => 'Test post by author',
'metaTitle' => '',
'body' => 'a blog post',
'metaDescription' => '',
'delete' => 1
];

$response = $this->actingAs($user)
->put('/admin/blog/post/laravel-test-post-1', $data);
$response->assertStatus(302);
$this->assertGreaterThan(0, strpos($response->getTargetUrl(), '/admin/blog/post'));
}
}

0 comments on commit 2e9dc1d

Please sign in to comment.