Files
fn_b/tests/Unit/AppleIdArticleLayoutTest.php
T

86 lines
4.7 KiB
PHP

<?php
namespace Tests\Unit;
use App\Services\AppleIdArticleLayout;
use PHPUnit\Framework\TestCase;
final class AppleIdArticleLayoutTest extends TestCase
{
private function row(string $account, string $password, string $region = '美区'): string
{
$a = htmlspecialchars($account, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$p = htmlspecialchars($password, ENT_QUOTES | ENT_HTML5, 'UTF-8');
return '<p><p>账号:<a data-clipboard-text="'.$a.'">'.$a.'</a>,密码:<a data-clipboard-text="'.$p.'">'.$p.'</a>,地区:'.$region.',检测时间:<span>2026-11-01 09:01:02</span></p></p>';
}
public function test_legacy_nested_paragraphs_preserve_exact_copy_values_and_timestamps(): void
{
$html = '<p><h2>免费苹果账号(用于使用小火箭、shadowrocket):</h2></p>'
.$this->row('person+&[email protected]', 'p<&"\'! $0');
$r = AppleIdArticleLayout::prepare($html);
self::assertNotNull($r);
self::assertSame('person+&[email protected]', $r['accounts'][0]['account']);
self::assertSame('p<&"\'! $0', $r['accounts'][0]['password']);
self::assertSame('2026-11-01 09:01:02', $r['accounts'][0]['checked_at']);
self::assertSame(['美区' => 1], $r['regions']);
}
public function test_duplicate_accounts_and_regions_remain_in_original_order(): void
{
$r = AppleIdArticleLayout::prepare('<h2>免费苹果账号</h2>'.$this->row('[email protected]', 'first', '香港').$this->row('[email protected]', 'second', '香港').$this->row('[email protected]', '0', '未知'));
self::assertCount(3, $r['accounts']);
self::assertSame(['first', 'second', '0'], array_column($r['accounts'], 'password'));
self::assertSame(['香港' => 2, '未知' => 1], $r['regions']);
}
public function test_identical_app_store_warnings_merge_but_supplementary_content_remains(): void
{
$warning = '<p>⚠️ 使用免费共享苹果账号必须从App Store登录,千万不要登录「iCloud」。</p>';
$html = '<h2>免费苹果账号</h2><p><em>额外说明</em></p>'.str_repeat($warning, 3).$this->row('[email protected]', 'example').'<p><a href="/client/ios">iOS教程</a></p>';
$r = AppleIdArticleLayout::prepare($html);
self::assertCount(1, $r['warnings']);
self::assertStringContainsString('iCloud', $r['warnings'][0]);
self::assertStringContainsString('额外说明', $r['intro']);
self::assertStringContainsString('href="/client/ios"', $r['outro']);
}
public function test_incomplete_extra_or_unknown_copy_structures_use_legacy_fallback(): void
{
$html = '<h2>免费苹果账号</h2>'.$this->row('[email protected]', 'example');
self::assertNull(AppleIdArticleLayout::prepare($html.'<p><a data-clipboard-text="extra">附加字段</a></p>'));
self::assertNull(AppleIdArticleLayout::prepare(str_replace('密码:', '未知字段:', $html)));
self::assertNull(AppleIdArticleLayout::prepare('<h2>免费苹果账号</h2><section>'.$this->row('[email protected]', 'example').'</section>'));
self::assertNull(AppleIdArticleLayout::prepare('<p>普通文章</p>'));
}
public function test_source_html_is_not_modified_and_recommendation_slot_is_not_repeated(): void
{
$html = '<h2>免费苹果账号</h2><!-- fna-airport-recommendations -->'.$this->row('[email protected]', 'example');
$before = $html;
$r = AppleIdArticleLayout::prepare($html);
self::assertSame($before, $html);
self::assertStringNotContainsString('fna-airport-recommendations', $r['intro'].$r['outro']);
}
public function test_missing_region_or_check_time_does_not_drop_an_account_or_invent_a_time(): void
{
$html = '<h2>免费苹果账号</h2>'.str_replace(['地区:美区', '2026-11-01 09:01:02'], ['地区:', ''], $this->row('[email protected]', 'example'));
$r = AppleIdArticleLayout::prepare($html);
self::assertCount(1, $r['accounts']);
self::assertSame('未注明', $r['accounts'][0]['region']);
self::assertSame('', $r['accounts'][0]['checked_at']);
}
public function test_empty_historical_update_has_no_invented_accounts(): void
{
$r = AppleIdArticleLayout::prepare('<h2>免费苹果账号</h2><p>必须从App Store登录,千万不要登录iCloud。</p>');
self::assertNotNull($r);
self::assertSame([], $r['accounts']);
self::assertSame([], $r['regions']);
self::assertCount(1, $r['warnings']);
self::assertNull(AppleIdArticleLayout::prepare('<h2>免费苹果账号</h2><p>账号:[email protected],密码:example</p>'));
self::assertNull(AppleIdArticleLayout::prepare('<p>免费苹果ID说明</p>'));
}
}