2016-01-04 10:45:54 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 01:35:14 +01:00
|
|
|
namespace Shaarli\Bookmark;
|
|
|
|
|
2019-06-08 13:59:19 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-12-03 01:35:14 +01:00
|
|
|
|
|
|
|
require_once 'tests/utils/CurlUtils.php';
|
2016-01-04 10:45:54 +01:00
|
|
|
|
|
|
|
/**
|
2018-12-03 01:35:14 +01:00
|
|
|
* Class LinkUtilsTest.
|
|
|
|
*/
|
2019-06-08 13:59:19 +02:00
|
|
|
class LinkUtilsTest extends 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
|
|
|
}
|
|
|
|
|
2020-09-26 13:28:38 +02:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-06-08 13:59:19 +02:00
|
|
|
/**
|
|
|
|
* Test html_extract_tag() when the tag <meta name= is found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractExistentNameTag()
|
|
|
|
{
|
|
|
|
$description = 'Bob and Alice share cookies.';
|
|
|
|
$html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test html_extract_tag() when the tag <meta name= is not found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractNonExistentNameTag()
|
|
|
|
{
|
|
|
|
$html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test html_extract_tag() when the tag <meta property="og: is found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractExistentOgTag()
|
|
|
|
{
|
|
|
|
$description = 'Bob and Alice share cookies.';
|
|
|
|
$html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test html_extract_tag() when the tag <meta property="og: is not found.
|
|
|
|
*/
|
|
|
|
public function testHtmlExtractNonExistentOgTag()
|
|
|
|
{
|
|
|
|
$html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
}
|
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
/**
|
|
|
|
* Test the download callback with valid value
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOk()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_ok'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$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',
|
2019-06-08 13:59:19 +02:00
|
|
|
'<title>ignored</title>'
|
|
|
|
. '<meta name="description" content="desc" />'
|
|
|
|
. '<meta name="keywords" content="key1,key2" />',
|
2017-09-30 11:04:13 +02:00
|
|
|
];
|
|
|
|
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);
|
2019-06-08 13:59:19 +02:00
|
|
|
$this->assertEmpty($desc);
|
|
|
|
$this->assertEmpty($keywords);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no charset
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkNoCharset()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_no_charset'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$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',
|
2019-06-08 13:59:19 +02:00
|
|
|
'<title>ignored</title>'
|
|
|
|
. '<meta name="description" content="desc" />'
|
|
|
|
. '<meta name="keywords" content="key1,key2" />',
|
2017-09-30 11:04:13 +02:00
|
|
|
];
|
|
|
|
foreach ($data as $key => $line) {
|
|
|
|
$ignore = null;
|
|
|
|
$this->assertEquals(strlen($line), $callback($ignore, $line));
|
|
|
|
}
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEquals('Refactoring · GitHub', $title);
|
2019-06-08 13:59:19 +02:00
|
|
|
$this->assertEmpty($desc);
|
|
|
|
$this->assertEmpty($keywords);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no charset
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkHtmlCharset()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_no_charset'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$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',
|
2019-06-08 13:59:19 +02:00
|
|
|
'<title>ignored</title>'
|
|
|
|
. '<meta name="description" content="desc" />'
|
|
|
|
. '<meta name="keywords" content="key1,key2" />',
|
2017-09-30 11:04:13 +02:00
|
|
|
];
|
|
|
|
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);
|
2019-06-08 13:59:19 +02:00
|
|
|
$this->assertEmpty($desc);
|
|
|
|
$this->assertEmpty($keywords);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no title
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkNoTitle()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_ok'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$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);
|
2019-06-08 13:59:19 +02:00
|
|
|
$this->assertEmpty($desc);
|
|
|
|
$this->assertEmpty($keywords);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with an invalid content type.
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackInvalidContentType()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_ct_ko'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$ignore = null;
|
|
|
|
$this->assertFalse($callback($ignore, ''));
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEmpty($title);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with an invalid response code.
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackInvalidResponseCode()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = $callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_rc_ko'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$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()
|
|
|
|
{
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = $callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
false,
|
|
|
|
'ut_curl_getinfo_rs_ct_ko'
|
|
|
|
);
|
2017-09-30 11:04:13 +02:00
|
|
|
$ignore = null;
|
|
|
|
$this->assertFalse($callback($ignore, ''));
|
|
|
|
$this->assertEmpty($charset);
|
|
|
|
$this->assertEmpty($title);
|
|
|
|
}
|
|
|
|
|
2019-06-08 13:59:19 +02:00
|
|
|
/**
|
|
|
|
* Test the download callback with valid value, and retrieve_description option enabled.
|
|
|
|
*/
|
|
|
|
public function testCurlDownloadCallbackOkWithDesc()
|
|
|
|
{
|
|
|
|
$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">'
|
|
|
|
. '<title>Refactoring · GitHub</title>'
|
|
|
|
. '<link rel="search" type="application/opensea',
|
|
|
|
'end' => '<title>ignored</title>'
|
|
|
|
. '<meta name="description" content="link desc" />'
|
|
|
|
. '<meta name="keywords" content="key1,key2" />',
|
|
|
|
];
|
|
|
|
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">'
|
|
|
|
. '<title>Refactoring · GitHub</title>'
|
|
|
|
. '<link rel="search" type="application/opensea',
|
|
|
|
'end' => '<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);
|
|
|
|
$this->assertEmpty($desc);
|
|
|
|
$this->assertEmpty($keywords);
|
|
|
|
}
|
|
|
|
|
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 = '')
|
|
|
|
{
|
2020-05-12 12:44:48 +02:00
|
|
|
$hashtagLink = '<a href="' . $index . './add-tag/$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
|
|
|
}
|