77 lines
4.3 KiB
PHP
77 lines
4.3 KiB
PHP
<?php
|
||
|
||
namespace Tests\Feature;
|
||
|
||
use App\Models\Articles;
|
||
use App\Models\Categories;
|
||
use App\Services\ArticleRecommendations;
|
||
use Tests\TestCase;
|
||
|
||
class ArticleRecommendationsTest extends TestCase
|
||
{
|
||
private function article(string $slug, string $html, ?string $parent = null): Articles
|
||
{
|
||
$category = new Categories();$category->setRawAttributes(['sSlug' => $slug]);
|
||
$ancestor = new Categories();$ancestor->setRawAttributes(['sSlug' => $parent]);
|
||
$category->setRelation('oParent', $parent ? $ancestor : null);
|
||
$article = new Articles();$article->setRawAttributes(['sContent' => $html]);
|
||
$article->setRelation('oCategory', $category);
|
||
return $article;
|
||
}
|
||
|
||
public function test_legacy_promotion_is_replaced_without_touching_subscription_or_account_content(): void
|
||
{
|
||
foreach (['freenode', 'shadowrocketids'] as $category) {
|
||
$tail = '<p><h2>免费资源:</h2></p><p><a data-clipboard-text="https://example.invalid/private-sub">复制订阅</a></p><pre>account:example</pre>';
|
||
$html = '<p>文章开头</p><p><h2>机场推荐 - 福云:</h2></p><p>* 七折,折后8元</p><p><b><a target="__blank" href="https://fuuu.cloud">👉 点击进入机场官网:福云。</a></b></p>'.$tail;
|
||
$article = $this->article($category, $html);
|
||
$prepared = ArticleRecommendations::prepare($article);
|
||
$this->assertTrue($prepared['inline']);
|
||
$this->assertSame('<p>文章开头</p>'.ArticleRecommendations::SLOT.$tail, $prepared['body']);
|
||
$this->assertSame($html, $article->sContent);
|
||
}
|
||
}
|
||
|
||
public function test_no_promotion_link_means_no_destructive_guess_and_a_single_append(): void
|
||
{
|
||
$html = '<h2>机场推荐 - 福云:</h2><p>用户另写的内容</p><h2>订阅</h2><p>保留</p>';
|
||
$result = ArticleRecommendations::prepare($this->article('freenode', $html));
|
||
$this->assertTrue($result['enabled']);$this->assertFalse($result['inline']);
|
||
$this->assertSame($html, $result['body']);
|
||
}
|
||
|
||
public function test_all_intro_subcategories_append_and_advanced_articles_have_one_working_jump(): void
|
||
{
|
||
$html = '<div class="tutorial-body"><p>先配置订阅。</p><p>连接线路。</p></div>';
|
||
foreach (['windows','andorid','ios','mac','linux'] as $category) {
|
||
$result = ArticleRecommendations::prepare($this->article($category, $html, 'client'));
|
||
$this->assertTrue($result['enabled']);$this->assertFalse($result['inline']);$this->assertSame($html, $result['body']);
|
||
}
|
||
$article = $this->article('advanced', $html);
|
||
$first = ArticleRecommendations::prepare($article);
|
||
$this->assertSame(1, substr_count($first['body'], 'href="#airport-recommendations"'));
|
||
$again = ArticleRecommendations::prepare($this->article('advanced', $first['body']));
|
||
$this->assertSame($first['body'], $again['body']);
|
||
}
|
||
|
||
public function test_disable_and_unrelated_categories_leave_content_unchanged(): void
|
||
{
|
||
$this->assertFalse(ArticleRecommendations::prepare($this->article('news', '<p>正文</p>'))['enabled']);
|
||
config(['airport_recommendations.enabled' => false]);
|
||
$result = ArticleRecommendations::prepare($this->article('advanced', '<p>订阅</p>'));
|
||
$this->assertFalse($result['enabled']);$this->assertSame('<p>订阅</p>', $result['body']);
|
||
}
|
||
|
||
public function test_shared_view_outputs_configured_prices_coupon_and_sponsored_links_without_javascript(): void
|
||
{
|
||
$html = view('components.airport-recommendations')->render();
|
||
foreach (['福云', 'DP-LINK.NET', 'hardnerd', '>18<', '>26<', '>28<'] as $text) $this->assertStringContainsString($text, $html);
|
||
$this->assertSame(1, substr_count($html, '<section id="airport-recommendations"'));
|
||
$this->assertSame(3, substr_count($html, 'rel="sponsored nofollow noopener noreferrer"'));
|
||
$this->assertSame(3, substr_count($html, 'data-airport-coupon="clashfreenode.com"'));
|
||
config(['airport_recommendations.providers.fuc.price' => '20', 'airport_recommendations.providers.dpl.enabled' => false]);
|
||
$updated = view('components.airport-recommendations')->render();
|
||
$this->assertStringContainsString('>20<', $updated);$this->assertStringNotContainsString('DP-LINK.NET', $updated);
|
||
}
|
||
}
|