2016-01-04 10:45:54 +01:00
|
|
|
<?php
|
|
|
|
|
2018-12-03 01:35:14 +01:00
|
|
|
namespace Shaarli\Bookmark;
|
|
|
|
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\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.';
|
2020-09-03 17:46:26 +02:00
|
|
|
|
|
|
|
// Simple one line
|
2019-06-08 13:59:19 +02:00
|
|
|
$html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
2020-09-03 17:46:26 +02:00
|
|
|
|
|
|
|
// Simple OpenGraph
|
|
|
|
$html = '<meta property="og:description" content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// Simple reversed OpenGraph
|
|
|
|
$html = '<meta content="' . $description . '" property="og:description">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// ItemProp OpenGraph
|
|
|
|
$html = '<meta itemprop="og:description" content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph without quotes
|
|
|
|
$html = '<meta property=og:description content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph reversed without quotes
|
|
|
|
$html = '<meta content="' . $description . '" property=og:description>';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph with noise
|
|
|
|
$html = '<meta tag1="content1" property="og:description" tag2="content2" content="' .
|
|
|
|
$description . '" tag3="content3">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph reversed with noise
|
|
|
|
$html = '<meta tag1="content1" content="' . $description . '" ' .
|
|
|
|
'tag3="content3" tag2="content2" property="og:description">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph multiple properties start
|
|
|
|
$html = '<meta property="unrelated og:description" content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph multiple properties end
|
|
|
|
$html = '<meta property="og:description unrelated" content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph multiple properties both end
|
|
|
|
$html = '<meta property="og:unrelated1 og:description og:unrelated2" content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph multiple properties both end with noise
|
|
|
|
$html = '<meta tag1="content1" property="og:unrelated1 og:description og:unrelated2" '.
|
|
|
|
'tag2="content2" content="' . $description . '" tag3="content3">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph reversed multiple properties start
|
|
|
|
$html = '<meta content="' . $description . '" property="unrelated og:description">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph reversed multiple properties end
|
|
|
|
$html = '<meta content="' . $description . '" property="og:description unrelated">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph reversed multiple properties both end
|
|
|
|
$html = '<meta content="' . $description . '" property="og:unrelated1 og:description og:unrelated2">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// OpenGraph reversed multiple properties both end with noise
|
|
|
|
$html = '<meta tag1="content1" content="' . $description . '" tag2="content2" '.
|
|
|
|
'property="og:unrelated1 og:description og:unrelated2" tag3="content3">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
// Suggestion from #1375
|
|
|
|
$html = '<meta property="og:description" name="description" content="' . $description . '">';
|
|
|
|
$this->assertEquals($description, html_extract_tag('description', $html));
|
2019-06-08 13:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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));
|
2020-09-03 17:46:26 +02:00
|
|
|
|
|
|
|
// Partial meta tag
|
|
|
|
$html = '<meta content="Brief description">';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
$html = '<meta property="og:description">';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
$html = '<meta tag1="content1" property="og:description">';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
$html = '<meta property="og:description" tag1="content1">';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
$html = '<meta tag1="content1" content="Brief description">';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
|
|
|
|
|
|
|
$html = '<meta content="Brief description" tag1="content1">';
|
|
|
|
$this->assertFalse(html_extract_tag('description', $html));
|
2019-06-08 13:59:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
|
|
|
|
2020-10-15 11:20:33 +02:00
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertIsInt($callback(null, $chunk));
|
|
|
|
}
|
|
|
|
|
|
|
|
static::assertSame('utf-8', $charset);
|
|
|
|
}
|
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
/**
|
|
|
|
* Test the download callback with valid value
|
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlDownloadCallbackOk(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$charset = 'utf-8';
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
2020-10-22 16:21:03 +02:00
|
|
|
false,
|
|
|
|
' '
|
2019-06-08 13:59:19 +02:00
|
|
|
);
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$data = [
|
2020-10-15 11:20:33 +02:00
|
|
|
'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
|
|
|
];
|
2020-10-15 11:20:33 +02:00
|
|
|
|
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertSame(strlen($chunk), $callback(null, $chunk));
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
2020-10-15 11:20:33 +02:00
|
|
|
|
|
|
|
static::assertSame('utf-8', $charset);
|
|
|
|
static::assertSame('Refactoring · GitHub', $title);
|
|
|
|
static::assertEmpty($desc);
|
|
|
|
static::assertEmpty($keywords);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the header callback with valid value
|
|
|
|
*/
|
|
|
|
public function testCurlHeaderCallbackNoCharset(): void
|
|
|
|
{
|
|
|
|
$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);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download callback with valid values and no charset
|
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlDownloadCallbackOkNoCharset(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$charset = null;
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
2020-10-22 16:21:03 +02:00
|
|
|
false,
|
|
|
|
' '
|
2019-06-08 13:59:19 +02:00
|
|
|
);
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$data = [
|
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
|
|
|
];
|
2020-10-15 11:20:33 +02:00
|
|
|
|
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertSame(strlen($chunk), $callback(null, $chunk));
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$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
|
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlDownloadCallbackOkHtmlCharset(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$charset = null;
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
2020-10-22 16:21:03 +02:00
|
|
|
false,
|
|
|
|
' '
|
2019-06-08 13:59:19 +02:00
|
|
|
);
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$data = [
|
|
|
|
'<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
|
|
|
];
|
2020-10-15 11:20:33 +02:00
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertSame(strlen($chunk), $callback(null, $chunk));
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$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
|
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlDownloadCallbackOkNoTitle(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$charset = 'utf-8';
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
2020-10-22 16:21:03 +02:00
|
|
|
false,
|
|
|
|
' '
|
2019-06-08 13:59:19 +02:00
|
|
|
);
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$data = [
|
|
|
|
'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
|
|
|
|
'ignored',
|
|
|
|
];
|
2020-10-15 11:20:33 +02:00
|
|
|
|
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertSame(strlen($chunk), $callback(null, $chunk));
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2017-09-30 11:04:13 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-15 11:20:33 +02:00
|
|
|
* Test the header callback with an invalid content type.
|
2017-09-30 11:04:13 +02:00
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlHeaderCallbackInvalidContentType(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ct_ko');
|
|
|
|
$data = [
|
|
|
|
'HTTP/1.1 200 OK',
|
|
|
|
];
|
|
|
|
|
|
|
|
static::assertFalse($callback(null, $data[0]));
|
|
|
|
static::assertNull($charset);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-15 11:20:33 +02:00
|
|
|
* Test the header callback with an invalid response code.
|
2017-09-30 11:04:13 +02:00
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlHeaderCallbackInvalidResponseCode(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko');
|
|
|
|
|
|
|
|
static::assertFalse($callback(null, ''));
|
|
|
|
static::assertNull($charset);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-15 11:20:33 +02:00
|
|
|
* Test the header callback with an invalid content type and response code.
|
2017-09-30 11:04:13 +02:00
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void
|
2017-09-30 11:04:13 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko');
|
|
|
|
|
|
|
|
static::assertFalse($callback(null, ''));
|
|
|
|
static::assertNull($charset);
|
2017-09-30 11:04:13 +02:00
|
|
|
}
|
|
|
|
|
2019-06-08 13:59:19 +02:00
|
|
|
/**
|
|
|
|
* Test the download callback with valid value, and retrieve_description option enabled.
|
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlDownloadCallbackOkWithDesc(): void
|
2019-06-08 13:59:19 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$charset = 'utf-8';
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
2020-10-22 16:21:03 +02:00
|
|
|
true,
|
|
|
|
' '
|
2019-06-08 13:59:19 +02:00
|
|
|
);
|
|
|
|
$data = [
|
|
|
|
'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" />',
|
|
|
|
];
|
2020-10-15 11:20:33 +02:00
|
|
|
|
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertSame(strlen($chunk), $callback(null, $chunk));
|
2019-06-08 13:59:19 +02:00
|
|
|
}
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2019-06-08 13:59:19 +02:00
|
|
|
$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.
|
|
|
|
*/
|
2020-10-15 11:20:33 +02:00
|
|
|
public function testCurlDownloadCallbackOkWithDescNotFound(): void
|
2019-06-08 13:59:19 +02:00
|
|
|
{
|
2020-10-15 11:20:33 +02:00
|
|
|
$charset = 'utf-8';
|
2019-06-08 13:59:19 +02:00
|
|
|
$callback = get_curl_download_callback(
|
|
|
|
$charset,
|
|
|
|
$title,
|
|
|
|
$desc,
|
|
|
|
$keywords,
|
|
|
|
true,
|
|
|
|
'ut_curl_getinfo_ok'
|
|
|
|
);
|
|
|
|
$data = [
|
|
|
|
'th=device-width">'
|
|
|
|
. '<title>Refactoring · GitHub</title>'
|
|
|
|
. '<link rel="search" type="application/opensea',
|
|
|
|
'end' => '<title>ignored</title>',
|
|
|
|
];
|
2020-10-15 11:20:33 +02:00
|
|
|
|
|
|
|
foreach ($data as $chunk) {
|
|
|
|
static::assertSame(strlen($chunk), $callback(null, $chunk));
|
2019-06-08 13:59:19 +02:00
|
|
|
}
|
2020-10-15 11:20:33 +02:00
|
|
|
|
2019-06-08 13:59:19 +02:00
|
|
|
$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);
|
|
|
|
|
2020-09-29 14:41:40 +02:00
|
|
|
$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);
|
2016-05-10 23:18:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test hashtags auto-link without index URL.
|
|
|
|
*/
|
|
|
|
public function testHashtagAutolinkNoIndex()
|
|
|
|
{
|
|
|
|
$rawDescription = 'blabla #hashtag x#nothashtag';
|
|
|
|
$autolinkedDescription = hashtag_autolink($rawDescription);
|
|
|
|
|
2020-09-29 14:41:40 +02:00
|
|
|
$this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
|
|
|
|
$this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
|
|
|
|
$this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
|
2016-05-10 23:18:04 +02:00
|
|
|
}
|
|
|
|
|
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'));
|
|
|
|
}
|
|
|
|
|
2020-10-22 16:21:03 +02:00
|
|
|
/**
|
|
|
|
* Test tags_str2array with whitespace separator.
|
|
|
|
*/
|
|
|
|
public function testTagsStr2ArrayWithSpaceSeparator(): void
|
|
|
|
{
|
|
|
|
$separator = ' ';
|
|
|
|
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator));
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator));
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array(' tag1 tag2 tag3 ', $separator));
|
|
|
|
static::assertSame(['tag1@', 'tag2,', '.tag3'], tags_str2array(' tag1@ tag2, .tag3 ', $separator));
|
|
|
|
static::assertSame([], tags_str2array('', $separator));
|
|
|
|
static::assertSame([], tags_str2array(' ', $separator));
|
|
|
|
static::assertSame([], tags_str2array(null, $separator));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test tags_str2array with @ separator.
|
|
|
|
*/
|
|
|
|
public function testTagsStr2ArrayWithCharSeparator(): void
|
|
|
|
{
|
|
|
|
$separator = '@';
|
|
|
|
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@tag2@tag3', $separator));
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@@@@tag2@@@@tag3', $separator));
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('@@@tag1@@@tag2@@@@tag3@@', $separator));
|
|
|
|
static::assertSame(
|
|
|
|
['tag1#', 'tag2, and other', '.tag3'],
|
|
|
|
tags_str2array('@@@ tag1# @@@ tag2, and other @@@@.tag3@@', $separator)
|
|
|
|
);
|
|
|
|
static::assertSame([], tags_str2array('', $separator));
|
|
|
|
static::assertSame([], tags_str2array(' ', $separator));
|
|
|
|
static::assertSame([], tags_str2array(null, $separator));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test tags_array2str with ' ' separator.
|
|
|
|
*/
|
|
|
|
public function testTagsArray2StrWithSpaceSeparator(): void
|
|
|
|
{
|
|
|
|
$separator = ' ';
|
|
|
|
|
|
|
|
static::assertSame('tag1 tag2 tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator));
|
|
|
|
static::assertSame('tag1, tag2@ tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator));
|
|
|
|
static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', 'tag2', 'tag3 '], $separator));
|
|
|
|
static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator));
|
|
|
|
static::assertSame('tag1', tags_array2str([' tag1 '], $separator));
|
|
|
|
static::assertSame('', tags_array2str([' '], $separator));
|
|
|
|
static::assertSame('', tags_array2str([], $separator));
|
|
|
|
static::assertSame('', tags_array2str(null, $separator));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test tags_array2str with @ separator.
|
|
|
|
*/
|
|
|
|
public function testTagsArray2StrWithCharSeparator(): void
|
|
|
|
{
|
|
|
|
$separator = '@';
|
|
|
|
|
|
|
|
static::assertSame('tag1@tag2@tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator));
|
|
|
|
static::assertSame('tag1,@tag2@tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator));
|
|
|
|
static::assertSame(
|
|
|
|
'tag1@tag2, and other@tag3',
|
|
|
|
tags_array2str(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator)
|
|
|
|
);
|
|
|
|
static::assertSame('tag1@tag2@tag3', tags_array2str(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator));
|
|
|
|
static::assertSame('tag1', tags_array2str(['@@@@tag1@@@@'], $separator));
|
|
|
|
static::assertSame('', tags_array2str(['@@@'], $separator));
|
|
|
|
static::assertSame('', tags_array2str([], $separator));
|
|
|
|
static::assertSame('', tags_array2str(null, $separator));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test tags_array2str with @ separator.
|
|
|
|
*/
|
|
|
|
public function testTagsFilterWithSpaceSeparator(): void
|
|
|
|
{
|
|
|
|
$separator = ' ';
|
|
|
|
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator));
|
|
|
|
static::assertSame(['tag1,', 'tag2@', 'tag3'], tags_filter(['tag1,', 'tag2@', 'tag3'], $separator));
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', 'tag2', 'tag3 '], $separator));
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator));
|
|
|
|
static::assertSame(['tag1'], tags_filter([' tag1 '], $separator));
|
|
|
|
static::assertSame([], tags_filter([' '], $separator));
|
|
|
|
static::assertSame([], tags_filter([], $separator));
|
|
|
|
static::assertSame([], tags_filter(null, $separator));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test tags_array2str with @ separator.
|
|
|
|
*/
|
|
|
|
public function testTagsArrayFilterWithSpaceSeparator(): void
|
|
|
|
{
|
|
|
|
$separator = '@';
|
|
|
|
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator));
|
|
|
|
static::assertSame(['tag1,', 'tag2#', 'tag3'], tags_filter(['tag1,', 'tag2#', 'tag3'], $separator));
|
|
|
|
static::assertSame(
|
|
|
|
['tag1', 'tag2, and other', 'tag3'],
|
|
|
|
tags_filter(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator)
|
|
|
|
);
|
|
|
|
static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator));
|
|
|
|
static::assertSame(['tag1'], tags_filter(['@@@@tag1@@@@'], $separator));
|
|
|
|
static::assertSame([], tags_filter(['@@@'], $separator));
|
|
|
|
static::assertSame([], tags_filter([], $separator));
|
|
|
|
static::assertSame([], tags_filter(null, $separator));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|