X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Fbookmark%2FLinkUtilsTest.php;h=29941c8cd0ed32307faa0eea5bde9b99b3d77967;hb=458b6b9918ec27154dd45416947bb93bedb97109;hp=bd08f03670a68cba16bea8895a906b35c5b953ec;hpb=5bb384cd2742ff4f982cbe914cee551b74ab4bad;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index bd08f036..29941c8c 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. @@ -42,6 +42,19 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase $this->assertEquals(strtolower($charset), header_extract_charset($headers)); } + /** + * Test headers_extract_charset() when the charset is found with odd quotes. + */ + public function testHeadersExtractExistentCharsetWithQuotes() + { + $charset = 'x-MacCroatian'; + $headers = 'text/html; charset="' . $charset . '"otherstuff="test"'; + $this->assertEquals(strtolower($charset), header_extract_charset($headers)); + + $headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"'; + $this->assertEquals(strtolower($charset), header_extract_charset($headers)); + } + /** * Test headers_extract_charset() when the charset is not found. */ @@ -75,12 +88,146 @@ 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)); + + // Simple OpenGraph + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // Simple reversed OpenGraph + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // ItemProp OpenGraph + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph without quotes + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph reversed without quotes + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph with noise + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph reversed with noise + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph multiple properties start + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph multiple properties end + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph multiple properties both end + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph multiple properties both end with noise + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph reversed multiple properties start + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph reversed multiple properties end + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph reversed multiple properties both end + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // OpenGraph reversed multiple properties both end with noise + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + + // Suggestion from #1375 + $html = ''; + $this->assertEquals($description, html_extract_tag('description', $html)); + } + + /** + * Test html_extract_tag() when the tag assertFalse(html_extract_tag('description', $html)); + + // Partial meta tag + $html = ''; + $this->assertFalse(html_extract_tag('description', $html)); + + $html = ''; + $this->assertFalse(html_extract_tag('description', $html)); + + $html = ''; + $this->assertFalse(html_extract_tag('description', $html)); + + $html = ''; + $this->assertFalse(html_extract_tag('description', $html)); + + $html = ''; + $this->assertFalse(html_extract_tag('description', $html)); + + $html = ''; + $this->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 +237,9 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase 'end' => 'th=device-width">' . 'Refactoring · GitHub' . '' + . '', ]; foreach ($data as $key => $line) { $ignore = null; @@ -102,6 +251,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 +260,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 +283,8 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase } $this->assertEmpty($charset); $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEmpty($desc); + $this->assertEmpty($keywords); } /** @@ -130,14 +292,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 +320,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 +329,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); } }