Files
SkinbaseNova/tests/Feature/TagPageTest.php

130 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Artwork;
use App\Models\Category;
use App\Models\ContentType;
use App\Models\Tag;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
it('renders the tag page with correct title and canonical', function (): void {
$tag = Tag::factory()->create(['name' => 'Cyberpunk', 'slug' => 'cyberpunk', 'is_active' => true]);
$response = $this->get('/tag/cyberpunk');
$response->assertOk();
$html = $response->getContent();
expect($html)
->toContain('Cyberpunk')
->toContain('index,follow');
});
it('returns 404 for a non-existent tag slug', function (): void {
$this->get('/tag/does-not-exist-xyz')->assertNotFound();
});
it('renders tag page with artworks from the tag', function (): void {
// The tag page uses Meilisearch (SCOUT_DRIVER=null in tests → empty results).
// We verify the page renders correctly with tag metadata; artwork grid
// content is covered by browser/e2e tests against a live index.
$tag = Tag::factory()->create(['name' => 'Night', 'slug' => 'night', 'is_active' => true]);
$response = $this->get('/tag/night');
$response->assertOk();
expect($response->getContent())
->toContain('Night')
->toContain('index,follow');
});
it('shows pagination rel links on tag pages with enough artworks', function (): void {
// NOTE: pagination rel links are injected only when the Meilisearch paginator
// returns > per_page results. SCOUT_DRIVER=null returns an empty paginator
// in feature tests, so we only assert the page renders without error.
// Full pagination behaviour is verified via e2e tests.
Tag::factory()->create(['name' => 'Nature', 'slug' => 'nature', 'is_active' => true]);
$this->get('/tag/nature')->assertOk();
});
it('includes JSON-LD CollectionPage schema on tag pages', function (): void {
Tag::factory()->create(['name' => 'Abstract', 'slug' => 'abstract', 'is_active' => true]);
$html = $this->get('/tag/abstract')->assertOk()->getContent();
expect($html)
->toContain('application/ld+json')
->toContain('CollectionPage');
});
it('supports sort parameter without error', function (): void {
Tag::factory()->create(['name' => 'Space', 'slug' => 'space', 'is_active' => true]);
foreach (['popular', 'latest', 'likes', 'downloads'] as $sort) {
$this->get("/tag/space?sort={$sort}")->assertOk();
}
});
it('keeps tag page artwork relation queries bounded when the gallery is populated', function (): void {
$tag = Tag::factory()->create(['name' => 'Pixel Art', 'slug' => 'pixel-art', 'is_active' => true]);
$author = User::factory()->create([
'name' => 'Pixel Artist',
'username' => 'pixelartist',
]);
$contentType = ContentType::create([
'name' => 'Photography',
'slug' => 'photography',
'description' => 'Photography content',
]);
$category = Category::create([
'content_type_id' => $contentType->id,
'parent_id' => null,
'name' => 'Abstract',
'slug' => 'abstract-pixel-art',
'description' => 'Abstract works',
'is_active' => true,
'sort_order' => 0,
]);
foreach (range(1, 12) as $index) {
$artwork = Artwork::factory()->for($author)->create([
'title' => 'Pixel Art ' . $index,
'slug' => 'pixel-art-' . $index,
'published_at' => now()->subMinutes($index),
'is_public' => true,
'is_approved' => true,
]);
$artwork->categories()->attach($category->id);
$artwork->tags()->attach($tag->id, ['source' => 'user', 'confidence' => 1]);
}
$categoryQueryCount = 0;
$userQueryCount = 0;
DB::listen(function ($query) use (&$categoryQueryCount, &$userQueryCount): void {
if (preg_match('/\bfrom\s+["`\[]?categories\b/i', $query->sql) === 1 || preg_match('/\bfrom\s+["`\[]?category_content_type\b/i', $query->sql) === 1) {
$categoryQueryCount++;
}
if (preg_match('/\bfrom\s+["`\[]?users\b/i', $query->sql) === 1) {
$userQueryCount++;
}
});
$this->get('/tag/pixel-art')
->assertOk()
->assertSee('Pixel Art', false);
expect($categoryQueryCount)->toBeLessThanOrEqual(4);
expect($userQueryCount)->toBeLessThanOrEqual(3);
});