aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bookmark/LinkUtilsTest.php204
1 files changed, 193 insertions, 11 deletions
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php
index 25fb3043..78cb8f2a 100644
--- a/tests/bookmark/LinkUtilsTest.php
+++ b/tests/bookmark/LinkUtilsTest.php
@@ -2,14 +2,16 @@
2 2
3namespace Shaarli\Bookmark; 3namespace Shaarli\Bookmark;
4 4
5use PHPUnit\Framework\TestCase;
5use ReferenceLinkDB; 6use ReferenceLinkDB;
7use Shaarli\Config\ConfigManager;
6 8
7require_once 'tests/utils/CurlUtils.php'; 9require_once 'tests/utils/CurlUtils.php';
8 10
9/** 11/**
10 * Class LinkUtilsTest. 12 * Class LinkUtilsTest.
11 */ 13 */
12class LinkUtilsTest extends \PHPUnit\Framework\TestCase 14class LinkUtilsTest extends TestCase
13{ 15{
14 /** 16 /**
15 * Test html_extract_title() when the title is found. 17 * Test html_extract_title() when the title is found.
@@ -76,11 +78,56 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
76 } 78 }
77 79
78 /** 80 /**
81 * Test html_extract_tag() when the tag <meta name= is found.
82 */
83 public function testHtmlExtractExistentNameTag()
84 {
85 $description = 'Bob and Alice share cookies.';
86 $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
87 $this->assertEquals($description, html_extract_tag('description', $html));
88 }
89
90 /**
91 * Test html_extract_tag() when the tag <meta name= is not found.
92 */
93 public function testHtmlExtractNonExistentNameTag()
94 {
95 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
96 $this->assertFalse(html_extract_tag('description', $html));
97 }
98
99 /**
100 * Test html_extract_tag() when the tag <meta property="og: is found.
101 */
102 public function testHtmlExtractExistentOgTag()
103 {
104 $description = 'Bob and Alice share cookies.';
105 $html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>';
106 $this->assertEquals($description, html_extract_tag('description', $html));
107 }
108
109 /**
110 * Test html_extract_tag() when the tag <meta property="og: is not found.
111 */
112 public function testHtmlExtractNonExistentOgTag()
113 {
114 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
115 $this->assertFalse(html_extract_tag('description', $html));
116 }
117
118 /**
79 * Test the download callback with valid value 119 * Test the download callback with valid value
80 */ 120 */
81 public function testCurlDownloadCallbackOk() 121 public function testCurlDownloadCallbackOk()
82 { 122 {
83 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); 123 $callback = get_curl_download_callback(
124 $charset,
125 $title,
126 $desc,
127 $keywords,
128 false,
129 'ut_curl_getinfo_ok'
130 );
84 $data = [ 131 $data = [
85 'HTTP/1.1 200 OK', 132 'HTTP/1.1 200 OK',
86 'Server: GitHub.com', 133 'Server: GitHub.com',
@@ -90,7 +137,9 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
90 'end' => 'th=device-width">' 137 'end' => 'th=device-width">'
91 . '<title>Refactoring · GitHub</title>' 138 . '<title>Refactoring · GitHub</title>'
92 . '<link rel="search" type="application/opensea', 139 . '<link rel="search" type="application/opensea',
93 '<title>ignored</title>', 140 '<title>ignored</title>'
141 . '<meta name="description" content="desc" />'
142 . '<meta name="keywords" content="key1,key2" />',
94 ]; 143 ];
95 foreach ($data as $key => $line) { 144 foreach ($data as $key => $line) {
96 $ignore = null; 145 $ignore = null;
@@ -102,6 +151,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
102 } 151 }
103 $this->assertEquals('utf-8', $charset); 152 $this->assertEquals('utf-8', $charset);
104 $this->assertEquals('Refactoring · GitHub', $title); 153 $this->assertEquals('Refactoring · GitHub', $title);
154 $this->assertEmpty($desc);
155 $this->assertEmpty($keywords);
105 } 156 }
106 157
107 /** 158 /**
@@ -109,13 +160,22 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
109 */ 160 */
110 public function testCurlDownloadCallbackOkNoCharset() 161 public function testCurlDownloadCallbackOkNoCharset()
111 { 162 {
112 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); 163 $callback = get_curl_download_callback(
164 $charset,
165 $title,
166 $desc,
167 $keywords,
168 false,
169 'ut_curl_getinfo_no_charset'
170 );
113 $data = [ 171 $data = [
114 'HTTP/1.1 200 OK', 172 'HTTP/1.1 200 OK',
115 'end' => 'th=device-width">' 173 'end' => 'th=device-width">'
116 . '<title>Refactoring · GitHub</title>' 174 . '<title>Refactoring · GitHub</title>'
117 . '<link rel="search" type="application/opensea', 175 . '<link rel="search" type="application/opensea',
118 '<title>ignored</title>', 176 '<title>ignored</title>'
177 . '<meta name="description" content="desc" />'
178 . '<meta name="keywords" content="key1,key2" />',
119 ]; 179 ];
120 foreach ($data as $key => $line) { 180 foreach ($data as $key => $line) {
121 $ignore = null; 181 $ignore = null;
@@ -123,6 +183,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
123 } 183 }
124 $this->assertEmpty($charset); 184 $this->assertEmpty($charset);
125 $this->assertEquals('Refactoring · GitHub', $title); 185 $this->assertEquals('Refactoring · GitHub', $title);
186 $this->assertEmpty($desc);
187 $this->assertEmpty($keywords);
126 } 188 }
127 189
128 /** 190 /**
@@ -130,14 +192,23 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
130 */ 192 */
131 public function testCurlDownloadCallbackOkHtmlCharset() 193 public function testCurlDownloadCallbackOkHtmlCharset()
132 { 194 {
133 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); 195 $callback = get_curl_download_callback(
196 $charset,
197 $title,
198 $desc,
199 $keywords,
200 false,
201 'ut_curl_getinfo_no_charset'
202 );
134 $data = [ 203 $data = [
135 'HTTP/1.1 200 OK', 204 'HTTP/1.1 200 OK',
136 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', 205 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
137 'end' => 'th=device-width">' 206 'end' => 'th=device-width">'
138 . '<title>Refactoring · GitHub</title>' 207 . '<title>Refactoring · GitHub</title>'
139 . '<link rel="search" type="application/opensea', 208 . '<link rel="search" type="application/opensea',
140 '<title>ignored</title>', 209 '<title>ignored</title>'
210 . '<meta name="description" content="desc" />'
211 . '<meta name="keywords" content="key1,key2" />',
141 ]; 212 ];
142 foreach ($data as $key => $line) { 213 foreach ($data as $key => $line) {
143 $ignore = null; 214 $ignore = null;
@@ -149,6 +220,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
149 } 220 }
150 $this->assertEquals('utf-8', $charset); 221 $this->assertEquals('utf-8', $charset);
151 $this->assertEquals('Refactoring · GitHub', $title); 222 $this->assertEquals('Refactoring · GitHub', $title);
223 $this->assertEmpty($desc);
224 $this->assertEmpty($keywords);
152 } 225 }
153 226
154 /** 227 /**
@@ -156,7 +229,14 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
156 */ 229 */
157 public function testCurlDownloadCallbackOkNoTitle() 230 public function testCurlDownloadCallbackOkNoTitle()
158 { 231 {
159 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); 232 $callback = get_curl_download_callback(
233 $charset,
234 $title,
235 $desc,
236 $keywords,
237 false,
238 'ut_curl_getinfo_ok'
239 );
160 $data = [ 240 $data = [
161 'HTTP/1.1 200 OK', 241 'HTTP/1.1 200 OK',
162 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', 242 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
@@ -168,6 +248,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
168 } 248 }
169 $this->assertEquals('utf-8', $charset); 249 $this->assertEquals('utf-8', $charset);
170 $this->assertEmpty($title); 250 $this->assertEmpty($title);
251 $this->assertEmpty($desc);
252 $this->assertEmpty($keywords);
171 } 253 }
172 254
173 /** 255 /**
@@ -175,7 +257,14 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
175 */ 257 */
176 public function testCurlDownloadCallbackInvalidContentType() 258 public function testCurlDownloadCallbackInvalidContentType()
177 { 259 {
178 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ct_ko'); 260 $callback = get_curl_download_callback(
261 $charset,
262 $title,
263 $desc,
264 $keywords,
265 false,
266 'ut_curl_getinfo_ct_ko'
267 );
179 $ignore = null; 268 $ignore = null;
180 $this->assertFalse($callback($ignore, '')); 269 $this->assertFalse($callback($ignore, ''));
181 $this->assertEmpty($charset); 270 $this->assertEmpty($charset);
@@ -187,7 +276,14 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
187 */ 276 */
188 public function testCurlDownloadCallbackInvalidResponseCode() 277 public function testCurlDownloadCallbackInvalidResponseCode()
189 { 278 {
190 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rc_ko'); 279 $callback = $callback = get_curl_download_callback(
280 $charset,
281 $title,
282 $desc,
283 $keywords,
284 false,
285 'ut_curl_getinfo_rc_ko'
286 );
191 $ignore = null; 287 $ignore = null;
192 $this->assertFalse($callback($ignore, '')); 288 $this->assertFalse($callback($ignore, ''));
193 $this->assertEmpty($charset); 289 $this->assertEmpty($charset);
@@ -199,7 +295,14 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
199 */ 295 */
200 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode() 296 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
201 { 297 {
202 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rs_ct_ko'); 298 $callback = $callback = get_curl_download_callback(
299 $charset,
300 $title,
301 $desc,
302 $keywords,
303 false,
304 'ut_curl_getinfo_rs_ct_ko'
305 );
203 $ignore = null; 306 $ignore = null;
204 $this->assertFalse($callback($ignore, '')); 307 $this->assertFalse($callback($ignore, ''));
205 $this->assertEmpty($charset); 308 $this->assertEmpty($charset);
@@ -207,6 +310,85 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase
207 } 310 }
208 311
209 /** 312 /**
313 * Test the download callback with valid value, and retrieve_description option enabled.
314 */
315 public function testCurlDownloadCallbackOkWithDesc()
316 {
317 $callback = get_curl_download_callback(
318 $charset,
319 $title,
320 $desc,
321 $keywords,
322 true,
323 'ut_curl_getinfo_ok'
324 );
325 $data = [
326 'HTTP/1.1 200 OK',
327 'Server: GitHub.com',
328 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
329 'Content-Type: text/html; charset=utf-8',
330 'Status: 200 OK',
331 'th=device-width">'
332 . '<title>Refactoring · GitHub</title>'
333 . '<link rel="search" type="application/opensea',
334 'end' => '<title>ignored</title>'
335 . '<meta name="description" content="link desc" />'
336 . '<meta name="keywords" content="key1,key2" />',
337 ];
338 foreach ($data as $key => $line) {
339 $ignore = null;
340 $expected = $key !== 'end' ? strlen($line) : false;
341 $this->assertEquals($expected, $callback($ignore, $line));
342 if ($expected === false) {
343 break;
344 }
345 }
346 $this->assertEquals('utf-8', $charset);
347 $this->assertEquals('Refactoring · GitHub', $title);
348 $this->assertEquals('link desc', $desc);
349 $this->assertEquals('key1 key2', $keywords);
350 }
351
352 /**
353 * Test the download callback with valid value, and retrieve_description option enabled,
354 * but no desc or keyword defined in the page.
355 */
356 public function testCurlDownloadCallbackOkWithDescNotFound()
357 {
358 $callback = get_curl_download_callback(
359 $charset,
360 $title,
361 $desc,
362 $keywords,
363 true,
364 'ut_curl_getinfo_ok'
365 );
366 $data = [
367 'HTTP/1.1 200 OK',
368 'Server: GitHub.com',
369 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
370 'Content-Type: text/html; charset=utf-8',
371 'Status: 200 OK',
372 'th=device-width">'
373 . '<title>Refactoring · GitHub</title>'
374 . '<link rel="search" type="application/opensea',
375 'end' => '<title>ignored</title>',
376 ];
377 foreach ($data as $key => $line) {
378 $ignore = null;
379 $expected = $key !== 'end' ? strlen($line) : false;
380 $this->assertEquals($expected, $callback($ignore, $line));
381 if ($expected === false) {
382 break;
383 }
384 }
385 $this->assertEquals('utf-8', $charset);
386 $this->assertEquals('Refactoring · GitHub', $title);
387 $this->assertEmpty($desc);
388 $this->assertEmpty($keywords);
389 }
390
391 /**
210 * Test count_private. 392 * Test count_private.
211 */ 393 */
212 public function testCountPrivateLinks() 394 public function testCountPrivateLinks()