X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Fbookmark%2FLinkUtilsTest.php;h=c2f9f9305716829b91ba6ffe36cfc9a8a29cb80e;hb=b3bd8c3e8d367975980043e772f7cd78b7f96bc6;hp=25fb30435c8ef09c91623f6b416c1b6300ff459e;hpb=cc69aad4a903dc603f47cfa87aeb1865b03c8950;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 25fb3043..c2f9f930 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. */ @@ -76,143 +89,401 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase } /** - * Test the download callback with valid value + * 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 header callback with valid value + */ + public function testCurlHeaderCallbackOk(): void + { + $callback = get_curl_header_callback($charset, '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', - 'end' => 'th=device-width">' + ]; + + foreach ($data as $chunk) { + static::assertIsInt($callback(null, $chunk)); + } + + static::assertSame('utf-8', $charset); + } + + /** + * Test the download callback with valid value + */ + public function testCurlDownloadCallbackOk(): void + { + $charset = 'utf-8'; + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + ' ' + ); + + $data = [ + 'th=device-width">' . '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; - } + + foreach ($data as $chunk) { + static::assertSame(strlen($chunk), $callback(null, $chunk)); } - $this->assertEquals('utf-8', $charset); - $this->assertEquals('Refactoring · GitHub', $title); + + static::assertSame('utf-8', $charset); + static::assertSame('Refactoring · GitHub', $title); + static::assertEmpty($desc); + static::assertEmpty($keywords); } /** - * Test the download callback with valid values and no charset + * Test the header callback with valid value */ - public function testCurlDownloadCallbackOkNoCharset() + public function testCurlHeaderCallbackNoCharset(): void { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); + $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_no_charset'); $data = [ 'HTTP/1.1 200 OK', + ]; + + foreach ($data as $chunk) { + static::assertSame(strlen($chunk), $callback(null, $chunk)); + } + + static::assertFalse($charset); + } + + /** + * Test the download callback with valid values and no charset + */ + public function testCurlDownloadCallbackOkNoCharset(): void + { + $charset = null; + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + ' ' + ); + + $data = [ 'end' => 'th=device-width">' . 'Refactoring · GitHub' . '' + . '', ]; - foreach ($data as $key => $line) { - $ignore = null; - $this->assertEquals(strlen($line), $callback($ignore, $line)); + + foreach ($data as $chunk) { + static::assertSame(strlen($chunk), $callback(null, $chunk)); } + $this->assertEmpty($charset); $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEmpty($desc); + $this->assertEmpty($keywords); } /** * Test the download callback with valid values and no charset */ - public function testCurlDownloadCallbackOkHtmlCharset() + public function testCurlDownloadCallbackOkHtmlCharset(): void { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); + $charset = null; + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + ' ' + ); + $data = [ - 'HTTP/1.1 200 OK', '', 'end' => 'th=device-width">' . '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; - } + foreach ($data as $chunk) { + static::assertSame(strlen($chunk), $callback(null, $chunk)); } + $this->assertEquals('utf-8', $charset); $this->assertEquals('Refactoring · GitHub', $title); + $this->assertEmpty($desc); + $this->assertEmpty($keywords); } /** * Test the download callback with valid values and no title */ - public function testCurlDownloadCallbackOkNoTitle() + public function testCurlDownloadCallbackOkNoTitle(): void { - $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); + $charset = 'utf-8'; + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + false, + ' ' + ); + $data = [ - 'HTTP/1.1 200 OK', 'end' => 'th=device-width">Refactoring · GitHub' + . 'Refactoring · GitHub' + . '' + . '', + ]; + + foreach ($data as $chunk) { + static::assertSame(strlen($chunk), $callback(null, $chunk)); + } + + $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(): void + { + $charset = 'utf-8'; + $callback = get_curl_download_callback( + $charset, + $title, + $desc, + $keywords, + true, + 'ut_curl_getinfo_ok' + ); + $data = [ + 'th=device-width">' + . 'Refactoring · GitHub' + . '#$1'; + $hashtagLink = '#$1'; return str_replace('$1', $hashtag, $hashtagLink); } }