namespacing: move LinkUtils along \Shaarli\Bookmark classes

Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
VirtualTam 2018-12-03 01:35:14 +01:00
parent 6696729b88
commit fe3713d2e5
5 changed files with 125 additions and 118 deletions

View file

@ -7,7 +7,7 @@
* *
* @param string $charset to extract from the downloaded page (reference) * @param string $charset to extract from the downloaded page (reference)
* @param string $title to extract from the downloaded page (reference) * @param string $title to extract from the downloaded page (reference)
* @param string $curlGetInfo Optionnaly overrides curl_getinfo function * @param string $curlGetInfo Optionally overrides curl_getinfo function
* *
* @return Closure * @return Closure
*/ */
@ -198,7 +198,7 @@ function space2nbsp($text)
* *
* @param string $description shaare's description. * @param string $description shaare's description.
* @param string $redirector if a redirector is set, use it to gerenate links. * @param string $redirector if a redirector is set, use it to gerenate links.
* @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not. * @param bool $urlEncode Use `urlencode()` on the URL after the redirector or not.
* @param string $indexUrl URL to Shaarli's index. * @param string $indexUrl URL to Shaarli's index.
* @return string formatted description. * @return string formatted description.

View file

@ -57,13 +57,13 @@
// Shaarli library // Shaarli library
require_once 'application/ApplicationUtils.php'; require_once 'application/ApplicationUtils.php';
require_once 'application/bookmark/LinkUtils.php';
require_once 'application/config/ConfigPlugin.php'; require_once 'application/config/ConfigPlugin.php';
require_once 'application/feed/Cache.php'; require_once 'application/feed/Cache.php';
require_once 'application/http/HttpUtils.php'; require_once 'application/http/HttpUtils.php';
require_once 'application/http/UrlUtils.php'; require_once 'application/http/UrlUtils.php';
require_once 'application/FileUtils.php'; require_once 'application/FileUtils.php';
require_once 'application/History.php'; require_once 'application/History.php';
require_once 'application/LinkUtils.php';
require_once 'application/NetscapeBookmarkUtils.php'; require_once 'application/NetscapeBookmarkUtils.php';
require_once 'application/TimeZone.php'; require_once 'application/TimeZone.php';
require_once 'application/Utils.php'; require_once 'application/Utils.php';

View file

@ -1,11 +1,15 @@
<?php <?php
require_once 'application/LinkUtils.php'; namespace Shaarli\Bookmark;
use ReferenceLinkDB;
require_once 'tests/utils/CurlUtils.php';
/** /**
* Class LinkUtilsTest. * Class LinkUtilsTest.
*/ */
class LinkUtilsTest extends PHPUnit_Framework_TestCase class LinkUtilsTest extends \PHPUnit\Framework\TestCase
{ {
/** /**
* Test html_extract_title() when the title is found. * Test html_extract_title() when the title is found.
@ -13,9 +17,9 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
public function testHtmlExtractExistentTitle() public function testHtmlExtractExistentTitle()
{ {
$title = 'Read me please.'; $title = 'Read me please.';
$html = '<html><meta>stuff</meta><title>'. $title .'</title></html>'; $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>';
$this->assertEquals($title, html_extract_title($html)); $this->assertEquals($title, html_extract_title($html));
$html = '<html><title>'. $title .'</title>blabla<title>another</title></html>'; $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>';
$this->assertEquals($title, html_extract_title($html)); $this->assertEquals($title, html_extract_title($html));
} }
@ -34,7 +38,7 @@ public function testHtmlExtractNonExistentTitle()
public function testHeadersExtractExistentCharset() public function testHeadersExtractExistentCharset()
{ {
$charset = 'x-MacCroatian'; $charset = 'x-MacCroatian';
$headers = 'text/html; charset='. $charset; $headers = 'text/html; charset=' . $charset;
$this->assertEquals(strtolower($charset), header_extract_charset($headers)); $this->assertEquals(strtolower($charset), header_extract_charset($headers));
} }
@ -56,7 +60,7 @@ public function testHeadersExtractNonExistentCharset()
public function testHtmlExtractExistentCharset() public function testHtmlExtractExistentCharset()
{ {
$charset = 'x-MacCroatian'; $charset = 'x-MacCroatian';
$html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>'; $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
$this->assertEquals(strtolower($charset), html_extract_charset($html)); $this->assertEquals(strtolower($charset), html_extract_charset($html));
} }
@ -84,8 +88,8 @@ public function testCurlDownloadCallbackOk()
'Content-Type: text/html; charset=utf-8', 'Content-Type: text/html; charset=utf-8',
'Status: 200 OK', 'Status: 200 OK',
'end' => 'th=device-width">' 'end' => 'th=device-width">'
.'<title>Refactoring · GitHub</title>' . '<title>Refactoring · GitHub</title>'
.'<link rel="search" type="application/opensea', . '<link rel="search" type="application/opensea',
'<title>ignored</title>', '<title>ignored</title>',
]; ];
foreach ($data as $key => $line) { foreach ($data as $key => $line) {
@ -109,8 +113,8 @@ public function testCurlDownloadCallbackOkNoCharset()
$data = [ $data = [
'HTTP/1.1 200 OK', 'HTTP/1.1 200 OK',
'end' => 'th=device-width">' 'end' => 'th=device-width">'
.'<title>Refactoring · GitHub</title>' . '<title>Refactoring · GitHub</title>'
.'<link rel="search" type="application/opensea', . '<link rel="search" type="application/opensea',
'<title>ignored</title>', '<title>ignored</title>',
]; ];
foreach ($data as $key => $line) { foreach ($data as $key => $line) {
@ -131,8 +135,8 @@ public function testCurlDownloadCallbackOkHtmlCharset()
'HTTP/1.1 200 OK', 'HTTP/1.1 200 OK',
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
'end' => 'th=device-width">' 'end' => 'th=device-width">'
.'<title>Refactoring · GitHub</title>' . '<title>Refactoring · GitHub</title>'
.'<link rel="search" type="application/opensea', . '<link rel="search" type="application/opensea',
'<title>ignored</title>', '<title>ignored</title>',
]; ];
foreach ($data as $key => $line) { foreach ($data as $key => $line) {
@ -218,19 +222,19 @@ public function testText2clickableWithoutRedirector()
{ {
$text = 'stuff http://hello.there/is=someone#here otherstuff'; $text = 'stuff http://hello.there/is=someone#here otherstuff';
$expectedText = 'stuff <a href="http://hello.there/is=someone#here">' $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
.'http://hello.there/is=someone#here</a> otherstuff'; . 'http://hello.there/is=someone#here</a> otherstuff';
$processedText = text2clickable($text, ''); $processedText = text2clickable($text, '');
$this->assertEquals($expectedText, $processedText); $this->assertEquals($expectedText, $processedText);
$text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
$expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
.'http://hello.there/is=someone#here(please)</a> otherstuff'; . 'http://hello.there/is=someone#here(please)</a> otherstuff';
$processedText = text2clickable($text, ''); $processedText = text2clickable($text, '');
$this->assertEquals($expectedText, $processedText); $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 <a href="http://hello.there/is=someone#here(please)&no">' $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
.'http://hello.there/is=someone#here(please)&no</a> otherstuff'; . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
$processedText = text2clickable($text, ''); $processedText = text2clickable($text, '');
$this->assertEquals($expectedText, $processedText); $this->assertEquals($expectedText, $processedText);
} }
@ -242,7 +246,7 @@ public function testText2clickableWithRedirector()
{ {
$text = 'stuff http://hello.there/is=someone#here otherstuff'; $text = 'stuff http://hello.there/is=someone#here otherstuff';
$redirector = 'http://redirector.to'; $redirector = 'http://redirector.to';
$expectedText = 'stuff <a href="'. $expectedText = 'stuff <a href="' .
$redirector . $redirector .
urlencode('http://hello.there/is=someone#here') . urlencode('http://hello.there/is=someone#here') .
'">http://hello.there/is=someone#here</a> otherstuff'; '">http://hello.there/is=someone#here</a> otherstuff';
@ -257,7 +261,7 @@ public function testText2clickableWithRedirectorDontEncode()
{ {
$text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff'; $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
$redirector = 'http://redirector.to'; $redirector = 'http://redirector.to';
$expectedText = 'stuff <a href="'. $expectedText = 'stuff <a href="' .
$redirector . $redirector .
'http://hello.there/?is=someone&or=something#here' . 'http://hello.there/?is=someone&or=something#here' .
'">http://hello.there/?is=someone&or=something#here</a> otherstuff'; '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
@ -270,8 +274,8 @@ public function testText2clickableWithRedirectorDontEncode()
*/ */
public function testSpace2nbsp() public function testSpace2nbsp()
{ {
$text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
$expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?'. PHP_EOL .'&nbsp;Really?'; $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?' . PHP_EOL . '&nbsp;Really?';
$processedText = space2nbsp($text); $processedText = space2nbsp($text);
$this->assertEquals($expectedText, $processedText); $this->assertEquals($expectedText, $processedText);
} }
@ -317,105 +321,13 @@ public function testHashtagAutolinkNoIndex()
* Util function to build an hashtag link. * Util function to build an hashtag link.
* *
* @param string $hashtag Hashtag name. * @param string $hashtag Hashtag name.
* @param string $index Index URL. * @param string $index Index URL.
* *
* @return string HTML hashtag link. * @return string HTML hashtag link.
*/ */
private function getHashtagLink($hashtag, $index = '') private function getHashtagLink($hashtag, $index = '')
{ {
$hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>'; $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
return str_replace('$1', $hashtag, $hashtagLink); return str_replace('$1', $hashtag, $hashtagLink);
} }
} }
// old style mock: PHPUnit doesn't allow function mock
/**
* Returns code 200 or html content type.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 200 or 'text/html'
*/
function ut_curl_getinfo_ok($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 200;
case CURLINFO_CONTENT_TYPE:
return 'text/html; charset=utf-8';
}
}
/**
* Returns code 200 or html content type without charset.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 200 or 'text/html'
*/
function ut_curl_getinfo_no_charset($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 200;
case CURLINFO_CONTENT_TYPE:
return 'text/html';
}
}
/**
* Invalid response code.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 404 or 'text/html'
*/
function ut_curl_getinfo_rc_ko($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 404;
case CURLINFO_CONTENT_TYPE:
return 'text/html; charset=utf-8';
}
}
/**
* Invalid content type.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 200 or 'text/plain'
*/
function ut_curl_getinfo_ct_ko($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 200;
case CURLINFO_CONTENT_TYPE:
return 'text/plain';
}
}
/**
* Invalid response code and content type.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 404 or 'text/plain'
*/
function ut_curl_getinfo_rs_ct_ko($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 404;
case CURLINFO_CONTENT_TYPE:
return 'text/plain';
}
}

View file

@ -5,6 +5,7 @@
* PluginMarkdownTest.php * PluginMarkdownTest.php
*/ */
require_once 'application/bookmark/LinkUtils.php';
require_once 'application/Utils.php'; require_once 'application/Utils.php';
require_once 'plugins/markdown/markdown.php'; require_once 'plugins/markdown/markdown.php';

94
tests/utils/CurlUtils.php Normal file
View file

@ -0,0 +1,94 @@
<?php
/**
* Old-style mock for cURL, as PHPUnit doesn't allow to mock global functions
*/
/**
* Returns code 200 or html content type.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 200 or 'text/html'
*/
function ut_curl_getinfo_ok($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 200;
case CURLINFO_CONTENT_TYPE:
return 'text/html; charset=utf-8';
}
}
/**
* Returns code 200 or html content type without charset.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 200 or 'text/html'
*/
function ut_curl_getinfo_no_charset($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 200;
case CURLINFO_CONTENT_TYPE:
return 'text/html';
}
}
/**
* Invalid response code.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 404 or 'text/html'
*/
function ut_curl_getinfo_rc_ko($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 404;
case CURLINFO_CONTENT_TYPE:
return 'text/html; charset=utf-8';
}
}
/**
* Invalid content type.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 200 or 'text/plain'
*/
function ut_curl_getinfo_ct_ko($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 200;
case CURLINFO_CONTENT_TYPE:
return 'text/plain';
}
}
/**
* Invalid response code and content type.
*
* @param resource $ch cURL resource
* @param int $type cURL info type
*
* @return int|string 404 or 'text/plain'
*/
function ut_curl_getinfo_rs_ct_ko($ch, $type)
{
switch ($type) {
case CURLINFO_RESPONSE_CODE:
return 404;
case CURLINFO_CONTENT_TYPE:
return 'text/plain';
}
}