fn_b/tests/Feature/LatestFreeNodeHomeTest.php

81 lines
3.2 KiB
PHP

<?php
namespace Tests\Feature;
use App\Services\ArticlesService;
use App\Services\ArticleResponseCacheProfile;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use Tests\TestCase;
#[RunTestsInSeparateProcesses]
#[PreserveGlobalState(false)]
class LatestFreeNodeHomeTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
config(['database.default' => 'sqlite', 'database.connections.sqlite.database' => ':memory:']);
DB::purge('sqlite');
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('sSlug');
});
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('sSlug');
$table->integer('iCategoryId');
$table->integer('iStatus')->default(1);
$table->integer('iIsTop')->default(0);
$table->timestamps();
});
DB::table('categories')->insert([['id' => 1, 'sSlug' => 'freenode'], ['id' => 2, 'sSlug' => 'client']]);
$this->travelTo(\Carbon\Carbon::parse('2026-10-27 12:00:00', 'UTC'));
}
protected function tearDown(): void
{
$this->travelBack();
parent::tearDown();
}
public function test_latest_date_wins_over_pins_and_ignores_other_categories_drafts_and_future(): void
{
foreach ([
[1, 'old-pinned', 1, 1, 1, '2026-10-20 00:00:00'],
[2, 'latest-free', 1, 1, 0, '2026-10-21 00:00:00'],
[3, 'tutorial', 2, 1, 0, '2026-10-27 01:00:00'],
[4, 'draft', 1, 0, 0, '2026-10-27 02:00:00'],
[5, 'future', 1, 1, 0, '2026-10-28 00:00:00'],
] as [$id, $slug, $category, $status, $pin, $date]) {
DB::table('articles')->insert(['id' => $id, 'sSlug' => $slug, 'iCategoryId' => $category,
'iStatus' => $status, 'iIsTop' => $pin, 'created_at' => $date]);
}
$this->assertSame('latest-free', app(ArticlesService::class)->getLatestFreeNodeSlug());
DB::table('articles')->insert(['sSlug' => 'new-free', 'iCategoryId' => 1, 'created_at' => '2026-10-27 03:00:00']);
$this->assertSame('new-free', app(ArticlesService::class)->getLatestFreeNodeSlug());
}
public function test_empty_pool_and_equal_dates_are_deterministic(): void
{
$service = app(ArticlesService::class);
$this->assertNull($service->getLatestFreeNodeSlug());
foreach (['first', 'second'] as $slug) {
DB::table('articles')->insert(['sSlug' => $slug, 'iCategoryId' => 1, 'created_at' => '2026-10-21 00:00:00']);
}
$this->assertSame('second', $service->getLatestFreeNodeSlug());
}
public function test_home_bypasses_cache_and_categories_keep_existing_cache_policy(): void
{
config(['responsecache.enabled' => true]);
$profile = app(ArticleResponseCacheProfile::class);
$this->assertFalse($profile->enabled(Request::create('/')));
$this->assertTrue($profile->enabled(Request::create('/freenode')));
}
}