Files
SkinbaseNova/tests/Feature/ArtworkSearchDocumentTest.php
klevze 6b00652a20 Allow artworks to have one primary category and extra secondaries.
Keep artwork_category as membership, store a deterministic primary_category_id, and let upload plus studio pick up to five additional same-type categories without changing canonical URLs.
2026-09-20 14:50:12 +02:00

64 lines
2.6 KiB
PHP

<?php
use App\Models\Artwork;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
function createSearchDocumentContentType(string $name, int $sortOrder = 0): int
{
return (int) DB::table('content_types')->insertGetId([
'name' => $name,
'slug' => Str::slug($name) . '-' . Str::lower(Str::random(6)),
'description' => null,
'created_at' => now(),
'updated_at' => now(),
]);
}
function createSearchDocumentCategory(int $contentTypeId, string $name, int $sortOrder = 0): int
{
return (int) DB::table('categories')->insertGetId([
'content_type_id' => $contentTypeId,
'parent_id' => null,
'name' => $name,
'slug' => Str::slug($name) . '-' . Str::lower(Str::random(6)),
'description' => null,
'image' => null,
'is_active' => true,
'sort_order' => $sortOrder,
'created_at' => now(),
'updated_at' => now(),
]);
}
it('indexes all attached categories and content types while preserving a primary category', function (): void {
$user = User::factory()->create();
$artwork = Artwork::factory()->for($user)->create([
'is_public' => true,
'is_approved' => true,
'published_at' => now()->subMinute(),
]);
$wallpapersId = createSearchDocumentContentType('Wallpapers');
$digitalArtId = createSearchDocumentContentType('Digital Art');
$fantasyId = createSearchDocumentCategory($wallpapersId, 'Fantasy', 10);
$mattePaintingId = createSearchDocumentCategory($digitalArtId, 'Matte Painting', 20);
$artwork->categories()->sync([$mattePaintingId, $fantasyId]);
$artwork->forceFill(['primary_category_id' => $fantasyId])->save();
$payload = $artwork->fresh(['categories.contentType', 'primaryCategory.contentType'])->toSearchableArray();
$fantasySlug = (string) DB::table('categories')->where('id', $fantasyId)->value('slug');
$mattePaintingSlug = (string) DB::table('categories')->where('id', $mattePaintingId)->value('slug');
$wallpapersSlug = (string) DB::table('content_types')->where('id', $wallpapersId)->value('slug');
$digitalArtSlug = (string) DB::table('content_types')->where('id', $digitalArtId)->value('slug');
expect($payload['category'])->toBe($fantasySlug)
->and($payload['primary_category_id'])->toBe($fantasyId)
->and($payload['category_ids'])->toEqualCanonicalizing([$fantasyId, $mattePaintingId])
->and($payload['content_type'])->toBe($wallpapersSlug)
->and($payload['categories'])->toBe([$fantasySlug, $mattePaintingSlug])
->and($payload['content_types'])->toBe([$wallpapersSlug, $digitalArtSlug]);
});