aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bookmark/LinkUtilsTest.php (renamed from tests/LinkUtilsTest.php)142
-rw-r--r--tests/plugins/PluginMarkdownTest.php1
-rw-r--r--tests/utils/CurlUtils.php94
3 files changed, 122 insertions, 115 deletions
diff --git a/tests/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php
index 5407159a..1b8688e6 100644
--- a/tests/LinkUtilsTest.php
+++ b/tests/bookmark/LinkUtilsTest.php
@@ -1,11 +1,15 @@
1<?php 1<?php
2 2
3require_once 'application/LinkUtils.php'; 3namespace Shaarli\Bookmark;
4
5use ReferenceLinkDB;
6
7require_once 'tests/utils/CurlUtils.php';
4 8
5/** 9/**
6* Class LinkUtilsTest. 10 * Class LinkUtilsTest.
7*/ 11 */
8class LinkUtilsTest extends PHPUnit_Framework_TestCase 12class LinkUtilsTest extends \PHPUnit\Framework\TestCase
9{ 13{
10 /** 14 /**
11 * Test html_extract_title() when the title is found. 15 * Test html_extract_title() when the title is found.
@@ -13,9 +17,9 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
13 public function testHtmlExtractExistentTitle() 17 public function testHtmlExtractExistentTitle()
14 { 18 {
15 $title = 'Read me please.'; 19 $title = 'Read me please.';
16 $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>'; 20 $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>';
17 $this->assertEquals($title, html_extract_title($html)); 21 $this->assertEquals($title, html_extract_title($html));
18 $html = '<html><title>'. $title .'</title>blabla<title>another</title></html>'; 22 $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>';
19 $this->assertEquals($title, html_extract_title($html)); 23 $this->assertEquals($title, html_extract_title($html));
20 } 24 }
21 25
@@ -34,7 +38,7 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
34 public function testHeadersExtractExistentCharset() 38 public function testHeadersExtractExistentCharset()
35 { 39 {
36 $charset = 'x-MacCroatian'; 40 $charset = 'x-MacCroatian';
37 $headers = 'text/html; charset='. $charset; 41 $headers = 'text/html; charset=' . $charset;
38 $this->assertEquals(strtolower($charset), header_extract_charset($headers)); 42 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
39 } 43 }
40 44
@@ -56,7 +60,7 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
56 public function testHtmlExtractExistentCharset() 60 public function testHtmlExtractExistentCharset()
57 { 61 {
58 $charset = 'x-MacCroatian'; 62 $charset = 'x-MacCroatian';
59 $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>'; 63 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
60 $this->assertEquals(strtolower($charset), html_extract_charset($html)); 64 $this->assertEquals(strtolower($charset), html_extract_charset($html));
61 } 65 }
62 66
@@ -84,8 +88,8 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
84 'Content-Type: text/html; charset=utf-8', 88 'Content-Type: text/html; charset=utf-8',
85 'Status: 200 OK', 89 'Status: 200 OK',
86 'end' => 'th=device-width">' 90 'end' => 'th=device-width">'
87 .'<title>Refactoring · GitHub</title>' 91 . '<title>Refactoring · GitHub</title>'
88 .'<link rel="search" type="application/opensea', 92 . '<link rel="search" type="application/opensea',
89 '<title>ignored</title>', 93 '<title>ignored</title>',
90 ]; 94 ];
91 foreach ($data as $key => $line) { 95 foreach ($data as $key => $line) {
@@ -109,8 +113,8 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
109 $data = [ 113 $data = [
110 'HTTP/1.1 200 OK', 114 'HTTP/1.1 200 OK',
111 'end' => 'th=device-width">' 115 'end' => 'th=device-width">'
112 .'<title>Refactoring · GitHub</title>' 116 . '<title>Refactoring · GitHub</title>'
113 .'<link rel="search" type="application/opensea', 117 . '<link rel="search" type="application/opensea',
114 '<title>ignored</title>', 118 '<title>ignored</title>',
115 ]; 119 ];
116 foreach ($data as $key => $line) { 120 foreach ($data as $key => $line) {
@@ -131,8 +135,8 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
131 'HTTP/1.1 200 OK', 135 'HTTP/1.1 200 OK',
132 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', 136 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
133 'end' => 'th=device-width">' 137 'end' => 'th=device-width">'
134 .'<title>Refactoring · GitHub</title>' 138 . '<title>Refactoring · GitHub</title>'
135 .'<link rel="search" type="application/opensea', 139 . '<link rel="search" type="application/opensea',
136 '<title>ignored</title>', 140 '<title>ignored</title>',
137 ]; 141 ];
138 foreach ($data as $key => $line) { 142 foreach ($data as $key => $line) {
@@ -218,19 +222,19 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
218 { 222 {
219 $text = 'stuff http://hello.there/is=someone#here otherstuff'; 223 $text = 'stuff http://hello.there/is=someone#here otherstuff';
220 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">' 224 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
221 .'http://hello.there/is=someone#here</a> otherstuff'; 225 . 'http://hello.there/is=someone#here</a> otherstuff';
222 $processedText = text2clickable($text, ''); 226 $processedText = text2clickable($text, '');
223 $this->assertEquals($expectedText, $processedText); 227 $this->assertEquals($expectedText, $processedText);
224 228
225 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; 229 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
226 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' 230 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
227 .'http://hello.there/is=someone#here(please)</a> otherstuff'; 231 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
228 $processedText = text2clickable($text, ''); 232 $processedText = text2clickable($text, '');
229 $this->assertEquals($expectedText, $processedText); 233 $this->assertEquals($expectedText, $processedText);
230 234
231 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; 235 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
232 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">' 236 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
233 .'http://hello.there/is=someone#here(please)&no</a> otherstuff'; 237 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
234 $processedText = text2clickable($text, ''); 238 $processedText = text2clickable($text, '');
235 $this->assertEquals($expectedText, $processedText); 239 $this->assertEquals($expectedText, $processedText);
236 } 240 }
@@ -242,7 +246,7 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
242 { 246 {
243 $text = 'stuff http://hello.there/is=someone#here otherstuff'; 247 $text = 'stuff http://hello.there/is=someone#here otherstuff';
244 $redirector = 'http://redirector.to'; 248 $redirector = 'http://redirector.to';
245 $expectedText = 'stuff <a href="'. 249 $expectedText = 'stuff <a href="' .
246 $redirector . 250 $redirector .
247 urlencode('http://hello.there/is=someone#here') . 251 urlencode('http://hello.there/is=someone#here') .
248 '">http://hello.there/is=someone#here</a> otherstuff'; 252 '">http://hello.there/is=someone#here</a> otherstuff';
@@ -257,7 +261,7 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
257 { 261 {
258 $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff'; 262 $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
259 $redirector = 'http://redirector.to'; 263 $redirector = 'http://redirector.to';
260 $expectedText = 'stuff <a href="'. 264 $expectedText = 'stuff <a href="' .
261 $redirector . 265 $redirector .
262 'http://hello.there/?is=someone&or=something#here' . 266 'http://hello.there/?is=someone&or=something#here' .
263 '">http://hello.there/?is=someone&or=something#here</a> otherstuff'; 267 '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
@@ -270,8 +274,8 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
270 */ 274 */
271 public function testSpace2nbsp() 275 public function testSpace2nbsp()
272 { 276 {
273 $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; 277 $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
274 $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?'. PHP_EOL .'&nbsp;Really?'; 278 $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?' . PHP_EOL . '&nbsp;Really?';
275 $processedText = space2nbsp($text); 279 $processedText = space2nbsp($text);
276 $this->assertEquals($expectedText, $processedText); 280 $this->assertEquals($expectedText, $processedText);
277 } 281 }
@@ -317,105 +321,13 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
317 * Util function to build an hashtag link. 321 * Util function to build an hashtag link.
318 * 322 *
319 * @param string $hashtag Hashtag name. 323 * @param string $hashtag Hashtag name.
320 * @param string $index Index URL. 324 * @param string $index Index URL.
321 * 325 *
322 * @return string HTML hashtag link. 326 * @return string HTML hashtag link.
323 */ 327 */
324 private function getHashtagLink($hashtag, $index = '') 328 private function getHashtagLink($hashtag, $index = '')
325 { 329 {
326 $hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>'; 330 $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
327 return str_replace('$1', $hashtag, $hashtagLink); 331 return str_replace('$1', $hashtag, $hashtagLink);
328 } 332 }
329} 333}
330
331// old style mock: PHPUnit doesn't allow function mock
332
333/**
334 * Returns code 200 or html content type.
335 *
336 * @param resource $ch cURL resource
337 * @param int $type cURL info type
338 *
339 * @return int|string 200 or 'text/html'
340 */
341function ut_curl_getinfo_ok($ch, $type)
342{
343 switch ($type) {
344 case CURLINFO_RESPONSE_CODE:
345 return 200;
346 case CURLINFO_CONTENT_TYPE:
347 return 'text/html; charset=utf-8';
348 }
349}
350
351/**
352 * Returns code 200 or html content type without charset.
353 *
354 * @param resource $ch cURL resource
355 * @param int $type cURL info type
356 *
357 * @return int|string 200 or 'text/html'
358 */
359function ut_curl_getinfo_no_charset($ch, $type)
360{
361 switch ($type) {
362 case CURLINFO_RESPONSE_CODE:
363 return 200;
364 case CURLINFO_CONTENT_TYPE:
365 return 'text/html';
366 }
367}
368
369/**
370 * Invalid response code.
371 *
372 * @param resource $ch cURL resource
373 * @param int $type cURL info type
374 *
375 * @return int|string 404 or 'text/html'
376 */
377function ut_curl_getinfo_rc_ko($ch, $type)
378{
379 switch ($type) {
380 case CURLINFO_RESPONSE_CODE:
381 return 404;
382 case CURLINFO_CONTENT_TYPE:
383 return 'text/html; charset=utf-8';
384 }
385}
386
387/**
388 * Invalid content type.
389 *
390 * @param resource $ch cURL resource
391 * @param int $type cURL info type
392 *
393 * @return int|string 200 or 'text/plain'
394 */
395function ut_curl_getinfo_ct_ko($ch, $type)
396{
397 switch ($type) {
398 case CURLINFO_RESPONSE_CODE:
399 return 200;
400 case CURLINFO_CONTENT_TYPE:
401 return 'text/plain';
402 }
403}
404
405/**
406 * Invalid response code and content type.
407 *
408 * @param resource $ch cURL resource
409 * @param int $type cURL info type
410 *
411 * @return int|string 404 or 'text/plain'
412 */
413function ut_curl_getinfo_rs_ct_ko($ch, $type)
414{
415 switch ($type) {
416 case CURLINFO_RESPONSE_CODE:
417 return 404;
418 case CURLINFO_CONTENT_TYPE:
419 return 'text/plain';
420 }
421}
diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php
index 44364b05..d6951866 100644
--- a/tests/plugins/PluginMarkdownTest.php
+++ b/tests/plugins/PluginMarkdownTest.php
@@ -5,6 +5,7 @@ use Shaarli\Config\ConfigManager;
5 * PluginMarkdownTest.php 5 * PluginMarkdownTest.php
6 */ 6 */
7 7
8require_once 'application/bookmark/LinkUtils.php';
8require_once 'application/Utils.php'; 9require_once 'application/Utils.php';
9require_once 'plugins/markdown/markdown.php'; 10require_once 'plugins/markdown/markdown.php';
10 11
diff --git a/tests/utils/CurlUtils.php b/tests/utils/CurlUtils.php
new file mode 100644
index 00000000..1cc4907e
--- /dev/null
+++ b/tests/utils/CurlUtils.php
@@ -0,0 +1,94 @@
1<?php
2/**
3 * Old-style mock for cURL, as PHPUnit doesn't allow to mock global functions
4 */
5
6/**
7 * Returns code 200 or html content type.
8 *
9 * @param resource $ch cURL resource
10 * @param int $type cURL info type
11 *
12 * @return int|string 200 or 'text/html'
13 */
14function ut_curl_getinfo_ok($ch, $type)
15{
16 switch ($type) {
17 case CURLINFO_RESPONSE_CODE:
18 return 200;
19 case CURLINFO_CONTENT_TYPE:
20 return 'text/html; charset=utf-8';
21 }
22}
23
24/**
25 * Returns code 200 or html content type without charset.
26 *
27 * @param resource $ch cURL resource
28 * @param int $type cURL info type
29 *
30 * @return int|string 200 or 'text/html'
31 */
32function ut_curl_getinfo_no_charset($ch, $type)
33{
34 switch ($type) {
35 case CURLINFO_RESPONSE_CODE:
36 return 200;
37 case CURLINFO_CONTENT_TYPE:
38 return 'text/html';
39 }
40}
41
42/**
43 * Invalid response code.
44 *
45 * @param resource $ch cURL resource
46 * @param int $type cURL info type
47 *
48 * @return int|string 404 or 'text/html'
49 */
50function ut_curl_getinfo_rc_ko($ch, $type)
51{
52 switch ($type) {
53 case CURLINFO_RESPONSE_CODE:
54 return 404;
55 case CURLINFO_CONTENT_TYPE:
56 return 'text/html; charset=utf-8';
57 }
58}
59
60/**
61 * Invalid content type.
62 *
63 * @param resource $ch cURL resource
64 * @param int $type cURL info type
65 *
66 * @return int|string 200 or 'text/plain'
67 */
68function ut_curl_getinfo_ct_ko($ch, $type)
69{
70 switch ($type) {
71 case CURLINFO_RESPONSE_CODE:
72 return 200;
73 case CURLINFO_CONTENT_TYPE:
74 return 'text/plain';
75 }
76}
77
78/**
79 * Invalid response code and content type.
80 *
81 * @param resource $ch cURL resource
82 * @param int $type cURL info type
83 *
84 * @return int|string 404 or 'text/plain'
85 */
86function ut_curl_getinfo_rs_ct_ko($ch, $type)
87{
88 switch ($type) {
89 case CURLINFO_RESPONSE_CODE:
90 return 404;
91 case CURLINFO_CONTENT_TYPE:
92 return 'text/plain';
93 }
94}