Files
laralog/tests/Feature/StickyPreviewTest.php
T
goukiandCursor ae47447062
Build and Deploy / verify (push) Successful in 40s
Build and Deploy / deploy (push) Successful in 36s
feat: restore sablog browse modes and clean imported HTML
Keep the public site readable after import (nbsp, highlighter, wrapping code, compact pager) and finish the admin extras already in this tree: sticky levels, preview, page cache, and live llms.txt.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-18 21:33:02 +00:00

124 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Blog\Support\PageCacheMiddleware;
use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class StickyPreviewTest extends TestCase
{
use RefreshDatabase;
private User $admin;
private Category $category;
protected function setUp(): void
{
parent::setUp();
$this->seed();
$this->admin = User::query()->where('email', 'admin@laralog.test')->firstOrFail();
$this->category = Category::query()->first() ?? Category::create([
'name' => '测试分类',
'slug' => 'sticky-test-cat',
]);
}
public function test_sticky_headline_stays_on_top(): void
{
$headline = $this->makePost('头条文章', Post::STICKY_HEADLINE, now()->subDays(3));
$strong = $this->makePost('强置顶文章', Post::STICKY_STRONG, now()->subDay());
$normal = $this->makePost('普通文章', Post::STICKY_NONE, now());
$ids = Post::query()->published()->orderedForList()->pluck('id')->all();
$this->assertSame(
[$headline->id, $strong->id, $normal->id],
array_values(array_intersect($ids, [$headline->id, $strong->id, $normal->id]))
);
$headline->refresh();
$this->assertTrue($headline->is_sticky);
$this->assertSame(Post::STICKY_HEADLINE, $headline->sticky_level);
$this->assertSame('置顶头条', $headline->stickyLabel());
}
public function test_guest_cannot_preview_draft_but_editor_can(): void
{
$draft = $this->makePost('草稿预览', Post::STICKY_NONE, null, 'draft');
$this->get($draft->url)->assertNotFound();
$this->actingAs($this->admin)
->get($draft->url)
->assertOk()
->assertSee('草稿预览', false)
->assertSee('预览中', false);
$editor = User::factory()->create([
'email' => 'editor-preview@laralog.test',
]);
$editor->assignRole('editor');
$this->actingAs($editor)
->get($draft->url)
->assertOk()
->assertSee('预览中', false);
$member = User::factory()->create([
'email' => 'member-preview@laralog.test',
]);
$member->assignRole('member');
$this->actingAs($member)
->get($draft->url)
->assertNotFound();
}
public function test_edit_page_has_preview_action(): void
{
$post = $this->makePost('可预览文章', Post::STICKY_LIGHT, now());
$this->actingAs($this->admin)
->get('/admin/posts/'.$post->id.'/edit')
->assertOk()
->assertSee('预览', false)
->assertSee($post->url, false);
}
public function test_page_cache_entries_store_url(): void
{
PageCacheMiddleware::flush();
$this->get('/')->assertOk();
$entries = PageCacheMiddleware::entries();
$this->assertNotEmpty($entries);
$this->assertNotSame('', $entries[0]['url']);
$this->assertSame(1, PageCacheMiddleware::count());
}
private function makePost(string $title, int $level, mixed $publishedAt, string $status = 'published'): Post
{
return Post::create([
'user_id' => $this->admin->id,
'category_id' => $this->category->id,
'title' => $title,
'slug' => str()->slug($title).'-'.uniqid(),
'content' => '内容',
'content_format' => 'markdown',
'status' => $status,
'sticky_level' => $level,
'published_at' => $publishedAt,
]);
}
}