3 require_once 'application/LinkUtils.php';
8 class LinkUtilsTest
extends PHPUnit_Framework_TestCase
11 * Test html_extract_title() when the title is found.
13 public function testHtmlExtractExistentTitle()
15 $title = 'Read me please.';
16 $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>';
17 $this->assertEquals($title, html_extract_title($html));
18 $html = '<html><title>'. $title .'</title>blabla<title>another</title></html>';
19 $this->assertEquals($title, html_extract_title($html));
23 * Test html_extract_title() when the title is not found.
25 public function testHtmlExtractNonExistentTitle()
27 $html = '<html><meta>stuff</meta></html>';
28 $this->assertFalse(html_extract_title($html));
32 * Test headers_extract_charset() when the charset is found.
34 public function testHeadersExtractExistentCharset()
36 $charset = 'x-MacCroatian';
37 $headers = 'text/html; charset='. $charset;
38 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
42 * Test headers_extract_charset() when the charset is not found.
44 public function testHeadersExtractNonExistentCharset()
47 $this->assertFalse(header_extract_charset($headers));
49 $headers = 'text/html';
50 $this->assertFalse(header_extract_charset($headers));
54 * Test html_extract_charset() when the charset is found.
56 public function testHtmlExtractExistentCharset()
58 $charset = 'x-MacCroatian';
59 $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>';
60 $this->assertEquals(strtolower($charset), html_extract_charset($html));
64 * Test html_extract_charset() when the charset is not found.
66 public function testHtmlExtractNonExistentCharset()
68 $html = '<html><meta>stuff</meta></html>';
69 $this->assertFalse(html_extract_charset($html));
70 $html = '<html><meta>stuff</meta><meta charset=""/></html>';
71 $this->assertFalse(html_extract_charset($html));
75 * Test the download callback with valid value
77 public function testCurlDownloadCallbackOk()
79 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok');
83 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
84 'Content-Type: text/html; charset=utf-8',
86 'end' => 'th=device-width"><title>Refactoring · GitHub</title><link rel="search" type="application/opensea',
87 '<title>ignored</title>',
89 foreach ($data as $key => $line) {
91 $expected = $key !== 'end' ? strlen($line) : false;
92 $this->assertEquals($expected, $callback($ignore, $line));
93 if ($expected === false) {
97 $this->assertEquals('utf-8', $charset);
98 $this->assertEquals('Refactoring · GitHub', $title);
102 * Test the download callback with valid values and no charset
104 public function testCurlDownloadCallbackOkNoCharset()
106 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset');
109 'end' => 'th=device-width"><title>Refactoring · GitHub</title><link rel="search" type="application/opensea',
110 '<title>ignored</title>',
112 foreach ($data as $key => $line) {
114 $this->assertEquals(strlen($line), $callback($ignore, $line));
116 $this->assertEmpty($charset);
117 $this->assertEquals('Refactoring · GitHub', $title);
121 * Test the download callback with valid values and no charset
123 public function testCurlDownloadCallbackOkHtmlCharset()
125 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset');
128 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
129 'end' => 'th=device-width"><title>Refactoring · GitHub</title><link rel="search" type="application/opensea',
130 '<title>ignored</title>',
132 foreach ($data as $key => $line) {
134 $expected = $key !== 'end' ? strlen($line) : false;
135 $this->assertEquals($expected, $callback($ignore, $line));
136 if ($expected === false) {
140 $this->assertEquals('utf-8', $charset);
141 $this->assertEquals('Refactoring · GitHub', $title);
145 * Test the download callback with valid values and no title
147 public function testCurlDownloadCallbackOkNoTitle()
149 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok');
152 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
155 foreach ($data as $key => $line) {
157 $this->assertEquals(strlen($line), $callback($ignore, $line));
159 $this->assertEquals('utf-8', $charset);
160 $this->assertEmpty($title);
164 * Test the download callback with an invalid content type.
166 public function testCurlDownloadCallbackInvalidContentType()
168 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ct_ko');
170 $this->assertFalse($callback($ignore, ''));
171 $this->assertEmpty($charset);
172 $this->assertEmpty($title);
176 * Test the download callback with an invalid response code.
178 public function testCurlDownloadCallbackInvalidResponseCode()
180 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rc_ko');
182 $this->assertFalse($callback($ignore, ''));
183 $this->assertEmpty($charset);
184 $this->assertEmpty($title);
188 * Test the download callback with an invalid content type and response code.
190 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
192 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rs_ct_ko');
194 $this->assertFalse($callback($ignore, ''));
195 $this->assertEmpty($charset);
196 $this->assertEmpty($title);
200 * Test count_private.
202 public function testCountPrivateLinks()
204 $refDB = new ReferenceLinkDB();
205 $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
209 * Test text2clickable without a redirector being set.
211 public function testText2clickableWithoutRedirector()
213 $text = 'stuff http://hello.there/is=someone#here otherstuff';
214 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff';
215 $processedText = text2clickable($text, '');
216 $this->assertEquals($expectedText, $processedText);
218 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
219 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">http://hello.there/is=someone#here(please)</a> otherstuff';
220 $processedText = text2clickable($text, '');
221 $this->assertEquals($expectedText, $processedText);
223 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
224 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">http://hello.there/is=someone#here(please)&no</a> otherstuff';
225 $processedText = text2clickable($text, '');
226 $this->assertEquals($expectedText, $processedText);
230 * Test text2clickable a redirector set.
232 public function testText2clickableWithRedirector()
234 $text = 'stuff http://hello.there/is=someone#here otherstuff';
235 $redirector = 'http://redirector.to';
236 $expectedText = 'stuff <a href="'.
238 urlencode('http://hello.there/is=someone#here') .
239 '">http://hello.there/is=someone#here</a> otherstuff';
240 $processedText = text2clickable($text, $redirector);
241 $this->assertEquals($expectedText, $processedText);
245 * Test text2clickable a redirector set and without URL encode.
247 public function testText2clickableWithRedirectorDontEncode()
249 $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
250 $redirector = 'http://redirector.to';
251 $expectedText = 'stuff <a href="'.
253 'http://hello.there/?is=someone&or=something#here' .
254 '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
255 $processedText = text2clickable($text, $redirector, false);
256 $this->assertEquals($expectedText, $processedText);
260 * Test testSpace2nbsp.
262 public function testSpace2nbsp()
264 $text = ' Are you thrilled by flags ?'. PHP_EOL
.' Really?';
265 $expectedText = ' Are you thrilled by flags ?'. PHP_EOL
.' Really?';
266 $processedText = space2nbsp($text);
267 $this->assertEquals($expectedText, $processedText);
271 * Test hashtags auto-link.
273 public function testHashtagAutolink()
275 $index = 'http://domain.tld/';
276 $rawDescription = '#hashtag\n
278 test#nothashtag #hashtag \#nothashtag\n
279 test #hashtag #hashtag test #hashtag.test\n
280 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
281 What is #ашок anyway?\n
283 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
285 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
286 $this->assertNotContains(' #hashtag', $autolinkedDescription);
287 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
288 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
289 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
290 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
291 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
295 * Test hashtags auto-link without index URL.
297 public function testHashtagAutolinkNoIndex()
299 $rawDescription = 'blabla #hashtag x#nothashtag';
300 $autolinkedDescription = hashtag_autolink($rawDescription);
302 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
303 $this->assertNotContains(' #hashtag', $autolinkedDescription);
304 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
308 * Util function to build an hashtag link.
310 * @param string $hashtag Hashtag name.
311 * @param string $index Index URL.
313 * @return string HTML hashtag link.
315 private function getHashtagLink($hashtag, $index = '')
317 $hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>';
318 return str_replace('$1', $hashtag, $hashtagLink);
322 // old style mock: PHPUnit doesn't allow
function mock
325 * Returns code 200 or html content type.
327 * @param resource $ch cURL resource
328 * @param int $type cURL info type
330 * @return int|string 200 or 'text/html'
332 function ut_curl_getinfo_ok($ch, $type)
335 case CURLINFO_RESPONSE_CODE
:
337 case CURLINFO_CONTENT_TYPE
:
338 return 'text/html; charset=utf-8';
343 * Returns code 200 or html content type without charset.
345 * @param resource $ch cURL resource
346 * @param int $type cURL info type
348 * @return int|string 200 or 'text/html'
350 function ut_curl_getinfo_no_charset($ch, $type)
353 case CURLINFO_RESPONSE_CODE
:
355 case CURLINFO_CONTENT_TYPE
:
361 * Invalid response code.
363 * @param resource $ch cURL resource
364 * @param int $type cURL info type
366 * @return int|string 404 or 'text/html'
368 function ut_curl_getinfo_rc_ko($ch, $type)
371 case CURLINFO_RESPONSE_CODE
:
373 case CURLINFO_CONTENT_TYPE
:
374 return 'text/html; charset=utf-8';
379 * Invalid content type.
381 * @param resource $ch cURL resource
382 * @param int $type cURL info type
384 * @return int|string 200 or 'text/plain'
386 function ut_curl_getinfo_ct_ko($ch, $type)
389 case CURLINFO_RESPONSE_CODE
:
391 case CURLINFO_CONTENT_TYPE
:
397 * Invalid response code and content type.
399 * @param resource $ch cURL resource
400 * @param int $type cURL info type
402 * @return int|string 404 or 'text/plain'
404 function ut_curl_getinfo_rs_ct_ko($ch, $type)
407 case CURLINFO_RESPONSE_CODE
:
409 case CURLINFO_CONTENT_TYPE
: