diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-11-12 13:11:07 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-11-12 13:11:07 +0100 |
commit | af50eba28a7bd286de4c8c9ee6dc5216b915d149 (patch) | |
tree | ffa30a9358e82d27be75d8fc5e57f3c8820dc6d3 /tests/bookmark/LinkUtilsTest.php | |
parent | b6f678a5a1d15acf284ebcec16c905e976671ce1 (diff) | |
parent | 1409f1c89a7ca01456ae2dcd6357d296e2b99f5a (diff) | |
download | Shaarli-latest.tar.gz Shaarli-latest.tar.zst Shaarli-latest.zip |
Merge tag 'v0.12.1' into latestlatest
v0.12.1
Diffstat (limited to 'tests/bookmark/LinkUtilsTest.php')
-rw-r--r-- | tests/bookmark/LinkUtilsTest.php | 446 |
1 files changed, 336 insertions, 110 deletions
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index ef00b92f..ddab4e3c 100644 --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php | |||
@@ -94,8 +94,108 @@ class LinkUtilsTest extends TestCase | |||
94 | public function testHtmlExtractExistentNameTag() | 94 | public function testHtmlExtractExistentNameTag() |
95 | { | 95 | { |
96 | $description = 'Bob and Alice share cookies.'; | 96 | $description = 'Bob and Alice share cookies.'; |
97 | |||
98 | // Simple one line | ||
97 | $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>'; | 99 | $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>'; |
98 | $this->assertEquals($description, html_extract_tag('description', $html)); | 100 | $this->assertEquals($description, html_extract_tag('description', $html)); |
101 | |||
102 | // Simple OpenGraph | ||
103 | $html = '<meta property="og:description" content="' . $description . '">'; | ||
104 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
105 | |||
106 | // Simple reversed OpenGraph | ||
107 | $html = '<meta content="' . $description . '" property="og:description">'; | ||
108 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
109 | |||
110 | // ItemProp OpenGraph | ||
111 | $html = '<meta itemprop="og:description" content="' . $description . '">'; | ||
112 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
113 | |||
114 | // OpenGraph without quotes | ||
115 | $html = '<meta property=og:description content="' . $description . '">'; | ||
116 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
117 | |||
118 | // OpenGraph reversed without quotes | ||
119 | $html = '<meta content="' . $description . '" property=og:description>'; | ||
120 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
121 | |||
122 | // OpenGraph with noise | ||
123 | $html = '<meta tag1="content1" property="og:description" tag2="content2" content="' . | ||
124 | $description . '" tag3="content3">'; | ||
125 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
126 | |||
127 | // OpenGraph reversed with noise | ||
128 | $html = '<meta tag1="content1" content="' . $description . '" ' . | ||
129 | 'tag3="content3" tag2="content2" property="og:description">'; | ||
130 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
131 | |||
132 | // OpenGraph multiple properties start | ||
133 | $html = '<meta property="unrelated og:description" content="' . $description . '">'; | ||
134 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
135 | |||
136 | // OpenGraph multiple properties end | ||
137 | $html = '<meta property="og:description unrelated" content="' . $description . '">'; | ||
138 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
139 | |||
140 | // OpenGraph multiple properties both end | ||
141 | $html = '<meta property="og:unrelated1 og:description og:unrelated2" content="' . $description . '">'; | ||
142 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
143 | |||
144 | // OpenGraph multiple properties both end with noise | ||
145 | $html = '<meta tag1="content1" property="og:unrelated1 og:description og:unrelated2" '. | ||
146 | 'tag2="content2" content="' . $description . '" tag3="content3">'; | ||
147 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
148 | |||
149 | // OpenGraph reversed multiple properties start | ||
150 | $html = '<meta content="' . $description . '" property="unrelated og:description">'; | ||
151 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
152 | |||
153 | // OpenGraph reversed multiple properties end | ||
154 | $html = '<meta content="' . $description . '" property="og:description unrelated">'; | ||
155 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
156 | |||
157 | // OpenGraph reversed multiple properties both end | ||
158 | $html = '<meta content="' . $description . '" property="og:unrelated1 og:description og:unrelated2">'; | ||
159 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
160 | |||
161 | // OpenGraph reversed multiple properties both end with noise | ||
162 | $html = '<meta tag1="content1" content="' . $description . '" tag2="content2" '. | ||
163 | 'property="og:unrelated1 og:description og:unrelated2" tag3="content3">'; | ||
164 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
165 | |||
166 | // Suggestion from #1375 | ||
167 | $html = '<meta property="og:description" name="description" content="' . $description . '">'; | ||
168 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * Test html_extract_tag() with double quoted content containing single quote, and the opposite. | ||
173 | */ | ||
174 | public function testHtmlExtractExistentNameTagWithMixedQuotes(): void | ||
175 | { | ||
176 | $description = 'Bob and Alice share M&M\'s.'; | ||
177 | |||
178 | $html = '<meta property="og:description" content="' . $description . '">'; | ||
179 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
180 | |||
181 | $html = '<meta tag1="content1" property="og:unrelated1 og:description og:unrelated2" '. | ||
182 | 'tag2="content2" content="' . $description . '" tag3="content3">'; | ||
183 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
184 | |||
185 | $html = '<meta property="og:description" name="description" content="' . $description . '">'; | ||
186 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
187 | |||
188 | $description = 'Bob and Alice share "cookies".'; | ||
189 | |||
190 | $html = '<meta property="og:description" content=\'' . $description . '\'>'; | ||
191 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
192 | |||
193 | $html = '<meta tag1="content1" property="og:unrelated1 og:description og:unrelated2" '. | ||
194 | 'tag2="content2" content=\'' . $description . '\' tag3="content3">'; | ||
195 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
196 | |||
197 | $html = '<meta property="og:description" name="description" content=\'' . $description . '\'>'; | ||
198 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
99 | } | 199 | } |
100 | 200 | ||
101 | /** | 201 | /** |
@@ -105,6 +205,25 @@ class LinkUtilsTest extends TestCase | |||
105 | { | 205 | { |
106 | $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>'; | 206 | $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>'; |
107 | $this->assertFalse(html_extract_tag('description', $html)); | 207 | $this->assertFalse(html_extract_tag('description', $html)); |
208 | |||
209 | // Partial meta tag | ||
210 | $html = '<meta content="Brief description">'; | ||
211 | $this->assertFalse(html_extract_tag('description', $html)); | ||
212 | |||
213 | $html = '<meta property="og:description">'; | ||
214 | $this->assertFalse(html_extract_tag('description', $html)); | ||
215 | |||
216 | $html = '<meta tag1="content1" property="og:description">'; | ||
217 | $this->assertFalse(html_extract_tag('description', $html)); | ||
218 | |||
219 | $html = '<meta property="og:description" tag1="content1">'; | ||
220 | $this->assertFalse(html_extract_tag('description', $html)); | ||
221 | |||
222 | $html = '<meta tag1="content1" content="Brief description">'; | ||
223 | $this->assertFalse(html_extract_tag('description', $html)); | ||
224 | |||
225 | $html = '<meta content="Brief description" tag1="content1">'; | ||
226 | $this->assertFalse(html_extract_tag('description', $html)); | ||
108 | } | 227 | } |
109 | 228 | ||
110 | /** | 229 | /** |
@@ -127,60 +246,93 @@ class LinkUtilsTest extends TestCase | |||
127 | } | 246 | } |
128 | 247 | ||
129 | /** | 248 | /** |
249 | * Test the header callback with valid value | ||
250 | */ | ||
251 | public function testCurlHeaderCallbackOk(): void | ||
252 | { | ||
253 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ok'); | ||
254 | $data = [ | ||
255 | 'HTTP/1.1 200 OK', | ||
256 | 'Server: GitHub.com', | ||
257 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
258 | 'Content-Type: text/html; charset=utf-8', | ||
259 | 'Status: 200 OK', | ||
260 | ]; | ||
261 | |||
262 | foreach ($data as $chunk) { | ||
263 | static::assertIsInt($callback(null, $chunk)); | ||
264 | } | ||
265 | |||
266 | static::assertSame('utf-8', $charset); | ||
267 | } | ||
268 | |||
269 | /** | ||
130 | * Test the download callback with valid value | 270 | * Test the download callback with valid value |
131 | */ | 271 | */ |
132 | public function testCurlDownloadCallbackOk() | 272 | public function testCurlDownloadCallbackOk(): void |
133 | { | 273 | { |
274 | $charset = 'utf-8'; | ||
134 | $callback = get_curl_download_callback( | 275 | $callback = get_curl_download_callback( |
135 | $charset, | 276 | $charset, |
136 | $title, | 277 | $title, |
137 | $desc, | 278 | $desc, |
138 | $keywords, | 279 | $keywords, |
139 | false, | 280 | false, |
140 | 'ut_curl_getinfo_ok' | 281 | ' ' |
141 | ); | 282 | ); |
283 | |||
142 | $data = [ | 284 | $data = [ |
143 | 'HTTP/1.1 200 OK', | 285 | 'th=device-width">' |
144 | 'Server: GitHub.com', | ||
145 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
146 | 'Content-Type: text/html; charset=utf-8', | ||
147 | 'Status: 200 OK', | ||
148 | 'end' => 'th=device-width">' | ||
149 | . '<title>Refactoring · GitHub</title>' | 286 | . '<title>Refactoring · GitHub</title>' |
150 | . '<link rel="search" type="application/opensea', | 287 | . '<link rel="search" type="application/opensea', |
151 | '<title>ignored</title>' | 288 | '<title>ignored</title>' |
152 | . '<meta name="description" content="desc" />' | 289 | . '<meta name="description" content="desc" />' |
153 | . '<meta name="keywords" content="key1,key2" />', | 290 | . '<meta name="keywords" content="key1,key2" />', |
154 | ]; | 291 | ]; |
155 | foreach ($data as $key => $line) { | 292 | |
156 | $ignore = null; | 293 | foreach ($data as $chunk) { |
157 | $expected = $key !== 'end' ? strlen($line) : false; | 294 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
158 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
159 | if ($expected === false) { | ||
160 | break; | ||
161 | } | ||
162 | } | 295 | } |
163 | $this->assertEquals('utf-8', $charset); | 296 | |
164 | $this->assertEquals('Refactoring · GitHub', $title); | 297 | static::assertSame('utf-8', $charset); |
165 | $this->assertEmpty($desc); | 298 | static::assertSame('Refactoring · GitHub', $title); |
166 | $this->assertEmpty($keywords); | 299 | static::assertEmpty($desc); |
300 | static::assertEmpty($keywords); | ||
301 | } | ||
302 | |||
303 | /** | ||
304 | * Test the header callback with valid value | ||
305 | */ | ||
306 | public function testCurlHeaderCallbackNoCharset(): void | ||
307 | { | ||
308 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_no_charset'); | ||
309 | $data = [ | ||
310 | 'HTTP/1.1 200 OK', | ||
311 | ]; | ||
312 | |||
313 | foreach ($data as $chunk) { | ||
314 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | ||
315 | } | ||
316 | |||
317 | static::assertFalse($charset); | ||
167 | } | 318 | } |
168 | 319 | ||
169 | /** | 320 | /** |
170 | * Test the download callback with valid values and no charset | 321 | * Test the download callback with valid values and no charset |
171 | */ | 322 | */ |
172 | public function testCurlDownloadCallbackOkNoCharset() | 323 | public function testCurlDownloadCallbackOkNoCharset(): void |
173 | { | 324 | { |
325 | $charset = null; | ||
174 | $callback = get_curl_download_callback( | 326 | $callback = get_curl_download_callback( |
175 | $charset, | 327 | $charset, |
176 | $title, | 328 | $title, |
177 | $desc, | 329 | $desc, |
178 | $keywords, | 330 | $keywords, |
179 | false, | 331 | false, |
180 | 'ut_curl_getinfo_no_charset' | 332 | ' ' |
181 | ); | 333 | ); |
334 | |||
182 | $data = [ | 335 | $data = [ |
183 | 'HTTP/1.1 200 OK', | ||
184 | 'end' => 'th=device-width">' | 336 | 'end' => 'th=device-width">' |
185 | . '<title>Refactoring · GitHub</title>' | 337 | . '<title>Refactoring · GitHub</title>' |
186 | . '<link rel="search" type="application/opensea', | 338 | . '<link rel="search" type="application/opensea', |
@@ -188,10 +340,11 @@ class LinkUtilsTest extends TestCase | |||
188 | . '<meta name="description" content="desc" />' | 340 | . '<meta name="description" content="desc" />' |
189 | . '<meta name="keywords" content="key1,key2" />', | 341 | . '<meta name="keywords" content="key1,key2" />', |
190 | ]; | 342 | ]; |
191 | foreach ($data as $key => $line) { | 343 | |
192 | $ignore = null; | 344 | foreach ($data as $chunk) { |
193 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | 345 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
194 | } | 346 | } |
347 | |||
195 | $this->assertEmpty($charset); | 348 | $this->assertEmpty($charset); |
196 | $this->assertEquals('Refactoring · GitHub', $title); | 349 | $this->assertEquals('Refactoring · GitHub', $title); |
197 | $this->assertEmpty($desc); | 350 | $this->assertEmpty($desc); |
@@ -201,18 +354,19 @@ class LinkUtilsTest extends TestCase | |||
201 | /** | 354 | /** |
202 | * Test the download callback with valid values and no charset | 355 | * Test the download callback with valid values and no charset |
203 | */ | 356 | */ |
204 | public function testCurlDownloadCallbackOkHtmlCharset() | 357 | public function testCurlDownloadCallbackOkHtmlCharset(): void |
205 | { | 358 | { |
359 | $charset = null; | ||
206 | $callback = get_curl_download_callback( | 360 | $callback = get_curl_download_callback( |
207 | $charset, | 361 | $charset, |
208 | $title, | 362 | $title, |
209 | $desc, | 363 | $desc, |
210 | $keywords, | 364 | $keywords, |
211 | false, | 365 | false, |
212 | 'ut_curl_getinfo_no_charset' | 366 | ' ' |
213 | ); | 367 | ); |
368 | |||
214 | $data = [ | 369 | $data = [ |
215 | 'HTTP/1.1 200 OK', | ||
216 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', | 370 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', |
217 | 'end' => 'th=device-width">' | 371 | 'end' => 'th=device-width">' |
218 | . '<title>Refactoring · GitHub</title>' | 372 | . '<title>Refactoring · GitHub</title>' |
@@ -221,14 +375,10 @@ class LinkUtilsTest extends TestCase | |||
221 | . '<meta name="description" content="desc" />' | 375 | . '<meta name="description" content="desc" />' |
222 | . '<meta name="keywords" content="key1,key2" />', | 376 | . '<meta name="keywords" content="key1,key2" />', |
223 | ]; | 377 | ]; |
224 | foreach ($data as $key => $line) { | 378 | foreach ($data as $chunk) { |
225 | $ignore = null; | 379 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
226 | $expected = $key !== 'end' ? strlen($line) : false; | ||
227 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
228 | if ($expected === false) { | ||
229 | break; | ||
230 | } | ||
231 | } | 380 | } |
381 | |||
232 | $this->assertEquals('utf-8', $charset); | 382 | $this->assertEquals('utf-8', $charset); |
233 | $this->assertEquals('Refactoring · GitHub', $title); | 383 | $this->assertEquals('Refactoring · GitHub', $title); |
234 | $this->assertEmpty($desc); | 384 | $this->assertEmpty($desc); |
@@ -238,25 +388,27 @@ class LinkUtilsTest extends TestCase | |||
238 | /** | 388 | /** |
239 | * Test the download callback with valid values and no title | 389 | * Test the download callback with valid values and no title |
240 | */ | 390 | */ |
241 | public function testCurlDownloadCallbackOkNoTitle() | 391 | public function testCurlDownloadCallbackOkNoTitle(): void |
242 | { | 392 | { |
393 | $charset = 'utf-8'; | ||
243 | $callback = get_curl_download_callback( | 394 | $callback = get_curl_download_callback( |
244 | $charset, | 395 | $charset, |
245 | $title, | 396 | $title, |
246 | $desc, | 397 | $desc, |
247 | $keywords, | 398 | $keywords, |
248 | false, | 399 | false, |
249 | 'ut_curl_getinfo_ok' | 400 | ' ' |
250 | ); | 401 | ); |
402 | |||
251 | $data = [ | 403 | $data = [ |
252 | 'HTTP/1.1 200 OK', | ||
253 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', | 404 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', |
254 | 'ignored', | 405 | 'ignored', |
255 | ]; | 406 | ]; |
256 | foreach ($data as $key => $line) { | 407 | |
257 | $ignore = null; | 408 | foreach ($data as $chunk) { |
258 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | 409 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
259 | } | 410 | } |
411 | |||
260 | $this->assertEquals('utf-8', $charset); | 412 | $this->assertEquals('utf-8', $charset); |
261 | $this->assertEmpty($title); | 413 | $this->assertEmpty($title); |
262 | $this->assertEmpty($desc); | 414 | $this->assertEmpty($desc); |
@@ -264,81 +416,56 @@ class LinkUtilsTest extends TestCase | |||
264 | } | 416 | } |
265 | 417 | ||
266 | /** | 418 | /** |
267 | * Test the download callback with an invalid content type. | 419 | * Test the header callback with an invalid content type. |
268 | */ | 420 | */ |
269 | public function testCurlDownloadCallbackInvalidContentType() | 421 | public function testCurlHeaderCallbackInvalidContentType(): void |
270 | { | 422 | { |
271 | $callback = get_curl_download_callback( | 423 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ct_ko'); |
272 | $charset, | 424 | $data = [ |
273 | $title, | 425 | 'HTTP/1.1 200 OK', |
274 | $desc, | 426 | ]; |
275 | $keywords, | 427 | |
276 | false, | 428 | static::assertFalse($callback(null, $data[0])); |
277 | 'ut_curl_getinfo_ct_ko' | 429 | static::assertNull($charset); |
278 | ); | ||
279 | $ignore = null; | ||
280 | $this->assertFalse($callback($ignore, '')); | ||
281 | $this->assertEmpty($charset); | ||
282 | $this->assertEmpty($title); | ||
283 | } | 430 | } |
284 | 431 | ||
285 | /** | 432 | /** |
286 | * Test the download callback with an invalid response code. | 433 | * Test the header callback with an invalid response code. |
287 | */ | 434 | */ |
288 | public function testCurlDownloadCallbackInvalidResponseCode() | 435 | public function testCurlHeaderCallbackInvalidResponseCode(): void |
289 | { | 436 | { |
290 | $callback = $callback = get_curl_download_callback( | 437 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko'); |
291 | $charset, | 438 | |
292 | $title, | 439 | static::assertFalse($callback(null, '')); |
293 | $desc, | 440 | static::assertNull($charset); |
294 | $keywords, | ||
295 | false, | ||
296 | 'ut_curl_getinfo_rc_ko' | ||
297 | ); | ||
298 | $ignore = null; | ||
299 | $this->assertFalse($callback($ignore, '')); | ||
300 | $this->assertEmpty($charset); | ||
301 | $this->assertEmpty($title); | ||
302 | } | 441 | } |
303 | 442 | ||
304 | /** | 443 | /** |
305 | * Test the download callback with an invalid content type and response code. | 444 | * Test the header callback with an invalid content type and response code. |
306 | */ | 445 | */ |
307 | public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode() | 446 | public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void |
308 | { | 447 | { |
309 | $callback = $callback = get_curl_download_callback( | 448 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko'); |
310 | $charset, | 449 | |
311 | $title, | 450 | static::assertFalse($callback(null, '')); |
312 | $desc, | 451 | static::assertNull($charset); |
313 | $keywords, | ||
314 | false, | ||
315 | 'ut_curl_getinfo_rs_ct_ko' | ||
316 | ); | ||
317 | $ignore = null; | ||
318 | $this->assertFalse($callback($ignore, '')); | ||
319 | $this->assertEmpty($charset); | ||
320 | $this->assertEmpty($title); | ||
321 | } | 452 | } |
322 | 453 | ||
323 | /** | 454 | /** |
324 | * Test the download callback with valid value, and retrieve_description option enabled. | 455 | * Test the download callback with valid value, and retrieve_description option enabled. |
325 | */ | 456 | */ |
326 | public function testCurlDownloadCallbackOkWithDesc() | 457 | public function testCurlDownloadCallbackOkWithDesc(): void |
327 | { | 458 | { |
459 | $charset = 'utf-8'; | ||
328 | $callback = get_curl_download_callback( | 460 | $callback = get_curl_download_callback( |
329 | $charset, | 461 | $charset, |
330 | $title, | 462 | $title, |
331 | $desc, | 463 | $desc, |
332 | $keywords, | 464 | $keywords, |
333 | true, | 465 | true, |
334 | 'ut_curl_getinfo_ok' | 466 | ' ' |
335 | ); | 467 | ); |
336 | $data = [ | 468 | $data = [ |
337 | 'HTTP/1.1 200 OK', | ||
338 | 'Server: GitHub.com', | ||
339 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
340 | 'Content-Type: text/html; charset=utf-8', | ||
341 | 'Status: 200 OK', | ||
342 | 'th=device-width">' | 469 | 'th=device-width">' |
343 | . '<title>Refactoring · GitHub</title>' | 470 | . '<title>Refactoring · GitHub</title>' |
344 | . '<link rel="search" type="application/opensea', | 471 | . '<link rel="search" type="application/opensea', |
@@ -346,14 +473,11 @@ class LinkUtilsTest extends TestCase | |||
346 | . '<meta name="description" content="link desc" />' | 473 | . '<meta name="description" content="link desc" />' |
347 | . '<meta name="keywords" content="key1,key2" />', | 474 | . '<meta name="keywords" content="key1,key2" />', |
348 | ]; | 475 | ]; |
349 | foreach ($data as $key => $line) { | 476 | |
350 | $ignore = null; | 477 | foreach ($data as $chunk) { |
351 | $expected = $key !== 'end' ? strlen($line) : false; | 478 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
352 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
353 | if ($expected === false) { | ||
354 | break; | ||
355 | } | ||
356 | } | 479 | } |
480 | |||
357 | $this->assertEquals('utf-8', $charset); | 481 | $this->assertEquals('utf-8', $charset); |
358 | $this->assertEquals('Refactoring · GitHub', $title); | 482 | $this->assertEquals('Refactoring · GitHub', $title); |
359 | $this->assertEquals('link desc', $desc); | 483 | $this->assertEquals('link desc', $desc); |
@@ -364,8 +488,9 @@ class LinkUtilsTest extends TestCase | |||
364 | * Test the download callback with valid value, and retrieve_description option enabled, | 488 | * Test the download callback with valid value, and retrieve_description option enabled, |
365 | * but no desc or keyword defined in the page. | 489 | * but no desc or keyword defined in the page. |
366 | */ | 490 | */ |
367 | public function testCurlDownloadCallbackOkWithDescNotFound() | 491 | public function testCurlDownloadCallbackOkWithDescNotFound(): void |
368 | { | 492 | { |
493 | $charset = 'utf-8'; | ||
369 | $callback = get_curl_download_callback( | 494 | $callback = get_curl_download_callback( |
370 | $charset, | 495 | $charset, |
371 | $title, | 496 | $title, |
@@ -375,24 +500,16 @@ class LinkUtilsTest extends TestCase | |||
375 | 'ut_curl_getinfo_ok' | 500 | 'ut_curl_getinfo_ok' |
376 | ); | 501 | ); |
377 | $data = [ | 502 | $data = [ |
378 | 'HTTP/1.1 200 OK', | ||
379 | 'Server: GitHub.com', | ||
380 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
381 | 'Content-Type: text/html; charset=utf-8', | ||
382 | 'Status: 200 OK', | ||
383 | 'th=device-width">' | 503 | 'th=device-width">' |
384 | . '<title>Refactoring · GitHub</title>' | 504 | . '<title>Refactoring · GitHub</title>' |
385 | . '<link rel="search" type="application/opensea', | 505 | . '<link rel="search" type="application/opensea', |
386 | 'end' => '<title>ignored</title>', | 506 | 'end' => '<title>ignored</title>', |
387 | ]; | 507 | ]; |
388 | foreach ($data as $key => $line) { | 508 | |
389 | $ignore = null; | 509 | foreach ($data as $chunk) { |
390 | $expected = $key !== 'end' ? strlen($line) : false; | 510 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
391 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
392 | if ($expected === false) { | ||
393 | break; | ||
394 | } | ||
395 | } | 511 | } |
512 | |||
396 | $this->assertEquals('utf-8', $charset); | 513 | $this->assertEquals('utf-8', $charset); |
397 | $this->assertEquals('Refactoring · GitHub', $title); | 514 | $this->assertEquals('Refactoring · GitHub', $title); |
398 | $this->assertEmpty($desc); | 515 | $this->assertEmpty($desc); |
@@ -493,6 +610,115 @@ class LinkUtilsTest extends TestCase | |||
493 | } | 610 | } |
494 | 611 | ||
495 | /** | 612 | /** |
613 | * Test tags_str2array with whitespace separator. | ||
614 | */ | ||
615 | public function testTagsStr2ArrayWithSpaceSeparator(): void | ||
616 | { | ||
617 | $separator = ' '; | ||
618 | |||
619 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator)); | ||
620 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator)); | ||
621 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array(' tag1 tag2 tag3 ', $separator)); | ||
622 | static::assertSame(['tag1@', 'tag2,', '.tag3'], tags_str2array(' tag1@ tag2, .tag3 ', $separator)); | ||
623 | static::assertSame([], tags_str2array('', $separator)); | ||
624 | static::assertSame([], tags_str2array(' ', $separator)); | ||
625 | static::assertSame([], tags_str2array(null, $separator)); | ||
626 | } | ||
627 | |||
628 | /** | ||
629 | * Test tags_str2array with @ separator. | ||
630 | */ | ||
631 | public function testTagsStr2ArrayWithCharSeparator(): void | ||
632 | { | ||
633 | $separator = '@'; | ||
634 | |||
635 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@tag2@tag3', $separator)); | ||
636 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@@@@tag2@@@@tag3', $separator)); | ||
637 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('@@@tag1@@@tag2@@@@tag3@@', $separator)); | ||
638 | static::assertSame( | ||
639 | ['tag1#', 'tag2, and other', '.tag3'], | ||
640 | tags_str2array('@@@ tag1# @@@ tag2, and other @@@@.tag3@@', $separator) | ||
641 | ); | ||
642 | static::assertSame([], tags_str2array('', $separator)); | ||
643 | static::assertSame([], tags_str2array(' ', $separator)); | ||
644 | static::assertSame([], tags_str2array(null, $separator)); | ||
645 | } | ||
646 | |||
647 | /** | ||
648 | * Test tags_array2str with ' ' separator. | ||
649 | */ | ||
650 | public function testTagsArray2StrWithSpaceSeparator(): void | ||
651 | { | ||
652 | $separator = ' '; | ||
653 | |||
654 | static::assertSame('tag1 tag2 tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator)); | ||
655 | static::assertSame('tag1, tag2@ tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator)); | ||
656 | static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', 'tag2', 'tag3 '], $separator)); | ||
657 | static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator)); | ||
658 | static::assertSame('tag1', tags_array2str([' tag1 '], $separator)); | ||
659 | static::assertSame('', tags_array2str([' '], $separator)); | ||
660 | static::assertSame('', tags_array2str([], $separator)); | ||
661 | static::assertSame('', tags_array2str(null, $separator)); | ||
662 | } | ||
663 | |||
664 | /** | ||
665 | * Test tags_array2str with @ separator. | ||
666 | */ | ||
667 | public function testTagsArray2StrWithCharSeparator(): void | ||
668 | { | ||
669 | $separator = '@'; | ||
670 | |||
671 | static::assertSame('tag1@tag2@tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator)); | ||
672 | static::assertSame('tag1,@tag2@tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator)); | ||
673 | static::assertSame( | ||
674 | 'tag1@tag2, and other@tag3', | ||
675 | tags_array2str(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator) | ||
676 | ); | ||
677 | static::assertSame('tag1@tag2@tag3', tags_array2str(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator)); | ||
678 | static::assertSame('tag1', tags_array2str(['@@@@tag1@@@@'], $separator)); | ||
679 | static::assertSame('', tags_array2str(['@@@'], $separator)); | ||
680 | static::assertSame('', tags_array2str([], $separator)); | ||
681 | static::assertSame('', tags_array2str(null, $separator)); | ||
682 | } | ||
683 | |||
684 | /** | ||
685 | * Test tags_array2str with @ separator. | ||
686 | */ | ||
687 | public function testTagsFilterWithSpaceSeparator(): void | ||
688 | { | ||
689 | $separator = ' '; | ||
690 | |||
691 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator)); | ||
692 | static::assertSame(['tag1,', 'tag2@', 'tag3'], tags_filter(['tag1,', 'tag2@', 'tag3'], $separator)); | ||
693 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', 'tag2', 'tag3 '], $separator)); | ||
694 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator)); | ||
695 | static::assertSame(['tag1'], tags_filter([' tag1 '], $separator)); | ||
696 | static::assertSame([], tags_filter([' '], $separator)); | ||
697 | static::assertSame([], tags_filter([], $separator)); | ||
698 | static::assertSame([], tags_filter(null, $separator)); | ||
699 | } | ||
700 | |||
701 | /** | ||
702 | * Test tags_array2str with @ separator. | ||
703 | */ | ||
704 | public function testTagsArrayFilterWithSpaceSeparator(): void | ||
705 | { | ||
706 | $separator = '@'; | ||
707 | |||
708 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator)); | ||
709 | static::assertSame(['tag1,', 'tag2#', 'tag3'], tags_filter(['tag1,', 'tag2#', 'tag3'], $separator)); | ||
710 | static::assertSame( | ||
711 | ['tag1', 'tag2, and other', 'tag3'], | ||
712 | tags_filter(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator) | ||
713 | ); | ||
714 | static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator)); | ||
715 | static::assertSame(['tag1'], tags_filter(['@@@@tag1@@@@'], $separator)); | ||
716 | static::assertSame([], tags_filter(['@@@'], $separator)); | ||
717 | static::assertSame([], tags_filter([], $separator)); | ||
718 | static::assertSame([], tags_filter(null, $separator)); | ||
719 | } | ||
720 | |||
721 | /** | ||
496 | * Util function to build an hashtag link. | 722 | * Util function to build an hashtag link. |
497 | * | 723 | * |
498 | * @param string $hashtag Hashtag name. | 724 | * @param string $hashtag Hashtag name. |