X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Fbookmark%2FLinkUtilsTest.php;h=a708795381f21925bae4609656048a979644f2d1;hb=a5a9cf23acd1248585173aa32757d9720b5f2d62;hp=1b8688e6d398fc1381e6b2acc586ae2562c282f2;hpb=fe3713d2e5c91e2d07af72b39f321521d3dd470c;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 1b8688e6..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 text2clickable without a redirector being set. + * Test the download callback with valid value, and retrieve_description option enabled, + * but no desc or keyword defined in the page. */ - public function testText2clickableWithoutRedirector() + 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' + . '' . 'http://hello.there/is=someone#here otherstuff'; - $processedText = text2clickable($text, ''); + $processedText = text2clickable($text); $this->assertEquals($expectedText, $processedText); $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; $expectedText = 'stuff ' . 'http://hello.there/is=someone#here(please) otherstuff'; - $processedText = text2clickable($text, ''); + $processedText = text2clickable($text); $this->assertEquals($expectedText, $processedText); + $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; $expectedText = 'stuff ' . 'http://hello.there/is=someone#here(please)&no otherstuff'; - $processedText = text2clickable($text, ''); - $this->assertEquals($expectedText, $processedText); - } - - /** - * Test text2clickable with a redirector set. - */ - public function testText2clickableWithRedirector() - { - $text = 'stuff http://hello.there/is=someone#here otherstuff'; - $redirector = 'http://redirector.to'; - $expectedText = 'stuff http://hello.there/is=someone#here otherstuff'; - $processedText = text2clickable($text, $redirector); - $this->assertEquals($expectedText, $processedText); - } - - /** - * Test text2clickable a redirector set and without URL encode. - */ - public function testText2clickableWithRedirectorDontEncode() - { - $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff'; - $redirector = 'http://redirector.to'; - $expectedText = 'stuff http://hello.there/?is=someone&or=something#here otherstuff'; - $processedText = text2clickable($text, $redirector, false); + $processedText = text2clickable($text); $this->assertEquals($expectedText, $processedText); } @@ -295,13 +437,13 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase カタカナ #カタカナ」カタカナ\n'; $autolinkedDescription = hashtag_autolink($rawDescription, $index); - $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription); - $this->assertNotContains(' #hashtag', $autolinkedDescription); - $this->assertNotContains('>#nothashtag', $autolinkedDescription); - $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription); - $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); - $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); - $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription); + $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); + $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); + $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); } /** @@ -312,9 +454,29 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase $rawDescription = 'blabla #hashtag x#nothashtag'; $autolinkedDescription = hashtag_autolink($rawDescription); - $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription); - $this->assertNotContains(' #hashtag', $autolinkedDescription); - $this->assertNotContains('>#nothashtag', $autolinkedDescription); + $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription); + $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); + $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); + } + + /** + * Test is_note with note URLs. + */ + public function testIsNote() + { + $this->assertTrue(is_note('?')); + $this->assertTrue(is_note('?abcDEf')); + $this->assertTrue(is_note('?_abcDEf#123')); + } + + /** + * Test is_note with non note URLs. + */ + public function testIsNotNote() + { + $this->assertFalse(is_note('')); + $this->assertFalse(is_note('nope')); + $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi')); } /** @@ -327,7 +489,7 @@ class LinkUtilsTest extends \PHPUnit\Framework\TestCase */ private function getHashtagLink($hashtag, $index = '') { - $hashtagLink = '#$1'; + $hashtagLink = '#$1'; return str_replace('$1', $hashtag, $hashtagLink); } }