3 require_once 'application/LinkUtils.php';
8 class LinkUtilsTest
extends PHPUnit_Framework_TestCase
11 * Test html_extract_title() when the title is found.
13 public function testHtmlExtractExistentTitle()
15 $title = 'Read me please.';
16 $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>';
17 $this->assertEquals($title, html_extract_title($html));
18 $html = '<html><title>'. $title .'</title>blabla<title>another</title></html>';
19 $this->assertEquals($title, html_extract_title($html));
23 * Test html_extract_title() when the title is not found.
25 public function testHtmlExtractNonExistentTitle()
27 $html = '<html><meta>stuff</meta></html>';
28 $this->assertFalse(html_extract_title($html));
32 * Test get_charset() with all priorities.
34 public function testGetCharset()
36 $headers = array('Content-Type' => 'text/html; charset=Headers');
37 $html = '<html><meta>stuff</meta><meta charset="Html"/></html>';
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(), ''));
46 * Test headers_extract_charset() when the charset is found.
48 public function testHeadersExtractExistentCharset()
50 $charset = 'x-MacCroatian';
51 $headers = array('Content-Type' => 'text/html; charset='. $charset);
52 $this->assertEquals(strtolower($charset), headers_extract_charset($headers));
56 * Test headers_extract_charset() when the charset is not found.
58 public function testHeadersExtractNonExistentCharset()
61 $this->assertFalse(headers_extract_charset($headers));
63 $headers = array('Content-Type' => 'text/html');
64 $this->assertFalse(headers_extract_charset($headers));
68 * Test html_extract_charset() when the charset is found.
70 public function testHtmlExtractExistentCharset()
72 $charset = 'x-MacCroatian';
73 $html = '<html><meta>stuff2</meta><meta charset="'. $charset .'"/></html>';
74 $this->assertEquals(strtolower($charset), html_extract_charset($html));
78 * Test html_extract_charset() when the charset is not found.
80 public function testHtmlExtractNonExistentCharset()
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));
91 public function testCountPrivateLinks()
93 $refDB = new ReferenceLinkDB();
94 $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
98 * Test text2clickable without a redirector being set.
100 public function testText2clickableWithoutRedirector()
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);
107 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
108 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">http://hello.there/is=someone#here(please)</a> otherstuff';
109 $processedText = text2clickable($text, '');
110 $this->assertEquals($expectedText, $processedText);
112 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
113 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">http://hello.there/is=someone#here(please)&no</a> otherstuff';
114 $processedText = text2clickable($text, '');
115 $this->assertEquals($expectedText, $processedText);
119 * Test text2clickable a redirector set.
121 public function testText2clickableWithRedirector()
123 $text = 'stuff http://hello.there/is=someone#here otherstuff';
124 $redirector = 'http://redirector.to';
125 $expectedText = 'stuff <a href="'.
127 urlencode('http://hello.there/is=someone#here') .
128 '">http://hello.there/is=someone#here</a> otherstuff';
129 $processedText = text2clickable($text, $redirector);
130 $this->assertEquals($expectedText, $processedText);
134 * Test text2clickable a redirector set and without URL encode.
136 public function testText2clickableWithRedirectorDontEncode()
138 $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
139 $redirector = 'http://redirector.to';
140 $expectedText = 'stuff <a href="'.
142 'http://hello.there/?is=someone&or=something#here' .
143 '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
144 $processedText = text2clickable($text, $redirector, false);
145 $this->assertEquals($expectedText, $processedText);
149 * Test testSpace2nbsp.
151 public function testSpace2nbsp()
153 $text = ' Are you thrilled by flags ?'. PHP_EOL
.' Really?';
154 $expectedText = ' Are you thrilled by flags ?'. PHP_EOL
.' Really?';
155 $processedText = space2nbsp($text);
156 $this->assertEquals($expectedText, $processedText);
160 * Test hashtags auto-link.
162 public function testHashtagAutolink()
164 $index = 'http://domain.tld/';
165 $rawDescription = '#hashtag\n
167 test#nothashtag #hashtag \#nothashtag\n
168 test #hashtag #hashtag test #hashtag.test\n
169 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
170 What is #ашок anyway?\n
172 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
174 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
175 $this->assertNotContains(' #hashtag', $autolinkedDescription);
176 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
177 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
178 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
179 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
180 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
184 * Test hashtags auto-link without index URL.
186 public function testHashtagAutolinkNoIndex()
188 $rawDescription = 'blabla #hashtag x#nothashtag';
189 $autolinkedDescription = hashtag_autolink($rawDescription);
191 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
192 $this->assertNotContains(' #hashtag', $autolinkedDescription);
193 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
197 * Util function to build an hashtag link.
199 * @param string $hashtag Hashtag name.
200 * @param string $index Index URL.
202 * @return string HTML hashtag link.
204 private function getHashtagLink($hashtag, $index = '')
206 $hashtagLink = '<a href="'. $index .'?addtag=$1" title="Hashtag $1">#$1</a>';
207 return str_replace('$1', $hashtag, $hashtagLink);