90 lines
2.6 KiB
PHP
90 lines
2.6 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Tests\Feature\Admin;
|
||
|
|
|
||
|
|
use App\Models\Post;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
|
|
use Tests\TestCase;
|
||
|
|
|
||
|
|
class PostCrudTest extends TestCase
|
||
|
|
{
|
||
|
|
use RefreshDatabase;
|
||
|
|
|
||
|
|
protected function admin(): User
|
||
|
|
{
|
||
|
|
$admin = User::factory()->create();
|
||
|
|
$admin->is_admin = true;
|
||
|
|
$admin->save();
|
||
|
|
|
||
|
|
return $admin;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_regular_user_cannot_access_post_admin(): void
|
||
|
|
{
|
||
|
|
$user = User::factory()->create();
|
||
|
|
|
||
|
|
$response = $this->actingAs($user)->get(route('admin.posts.index'));
|
||
|
|
|
||
|
|
$response->assertForbidden();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_create_a_published_post(): void
|
||
|
|
{
|
||
|
|
$admin = $this->admin();
|
||
|
|
|
||
|
|
$response = $this->actingAs($admin)->post(route('admin.posts.store'), [
|
||
|
|
'title' => 'New Training Tips',
|
||
|
|
'excerpt' => 'Short summary',
|
||
|
|
'body' => 'First paragraph.',
|
||
|
|
'published' => '1',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect(route('admin.posts.index'));
|
||
|
|
$this->assertDatabaseHas('posts', [
|
||
|
|
'title' => 'New Training Tips',
|
||
|
|
'slug' => 'new-training-tips',
|
||
|
|
'author_id' => $admin->id,
|
||
|
|
]);
|
||
|
|
$post = Post::where('slug', 'new-training-tips')->firstOrFail();
|
||
|
|
$this->assertTrue($post->is_published);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_create_a_draft_post(): void
|
||
|
|
{
|
||
|
|
$this->actingAs($this->admin())->post(route('admin.posts.store'), [
|
||
|
|
'title' => 'Unfinished Draft',
|
||
|
|
'body' => 'Work in progress.',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$post = Post::where('slug', 'unfinished-draft')->firstOrFail();
|
||
|
|
$this->assertFalse($post->is_published);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_update_a_post(): void
|
||
|
|
{
|
||
|
|
$post = Post::factory()->create(['title' => 'Old Title', 'published_at' => null]);
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->admin())->patch(route('admin.posts.update', $post), [
|
||
|
|
'title' => 'Updated Title',
|
||
|
|
'body' => $post->body,
|
||
|
|
'published' => '1',
|
||
|
|
]);
|
||
|
|
|
||
|
|
$response->assertRedirect(route('admin.posts.index'));
|
||
|
|
$post->refresh();
|
||
|
|
$this->assertEquals('Updated Title', $post->title);
|
||
|
|
$this->assertTrue($post->is_published);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_admin_can_delete_a_post(): void
|
||
|
|
{
|
||
|
|
$post = Post::factory()->create();
|
||
|
|
|
||
|
|
$response = $this->actingAs($this->admin())->delete(route('admin.posts.destroy', $post));
|
||
|
|
|
||
|
|
$response->assertRedirect(route('admin.posts.index'));
|
||
|
|
$this->assertDatabaseMissing('posts', ['id' => $post->id]);
|
||
|
|
}
|
||
|
|
}
|