1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-04-16 12:48:32 +02:00

Properly catch issues with encoding conversion in HTML meta helper (#225)

This commit is contained in:
Kovah 2021-02-04 16:02:49 +01:00
parent 0d18d7f540
commit 52e1c5f09f
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
2 changed files with 41 additions and 20 deletions

View File

@ -126,8 +126,13 @@ class HtmlMeta
?? self::$fallback['description'];
if (isset($metaTags['charset']) && strtolower($metaTags['charset']) !== 'utf-8') {
$title = iconv($metaTags['charset'], 'UTF-8', $title) ?: null;
$description = iconv($metaTags['charset'], 'UTF-8', $description) ?: null;
try {
$title = iconv($metaTags['charset'], 'UTF-8', $title) ?: null;
$description = iconv($metaTags['charset'], 'UTF-8', $description) ?: null;
} catch (\ErrorException $e) {
$title = null;
$description = null;
}
} elseif (mb_detect_encoding($title, 'UTF-8', true) === false) {
$title = null;
$description = null;

View File

@ -12,10 +12,8 @@ use Tests\TestCase;
class HtmlMetaHelperTest extends TestCase
{
/**
* Test the titleFromURL() helper funtion with a valid URL
* Will return the title of the Google frontpage: "Google"
*
* @return void
* Test the titleFromURL() helper function with a valid URL
* Will return the title of the DuckDuckGo frontpage: "DuckDuckGo"
*/
public function testTitleFromValidURL(): void
{
@ -40,10 +38,8 @@ class HtmlMetaHelperTest extends TestCase
}
/**
* Test the titleFromURL() helper funtion with a valid URL
* Will return the title of the Google frontpage: "Google"
*
* @return void
* Test the titleFromURL() helper function with a valid URL
* Will return the title of the DuckDuckGo frontpage: "DuckDuckGo".
*/
public function testAlternativeDescriptionFromValidURL(): void
{
@ -67,10 +63,8 @@ class HtmlMetaHelperTest extends TestCase
}
/**
* Test the titleFromURL() helper funtion with an invalid URL
* Will geturn just the host of the given URL
*
* @return void
* Test the titleFromURL() helper function with an invalid URL
* Will return just the host of the given URL.
*/
public function testTitleFromInvalidURL(): void
{
@ -88,10 +82,8 @@ class HtmlMetaHelperTest extends TestCase
}
/**
* Test the titleFromURL() helper funtion with an invalid URL
* Will geturn just the host of the given URL
*
* @return void
* Test the titleFromURL() helper function with an invalid URL
* Will return just the host of the given URL.
*/
public function testTitleFromURLwithoutProtocol(): void
{
@ -109,7 +101,7 @@ class HtmlMetaHelperTest extends TestCase
}
/**
* Test the titleFromURL() helper funtion with an valid URL that returns
* Test the titleFromURL() helper function with an valid URL that returns
* a certificate error.
* Will return just the host of the given URL and issue a new flash message.
*/
@ -139,7 +131,7 @@ class HtmlMetaHelperTest extends TestCase
}
/**
* Test the titleFromURL() helper funtion with an valid URL that is not
* Test the titleFromURL() helper function with an valid URL that is not
* accessible due to connection errors, such as a refused connection for
* a specific port.
* Will return just the host of the given URL and issue a new flash message.
@ -217,4 +209,28 @@ class HtmlMetaHelperTest extends TestCase
$this->assertArrayHasKey('title', $result);
$this->assertEquals('duckduckgo.com', $result['title']);
}
/**
* Test the titleFromURL() helper function with a valid URL.
* Should return the host as the title because conversion is not possible
* in this case.
*/
public function testMetaEncodingWithIncorrectCharset(): void
{
$testHtml = '<!DOCTYPE html><head>' .
'<title>DuckDuckGo</title>' .
'<meta charset="utf-8,windows-1251">' .
'</head></html>';
Http::fake([
'*' => Http::response($testHtml, 200),
]);
$url = 'https://duckduckgo.com/';
$result = HtmlMeta::getFromUrl($url);
$this->assertArrayHasKey('title', $result);
$this->assertEquals('duckduckgo.com', $result['title']);
$this->assertTrue($result['success']);
}
}