b2in/tests/Feature/DisplayPlaylistMigrationTest.php

95 lines
3.4 KiB
PHP

<?php
use App\Models\Display;
use App\Models\DisplayPlaylist;
use App\Models\DisplayPlaylistItem;
use App\Models\DisplayVersion;
use Illuminate\Support\Facades\Schema;
it('creates the display_playlists table with unique status per display', function () {
expect(Schema::hasTable('display_playlists'))->toBeTrue();
expect(Schema::hasColumns('display_playlists', [
'id', 'display_id', 'status', 'published_at', 'published_by', 'notes',
'created_at', 'updated_at',
]))->toBeTrue();
});
it('creates the display_playlist_items table', function () {
expect(Schema::hasTable('display_playlist_items'))->toBeTrue();
expect(Schema::hasColumns('display_playlist_items', [
'id', 'display_playlist_id', 'display_version_id', 'sort_order',
'created_at', 'updated_at',
]))->toBeTrue();
});
it('adds is_test and preview_token to displays', function () {
expect(Schema::hasColumns('displays', ['is_test', 'preview_token']))->toBeTrue();
});
it('drops the legacy display version pivot table', function () {
expect(Schema::hasTable('display_display_version'))->toBeFalse();
});
it('exposes a live playlist relation and a draft playlist relation on display', function () {
$display = Display::factory()->create();
$live = DisplayPlaylist::factory()->published()->create(['display_id' => $display->id]);
$draft = DisplayPlaylist::factory()->draft()->create(['display_id' => $display->id]);
expect($display->livePlaylist->is($live))->toBeTrue();
expect($display->draftPlaylist->is($draft))->toBeTrue();
});
it('prevents two playlists with the same status for one display', function () {
$display = Display::factory()->create();
DisplayPlaylist::factory()->published()->create(['display_id' => $display->id]);
DisplayPlaylist::factory()->published()->create(['display_id' => $display->id]);
})->throws(\Illuminate\Database\QueryException::class);
it('orders playlist items by sort_order on the modules relation', function () {
$display = Display::factory()->create();
$playlist = DisplayPlaylist::factory()->published()->create(['display_id' => $display->id]);
$modules = DisplayVersion::factory()->count(3)->create();
DisplayPlaylistItem::factory()->create([
'display_playlist_id' => $playlist->id,
'display_version_id' => $modules[2]->id,
'sort_order' => 2,
]);
DisplayPlaylistItem::factory()->create([
'display_playlist_id' => $playlist->id,
'display_version_id' => $modules[0]->id,
'sort_order' => 0,
]);
DisplayPlaylistItem::factory()->create([
'display_playlist_id' => $playlist->id,
'display_version_id' => $modules[1]->id,
'sort_order' => 1,
]);
expect($playlist->modules->pluck('id')->all())->toBe([
$modules[0]->id,
$modules[1]->id,
$modules[2]->id,
]);
});
it('generates a preview token on demand and persists it', function () {
$display = Display::factory()->create(['preview_token' => null]);
$token = $display->ensurePreviewToken();
expect($token)->toBeString();
expect(strlen($token))->toBeGreaterThanOrEqual(40);
expect($display->fresh()->preview_token)->toBe($token);
expect($display->ensurePreviewToken())->toBe($token);
});
it('marks a display as test display via factory state', function () {
$display = Display::factory()->test()->create();
expect($display->is_test)->toBeTrue();
expect($display->name)->toBe('Test-Display');
});