fn_b/tests/Feature/FreeNodeArticleLayoutTest.php

53 lines
3.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Feature;
use App\Services\FreeNodeArticleLayout;
use Tests\TestCase;
class FreeNodeArticleLayoutTest extends TestCase
{
private function html(bool $singbox = true): string
{
$html = '<p>本次更新共<b>0</b>个可用节点,最高速度3M/S。</p><p>覆盖日本、日本、新加坡等多个区域。</p><p>复制下方订阅链接,添加至客户端即可正常使用。</p><!-- fna-airport-recommendations --><p><h2>免费节点订阅链接:</h2></p>';
foreach (['v2ray.txt','clash.yaml',...($singbox ? ['singbox.txt'] : [])] as $format) {
$url = 'https://example.invalid/cfn/20261031-'.$format.'?key=a&amp;b=2';
$html .= '<p><b>客户端订阅链接:</b></p><p><a data-clipboard-text="'.$url.'" href="javascript:;"></p><p><span>'.$url.'</span></a></p>';
}
return $html.'<p>下载文件后再导入客户端。</p><p>温馨提示:</p><p>保留原来的使用提示。</p>';
}
public function test_malformed_legacy_markup_keeps_exact_urls_zero_count_notes_and_region_order(): void
{
$r = FreeNodeArticleLayout::prepare($this->html());
$this->assertNotNull($r);$this->assertSame(0, $r['count']);$this->assertSame('3M/S', $r['speed']);
$this->assertSame(['日本','新加坡'], $r['regions']);
$this->assertSame(['clash','v2ray','singbox'], array_column($r['subscriptions'], 'key'));
$this->assertSame('https://example.invalid/cfn/20261031-clash.yaml?key=a&b=2', $r['subscriptions'][0]['url']);
$this->assertStringContainsString('下载文件后再导入客户端', $r['notes']);
$this->assertStringContainsString('保留原来的使用提示', $r['usage']);
$this->assertStringNotContainsString('data-clipboard-text', $r['notes']);
}
public function test_older_two_format_articles_never_invent_a_singbox_url(): void
{
$r = FreeNodeArticleLayout::prepare($this->html(false));
$this->assertSame(['clash','v2ray'], array_column($r['subscriptions'], 'key'));
}
public function test_unknown_format_or_structure_returns_original_view_fallback(): void
{
$this->assertNull(FreeNodeArticleLayout::prepare('<p>自定义正文</p>'));
$this->assertNull(FreeNodeArticleLayout::prepare(str_replace('clash.yaml', 'unknown.txt', $this->html())));
$this->assertNull(FreeNodeArticleLayout::prepare(str_replace('https://example.invalid', 'javascript:alert(1)', $this->html())));
}
public function test_unknown_intro_and_ordinary_supplementary_links_are_not_removed(): void
{
$html = '<p><em>保留新的编辑说明。</em></p>'.str_replace('<p>温馨提示:</p>', '<p><a href="/client">查看更多教程</a></p><p>温馨提示:</p>', $this->html());
$r = FreeNodeArticleLayout::prepare($html);
$this->assertStringContainsString('保留新的编辑说明', $r['intro']);
$this->assertStringContainsString('href="/client"', $r['notes']);
}
}