diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-01-04 10:45:54 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-01-11 21:19:31 +0100 |
commit | 1557cefbd76257ceb830f65806831b490faf0acc (patch) | |
tree | 787f6d8fdabe8ea2fc0c37b61d616e667cdfbda5 /tests/LinkUtilsTest.php | |
parent | c0a50f3663e207d5df007e0fa321219c1b32d6ea (diff) | |
download | Shaarli-1557cefbd76257ceb830f65806831b490faf0acc.tar.gz Shaarli-1557cefbd76257ceb830f65806831b490faf0acc.tar.zst Shaarli-1557cefbd76257ceb830f65806831b490faf0acc.zip |
Fixes #410 - Retrieve title fails in multiple cases
* `get_http_url()` renamed to `get_http_response()`.
* Use the same HTTP context to retrieve response headers and content.
* Follow HTTP 301 and 302 redirections to retrieve the title (default max 3 redirections).
* Add `LinkUtils` to extract titles and charset.
* Try to retrieve charset from HTTP headers first (new), then HTML content.
* Use mb_string to re-encode title if necessary.
Diffstat (limited to 'tests/LinkUtilsTest.php')
-rw-r--r-- | tests/LinkUtilsTest.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/LinkUtilsTest.php b/tests/LinkUtilsTest.php new file mode 100644 index 00000000..c2257590 --- /dev/null +++ b/tests/LinkUtilsTest.php | |||
@@ -0,0 +1,85 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'application/LinkUtils.php'; | ||
4 | |||
5 | /** | ||
6 | * Class LinkUtilsTest. | ||
7 | */ | ||
8 | class LinkUtilsTest extends PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | /** | ||
11 | * Test html_extract_title() when the title is found. | ||
12 | */ | ||
13 | public function testHtmlExtractExistentTitle() | ||
14 | { | ||
15 | $title = 'Read me please.'; | ||
16 | $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>'; | ||
17 | $this->assertEquals($title, html_extract_title($html)); | ||
18 | } | ||
19 | |||
20 | /** | ||
21 | * Test html_extract_title() when the title is not found. | ||
22 | */ | ||
23 | public function testHtmlExtractNonExistentTitle() | ||
24 | { | ||
25 | $html = '<html><meta>stuff</meta></html>'; | ||
26 | $this->assertFalse(html_extract_title($html)); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Test get_charset() with all priorities. | ||
31 | */ | ||
32 | public function testGetCharset() | ||
33 | { | ||
34 | $headers = array('Content-Type' => 'text/html; charset=Headers'); | ||
35 | $html = '<html><meta>stuff</meta><meta charset="Html"/></html>'; | ||
36 | $default = 'default'; | ||
37 | $this->assertEquals('headers', get_charset($headers, $html, $default)); | ||
38 | $this->assertEquals('html', get_charset(array(), $html, $default)); | ||
39 | $this->assertEquals($default, get_charset(array(), '', $default)); | ||
40 | $this->assertEquals('utf-8', get_charset(array(), '')); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Test headers_extract_charset() when the charset is found. | ||
45 | */ | ||
46 | public function testHeadersExtractExistentCharset() | ||
47 | { | ||
48 | $charset = 'x-MacCroatian'; | ||
49 | $headers = array('Content-Type' => 'text/html; charset='. $charset); | ||
50 | $this->assertEquals(strtolower($charset), headers_extract_charset($headers)); | ||
51 | } | ||
52 | |||
53 | /** | ||
54 | * Test headers_extract_charset() when the charset is not found. | ||
55 | */ | ||
56 | public function testHeadersExtractNonExistentCharset() | ||
57 | { | ||
58 | $headers = array(); | ||
59 | $this->assertFalse(headers_extract_charset($headers)); | ||
60 | |||
61 | $headers = array('Content-Type' => 'text/html'); | ||
62 | $this->assertFalse(headers_extract_charset($headers)); | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Test html_extract_charset() when the charset is found. | ||
67 | */ | ||
68 | public function testHtmlExtractExistentCharset() | ||
69 | { | ||
70 | $charset = 'x-MacCroatian'; | ||
71 | $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>'; | ||
72 | $this->assertEquals(strtolower($charset), html_extract_charset($html)); | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Test html_extract_charset() when the charset is not found. | ||
77 | */ | ||
78 | public function testHtmlExtractNonExistentCharset() | ||
79 | { | ||
80 | $html = '<html><meta>stuff</meta></html>'; | ||
81 | $this->assertFalse(html_extract_charset($html)); | ||
82 | $html = '<html><meta>stuff</meta><meta charset=""/></html>'; | ||
83 | $this->assertFalse(html_extract_charset($html)); | ||
84 | } | ||
85 | } | ||