]>
Commit | Line | Data |
---|---|---|
1557cefb A |
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)); | |
68ea1d2b A |
18 | $html = '<html><title>'. $title .'</title>blabla<title>another</title></html>'; |
19 | $this->assertEquals($title, html_extract_title($html)); | |
1557cefb A |
20 | } |
21 | ||
22 | /** | |
23 | * Test html_extract_title() when the title is not found. | |
24 | */ | |
25 | public function testHtmlExtractNonExistentTitle() | |
26 | { | |
27 | $html = '<html><meta>stuff</meta></html>'; | |
28 | $this->assertFalse(html_extract_title($html)); | |
29 | } | |
30 | ||
31 | /** | |
32 | * Test get_charset() with all priorities. | |
33 | */ | |
34 | public function testGetCharset() | |
35 | { | |
36 | $headers = array('Content-Type' => 'text/html; charset=Headers'); | |
37 | $html = '<html><meta>stuff</meta><meta charset="Html"/></html>'; | |
38 | $default = 'default'; | |
39 | $this->assertEquals('headers', get_charset($headers, $html, $default)); | |
40 | $this->assertEquals('html', get_charset(array(), $html, $default)); | |
41 | $this->assertEquals($default, get_charset(array(), '', $default)); | |
42 | $this->assertEquals('utf-8', get_charset(array(), '')); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Test headers_extract_charset() when the charset is found. | |
47 | */ | |
48 | public function testHeadersExtractExistentCharset() | |
49 | { | |
50 | $charset = 'x-MacCroatian'; | |
51 | $headers = array('Content-Type' => 'text/html; charset='. $charset); | |
52 | $this->assertEquals(strtolower($charset), headers_extract_charset($headers)); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Test headers_extract_charset() when the charset is not found. | |
57 | */ | |
58 | public function testHeadersExtractNonExistentCharset() | |
59 | { | |
60 | $headers = array(); | |
61 | $this->assertFalse(headers_extract_charset($headers)); | |
62 | ||
63 | $headers = array('Content-Type' => 'text/html'); | |
64 | $this->assertFalse(headers_extract_charset($headers)); | |
65 | } | |
66 | ||
67 | /** | |
68 | * Test html_extract_charset() when the charset is found. | |
69 | */ | |
70 | public function testHtmlExtractExistentCharset() | |
71 | { | |
72 | $charset = 'x-MacCroatian'; | |
73 | $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>'; | |
74 | $this->assertEquals(strtolower($charset), html_extract_charset($html)); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Test html_extract_charset() when the charset is not found. | |
79 | */ | |
80 | public function testHtmlExtractNonExistentCharset() | |
81 | { | |
82 | $html = '<html><meta>stuff</meta></html>'; | |
83 | $this->assertFalse(html_extract_charset($html)); | |
84 | $html = '<html><meta>stuff</meta><meta charset=""/></html>'; | |
85 | $this->assertFalse(html_extract_charset($html)); | |
86 | } | |
141a86c5 A |
87 | |
88 | /** | |
89 | * Test count_private. | |
90 | */ | |
91 | public function testCountPrivateLinks() | |
92 | { | |
93 | $refDB = new ReferenceLinkDB(); | |
94 | $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks())); | |
95 | } | |
9ccca401 A |
96 | |
97 | /** | |
98 | * Test text2clickable without a redirector being set. | |
99 | */ | |
100 | public function testText2clickableWithoutRedirector() | |
101 | { | |
102 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
103 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff'; | |
104 | $processedText = text2clickable($text, ''); | |
105 | $this->assertEquals($expectedText, $processedText); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Test text2clickable a redirector set. | |
110 | */ | |
111 | public function testText2clickableWithRedirector() | |
112 | { | |
113 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
114 | $redirector = 'http://redirector.to'; | |
115 | $expectedText = 'stuff <a href="'. | |
116 | $redirector . | |
117 | urlencode('http://hello.there/is=someone#here') . | |
118 | '">http://hello.there/is=someone#here</a> otherstuff'; | |
119 | $processedText = text2clickable($text, $redirector); | |
120 | $this->assertEquals($expectedText, $processedText); | |
121 | } | |
122 | ||
123 | /** | |
124 | * Test testSpace2nbsp. | |
125 | */ | |
126 | public function testSpace2nbsp() | |
127 | { | |
128 | $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; | |
129 | $expectedText = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; | |
130 | $processedText = space2nbsp($text); | |
131 | $this->assertEquals($expectedText, $processedText); | |
132 | } | |
133 | ||
134 | /** | |
135 | * Test hashtags auto-link. | |
136 | */ | |
137 | public function testHashtagAutolink() | |
138 | { | |
139 | $index = 'http://domain.tld/'; | |
140 | $rawDescription = '#hashtag\n | |
141 | # nothashtag\n | |
142 | test#nothashtag #hashtag \#nothashtag\n | |
143 | test #hashtag #hashtag test #hashtag.test\n | |
144 | #hashtag #hashtag-nothashtag #hashtag_hashtag\n | |
145 | What is #ашок anyway?\n | |
146 | カタカナ #カタカナ」カタカナ\n'; | |
147 | $autolinkedDescription = hashtag_autolink($rawDescription, $index); | |
148 | ||
149 | $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription); | |
150 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | |
151 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | |
152 | $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription); | |
153 | $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); | |
154 | $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); | |
155 | $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test hashtags auto-link without index URL. | |
160 | */ | |
161 | public function testHashtagAutolinkNoIndex() | |
162 | { | |
163 | $rawDescription = 'blabla #hashtag x#nothashtag'; | |
164 | $autolinkedDescription = hashtag_autolink($rawDescription); | |
165 | ||
166 | $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription); | |
167 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | |
168 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Util function to build an hashtag link. | |
173 | * | |
174 | * @param string $hashtag Hashtag name. | |
175 | * @param string $index Index URL. | |
176 | * | |
177 | * @return string HTML hashtag link. | |
178 | */ | |
179 | private function getHashtagLink($hashtag, $index = '') | |
180 | { | |
181 | $hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>'; | |
182 | return str_replace('$1', $hashtag, $hashtagLink); | |
183 | } | |
1557cefb | 184 | } |