X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Fbookmark%2FLinkUtilsTest.php;h=a708795381f21925bae4609656048a979644f2d1;hb=a5a9cf23acd1248585173aa32757d9720b5f2d62;hp=25fb30435c8ef09c91623f6b416c1b6300ff459e;hpb=786f35f2700d83687ef494b98e3dace37d268e1c;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 25fb3043..a7087953 100644 --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php @@ -2,14 +2,14 @@ namespace Shaarli\Bookmark; -use ReferenceLinkDB; +use Shaarli\TestCase; require_once 'tests/utils/CurlUtils.php'; /** * Class LinkUtilsTest. */ -class LinkUtilsTest extends \PHPUnit\Framework\TestCase +class LinkUtilsTest extends TestCase { /** * Test html_extract_title() when the title is found. @@ -75,12 +75,57 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase $this->assertFalse(html_extract_charset($html)); } + /** + * Test html_extract_tag() when the tag '; + $this->assertEquals($description, html_extract_tag('description', $html)); + } + + /** + * Test html_extract_tag() when the tag assertFalse(html_extract_tag('description', $html)); + } + + /** + * Test html_extract_tag() when the tag '; + $this->assertEquals($description, html_extract_tag('description', $html)); + } + + /** + * Test html_extract_tag() when the tag '; + $this->assertFalse(html_extract_tag('description', $html)); + } + /** * Test the download callback with valid value */ public function testCurlDownloadCallbackOk() { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + 'ut_curl_getinfo_ok' + ); $data = [ 'HTTP/1.1 200 OK', 'Server: GitHub.com', @@ -90,7 +135,9 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase 'end' => 'th=device-width">' . 'Refactoring · GitHub' . '' + . '', ]; foreach ($data as $key => $line) { $ignore = null; @@ -102,6 +149,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase } $this->assertEquals('utf-8', $charset); $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEmpty($desc); + $this->assertEmpty($keywords); } /** @@ -109,13 +158,22 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase */ public function testCurlDownloadCallbackOkNoCharset() { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + 'ut_curl_getinfo_no_charset' + ); $data = [ 'HTTP/1.1 200 OK', 'end' => 'th=device-width">' . 'Refactoring · GitHub' . '' + . '', ]; foreach ($data as $key => $line) { $ignore = null; @@ -123,6 +181,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase } $this->assertEmpty($charset); $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEmpty($desc); + $this->assertEmpty($keywords); } /** @@ -130,14 +190,23 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase */ public function testCurlDownloadCallbackOkHtmlCharset() { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + 'ut_curl_getinfo_no_charset' + ); $data = [ 'HTTP/1.1 200 OK', '', 'end' => 'th=device-width">' . 'Refactoring · GitHub' . '' + . '', ]; foreach ($data as $key => $line) { $ignore = null; @@ -149,6 +218,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase } $this->assertEquals('utf-8', $charset); $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEmpty($desc); + $this->assertEmpty($keywords); } /** @@ -156,7 +227,14 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase */ public function testCurlDownloadCallbackOkNoTitle() { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + 'ut_curl_getinfo_ok' + ); $data = [ 'HTTP/1.1 200 OK', 'end' => 'th=device-width">Refactoring · GitHub' + . 'Refactoring · GitHub' + . '' + . '', + ]; + foreach ($data as $key => $line) { + $ignore = null; + $expected = $key !== 'end' ? strlen($line) : false; + $this->assertEquals($expected, $callback($ignore, $line)); + if ($expected === false) { + break; + } + } + $this->assertEquals('utf-8', $charset); + $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEquals('link desc', $desc); + $this->assertEquals('key1 key2', $keywords); + } + + /** + * Test the download callback with valid value, and retrieve_description option enabled, + * but no desc or keyword defined in the page. + */ + public function testCurlDownloadCallbackOkWithDescNotFound() + { + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + true, + 'ut_curl_getinfo_ok' + ); + $data = [ + 'HTTP/1.1 200 OK', + 'Server: GitHub.com', + 'Date: Sat, 28 Oct 2017 12:01:33 GMT', + 'Content-Type: text/html; charset=utf-8', + 'Status: 200 OK', + 'th=device-width">' + . 'Refactoring · GitHub' + . '#$1'; + $hashtagLink = '#$1'; return str_replace('$1', $hashtag, $hashtagLink); } }