2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-16 14:26:35 +01:00
|
|
|
namespace Tests\Feature\App\Http\Controllers\Bo;
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
use App\Dto\SlideDto;
|
2026-11-14 16:06:34 +01:00
|
|
|
use App\Http\Requests\Bo\Slides\StoreSlideRequest;
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Models\Slide;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use Domain\Enums\UserRole;
|
2026-11-16 16:26:51 +01:00
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
2026-11-16 15:19:01 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2026-11-16 16:26:51 +01:00
|
|
|
use Webmozart\Assert\Assert;
|
2026-11-16 15:19:01 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
describe('SlidesController', function (): void {
|
|
|
|
|
beforeEach(function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
|
|
|
|
|
$this->actingAs($this->user, 'backend');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can access index', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
|
|
|
|
// GIVEN
|
|
|
|
|
Slide::factory()->create();
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
// WHEN
|
|
|
|
|
$response = $this->get('/bo/slides');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($response->status())->toBe(200);
|
|
|
|
|
$response->assertViewIs('bo.slides.index');
|
2026-11-16 16:26:51 +01:00
|
|
|
$response->assertViewHas('slides', static function (mixed $slides): bool {
|
|
|
|
|
Assert::isInstanceOf($slides, LengthAwarePaginator::class);
|
|
|
|
|
|
|
|
|
|
return $slides->items()[0] instanceof SlideDto;
|
|
|
|
|
});
|
2026-10-02 19:44:29 +02:00
|
|
|
$response->assertSee(__('Visible'));
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can access create', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// WHEN
|
|
|
|
|
$response = $this->get('/bo/slides/create');
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($response->status())->toBe(200);
|
|
|
|
|
$response->assertViewIs('bo.slides.create');
|
2026-11-14 16:06:34 +01:00
|
|
|
$extensions = implode(', ', array_map(
|
|
|
|
|
static fn (string $ext): string => '.' . ltrim($ext, '.'),
|
|
|
|
|
StoreSlideRequest::ALLOWED_EXTENSIONS,
|
|
|
|
|
));
|
|
|
|
|
$expectedHelper = __('Allowed extensions: :extensions (:maxSize MB max). Required dimensions: :width px by :height px', [
|
|
|
|
|
'extensions' => $extensions,
|
|
|
|
|
'maxSize' => StoreSlideRequest::MAX_FILE_SIZE_KB / 1024,
|
|
|
|
|
'width' => StoreSlideRequest::PICTURE_WIDTH,
|
|
|
|
|
'height' => StoreSlideRequest::PICTURE_HEIGHT,
|
|
|
|
|
]);
|
|
|
|
|
|
2026-11-16 16:26:51 +01:00
|
|
|
$response->assertSee(json_encode($expectedHelper, JSON_THROW_ON_ERROR), false);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can store a slide', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$data = [
|
|
|
|
|
'title' => 'New Slide Title',
|
|
|
|
|
'text' => 'This is the new slide text',
|
|
|
|
|
'link' => 'https://example.com',
|
|
|
|
|
'link_label' => 'Click here',
|
|
|
|
|
'link_new_tab' => false,
|
|
|
|
|
'connected' => false,
|
|
|
|
|
'is_mobile' => false,
|
|
|
|
|
'is_published' => true,
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->post('/bo/slides', $data);
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-09-29 15:29:43 +02:00
|
|
|
$slide = Slide::query()->where('title', 'New Slide Title')->first();
|
2026-11-16 16:26:51 +01:00
|
|
|
Assert::notNull($slide);
|
2026-06-14 21:32:07 +02:00
|
|
|
expect($slide)->not->toBeNull();
|
|
|
|
|
$response->assertRedirect("/bo/slides/{$slide->id}/edit");
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can access edit', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$slide = Slide::factory()->create();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->get("/bo/slides/{$slide->id}/edit");
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($response->status())->toBe(200);
|
|
|
|
|
$response->assertViewIs('bo.slides.edit');
|
2026-09-29 15:29:43 +02:00
|
|
|
$response->assertViewHas('slide', fn ($slideData) => $slideData instanceof SlideDto && $slideData->id === $slide->id);
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can update a slide', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$slide = Slide::factory()->create();
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
|
'title' => 'Updated Slide Title',
|
|
|
|
|
'text' => 'This is the new slide text updated',
|
|
|
|
|
'link' => 'https://example.com/updated',
|
|
|
|
|
'link_label' => 'Click here updated',
|
|
|
|
|
'link_new_tab' => true,
|
|
|
|
|
'connected' => true,
|
|
|
|
|
'is_mobile' => true,
|
|
|
|
|
'is_published' => false,
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->put("/bo/slides/{$slide->id}", $data);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$slide->refresh();
|
|
|
|
|
expect($slide->title)->toBe('Updated Slide Title');
|
|
|
|
|
$response->assertRedirect("/bo/slides/{$slide->id}/edit");
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can delete a slide', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$slide = Slide::factory()->create();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->delete("/bo/slides/{$slide->id}");
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-09-29 15:29:43 +02:00
|
|
|
expect(Slide::query()->find($slide->id))->toBeNull();
|
2026-06-14 21:32:07 +02:00
|
|
|
$response->assertRedirect('/bo/slides');
|
|
|
|
|
});
|
|
|
|
|
|
2026-09-29 15:29:43 +02:00
|
|
|
it('can change order', function (): void {
|
|
|
|
|
/** @var \Tests\TestCase $this */
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$slide1 = Slide::factory()->create(['order' => 1]);
|
|
|
|
|
$slide2 = Slide::factory()->create(['order' => 2]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = $this->from('/bo/slides')->patch("/bo/slides/{$slide2->id}/up");
|
|
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 12:46:06 +01:00
|
|
|
$slide1->refresh();
|
|
|
|
|
$slide2->refresh();
|
|
|
|
|
|
|
|
|
|
expect($response->status())->toBe(302)
|
|
|
|
|
->and($slide1->order)->toBe(2)
|
|
|
|
|
->and($slide2->order)->toBe(1);
|
2026-06-14 21:32:07 +02:00
|
|
|
$response->assertRedirect('/bo/slides');
|
|
|
|
|
});
|
2026-11-16 15:19:01 +01:00
|
|
|
|
|
|
|
|
it('redirects unauthenticated guest to login', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
auth('backend')->logout();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = get('/bo/slides');
|
|
|
|
|
|
|
|
|
|
// 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(Slide::class, $deniedPolicy::class);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$createResponse = get('/bo/slides/create');
|
|
|
|
|
$storeResponse = post('/bo/slides', [
|
|
|
|
|
'title' => 'Slide interdit',
|
|
|
|
|
'text' => 'Contenu valide du slide',
|
|
|
|
|
'link_new_tab' => false,
|
|
|
|
|
'connected' => false,
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$createResponse->assertForbidden();
|
|
|
|
|
$storeResponse->assertForbidden();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('redirects back with session validation errors on invalid web store payload', function (): void {
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = from('/bo/slides/create')->post('/bo/slides', [
|
|
|
|
|
'title' => 'AB',
|
|
|
|
|
'text' => '',
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertRedirect('/bo/slides/create')
|
|
|
|
|
->assertSessionHasErrors(['title', 'text']);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('returns 422 JSON validation errors on invalid JSON store payload', function (): void {
|
|
|
|
|
// WHEN
|
|
|
|
|
$response = postJson('/bo/slides', [
|
|
|
|
|
'title' => 'AB',
|
|
|
|
|
'text' => '',
|
|
|
|
|
'client_timezone' => 'Europe/Paris',
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
$response->assertUnprocessable()
|
|
|
|
|
->assertJsonValidationErrors(['title', 'text']);
|
|
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|