3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4 print "This script must be run from the command line\n";
8 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 define('GNUSOCIAL', true);
10 define('STATUSNET', true); // compatibility
12 require_once INSTALLDIR . '/lib/common.php';
14 class URLDetectionTest extends PHPUnit_Framework_TestCase
17 * @dataProvider provider
20 public function testProduction($content, $expected)
22 $rendered = common_render_text($content);
24 $rendered = preg_replace('/id="attachment-\d+"/', 'id="attachment-XXX"', $rendered);
25 $this->assertEquals($expected, $rendered);
29 * @dataProvider linkifyProvider
32 public function testLinkifyProduction($content, $expected, $config)
34 $rendered = common_render_text($content);
36 $rendered = preg_replace('/id="attachment-\d+"/', 'id="attachment-XXX"', $rendered);
37 if(common_config('linkify', $config)){
38 $this->assertEquals($expected, $rendered);
40 $content = common_remove_unicode_formatting(nl2br(htmlspecialchars($content)));
41 $this->assertEquals($content, $rendered);
45 static public function provider()
48 array('not a link :: no way',
49 'not a link :: no way'),
50 array('link http://www.somesite.com/xyz/35637563@N00/52803365/ link',
51 'link <a href="http://www.somesite.com/xyz/35637563@N00/52803365/" title="http://www.somesite.com/xyz/35637563@N00/52803365/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://www.somesite.com/xyz/35637563@N00/52803365/</a> link'),
52 array('http://127.0.0.1',
53 '<a href="http://127.0.0.1/" title="http://127.0.0.1/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://127.0.0.1</a>'),
54 array('http://[::1]:99/test.php',
55 '<a href="http://[::1]:99/test.php" title="http://[::1]:99/test.php" rel="nofollow external">http://[::1]:99/test.php</a>'),
56 array('http://::1/test.php',
57 '<a href="http://::1/test.php" title="http://::1/test.php" rel="nofollow external">http://::1/test.php</a>'),
59 '<a href="http://::1/" title="http://::1/" rel="nofollow external">http://::1</a>'),
60 array('http://127.0.0.1',
61 '<a href="http://127.0.0.1/" title="http://127.0.0.1/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://127.0.0.1</a>'),
62 array('http://example.com',
63 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>'),
64 array('http://example.com.',
65 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>.'),
66 array('/var/lib/example.so',
67 '/var/lib/example.so'),
70 array('mailto:user@example.com',
71 '<a href="mailto:user@example.com" title="mailto:user@example.com" rel="nofollow external">mailto:user@example.com</a>'),
72 array('mailto:user@example.com?subject=test',
73 '<a href="mailto:user@example.com?subject=test" title="mailto:user@example.com?subject=test" rel="nofollow external">mailto:user@example.com?subject=test</a>'),
74 array('xmpp:user@example.com',
75 '<a href="xmpp:user@example.com" title="xmpp:user@example.com" rel="nofollow external">xmpp:user@example.com</a>'),
77 '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('example'))) . '" rel="tag">example</a></span>'),
79 '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('example.com'))) . '" rel="tag">example.com</a></span>'),
81 '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('.net'))) . '" rel="tag">.net</a></span>'),
82 array('http://example',
83 '<a href="http://example/" title="http://example/" rel="nofollow external">http://example</a>'),
84 array('http://3xampl3',
85 '<a href="http://3xampl3/" title="http://3xampl3/" rel="nofollow external">http://3xampl3</a>'),
86 array('http://example/',
87 '<a href="http://example/" title="http://example/" rel="nofollow external">http://example/</a>'),
88 array('http://example/path',
89 '<a href="http://example/path" title="http://example/path" rel="nofollow external">http://example/path</a>'),
90 array('http://example.com',
91 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>'),
92 array('https://example.com',
93 '<a href="https://example.com/" title="https://example.com/" rel="nofollow external">https://example.com</a>'),
94 array('ftp://example.com',
95 '<a href="ftp://example.com/" title="ftp://example.com/" rel="nofollow external">ftp://example.com</a>'),
96 array('ftps://example.com',
97 '<a href="ftps://example.com/" title="ftps://example.com/" rel="nofollow external">ftps://example.com</a>'),
98 array('http://user@example.com',
99 '<a href="http://@example.com/" title="http://@example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://user@example.com</a>'),
100 array('http://user:pass@example.com',
101 '<a href="http://@example.com/" title="http://@example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://user:pass@example.com</a>'),
102 array('http://example.com:8080',
103 '<a href="http://example.com:8080/" title="http://example.com:8080/" rel="nofollow external">http://example.com:8080</a>'),
104 array('http://example.com:8080/test.php',
105 '<a href="http://example.com:8080/test.php" title="http://example.com:8080/test.php" rel="nofollow external">http://example.com:8080/test.php</a>'),
106 array('http://www.example.com',
107 '<a href="http://www.example.com/" title="http://www.example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://www.example.com</a>'),
108 array('http://example.com/',
109 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/</a>'),
110 array('http://example.com/path',
111 '<a href="http://example.com/path" title="http://example.com/path" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path</a>'),
112 array('http://example.com/path.html',
113 '<a href="http://example.com/path.html" title="http://example.com/path.html" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path.html</a>'),
114 array('http://example.com/path.html#fragment',
115 '<a href="http://example.com/path.html#fragment" title="http://example.com/path.html#fragment" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path.html#fragment</a>'),
116 array('http://example.com/path.php?foo=bar&bar=foo',
117 '<a href="http://example.com/path.php?foo=bar&bar=foo" title="http://example.com/path.php?foo=bar&bar=foo" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path.php?foo=bar&bar=foo</a>'),
118 array('http://example.com.',
119 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>.'),
120 array('http://müllärör.de',
121 '<a href="http://müllärör.de/" title="http://müllärör.de/" rel="nofollow external">http://müllärör.de</a>'),
122 array('http://ﺱﺲﺷ.com',
123 '<a href="http://ﺱﺲﺷ.com/" title="http://ﺱﺲﺷ.com/" rel="nofollow external">http://ﺱﺲﺷ.com</a>'),
124 array('http://сделаткартинки.com',
125 '<a href="http://сделаткартинки.com/" title="http://сделаткартинки.com/" rel="nofollow external">http://сделаткартинки.com</a>'),
126 array('http://tūdaliņ.lv',
127 '<a href="http://tūdaliņ.lv/" title="http://tūdaliņ.lv/" rel="nofollow external">http://tūdaliņ.lv</a>'),
128 array('http://brændendekærlighed.com',
129 '<a href="http://brændendekærlighed.com/" title="http://brændendekærlighed.com/" rel="nofollow external">http://brændendekærlighed.com</a>'),
130 array('http://あーるいん.com',
131 '<a href="http://あーるいん.com/" title="http://あーるいん.com/" rel="nofollow external">http://あーるいん.com</a>'),
132 array('http://예비교사.com',
133 '<a href="http://예비교사.com/" title="http://예비교사.com/" rel="nofollow external">http://예비교사.com</a>'),
134 array('http://example.com.',
135 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>.'),
136 array('http://example.com?',
137 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>?'),
138 array('http://example.com!',
139 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>!'),
140 array('http://example.com,',
141 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>,'),
142 array('http://example.com;',
143 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>;'),
144 array('http://example.com:',
145 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>:'),
146 array('\'http://example.com\'',
147 '\'<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>\''),
148 array('"http://example.com"',
149 '"<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>"'),
150 array('"http://example.com/"',
151 '"<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/</a>"'),
152 array('http://example.com',
153 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>'),
154 array('(http://example.com)',
155 '(<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>)'),
156 array('[http://example.com]',
157 '[<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>]'),
158 array('<http://example.com>',
159 '<<a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a>>'),
160 array('http://example.com/path/(foo)/bar',
161 '<a href="http://example.com/path/" title="http://example.com/path/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/</a>(foo)/bar'),
162 array('http://example.com/path/[foo]/bar',
163 '<a href="http://example.com/path/" title="http://example.com/path/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/</a>[foo]/bar'),
164 array('http://example.com/path/foo/(bar)',
165 '<a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>(bar)'),
166 //Not a valid url - urls cannot contain unencoded square brackets
167 array('http://example.com/path/foo/[bar]',
168 '<a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>[bar]'),
169 array('Hey, check out my cool site http://example.com okay?',
170 'Hey, check out my cool site <a href="http://example.com/" title="http://example.com/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com</a> okay?'),
171 array('What about parens (e.g. http://example.com/path/foo/(bar))?',
172 'What about parens (e.g. <a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>(bar))?'),
173 array('What about parens (e.g. http://example.com/path/foo/(bar)?',
174 'What about parens (e.g. <a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>(bar)?'),
175 array('What about parens (e.g. http://example.com/path/foo/(bar).)?',
176 'What about parens (e.g. <a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>(bar).)?'),
177 //Not a valid url - urls cannot contain unencoded commas
178 array('What about parens (e.g. http://example.com/path/(foo,bar)?',
179 'What about parens (e.g. <a href="http://example.com/path/" title="http://example.com/path/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/</a>(foo,bar)?'),
180 array('Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?',
181 'Unbalanced too (e.g. <a href="http://example.com/path/" title="http://example.com/path/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/</a>((((foo)/bar)?'),
182 array('Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?',
183 'Unbalanced too (e.g. <a href="http://example.com/path/" title="http://example.com/path/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/</a>(foo))))/bar)?'),
184 array('Unbalanced too (e.g. http://example.com/path/foo/((((bar)?',
185 'Unbalanced too (e.g. <a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>((((bar)?'),
186 array('Unbalanced too (e.g. http://example.com/path/foo/(bar))))?',
187 'Unbalanced too (e.g. <a href="http://example.com/path/foo/" title="http://example.com/path/foo/" rel="nofollow external noreferrer" class="attachment" id="attachment-XXX">http://example.com/path/foo/</a>(bar))))?'),
195 // scheme-less HTTP URLs with @ in the path: http://status.net/open-source/issues/2248
196 array('http://flickr.com/photos/34807140@N05/3838905434',
197 '<a href="http://www.flickr.com/photos/34807140@N05/3838905434" title="http://www.flickr.com/photos/34807140@N05/3838905434" rel="nofollow external noreferrer" class="attachment thumbnail" id="attachment-XXX">http://flickr.com/photos/34807140@N05/3838905434</a>'),
201 static public function linkifyProvider()
204 //bare ip addresses are no longer supported
206 '<a href="http://127.0.0.1/" title="http://127.0.0.1/" rel="nofollow external">127.0.0.1</a>',
208 array('127.0.0.1:99',
209 '<a href="http://127.0.0.1:99/" title="http://127.0.0.1:99/" rel="nofollow external">127.0.0.1:99</a>',
211 array('127.0.0.1/Name:test.php',
212 '<a href="http://127.0.0.1/Name:test.php" title="http://127.0.0.1/Name:test.php" rel="nofollow external">127.0.0.1/Name:test.php</a>',
214 array('127.0.0.1/~test',
215 '<a href="http://127.0.0.1/~test" title="http://127.0.0.1/~test" rel="nofollow external">127.0.0.1/~test</a>',
217 array('127.0.0.1/+test',
218 '<a href="http://127.0.0.1/+test" title="http://127.0.0.1/+test" rel="nofollow external">127.0.0.1/+test</a>',
220 array('127.0.0.1/$test',
221 '<a href="http://127.0.0.1/$test" title="http://127.0.0.1/$test" rel="nofollow external">127.0.0.1/$test</a>',
223 array('127.0.0.1/\'test',
224 '<a href="http://127.0.0.1/\'test" title="http://127.0.0.1/\'test" rel="nofollow external">127.0.0.1/\'test</a>',
226 array('127.0.0.1/"test',
227 '<a href="http://127.0.0.1/" title="http://127.0.0.1/" rel="nofollow external">127.0.0.1/</a>"test',
229 array('127.0.0.1/test"test',
230 '<a href="http://127.0.0.1/test" title="http://127.0.0.1/test" rel="nofollow external">127.0.0.1/test</a>"test',
232 array('127.0.0.1/-test',
233 '<a href="http://127.0.0.1/-test" title="http://127.0.0.1/-test" rel="nofollow external">127.0.0.1/-test</a>',
235 array('127.0.0.1/_test',
236 '<a href="http://127.0.0.1/_test" title="http://127.0.0.1/_test" rel="nofollow external">127.0.0.1/_test</a>',
238 array('127.0.0.1/!test',
239 '<a href="http://127.0.0.1/!test" title="http://127.0.0.1/!test" rel="nofollow external">127.0.0.1/!test</a>',
241 array('127.0.0.1/*test',
242 '<a href="http://127.0.0.1/*test" title="http://127.0.0.1/*test" rel="nofollow external">127.0.0.1/*test</a>',
244 array('127.0.0.1/test%20stuff',
245 '<a href="http://127.0.0.1/test%20stuff" title="http://127.0.0.1/test%20stuff" rel="nofollow external">127.0.0.1/test%20stuff</a>',
247 array('2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php',
248 '<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php" title="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php" rel="nofollow external">2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php</a>',
250 array('[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php',
251 '<a href="http://[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php" title="http://[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php" rel="nofollow external">[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php</a>',
253 array('2001:4978:1b5:0:21d:e0ff:fe66:59ab',
254 '<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/" title="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/" rel="nofollow external">2001:4978:1b5:0:21d:e0ff:fe66:59ab</a>',
257 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>',
259 array('flickr.com/photos/34807140@N05/3838905434',
260 '<a href="http://flickr.com/photos/34807140@N05/3838905434" title="http://flickr.com/photos/34807140@N05/3838905434" class="attachment thumbnail" id="attachment-XXX" rel="nofollow external">flickr.com/photos/34807140@N05/3838905434</a>',
262 array('What about parens (e.g. example.com/path/foo/(bar))?',
263 'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" title="http://example.com/path/foo/(bar)" rel="nofollow external">example.com/path/foo/(bar)</a>)?',
265 array('What about parens (e.g. example.com/path/foo/(bar)?',
266 'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" title="http://example.com/path/foo/(bar)" rel="nofollow external">example.com/path/foo/(bar)</a>?',
268 array('What about parens (e.g. example.com/path/foo/(bar).)?',
269 'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" title="http://example.com/path/foo/(bar)" rel="nofollow external">example.com/path/foo/(bar)</a>.?',
271 array('What about parens (e.g. example.com/path/(foo,bar)?',
272 'What about parens (e.g. <a href="http://example.com/path/(foo,bar)" title="http://example.com/path/(foo,bar)" rel="nofollow external">example.com/path/(foo,bar)</a>?',
275 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>',
278 '<a href="http://example.org/" title="http://example.org/" rel="nofollow external">example.org</a>',
280 array('example.co.uk',
281 '<a href="http://example.co.uk/" title="http://example.co.uk/" rel="nofollow external">example.co.uk</a>',
283 array('www.example.co.uk',
284 '<a href="http://www.example.co.uk/" title="http://www.example.co.uk/" rel="nofollow external">www.example.co.uk</a>',
286 array('farm1.images.example.co.uk',
287 '<a href="http://farm1.images.example.co.uk/" title="http://farm1.images.example.co.uk/" rel="nofollow external">farm1.images.example.co.uk</a>',
289 array('example.museum',
290 '<a href="http://example.museum/" title="http://example.museum/" rel="nofollow external">example.museum</a>',
292 array('example.travel',
293 '<a href="http://example.travel/" title="http://example.travel/" rel="nofollow external">example.travel</a>',
295 array('example.com.',
296 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>.',
298 array('example.com?',
299 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>?',
301 array('example.com!',
302 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>!',
304 array('example.com,',
305 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>,',
307 array('example.com;',
308 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>;',
310 array('example.com:',
311 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>:',
313 array('\'example.com\'',
314 '\'<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>\'',
316 array('"example.com"',
317 '"<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>"',
320 '<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>',
322 array('(example.com)',
323 '(<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>)',
325 array('[example.com]',
326 '[<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>]',
328 array('<example.com>',
329 '<<a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>>',
331 array('Hey, check out my cool site example.com okay?',
332 'Hey, check out my cool site <a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a> okay?',
334 array('Hey, check out my cool site example.com.I made it.',
335 'Hey, check out my cool site <a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>.I made it.',
337 array('Hey, check out my cool site example.com.Funny thing...',
338 'Hey, check out my cool site <a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>.Funny thing...',
340 array('Hey, check out my cool site example.com.You will love it.',
341 'Hey, check out my cool site <a href="http://example.com/" title="http://example.com/" rel="nofollow external">example.com</a>.You will love it.',
343 array('example.com:8080/test.php',
344 '<a href="http://example.com:8080/test.php" title="http://example.com:8080/test.php" rel="nofollow external">example.com:8080/test.php</a>',
346 array('user_name+other@example.com',
347 '<a href="mailto:user_name+other@example.com" title="mailto:user_name+other@example.com" rel="nofollow external">user_name+other@example.com</a>',
349 array('user@example.com',
350 '<a href="mailto:user@example.com" title="mailto:user@example.com" rel="nofollow external">user@example.com</a>',