Audit merged directory links and test rendered fallbacks
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require dirname(__DIR__) . '/vendor/autoload.php';
|
||||
|
||||
use App\Content\View;
|
||||
|
||||
$primary = 'https://example.com/product?a=1&b=2';
|
||||
$cases = [
|
||||
'configured-with-detail' => [['card' => ['url' => $primary], 'body' => '<p>有正文</p>'], $primary, true],
|
||||
'configured-only' => [['card' => ['url' => '/pagepan']], '/pagepan', false],
|
||||
'summary-only' => [['summary' => '仅摘要'], '/articles/summary-only', false],
|
||||
'editor-placeholder' => [['body' => '<p><br></p><p> ​</p>'], '', false],
|
||||
'markdown-comment' => [['body' => '<!-- 草稿注释 -->', 'body_format' => 'markdown'], '', false],
|
||||
'media-body' => [['body' => '<video controls><source src="/assets/demo.mp4"></video>'], '/articles/media-body', false],
|
||||
'unconfigured' => [[], '', false],
|
||||
];
|
||||
$items = [];
|
||||
foreach ($cases as $slug => [$fields]) {
|
||||
$items[] = $fields + ['slug' => $slug, 'title' => $slug, 'card' => [], 'summary' => '', 'body' => '', 'body_format' => 'html'];
|
||||
}
|
||||
|
||||
function parseFragment(string $html): DOMXPath
|
||||
{
|
||||
$document = new DOMDocument();
|
||||
$document->loadHTML('<meta charset="utf-8">' . $html, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
||||
|
||||
return new DOMXPath($document);
|
||||
}
|
||||
|
||||
function equal(mixed $actual, mixed $expected, string $message): void
|
||||
{
|
||||
if ($actual !== $expected) {
|
||||
throw new RuntimeException($message . ' ' . json_encode([$expected, $actual], JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
$services = parseFragment(View::fragment('services', ['products' => $items]));
|
||||
$network = parseFragment(View::fragment('network-groups', [
|
||||
'network' => ['partners' => ['name' => '合作伙伴', 'articles' => $items]], 'standalone' => true,
|
||||
]));
|
||||
foreach ($cases as $slug => [, $target, $detail]) {
|
||||
$card = "//article[h2='$slug' or div/h2='$slug']";
|
||||
equal($services->evaluate("count($card)"), 1.0, "$slug 服务卡片存在");
|
||||
equal($services->evaluate("string($card/div/h2/a/@href)"), $target, "$slug 服务标题目标");
|
||||
equal($services->evaluate("string($card/div/div/a[contains(@class, 'button-primary')]/@href)"), $target, "$slug 服务按钮目标");
|
||||
equal($services->evaluate("boolean($card/div/div/a[@class='catalog-detail'][@href='/articles/$slug'])"), $detail, "$slug 服务独立详情");
|
||||
|
||||
$item = "//li[*[1]/span='$slug']";
|
||||
equal($network->evaluate("count($item)"), 1.0, "$slug 伙伴卡片存在");
|
||||
equal($network->evaluate("string($item/*[1]/@href)"), $target, "$slug 伙伴卡片目标");
|
||||
equal($network->evaluate("boolean($item/a[@class='network-detail'][@href='/articles/$slug'])"), $detail, "$slug 伙伴独立详情");
|
||||
}
|
||||
|
||||
printf("通过 %d 组目录卡片测试。\n", count($cases));
|
||||
Reference in New Issue
Block a user