62 lines
3.1 KiB
PHP
62 lines
3.1 KiB
PHP
<?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-with-extra' => [['card' => ['url' => '/sc', 'extra_url' => '/pagepan#download', 'extra_label' => '下载'], 'summary' => '简介'], '/sc', 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('service-cards', ['products' => $items]));
|
|
$network = parseFragment(View::fragment('network-groups', [
|
|
'network' => ['partners' => ['name' => '合作伙伴', 'articles' => $items]], 'standalone' => true,
|
|
]));
|
|
foreach ($cases as $slug => [, $target, $detail]) {
|
|
$card = "//article[div/h3='$slug']";
|
|
equal($services->evaluate("count($card)"), 1.0, "$slug 服务卡片存在");
|
|
equal($services->evaluate("string($card/div/h3/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[@href='/articles/$slug'][not(contains(@class, 'button-primary'))])"), false, "$slug 不追加详情");
|
|
if ($slug === 'configured-with-extra') {
|
|
equal($services->evaluate("string($card/div/div/a[@href='/pagepan#download'])"), '下载', "$slug 保留附加链接");
|
|
equal($services->evaluate("count($card/div/div/a)"), 2.0, "$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));
|