forked from andreskrey/readability.php
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathPhpFeaturesTest.php
More file actions
129 lines (111 loc) · 4.95 KB
/
Copy pathPhpFeaturesTest.php
File metadata and controls
129 lines (111 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
declare(strict_types=1);
namespace fivefilters\Readability\Test;
use fivefilters\Readability\Readability;
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
/**
* PHP-specific features that are not part of Readability.js: image extraction,
* the keep-inline-byline option, and PSR-3 logging. (Reinstated from 3.x.)
*/
class PhpFeaturesTest extends TestCase
{
private const string BODY = '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam quis nostrud.</p>';
private static function page(string $head = '', string $article = ''): string
{
$paragraphs = str_repeat(self::BODY, 12);
return "<html><head><title>Test</title>{$head}</head><body><article>{$article}{$paragraphs}</article></body></html>";
}
public function testLeadImageFromOgImage(): void
{
$html = self::page('<meta property="og:image" content="https://example.com/lead.jpg">');
$article = new Readability()->parse($html);
$this->assertSame('https://example.com/lead.jpg', $article->image);
$this->assertContains('https://example.com/lead.jpg', $article->images);
}
public function testLeadImageFromTwitterImage(): void
{
$html = self::page('<meta name="twitter:image" content="https://example.com/tw.jpg">');
$article = new Readability()->parse($html);
$this->assertSame('https://example.com/tw.jpg', $article->image);
}
public function testLeadImageFromLinkRel(): void
{
$html = self::page('<link rel="image_src" href="https://example.com/link.jpg">');
$article = new Readability()->parse($html);
$this->assertSame('https://example.com/link.jpg', $article->image);
}
public function testNoLeadImage(): void
{
$article = new Readability()->parse(self::page());
$this->assertNull($article->image);
$this->assertSame([], $article->images);
}
public function testImagesListCollectsContentImagesAndDeduplicates(): void
{
$html = self::page(
'<meta property="og:image" content="https://example.com/lead.jpg">',
'<img src="https://example.com/a.jpg"><img src="https://example.com/b.jpg"><img src="https://example.com/lead.jpg">'
);
$article = new Readability()->parse($html);
// Lead image first, then content images, with the duplicate lead image collapsed.
$this->assertSame([
'https://example.com/lead.jpg',
'https://example.com/a.jpg',
'https://example.com/b.jpg',
], $article->images);
}
public function testImagesAreAbsolutizedWhenFixRelativeUrlsEnabled(): void
{
$html = self::page(
'<meta property="og:image" content="/lead.jpg">',
'<img src="pics/inline.jpg">'
);
$article = new Readability(
fixRelativeURLs: true,
originalURL: 'https://example.com/news/',
)->parse($html);
$this->assertSame('https://example.com/lead.jpg', $article->image);
$this->assertSame([
'https://example.com/lead.jpg',
'https://example.com/news/pics/inline.jpg',
], $article->images);
}
public function testInlineBylineRemovedByDefault(): void
{
$html = self::page('', '<p class="byline">By Jane Doe</p>');
$article = new Readability()->parse($html);
$this->assertSame('By Jane Doe', $article->byline);
$this->assertStringNotContainsString('By Jane Doe', $article->content);
}
public function testInlineBylineKeptWhenConfigured(): void
{
$html = self::page('', '<p class="byline">By Jane Doe</p>');
$article = new Readability(keepInlineByline: true)->parse($html);
// Still recorded as metadata...
$this->assertSame('By Jane Doe', $article->byline);
// ...but left in the content.
$this->assertStringContainsString('By Jane Doe', $article->content);
}
public function testPsrLoggerReceivesMessages(): void
{
$logger = new class extends AbstractLogger {
/** @var list<string> */
public array $messages = [];
// Untyped $level/$message keep this compatible with every psr/log major (v1 has no parameter types).
public function log($level, $message, array $context = []): void
{
$this->messages[] = (string) $message;
}
};
new Readability(logger: $logger)->parse(self::page());
$this->assertNotEmpty($logger->messages, 'logger should receive debug messages even with debug=false');
}
public function testNoLoggingWithoutDebugOrLogger(): void
{
// With neither a logger nor the debug flag, log() is a no-op; this
// parse should simply succeed without touching error_log.
$article = new Readability()->parse(self::page());
$this->assertNotSame('', $article->content);
}
}