seed();
\App\Blog\Support\PageCacheMiddleware::flush();
$user = User::create(['name' => '作者', 'email' => 'i@t.com', 'password' => bcrypt('x')]);
$cat = Category::create(['name' => '技术', 'slug' => 'tech']);
Post::create([
'user_id' => $user->id,
'category_id' => $cat->id,
'title' => '注入测试',
'slug' => 'inject-test',
'content' => '正文内容',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
}
public function test_injection_blocks_render_in_head_and_body(): void
{
Setting::set('inject.blocks', [
['point' => 'head', 'title' => '统计', 'html' => '', 'sort' => 1, 'enabled' => true],
['point' => 'body_end', 'title' => '底部统计', 'html' => '', 'sort' => 1, 'enabled' => true],
['point' => 'sidebar_top', 'title' => '广告', 'html' => '
广告位A
', 'sort' => 1, 'enabled' => true],
]);
$html = $this->get('/')->getContent();
$this->assertStringContainsString('ADMIN_TEST_STAT', $html); // head 注入
$this->assertStringContainsString('BOTTOM_TEST', $html); // body_end 注入
$this->assertStringContainsString('广告位A', $html); // sidebar_top 注入
}
public function test_multiple_blocks_in_same_point_render_in_order(): void
{
Setting::set('inject.blocks', [
['point' => 'footer', 'title' => '第一条', 'html' => 'first
', 'sort' => 1, 'enabled' => true],
['point' => 'footer', 'title' => '第二条', 'html' => 'second
', 'sort' => 2, 'enabled' => true],
]);
$html = $this->get('/posts/inject-test.shtml')->getContent();
$pos1 = strpos($html, 'id="inject-1"');
$pos2 = strpos($html, 'id="inject-2"');
$this->assertNotFalse($pos1);
$this->assertNotFalse($pos2);
$this->assertLessThan($pos2, $pos1); // 按 sort 顺序
}
public function test_disabled_block_is_not_rendered(): void
{
Setting::set('inject.blocks', [
['point' => 'head', 'title' => '停用', 'html' => 'x
', 'sort' => 1, 'enabled' => false],
]);
$this->get('/')->assertDontSee('disabled-inject');
}
public function test_post_content_injection_points(): void
{
Setting::set('inject.blocks', [
['point' => 'post_content_top', 'title' => '正文前广告', 'html' => 'TOP
', 'sort' => 1, 'enabled' => true],
['point' => 'post_content_bottom', 'title' => '正文后广告', 'html' => 'BOTTOM
', 'sort' => 1, 'enabled' => true],
]);
$html = $this->get('/posts/inject-test.shtml')->getContent();
$this->assertStringContainsString('post-top-ad', $html);
$this->assertStringContainsString('post-bottom-ad', $html);
// 顶部注入应在正文容器前
$this->assertLessThan(strpos($html, ''), strpos($html, 'post-top-ad'));
}
public function test_theme_vars_are_usable(): void
{
Setting::set('theme_vars_sablog', ['ad_header' => '']);
// theme('var') 直接可用
$this->assertSame('', theme('var', 'ad_header', ''));
}
}