ldc-cocotte/tests/Feature/App/Http/Controllers/Bo/FaqsControllerTest.php

193 lines
5.3 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Controllers\Bo;
2026-06-14 21:32:07 +02:00
use App\Models\Faq;
use App\Models\User;
use Domain\Enums\UserRole;
use Illuminate\Support\Facades\Gate;
use function Pest\Laravel\from;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
use function Pest\Laravel\postJson;
2026-06-14 21:32:07 +02:00
describe('FaqsController', function (): void {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$user = User::factory()->create(['role' => UserRole::SuperAdmin]);
$this->actingAs($user, 'backend');
2026-06-14 21:32:07 +02:00
});
it('can access index', function (): void {
/** @var \Tests\TestCase $this */
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get('/bo/faqs');
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.faqs.index');
});
it('can access create', function (): void {
/** @var \Tests\TestCase $this */
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get('/bo/faqs/create');
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.faqs.create');
});
it('can store a faq', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$data = [
'question' => 'How does it work?',
'answer' => '<p>Like <strong>this</strong>.</p>',
2026-06-14 21:32:07 +02:00
];
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->post('/bo/faqs', $data);
// THEN
$faq = Faq::query()->firstOrFail();
expect($faq->question)->toBe('How does it work?');
expect($faq->answer)->toBe('<p>Like <strong>this</strong>.</p>');
2026-06-14 21:32:07 +02:00
$response->assertRedirect("/bo/faqs/{$faq->id}/edit");
});
it('can access edit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$faq = new Faq();
$faq->question = 'What is this?';
$faq->answer = 'This is an answer.';
2026-06-14 21:32:07 +02:00
$faq->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get("/bo/faqs/{$faq->id}/edit");
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.faqs.edit');
});
it('can update a faq', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$faq = new Faq();
$faq->question = 'Initial question?';
$faq->answer = 'Initial answer.';
2026-06-14 21:32:07 +02:00
$faq->save();
$data = [
'question' => 'Updated question?',
'answer' => '<p>Updated <em>answer</em>.</p>',
2026-06-14 21:32:07 +02:00
];
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->put("/bo/faqs/{$faq->id}", $data);
// THEN
2026-06-14 21:32:07 +02:00
$faq->refresh();
expect($faq->question)->toBe('Updated question?');
expect($faq->answer)->toBe('<p>Updated <em>answer</em>.</p>');
2026-06-14 21:32:07 +02:00
$response->assertRedirect("/bo/faqs/{$faq->id}/edit");
});
it('can delete a faq', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$faq = new Faq();
$faq->question = 'Question to delete?';
$faq->answer = 'Answer to delete.';
2026-06-14 21:32:07 +02:00
$faq->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->delete("/bo/faqs/{$faq->id}");
// THEN
expect(Faq::query()->find($faq->id))->toBeNull();
2026-06-14 21:32:07 +02:00
$response->assertRedirect('/bo/faqs');
});
it('can change order', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$faq = new Faq();
$faq->question = 'Question to reorder?';
$faq->answer = 'Answer to reorder.';
$faq->save();
// WHEN
$response = $this->patch("/bo/faqs/{$faq->id}/up");
// THEN
expect($response->status())->toBe(302);
});
it('redirects unauthenticated guest to login', function (): void {
// GIVEN
auth('backend')->logout();
// WHEN
$response = get('/bo/faqs');
// THEN
$response->assertRedirect('/bo/login');
});
it('forbids create and store when policy denies access', function (): void {
// GIVEN
$deniedPolicy = new class ()
{
public function create(): bool
{
return false;
}
};
Gate::policy(Faq::class, $deniedPolicy::class);
// WHEN
$createResponse = get('/bo/faqs/create');
$storeResponse = post('/bo/faqs', [
'question' => 'Question autorisée ?',
'answer' => 'Réponse autorisée.',
]);
// THEN
$createResponse->assertForbidden();
$storeResponse->assertForbidden();
});
it('redirects back with session validation errors on invalid web store payload', function (): void {
// WHEN
$response = from('/bo/faqs/create')->post('/bo/faqs', [
'question' => 'abc',
'answer' => 'abc',
]);
// THEN
$response->assertRedirect('/bo/faqs/create')
->assertSessionHasErrors(['question', 'answer']);
});
it('returns 422 JSON validation errors on invalid JSON store payload', function (): void {
// WHEN
$response = postJson('/bo/faqs', [
'question' => 'abc',
'answer' => 'abc',
]);
// THEN
$response->assertUnprocessable()
->assertJsonValidationErrors(['question', 'answer']);
});
2026-06-14 21:32:07 +02:00
});