]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/URLDetectionTest.php
Merge branch '0.8.x' of git@gitorious.org:laconica/mainline into 0.8.x
[quix0rs-gnu-social.git] / tests / URLDetectionTest.php
1 <?php
2
3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4     print "This script must be run from the command line\n";
5     exit();
6 }
7
8 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 define('STATUSNET', true);
10
11 require_once INSTALLDIR . '/lib/common.php';
12
13 class URLDetectionTest extends PHPUnit_Framework_TestCase
14 {
15     /**
16      * @dataProvider provider
17      *
18      */
19     public function testProduction($content, $expected)
20     {
21         $rendered = common_render_text($content);
22         $this->assertEquals($expected, $rendered);
23     }
24
25     static public function provider()
26     {
27         return array(
28                      array('http://127.0.0.1',
29                            '<a href="http://127.0.0.1/" rel="external">http://127.0.0.1</a>'),
30                      array('127.0.0.1',
31                            '<a href="http://127.0.0.1/" rel="external">127.0.0.1</a>'),
32                      array('127.0.0.1:99',
33                            '<a href="http://127.0.0.1:99/" rel="external">127.0.0.1:99</a>'),
34                      array('127.0.0.1/Name:test.php',
35                            '<a href="http://127.0.0.1/Name:test.php" rel="external">127.0.0.1/Name:test.php</a>'),
36                      array('127.0.0.1/~test',
37                            '<a href="http://127.0.0.1/~test" rel="external">127.0.0.1/~test</a>'),
38                      array('127.0.0.1/+test',
39                            '<a href="http://127.0.0.1/+test" rel="external">127.0.0.1/+test</a>'),
40                      array('127.0.0.1/$test',
41                            '<a href="http://127.0.0.1/$test" rel="external">127.0.0.1/$test</a>'),
42                      array('127.0.0.1/\'test',
43                            '<a href="http://127.0.0.1/\'test" rel="external">127.0.0.1/\'test</a>'),
44                      array('127.0.0.1/"test',
45                            '<a href="http://127.0.0.1/&quot;test" rel="external">127.0.0.1/&quot;test</a>'),
46                      array('127.0.0.1/-test',
47                            '<a href="http://127.0.0.1/-test" rel="external">127.0.0.1/-test</a>'),
48                      array('127.0.0.1/_test',
49                            '<a href="http://127.0.0.1/_test" rel="external">127.0.0.1/_test</a>'),
50                      array('127.0.0.1/!test',
51                            '<a href="http://127.0.0.1/!test" rel="external">127.0.0.1/!test</a>'),
52                      array('127.0.0.1/*test',
53                            '<a href="http://127.0.0.1/*test" rel="external">127.0.0.1/*test</a>'),
54                      array('127.0.0.1/test%20stuff',
55                            '<a href="http://127.0.0.1/test%20stuff" rel="external">127.0.0.1/test%20stuff</a>'),
56                      array('http://[::1]:99/test.php',
57                            '<a href="http://[::1]:99/test.php" rel="external">http://[::1]:99/test.php</a>'),
58                      array('http://::1/test.php',
59                            '<a href="http://::1/test.php" rel="external">http://::1/test.php</a>'),
60                      array('http://::1',
61                            '<a href="http://::1/" rel="external">http://::1</a>'),
62                      array('2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php',
63                            '<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php" rel="external">2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php</a>'),
64                      array('[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php',
65                            '<a href="http://[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php" rel="external">[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php</a>'),
66                      array('2001:4978:1b5:0:21d:e0ff:fe66:59ab',
67                            '<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/" rel="external">2001:4978:1b5:0:21d:e0ff:fe66:59ab</a>'),
68                      array('http://127.0.0.1',
69                            '<a href="http://127.0.0.1/" rel="external">http://127.0.0.1</a>'),
70                      array('example.com',
71                            '<a href="http://example.com/" rel="external">example.com</a>'),
72                      array('example.com',
73                            '<a href="http://example.com/" rel="external">example.com</a>'),
74                      array('http://example.com',
75                            '<a href="http://example.com/" rel="external">http://example.com</a>'),
76                      array('http://example.com.',
77                            '<a href="http://example.com/" rel="external">http://example.com</a>.'),
78                      array('/var/lib/example.so',
79                            '/var/lib/example.so'),
80                      array('example',
81                            'example'),
82                      array('user@example.com',
83                            '<a href="mailto:user@example.com" rel="external">user@example.com</a>'),
84                      array('user_name+other@example.com',
85                            '<a href="mailto:user_name+other@example.com" rel="external">user_name+other@example.com</a>'),
86                      array('mailto:user@example.com',
87                            '<a href="mailto:user@example.com" rel="external">mailto:user@example.com</a>'),
88                      array('mailto:user@example.com?subject=test',
89                            '<a href="mailto:user@example.com?subject=test" rel="external">mailto:user@example.com?subject=test</a>'),
90                      array('#example',
91                            '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('example'))) . '" rel="tag">example</a></span>'),
92                      array('#example.com',
93                            '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('example.com'))) . '" rel="tag">example.com</a></span>'),
94                      array('#.net',
95                            '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('.net'))) . '" rel="tag">.net</a></span>'),
96                      array('http://example',
97                            '<a href="http://example/" rel="external">http://example</a>'),
98                      array('http://3xampl3',
99                            '<a href="http://3xampl3/" rel="external">http://3xampl3</a>'),
100                      array('http://example/',
101                            '<a href="http://example/" rel="external">http://example/</a>'),
102                      array('http://example/path',
103                            '<a href="http://example/path" rel="external">http://example/path</a>'),
104                      array('http://example.com',
105                            '<a href="http://example.com/" rel="external">http://example.com</a>'),
106                      array('https://example.com',
107                            '<a href="https://example.com/" rel="external">https://example.com</a>'),
108                      array('ftp://example.com',
109                            '<a href="ftp://example.com/" rel="external">ftp://example.com</a>'),
110                      array('ftps://example.com',
111                            '<a href="ftps://example.com/" rel="external">ftps://example.com</a>'),
112                      array('http://user@example.com',
113                            '<a href="http://user@example.com/" rel="external">http://user@example.com</a>'),
114                      array('http://user:pass@example.com',
115                            '<a href="http://user:pass@example.com/" rel="external">http://user:pass@example.com</a>'),
116                      array('http://example.com:8080',
117                            '<a href="http://example.com:8080/" rel="external">http://example.com:8080</a>'),
118                      array('http://example.com:8080/test.php',
119                            '<a href="http://example.com:8080/test.php" rel="external">http://example.com:8080/test.php</a>'),
120                      array('example.com:8080/test.php',
121                            '<a href="http://example.com:8080/test.php" rel="external">example.com:8080/test.php</a>'),
122                      array('http://www.example.com',
123                            '<a href="http://www.example.com/" rel="external">http://www.example.com</a>'),
124                      array('http://example.com/',
125                            '<a href="http://example.com/" rel="external">http://example.com/</a>'),
126                      array('http://example.com/path',
127                            '<a href="http://example.com/path" rel="external">http://example.com/path</a>'),
128                      array('http://example.com/path.html',
129                            '<a href="http://example.com/path.html" rel="external">http://example.com/path.html</a>'),
130                      array('http://example.com/path.html#fragment',
131                            '<a href="http://example.com/path.html#fragment" rel="external">http://example.com/path.html#fragment</a>'),
132                      array('http://example.com/path.php?foo=bar&bar=foo',
133                            '<a href="http://example.com/path.php?foo=bar&amp;bar=foo" rel="external">http://example.com/path.php?foo=bar&amp;bar=foo</a>'),
134                      array('http://example.com.',
135                            '<a href="http://example.com/" rel="external">http://example.com</a>.'),
136                      array('http://müllärör.de',
137                            '<a href="http://m&#xFC;ll&#xE4;r&#xF6;r.de/" rel="external">http://müllärör.de</a>'),
138                      array('http://ﺱﺲﺷ.com',
139                            '<a href="http://&#xFEB1;&#xFEB2;&#xFEB7;.com/" rel="external">http://ﺱﺲﺷ.com</a>'),
140                      array('http://сделаткартинки.com',
141                            '<a href="http://&#x441;&#x434;&#x435;&#x43B;&#x430;&#x442;&#x43A;&#x430;&#x440;&#x442;&#x438;&#x43D;&#x43A;&#x438;.com/" rel="external">http://сделаткартинки.com</a>'),
142                      array('http://tūdaliņ.lv',
143                            '<a href="http://t&#x16B;dali&#x146;.lv/" rel="external">http://tūdaliņ.lv</a>'),
144                      array('http://brændendekærlighed.com',
145                            '<a href="http://br&#xE6;ndendek&#xE6;rlighed.com/" rel="external">http://brændendekærlighed.com</a>'),
146                      array('http://あーるいん.com',
147                            '<a href="http://&#x3042;&#x30FC;&#x308B;&#x3044;&#x3093;.com/" rel="external">http://あーるいん.com</a>'),
148                      array('http://예비교사.com',
149                            '<a href="http://&#xC608;&#xBE44;&#xAD50;&#xC0AC;.com/" rel="external">http://예비교사.com</a>'),
150                      array('http://example.com.',
151                            '<a href="http://example.com/" rel="external">http://example.com</a>.'),
152                      array('http://example.com?',
153                            '<a href="http://example.com/" rel="external">http://example.com</a>?'),
154                      array('http://example.com!',
155                            '<a href="http://example.com/" rel="external">http://example.com</a>!'),
156                      array('http://example.com,',
157                            '<a href="http://example.com/" rel="external">http://example.com</a>,'),
158                      array('http://example.com;',
159                            '<a href="http://example.com/" rel="external">http://example.com</a>;'),
160                      array('http://example.com:',
161                            '<a href="http://example.com/" rel="external">http://example.com</a>:'),
162                      array('\'http://example.com\'',
163                            '\'<a href="http://example.com/" rel="external">http://example.com</a>\''),
164                      array('"http://example.com"',
165                            '&quot;<a href="http://example.com/" rel="external">http://example.com</a>&quot;'),
166                      array('http://example.com',
167                            '<a href="http://example.com/" rel="external">http://example.com</a>'),
168                      array('(http://example.com)',
169                            '(<a href="http://example.com/" rel="external">http://example.com</a>)'),
170                      array('[http://example.com]',
171                            '[<a href="http://example.com/" rel="external">http://example.com</a>]'),
172                      array('<http://example.com>',
173                            '&lt;<a href="http://example.com/" rel="external">http://example.com</a>&gt;'),
174                      array('http://example.com/path/(foo)/bar',
175                            '<a href="http://example.com/path/(foo)/bar" rel="external">http://example.com/path/(foo)/bar</a>'),
176                      array('http://example.com/path/[foo]/bar',
177                            '<a href="http://example.com/path/[foo]/bar" rel="external">http://example.com/path/[foo]/bar</a>'),
178                      array('http://example.com/path/foo/(bar)',
179                            '<a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>'),
180                      //Not a valid url - urls cannot contain unencoded square brackets
181                      array('http://example.com/path/foo/[bar]',
182                            '<a href="http://example.com/path/foo/[bar]" rel="external">http://example.com/path/foo/[bar]</a>'),
183                      array('Hey, check out my cool site http://example.com okay?',
184                            'Hey, check out my cool site <a href="http://example.com/" rel="external">http://example.com</a> okay?'),
185                      array('What about parens (e.g. http://example.com/path/foo/(bar))?',
186                            'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>)?'),
187                      array('What about parens (e.g. http://example.com/path/foo/(bar)?',
188                            'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>?'),
189                      array('What about parens (e.g. http://example.com/path/foo/(bar).)?',
190                            'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>.)?'),
191                      //Not a valid url - urls cannot contain unencoded commas
192                      array('What about parens (e.g. http://example.com/path/(foo,bar)?',
193                            'What about parens (e.g. <a href="http://example.com/path/(foo,bar)" rel="external">http://example.com/path/(foo,bar)</a>?'),
194                      array('Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?',
195                            'Unbalanced too (e.g. <a href="http://example.com/path/((((foo)/bar)" rel="external">http://example.com/path/((((foo)/bar)</a>?'),
196                      array('Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?',
197                            'Unbalanced too (e.g. <a href="http://example.com/path/(foo))))/bar" rel="external">http://example.com/path/(foo))))/bar</a>)?'),
198                      array('Unbalanced too (e.g. http://example.com/path/foo/((((bar)?',
199                            'Unbalanced too (e.g. <a href="http://example.com/path/foo/((((bar)" rel="external">http://example.com/path/foo/((((bar)</a>?'),
200                      array('Unbalanced too (e.g. http://example.com/path/foo/(bar))))?',
201                            'Unbalanced too (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>)))?'),
202                      array('example.com',
203                            '<a href="http://example.com/" rel="external">example.com</a>'),
204                      array('example.org',
205                            '<a href="http://example.org/" rel="external">example.org</a>'),
206                      array('example.co.uk',
207                            '<a href="http://example.co.uk/" rel="external">example.co.uk</a>'),
208                      array('www.example.co.uk',
209                            '<a href="http://www.example.co.uk/" rel="external">www.example.co.uk</a>'),
210                      array('farm1.images.example.co.uk',
211                            '<a href="http://farm1.images.example.co.uk/" rel="external">farm1.images.example.co.uk</a>'),
212                      array('example.museum',
213                            '<a href="http://example.museum/" rel="external">example.museum</a>'),
214                      array('example.travel',
215                            '<a href="http://example.travel/" rel="external">example.travel</a>'),
216                      array('example.com.',
217                            '<a href="http://example.com/" rel="external">example.com</a>.'),
218                      array('example.com?',
219                            '<a href="http://example.com/" rel="external">example.com</a>?'),
220                      array('example.com!',
221                            '<a href="http://example.com/" rel="external">example.com</a>!'),
222                      array('example.com,',
223                            '<a href="http://example.com/" rel="external">example.com</a>,'),
224                      array('example.com;',
225                            '<a href="http://example.com/" rel="external">example.com</a>;'),
226                      array('example.com:',
227                            '<a href="http://example.com/" rel="external">example.com</a>:'),
228                      array('\'example.com\'',
229                            '\'<a href="http://example.com/" rel="external">example.com</a>\''),
230                      array('"example.com"',
231                            '&quot;<a href="http://example.com/" rel="external">example.com</a>&quot;'),
232                      array('example.com',
233                            '<a href="http://example.com/" rel="external">example.com</a>'),
234                      array('(example.com)',
235                            '(<a href="http://example.com/" rel="external">example.com</a>)'),
236                      array('[example.com]',
237                            '[<a href="http://example.com/" rel="external">example.com</a>]'),
238                      array('<example.com>',
239                            '&lt;<a href="http://example.com/" rel="external">example.com</a>&gt;'),
240                      array('Hey, check out my cool site example.com okay?',
241                            'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a> okay?'),
242                      array('Hey, check out my cool site example.com.I made it.',
243                            'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a>.I made it.'),
244                      array('Hey, check out my cool site example.com.Funny thing...',
245                            'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a>.Funny thing...'),
246                      array('Hey, check out my cool site example.com.You will love it.',
247                            'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a>.You will love it.'),
248                      array('What about parens (e.g. example.com/path/foo/(bar))?',
249                            'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">example.com/path/foo/(bar)</a>)?'),
250                      array('What about parens (e.g. example.com/path/foo/(bar)?',
251                            'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">example.com/path/foo/(bar)</a>?'),
252                      array('What about parens (e.g. example.com/path/foo/(bar).)?',
253                            'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">example.com/path/foo/(bar)</a>.)?'),
254                      array('What about parens (e.g. example.com/path/(foo,bar)?',
255                            'What about parens (e.g. <a href="http://example.com/path/(foo,bar)" rel="external">example.com/path/(foo,bar)</a>?'),
256                      array('file.ext',
257                            'file.ext'),
258                      array('file.html',
259                            'file.html'),
260                      array('file.php',
261                            'file.php')
262                      );
263     }
264 }
265