2016-01-04 10:45:54 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 01:35:14 +01:00
|
|
|
namespace Shaarli\Bookmark;
|
|
|
|
|
|
|
|
use ReferenceLinkDB;
|
|
|
|
|
|
|
|
require_once 'tests/utils/CurlUtils.php';
|
2016-01-04 10:45:54 +01:00
|
|
|
|
|
|
|
/**
|
2018-12-03 01:35:14 +01:00
|
|
|
* Class LinkUtilsTest.
|
|
|
|
*/
|
|
|
|
class LinkUtilsTest extends \PHPUnit\Framework\TestCase
|
2016-01-04 10:45:54 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test html_extract_title() when the title is found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractExistentTitle()
|
|
|
|
{
|
|
|
|
$title = 'Read me please.';
|
2018-12-03 01:35:14 +01:00
|
|
|
$html = '<html><meta>stuff</meta><title>' . $title . '</title></html>';
|
2016-01-04 10:45:54 +01:00
|
|
|
$this->assertEquals($title, html_extract_title($html));
|
2018-12-03 01:35:14 +01:00
|
|
|
$html = '<html><title>' . $title . '</title>blabla<title>another</title></html>';
|
2016-03-08 10:00:53 +01:00
|
|
|
$this->assertEquals($title, html_extract_title($html));
|
2016-01-04 10:45:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test html_extract_title() when the title is not found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractNonExistentTitle()
|
|
|
|
{
|
|
|
|
$html = '<html><meta>stuff</meta></html>';
|
|
|
|
$this->assertFalse(html_extract_title($html));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test headers_extract_charset() when the charset is found.
|
|
|
|
*/
|
|
|
|
public function testHeadersExtractExistentCharset()
|
|
|
|
{
|
|
|
|
$charset = 'x-MacCroatian';
|
2018-12-03 01:35:14 +01:00
|
|
|
$headers = 'text/html; charset=' . $charset;
|
2017-09-30 11:04:13 +02:00
|
|
|
$this->assertEquals(strtolower($charset), header_extract_charset($headers));
|
2016-01-04 10:45:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test headers_extract_charset() when the charset is not found.
|
|
|
|
*/
|
|
|
|
public function testHeadersExtractNonExistentCharset()
|
|
|
|
{
|
2017-09-30 11:04:13 +02:00
|
|
|
$headers = '';
|
|
|
|
$this->assertFalse(header_extract_charset($headers));
|
2016-01-04 10:45:54 +01:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$headers = 'text/html';
|
|
|
|
$this->assertFalse(header_extract_charset($headers));
|
2016-01-04 10:45:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test html_extract_charset() when the charset is found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractExistentCharset()
|
|
|
|
{
|
|
|
|
$charset = 'x-MacCroatian';
|
2018-12-03 01:35:14 +01:00
|
|
|
$html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
|
2016-01-04 10:45:54 +01:00
|
|
|
$this->assertEquals(strtolower($charset), html_extract_charset($html));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test html_extract_charset() when the charset is not found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractNonExistentCharset()
|
|
|
|
{
|
|
|
|
$html = '<html><meta>stuff</meta></html>';
|
|
|
|
$this->assertFalse(html_extract_charset($html));
|
|
|
|
$html = '<html><meta>stuff</meta><meta charset=""/></html>';
|
|
|
|
$this->assertFalse(html_extract_charset($html));
|
|
|
|
}
|
2016-05-11 00:05:22 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
/**
|
|
|
|
* Test the download callback with valid value
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOk()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, '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',
|
2018-10-13 01:40:04 +02:00
|
|
|
'end' => 'th=device-width">'
|
2018-12-03 01:35:14 +01:00
|
|
|
. '<title>Refactoring · GitHub</title>'
|
|
|
|
. '<link rel="search" type="application/opensea',
|
2017-09-30 11:04:13 +02:00
|
|
|
'<title>ignored</title>',
|
|
|
|
];
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no charset
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkNoCharset()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset');
|
|
|
|
$data = [
|
|
|
|
'HTTP/1.1 200 OK',
|
2018-10-13 01:40:04 +02:00
|
|
|
'end' => 'th=device-width">'
|
2018-12-03 01:35:14 +01:00
|
|
|
. '<title>Refactoring · GitHub</title>'
|
|
|
|
. '<link rel="search" type="application/opensea',
|
2017-09-30 11:04:13 +02:00
|
|
|
'<title>ignored</title>',
|
|
|
|
];
|
|
|
|
foreach ($data as $key => $line) {
|
|
|
|
$ignore = null;
|
|
|
|
$this->assertEquals(strlen($line), $callback($ignore, $line));
|
|
|
|
}
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEquals('Refactoring · GitHub', $title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no charset
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkHtmlCharset()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset');
|
|
|
|
$data = [
|
|
|
|
'HTTP/1.1 200 OK',
|
|
|
|
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
|
2018-10-13 01:40:04 +02:00
|
|
|
'end' => 'th=device-width">'
|
2018-12-03 01:35:14 +01:00
|
|
|
. '<title>Refactoring · GitHub</title>'
|
|
|
|
. '<link rel="search" type="application/opensea',
|
2017-09-30 11:04:13 +02:00
|
|
|
'<title>ignored</title>',
|
|
|
|
];
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no title
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkNoTitle()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok');
|
|
|
|
$data = [
|
|
|
|
'HTTP/1.1 200 OK',
|
|
|
|
'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
|
|
|
|
'ignored',
|
|
|
|
];
|
|
|
|
foreach ($data as $key => $line) {
|
|
|
|
$ignore = null;
|
|
|
|
$this->assertEquals(strlen($line), $callback($ignore, $line));
|
|
|
|
}
|
|
|
|
$this->assertEquals('utf-8', $charset);
|
|
|
|
$this->assertEmpty($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with an invalid content type.
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackInvalidContentType()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ct_ko');
|
|
|
|
$ignore = null;
|
|
|
|
$this->assertFalse($callback($ignore, ''));
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEmpty($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with an invalid response code.
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackInvalidResponseCode()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rc_ko');
|
|
|
|
$ignore = null;
|
|
|
|
$this->assertFalse($callback($ignore, ''));
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEmpty($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with an invalid content type and response code.
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
|
|
|
|
{
|
|
|
|
$callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rs_ct_ko');
|
|
|
|
$ignore = null;
|
|
|
|
$this->assertFalse($callback($ignore, ''));
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEmpty($title);
|
|
|
|
}
|
|
|
|
|
2016-05-11 00:05:22 +02:00
|
|
|
/**
|
|
|
|
* Test count_private.
|
|
|
|
*/
|
|
|
|
public function testCountPrivateLinks()
|
|
|
|
{
|
|
|
|
$refDB = new ReferenceLinkDB();
|
|
|
|
$this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
|
|
|
|
}
|
2016-05-10 23:18:04 +02:00
|
|
|
|
|
|
|
/**
|
2019-02-09 13:52:12 +01:00
|
|
|
* Test text2clickable.
|
2016-05-10 23:18:04 +02:00
|
|
|
*/
|
2019-02-09 13:52:12 +01:00
|
|
|
public function testText2clickable()
|
2016-05-10 23:18:04 +02:00
|
|
|
{
|
|
|
|
$text = 'stuff http://hello.there/is=someone#here otherstuff';
|
2018-10-13 01:40:04 +02:00
|
|
|
$expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
|
2018-12-03 01:35:14 +01:00
|
|
|
. 'http://hello.there/is=someone#here</a> otherstuff';
|
2019-02-09 13:52:12 +01:00
|
|
|
$processedText = text2clickable($text);
|
2016-05-10 23:18:04 +02:00
|
|
|
$this->assertEquals($expectedText, $processedText);
|
2017-09-29 18:52:38 +02:00
|
|
|
|
|
|
|
$text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
|
2018-10-13 01:40:04 +02:00
|
|
|
$expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
|
2018-12-03 01:35:14 +01:00
|
|
|
. 'http://hello.there/is=someone#here(please)</a> otherstuff';
|
2019-02-09 13:52:12 +01:00
|
|
|
$processedText = text2clickable($text);
|
2017-09-29 18:52:38 +02:00
|
|
|
$this->assertEquals($expectedText, $processedText);
|
|
|
|
|
2019-02-09 13:52:12 +01:00
|
|
|
$text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
|
2017-09-29 18:52:38 +02:00
|
|
|
$text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
|
2018-10-13 01:40:04 +02:00
|
|
|
$expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
|
2018-12-03 01:35:14 +01:00
|
|
|
. 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
|
2019-02-09 13:52:12 +01:00
|
|
|
$processedText = text2clickable($text);
|
2017-11-07 20:23:58 +01:00
|
|
|
$this->assertEquals($expectedText, $processedText);
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:18:04 +02:00
|
|
|
/**
|
|
|
|
* Test testSpace2nbsp.
|
|
|
|
*/
|
|
|
|
public function testSpace2nbsp()
|
|
|
|
{
|
2018-12-03 01:35:14 +01:00
|
|
|
$text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
|
|
|
|
$expectedText = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
|
2016-05-10 23:18:04 +02:00
|
|
|
$processedText = space2nbsp($text);
|
|
|
|
$this->assertEquals($expectedText, $processedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test hashtags auto-link.
|
|
|
|
*/
|
|
|
|
public function testHashtagAutolink()
|
|
|
|
{
|
|
|
|
$index = 'http://domain.tld/';
|
|
|
|
$rawDescription = '#hashtag\n
|
|
|
|
# nothashtag\n
|
|
|
|
test#nothashtag #hashtag \#nothashtag\n
|
|
|
|
test #hashtag #hashtag test #hashtag.test\n
|
|
|
|
#hashtag #hashtag-nothashtag #hashtag_hashtag\n
|
|
|
|
What is #ашок anyway?\n
|
|
|
|
カタカナ #カタカナ」カタカナ\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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test hashtags auto-link without index URL.
|
|
|
|
*/
|
|
|
|
public function testHashtagAutolinkNoIndex()
|
|
|
|
{
|
|
|
|
$rawDescription = 'blabla #hashtag x#nothashtag';
|
|
|
|
$autolinkedDescription = hashtag_autolink($rawDescription);
|
|
|
|
|
|
|
|
$this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
|
|
|
|
$this->assertNotContains(' #hashtag', $autolinkedDescription);
|
|
|
|
$this->assertNotContains('>#nothashtag', $autolinkedDescription);
|
|
|
|
}
|
|
|
|
|
2019-02-09 14:13:08 +01:00
|
|
|
/**
|
|
|
|
* 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'));
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:18:04 +02:00
|
|
|
/**
|
|
|
|
* Util function to build an hashtag link.
|
|
|
|
*
|
|
|
|
* @param string $hashtag Hashtag name.
|
2018-12-03 01:35:14 +01:00
|
|
|
* @param string $index Index URL.
|
2016-05-10 23:18:04 +02:00
|
|
|
*
|
|
|
|
* @return string HTML hashtag link.
|
|
|
|
*/
|
|
|
|
private function getHashtagLink($hashtag, $index = '')
|
|
|
|
{
|
2018-12-03 01:35:14 +01:00
|
|
|
$hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
|
2016-05-10 23:18:04 +02:00
|
|
|
return str_replace('$1', $hashtag, $hashtagLink);
|
|
|
|
}
|
2016-01-04 10:45:54 +01:00
|
|
|
}
|