62 lines
2.4 KiB
PHP
62 lines
2.4 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(),
|
|
'sort_order' => $sortOrder,
|
|
]);
|
|
}
|
|
|
|
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]);
|
|
|
|
$payload = $artwork->fresh(['categories.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['content_type'])->toBe($wallpapersSlug)
|
|
->and($payload['categories'])->toBe([$fantasySlug, $mattePaintingSlug])
|
|
->and($payload['content_types'])->toBe([$wallpapersSlug, $digitalArtSlug]);
|
|
}); |