]>
Commit | Line | Data |
---|---|---|
1557cefb A |
1 | <?php |
2 | ||
fe3713d2 V |
3 | namespace Shaarli\Bookmark; |
4 | ||
5 | use ReferenceLinkDB; | |
6 | ||
7 | require_once 'tests/utils/CurlUtils.php'; | |
1557cefb A |
8 | |
9 | /** | |
fe3713d2 V |
10 | * Class LinkUtilsTest. |
11 | */ | |
12 | class LinkUtilsTest extends \PHPUnit\Framework\TestCase | |
1557cefb A |
13 | { |
14 | /** | |
15 | * Test html_extract_title() when the title is found. | |
16 | */ | |
17 | public function testHtmlExtractExistentTitle() | |
18 | { | |
19 | $title = 'Read me please.'; | |
fe3713d2 | 20 | $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>'; |
1557cefb | 21 | $this->assertEquals($title, html_extract_title($html)); |
fe3713d2 | 22 | $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>'; |
68ea1d2b | 23 | $this->assertEquals($title, html_extract_title($html)); |
1557cefb A |
24 | } |
25 | ||
26 | /** | |
27 | * Test html_extract_title() when the title is not found. | |
28 | */ | |
29 | public function testHtmlExtractNonExistentTitle() | |
30 | { | |
31 | $html = '<html><meta>stuff</meta></html>'; | |
32 | $this->assertFalse(html_extract_title($html)); | |
33 | } | |
34 | ||
1557cefb A |
35 | /** |
36 | * Test headers_extract_charset() when the charset is found. | |
37 | */ | |
38 | public function testHeadersExtractExistentCharset() | |
39 | { | |
40 | $charset = 'x-MacCroatian'; | |
fe3713d2 | 41 | $headers = 'text/html; charset=' . $charset; |
d65342e3 | 42 | $this->assertEquals(strtolower($charset), header_extract_charset($headers)); |
1557cefb A |
43 | } |
44 | ||
45 | /** | |
46 | * Test headers_extract_charset() when the charset is not found. | |
47 | */ | |
48 | public function testHeadersExtractNonExistentCharset() | |
49 | { | |
d65342e3 A |
50 | $headers = ''; |
51 | $this->assertFalse(header_extract_charset($headers)); | |
1557cefb | 52 | |
d65342e3 A |
53 | $headers = 'text/html'; |
54 | $this->assertFalse(header_extract_charset($headers)); | |
1557cefb A |
55 | } |
56 | ||
57 | /** | |
58 | * Test html_extract_charset() when the charset is found. | |
59 | */ | |
60 | public function testHtmlExtractExistentCharset() | |
61 | { | |
62 | $charset = 'x-MacCroatian'; | |
fe3713d2 | 63 | $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>'; |
1557cefb A |
64 | $this->assertEquals(strtolower($charset), html_extract_charset($html)); |
65 | } | |
66 | ||
67 | /** | |
68 | * Test html_extract_charset() when the charset is not found. | |
69 | */ | |
70 | public function testHtmlExtractNonExistentCharset() | |
71 | { | |
72 | $html = '<html><meta>stuff</meta></html>'; | |
73 | $this->assertFalse(html_extract_charset($html)); | |
74 | $html = '<html><meta>stuff</meta><meta charset=""/></html>'; | |
75 | $this->assertFalse(html_extract_charset($html)); | |
76 | } | |
141a86c5 | 77 | |
d65342e3 A |
78 | /** |
79 | * Test the download callback with valid value | |
80 | */ | |
81 | public function testCurlDownloadCallbackOk() | |
82 | { | |
83 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); | |
84 | $data = [ | |
85 | 'HTTP/1.1 200 OK', | |
86 | 'Server: GitHub.com', | |
87 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | |
88 | 'Content-Type: text/html; charset=utf-8', | |
89 | 'Status: 200 OK', | |
9d9f6d75 | 90 | 'end' => 'th=device-width">' |
fe3713d2 V |
91 | . '<title>Refactoring · GitHub</title>' |
92 | . '<link rel="search" type="application/opensea', | |
d65342e3 A |
93 | '<title>ignored</title>', |
94 | ]; | |
95 | foreach ($data as $key => $line) { | |
96 | $ignore = null; | |
97 | $expected = $key !== 'end' ? strlen($line) : false; | |
98 | $this->assertEquals($expected, $callback($ignore, $line)); | |
99 | if ($expected === false) { | |
100 | break; | |
101 | } | |
102 | } | |
103 | $this->assertEquals('utf-8', $charset); | |
104 | $this->assertEquals('Refactoring · GitHub', $title); | |
105 | } | |
106 | ||
107 | /** | |
108 | * Test the download callback with valid values and no charset | |
109 | */ | |
110 | public function testCurlDownloadCallbackOkNoCharset() | |
111 | { | |
112 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); | |
113 | $data = [ | |
114 | 'HTTP/1.1 200 OK', | |
9d9f6d75 | 115 | 'end' => 'th=device-width">' |
fe3713d2 V |
116 | . '<title>Refactoring · GitHub</title>' |
117 | . '<link rel="search" type="application/opensea', | |
d65342e3 A |
118 | '<title>ignored</title>', |
119 | ]; | |
120 | foreach ($data as $key => $line) { | |
121 | $ignore = null; | |
122 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | |
123 | } | |
124 | $this->assertEmpty($charset); | |
125 | $this->assertEquals('Refactoring · GitHub', $title); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Test the download callback with valid values and no charset | |
130 | */ | |
131 | public function testCurlDownloadCallbackOkHtmlCharset() | |
132 | { | |
133 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); | |
134 | $data = [ | |
135 | 'HTTP/1.1 200 OK', | |
136 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', | |
9d9f6d75 | 137 | 'end' => 'th=device-width">' |
fe3713d2 V |
138 | . '<title>Refactoring · GitHub</title>' |
139 | . '<link rel="search" type="application/opensea', | |
d65342e3 A |
140 | '<title>ignored</title>', |
141 | ]; | |
142 | foreach ($data as $key => $line) { | |
143 | $ignore = null; | |
144 | $expected = $key !== 'end' ? strlen($line) : false; | |
145 | $this->assertEquals($expected, $callback($ignore, $line)); | |
146 | if ($expected === false) { | |
147 | break; | |
148 | } | |
149 | } | |
150 | $this->assertEquals('utf-8', $charset); | |
151 | $this->assertEquals('Refactoring · GitHub', $title); | |
152 | } | |
153 | ||
154 | /** | |
155 | * Test the download callback with valid values and no title | |
156 | */ | |
157 | public function testCurlDownloadCallbackOkNoTitle() | |
158 | { | |
159 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); | |
160 | $data = [ | |
161 | 'HTTP/1.1 200 OK', | |
162 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', | |
163 | 'ignored', | |
164 | ]; | |
165 | foreach ($data as $key => $line) { | |
166 | $ignore = null; | |
167 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | |
168 | } | |
169 | $this->assertEquals('utf-8', $charset); | |
170 | $this->assertEmpty($title); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Test the download callback with an invalid content type. | |
175 | */ | |
176 | public function testCurlDownloadCallbackInvalidContentType() | |
177 | { | |
178 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ct_ko'); | |
179 | $ignore = null; | |
180 | $this->assertFalse($callback($ignore, '')); | |
181 | $this->assertEmpty($charset); | |
182 | $this->assertEmpty($title); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Test the download callback with an invalid response code. | |
187 | */ | |
188 | public function testCurlDownloadCallbackInvalidResponseCode() | |
189 | { | |
190 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rc_ko'); | |
191 | $ignore = null; | |
192 | $this->assertFalse($callback($ignore, '')); | |
193 | $this->assertEmpty($charset); | |
194 | $this->assertEmpty($title); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Test the download callback with an invalid content type and response code. | |
199 | */ | |
200 | public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode() | |
201 | { | |
202 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rs_ct_ko'); | |
203 | $ignore = null; | |
204 | $this->assertFalse($callback($ignore, '')); | |
205 | $this->assertEmpty($charset); | |
206 | $this->assertEmpty($title); | |
207 | } | |
208 | ||
141a86c5 A |
209 | /** |
210 | * Test count_private. | |
211 | */ | |
212 | public function testCountPrivateLinks() | |
213 | { | |
214 | $refDB = new ReferenceLinkDB(); | |
215 | $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks())); | |
216 | } | |
9ccca401 A |
217 | |
218 | /** | |
520d2957 | 219 | * Test text2clickable. |
9ccca401 | 220 | */ |
520d2957 | 221 | public function testText2clickable() |
9ccca401 A |
222 | { |
223 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
9d9f6d75 | 224 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">' |
fe3713d2 | 225 | . 'http://hello.there/is=someone#here</a> otherstuff'; |
520d2957 | 226 | $processedText = text2clickable($text); |
9ccca401 | 227 | $this->assertEquals($expectedText, $processedText); |
601faf97 A |
228 | |
229 | $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; | |
9d9f6d75 | 230 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' |
fe3713d2 | 231 | . 'http://hello.there/is=someone#here(please)</a> otherstuff'; |
520d2957 | 232 | $processedText = text2clickable($text); |
601faf97 A |
233 | $this->assertEquals($expectedText, $processedText); |
234 | ||
520d2957 | 235 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
601faf97 | 236 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
9d9f6d75 | 237 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">' |
fe3713d2 | 238 | . 'http://hello.there/is=someone#here(please)&no</a> otherstuff'; |
520d2957 | 239 | $processedText = text2clickable($text); |
fd08b50a A |
240 | $this->assertEquals($expectedText, $processedText); |
241 | } | |
242 | ||
9ccca401 A |
243 | /** |
244 | * Test testSpace2nbsp. | |
245 | */ | |
246 | public function testSpace2nbsp() | |
247 | { | |
fe3713d2 V |
248 | $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; |
249 | $expectedText = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; | |
9ccca401 A |
250 | $processedText = space2nbsp($text); |
251 | $this->assertEquals($expectedText, $processedText); | |
252 | } | |
253 | ||
254 | /** | |
255 | * Test hashtags auto-link. | |
256 | */ | |
257 | public function testHashtagAutolink() | |
258 | { | |
259 | $index = 'http://domain.tld/'; | |
260 | $rawDescription = '#hashtag\n | |
261 | # nothashtag\n | |
262 | test#nothashtag #hashtag \#nothashtag\n | |
263 | test #hashtag #hashtag test #hashtag.test\n | |
264 | #hashtag #hashtag-nothashtag #hashtag_hashtag\n | |
265 | What is #ашок anyway?\n | |
266 | カタカナ #カタカナ」カタカナ\n'; | |
267 | $autolinkedDescription = hashtag_autolink($rawDescription, $index); | |
268 | ||
269 | $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription); | |
270 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | |
271 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | |
272 | $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription); | |
273 | $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); | |
274 | $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); | |
275 | $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); | |
276 | } | |
277 | ||
278 | /** | |
279 | * Test hashtags auto-link without index URL. | |
280 | */ | |
281 | public function testHashtagAutolinkNoIndex() | |
282 | { | |
283 | $rawDescription = 'blabla #hashtag x#nothashtag'; | |
284 | $autolinkedDescription = hashtag_autolink($rawDescription); | |
285 | ||
286 | $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription); | |
287 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | |
288 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | |
289 | } | |
290 | ||
a8e7da01 A |
291 | /** |
292 | * Test is_note with note URLs. | |
293 | */ | |
294 | public function testIsNote() | |
295 | { | |
296 | $this->assertTrue(is_note('?')); | |
297 | $this->assertTrue(is_note('?abcDEf')); | |
298 | $this->assertTrue(is_note('?_abcDEf#123')); | |
299 | } | |
300 | ||
301 | /** | |
302 | * Test is_note with non note URLs. | |
303 | */ | |
304 | public function testIsNotNote() | |
305 | { | |
306 | $this->assertFalse(is_note('')); | |
307 | $this->assertFalse(is_note('nope')); | |
308 | $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi')); | |
309 | } | |
310 | ||
9ccca401 A |
311 | /** |
312 | * Util function to build an hashtag link. | |
313 | * | |
314 | * @param string $hashtag Hashtag name. | |
fe3713d2 | 315 | * @param string $index Index URL. |
9ccca401 A |
316 | * |
317 | * @return string HTML hashtag link. | |
318 | */ | |
319 | private function getHashtagLink($hashtag, $index = '') | |
320 | { | |
fe3713d2 | 321 | $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>'; |
9ccca401 A |
322 | return str_replace('$1', $hashtag, $hashtagLink); |
323 | } | |
1557cefb | 324 | } |