it('redirects guest to login for PostResource index', function () {
$this->get(PostResource::getUrl('index'))
->assertRedirect(route('filament.admin.auth.login'));
});
it('shows post list page for authenticated user', function () {
$user = User::factory()->create();
$this->actingAs($user)
->get(PostResource::getUrl('index'))
->assertSuccessful(); // or ->assertForbidden();
});
it('has correct city relation', function () {
$city = Post::factory()->for(Province::factory())->create();
$user = User::factory()->for($city)->create();
expect($user->city())->toBeInstanceOf(BelongsTo::class)
->and($user->city)->toBeInstanceOf(Post::class)
->and($user->city->getKey())->toBe($city->getKey());
});
it('consultant has sessions relation', function () {
$consultant = Consultant::factory()
->has(ConsultantSession::factory()->count(3), Consultant::SESSIONS_RELATION)
->create();
expect($consultant->sessions())->toBeInstanceOf(HasMany::class)
->and($consultant->sessions)->toBeInstanceOf(Collection::class)
->and($consultant->sessions)->toHaveCount(3);
});
it('can render user index page', function () {
$this->get(UserResource::getUrl('index'))->assertSuccessful();
});
it('can render user create page', function () {
$this->get(UserResource::getUrl('create'))->assertSuccessful();
});
it('can render user edit page', function () {
$user = User::factory()->create();
$this->get(UserResource::getUrl('edit', [
'record' => $user,
]))->assertSuccessful();
});
it('can retrieve data', function () {
$post = Post::factory()->create();
livewire(PostResource\Pages\ViewPost::class, [
'record' => $post->getRouteKey(),
])
->assertFormSet([
'title' => $post->title,
]);
});
it('can create a post', function () {
$user = User::factory()->create();
$data = [
'title' => 'new title',
];
livewire(CreatePost::class)
->fillForm($data)
->call('create')
->assertHasNoFormErrors();
$this->assertDatabaseHas(Post::class, [
'title' => $data['title'],
]);
});
it('can update post data via edit page', function () {
$post = Post::factory()->create();
$newData = [
'title' => 'new title',
];
livewire(EditPost::class, [
'record' => $post->getKey(),
])
->fillForm($newData)
->call('save')
->assertHasNoFormErrors();
expect($post->refresh())
->title->toBe($newData['title']);
});
it('can delete a single post from table action', function () {
$post = Post::factory()->create();
livewire(ListPosts::class)
->callTableAction(DeleteAction::class, $post);
$this->assertModelMissing($post);
});
it('can bulk delete posts', function () {
$posts = Post::factory()->count(10)->create();
livewire(PostResource\Pages\ListPosts::class)
->callTableBulkAction(DeleteBulkAction::class, $posts);
foreach ($posts as $post) {
$this->assertModelMissing($post);
}
});
it('shows error if title is required', function () {
$data = [
'title' => '',
];
livewire(CreatePost::class)
->fillForm($data)
->call('create')
->assertHasFormErrors(['title' => 'required']);
});
it('can search posts by title', function () {
$posts = Post::factory()->count(10)->create();
$title = $posts->first()->title;
livewire(PostResource\Pages\ListPosts::class)
->searchTable($title)
->assertCanSeeTableRecords($posts->where('title', $title))
->assertCanNotSeeTableRecords($posts->where('title', '!=', $title));
});
it('admin can see all posts', function () {
$adminRole = Role::findOrCreate('admin');
$admin = User::factory()->create();
$admin->assignRole($adminRole);
$ownPosts = Post::factory()->count(2)->create(['user_id' => $admin->getKey()]);
$otherPosts = Post::factory()->count(3)->create();
$this->actingAs($admin);
livewire(ListPosts::class)
->assertCanSeeTableRecords([...$ownPosts, ...$otherPosts])
->assertCountTableRecords(5);
});
it('user can see only his own posts', function () {
$userRole = Role::findOrCreate('user');
$user = User::factory()->create();
$user->assignRole($userRole);
$ownPosts = Post::factory()->count(3)->create(['user_id' => $user->getKey()]);
$otherPosts = Post::factory()->count(4)->create();
$this->actingAs($user);
livewire(ListPosts::class)
->assertCanSeeTableRecords($ownPosts)
->assertCanNotSeeTableRecords($otherPosts)
->assertCountTableRecords(3);
});