create(['role' => UserRole::SuperAdmin]); $this->actingAs($user, 'backend'); }); it('can access index', function (): void { /** @var \Tests\TestCase $this */ // WHEN $response = $this->get('/bo/faqs'); // THEN expect($response->status())->toBe(200); $response->assertViewIs('bo.faqs.index'); }); it('can access create', function (): void { /** @var \Tests\TestCase $this */ // WHEN $response = $this->get('/bo/faqs/create'); // THEN expect($response->status())->toBe(200); $response->assertViewIs('bo.faqs.create'); }); it('can store a faq', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $data = [ 'question' => 'How does it work?', 'answer' => '

Like this.

', ]; // WHEN $response = $this->post('/bo/faqs', $data); // THEN $faq = Faq::query()->firstOrFail(); expect($faq->question)->toBe('How does it work?'); expect($faq->answer)->toBe('

Like this.

'); $response->assertRedirect("/bo/faqs/{$faq->id}/edit"); }); it('can access edit', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $faq = new Faq(); $faq->question = 'What is this?'; $faq->answer = 'This is an answer.'; $faq->save(); // WHEN $response = $this->get("/bo/faqs/{$faq->id}/edit"); // THEN expect($response->status())->toBe(200); $response->assertViewIs('bo.faqs.edit'); }); it('can update a faq', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $faq = new Faq(); $faq->question = 'Initial question?'; $faq->answer = 'Initial answer.'; $faq->save(); $data = [ 'question' => 'Updated question?', 'answer' => '

Updated answer.

', ]; // WHEN $response = $this->put("/bo/faqs/{$faq->id}", $data); // THEN $faq->refresh(); expect($faq->question)->toBe('Updated question?'); expect($faq->answer)->toBe('

Updated answer.

'); $response->assertRedirect("/bo/faqs/{$faq->id}/edit"); }); it('can delete a faq', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $faq = new Faq(); $faq->question = 'Question to delete?'; $faq->answer = 'Answer to delete.'; $faq->save(); // WHEN $response = $this->delete("/bo/faqs/{$faq->id}"); // THEN expect(Faq::query()->find($faq->id))->toBeNull(); $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']); }); });