We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

URLDetectionTest.php 18.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?php

if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
    print "This script must be run from the command line\n";
    exit();
}

define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('LACONICA', true);

require_once INSTALLDIR . '/lib/common.php';

class URLDetectionTest extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider provider
     *
     */
    public function testProduction($content, $expected)
    {
        $rendered = common_render_text($content);
        $this->assertEquals($expected, $rendered);
    }

    static public function provider()
    {
        return array(
28 29
                     array('http://127.0.0.1',
                           '<a href="http://127.0.0.1/" rel="external">http://127.0.0.1</a>'),
30 31
                     array('127.0.0.1',
                           '<a href="http://127.0.0.1/" rel="external">127.0.0.1</a>'),
32 33
                     array('127.0.0.1:99',
                           '<a href="http://127.0.0.1:99/" rel="external">127.0.0.1:99</a>'),
34 35
                     array('127.0.0.1/test.php',
                           '<a href="http://127.0.0.1/test.php" rel="external">127.0.0.1/test.php</a>'),
36 37
                     array('http://[::1]:99/test.php',
                           '<a href="http://[::1]:99/test.php" rel="external">http://[::1]:99/test.php</a>'),
38 39 40 41 42 43
                     array('http://::1/test.php',
                           '<a href="http://::1/test.php" rel="external">http://::1/test.php</a>'),
                     array('http://::1',
                           '<a href="http://::1/" rel="external">http://::1</a>'),
                     array('2001:4978:1b5:0:21d:e0ff:fe66:59ab/test.php',
                           '<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>'),
44 45
                     array('[2001:4978:1b5:0:21d:e0ff:fe66:59ab]:99/test.php',
                           '<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>'),
46 47 48 49 50 51 52 53 54 55 56 57 58 59
                     array('2001:4978:1b5:0:21d:e0ff:fe66:59ab',
                           '<a href="http://2001:4978:1b5:0:21d:e0ff:fe66:59ab/" rel="external">2001:4978:1b5:0:21d:e0ff:fe66:59ab</a>'),
                     array('http://127.0.0.1',
                           '<a href="http://127.0.0.1/" rel="external">http://127.0.0.1</a>'),
                     array('example.com',
                           '<a href="http://example.com/" rel="external">example.com</a>'),
                     array('example.com',
                           '<a href="http://example.com/" rel="external">example.com</a>'),
                     array('http://example.com',
                           '<a href="http://example.com/" rel="external">http://example.com</a>'),
                     array('http://example.com.',
                           '<a href="http://example.com/" rel="external">http://example.com</a>.'),
                     array('/var/lib/example.so',
                           '/var/lib/example.so'),
60 61
                     array('example',
                           'example'),
62 63 64 65 66 67 68 69 70 71 72 73 74 75
                     array('user@example.com',
                           '<a href="mailto:user@example.com" rel="external">user@example.com</a>'),
                     array('user_name+other@example.com',
                           '<a href="mailto:user_name+other@example.com" rel="external">user_name+other@example.com</a>'),
                     array('mailto:user@example.com',
                           '<a href="mailto:user@example.com" rel="external">mailto:user@example.com</a>'),
                     array('mailto:user@example.com?subject=test',
                           '<a href="mailto:user@example.com?subject=test" rel="external">mailto:user@example.com?subject=test</a>'),
                     array('#example',
                           '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('example'))) . '" rel="tag">example</a></span>'),
                     array('#example.com',
                           '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('example.com'))) . '" rel="tag">example.com</a></span>'),
                     array('#.net',
                           '#<span class="tag"><a href="' . common_local_url('tag', array('tag' => common_canonical_tag('.net'))) . '" rel="tag">.net</a></span>'),
76
                     array('http://example',
77
                           '<a href="http://example/" rel="external">http://example</a>'),
78 79
                     array('http://3xampl3',
                           '<a href="http://3xampl3/" rel="external">http://3xampl3</a>'),
80
                     array('http://example/',
81
                           '<a href="http://example/" rel="external">http://example/</a>'),
82
                     array('http://example/path',
83
                           '<a href="http://example/path" rel="external">http://example/path</a>'),
84
                     array('http://example.com',
85
                           '<a href="http://example.com/" rel="external">http://example.com</a>'),
86
                     array('https://example.com',
87
                           '<a href="https://example.com/" rel="external">https://example.com</a>'),
88
                     array('ftp://example.com',
89
                           '<a href="ftp://example.com/" rel="external">ftp://example.com</a>'),
90
                     array('ftps://example.com',
91
                           '<a href="ftps://example.com/" rel="external">ftps://example.com</a>'),
92
                     array('http://user@example.com',
93
                           '<a href="http://user@example.com/" rel="external">http://user@example.com</a>'),
94
                     array('http://user:pass@example.com',
95
                           '<a href="http://user:pass@example.com/" rel="external">http://user:pass@example.com</a>'),
96
                     array('http://example.com:8080',
97
                           '<a href="http://example.com:8080/" rel="external">http://example.com:8080</a>'),
98 99 100 101
                     array('http://example.com:8080/test.php',
                           '<a href="http://example.com:8080/test.php" rel="external">http://example.com:8080/test.php</a>'),
                     array('example.com:8080/test.php',
                           '<a href="http://example.com:8080/test.php" rel="external">example.com:8080/test.php</a>'),
102
                     array('http://www.example.com',
103
                           '<a href="http://www.example.com/" rel="external">http://www.example.com</a>'),
104
                     array('http://example.com/',
105
                           '<a href="http://example.com/" rel="external">http://example.com/</a>'),
106
                     array('http://example.com/path',
107
                           '<a href="http://example.com/path" rel="external">http://example.com/path</a>'),
108
                     array('http://example.com/path.html',
109
                           '<a href="http://example.com/path.html" rel="external">http://example.com/path.html</a>'),
110
                     array('http://example.com/path.html#fragment',
111
                           '<a href="http://example.com/path.html#fragment" rel="external">http://example.com/path.html#fragment</a>'),
112
                     array('http://example.com/path.php?foo=bar&bar=foo',
113
                           '<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>'),
114 115
                     array('http://example.com.',
                           '<a href="http://example.com/" rel="external">http://example.com</a>.'),
116
                     array('http://müllärör.de',
117
                           '<a href="http://m&#xFC;ll&#xE4;r&#xF6;r.de/" rel="external">http://müllärör.de</a>'),
118
                     array('http://ﺱﺲﺷ.com',
119
                           '<a href="http://&#xFEB1;&#xFEB2;&#xFEB7;.com/" rel="external">http://ﺱﺲﺷ.com</a>'),
120
                     array('http://сделаткартинки.com',
121
                           '<a href="http://&#x441;&#x434;&#x435;&#x43B;&#x430;&#x442;&#x43A;&#x430;&#x440;&#x442;&#x438;&#x43D;&#x43A;&#x438;.com/" rel="external">http://сделаткартинки.com</a>'),
122
                     array('http://tūdaliņ.lv',
123
                           '<a href="http://t&#x16B;dali&#x146;.lv/" rel="external">http://tūdaliņ.lv</a>'),
124
                     array('http://brændendekærlighed.com',
125
                           '<a href="http://br&#xE6;ndendek&#xE6;rlighed.com/" rel="external">http://brændendekærlighed.com</a>'),
126
                     array('http://あーるいん.com',
127
                           '<a href="http://&#x3042;&#x30FC;&#x308B;&#x3044;&#x3093;.com/" rel="external">http://あーるいん.com</a>'),
128
                     array('http://예비교사.com',
129
                           '<a href="http://&#xC608;&#xBE44;&#xAD50;&#xC0AC;.com/" rel="external">http://예비교사.com</a>'),
130
                     array('http://example.com.',
131
                           '<a href="http://example.com/" rel="external">http://example.com</a>.'),
132
                     array('http://example.com?',
133
                           '<a href="http://example.com/" rel="external">http://example.com</a>?'),
134
                     array('http://example.com!',
135
                           '<a href="http://example.com/" rel="external">http://example.com</a>!'),
136
                     array('http://example.com,',
137
                           '<a href="http://example.com/" rel="external">http://example.com</a>,'),
138
                     array('http://example.com;',
139
                           '<a href="http://example.com/" rel="external">http://example.com</a>;'),
140
                     array('http://example.com:',
141
                           '<a href="http://example.com/" rel="external">http://example.com</a>:'),
142
                     array('\'http://example.com\'',
143
                           '\'<a href="http://example.com/" rel="external">http://example.com</a>\''),
144
                     array('"http://example.com"',
145 146 147
                           '&quot;<a href="http://example.com/" rel="external">http://example.com</a>&quot;'),
                     array('http://example.com',
                           '<a href="http://example.com/" rel="external">http://example.com</a>'),
148
                     array('(http://example.com)',
149
                           '(<a href="http://example.com/" rel="external">http://example.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
                           '&lt;<a href="http://example.com/" rel="external">http://example.com</a>&gt;'),
154
                     array('http://example.com/path/(foo)/bar',
155
                           '<a href="http://example.com/path/(foo)/bar" rel="external">http://example.com/path/(foo)/bar</a>'),
156
                     array('http://example.com/path/[foo]/bar',
157
                           '<a href="http://example.com/path/[foo]/bar" rel="external">http://example.com/path/[foo]/bar</a>'),
158
                     array('http://example.com/path/foo/(bar)',
159
                           '<a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>'),
160
                     //Not a valid url - urls cannot contain unencoded square brackets
161 162
                     array('http://example.com/path/foo/[bar]',
                           '<a href="http://example.com/path/foo/[bar]" rel="external">http://example.com/path/foo/[bar]</a>'),
163
                     array('Hey, check out my cool site http://example.com okay?',
164
                           'Hey, check out my cool site <a href="http://example.com/" rel="external">http://example.com</a> okay?'),
165
                     array('What about parens (e.g. http://example.com/path/foo/(bar))?',
166
                           'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>)?'),
167
                     array('What about parens (e.g. http://example.com/path/foo/(bar)?',
168
                           'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>?'),
169
                     array('What about parens (e.g. http://example.com/path/foo/(bar).)?',
170
                           'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>.)?'),
171
                     //Not a valid url - urls cannot contain unencoded commas
172
                     array('What about parens (e.g. http://example.com/path/(foo,bar)?',
173
                           'What about parens (e.g. <a href="http://example.com/path/(foo,bar)" rel="external">http://example.com/path/(foo,bar)</a>?'),
174
                     array('Unbalanced too (e.g. http://example.com/path/((((foo)/bar)?',
175
                           'Unbalanced too (e.g. <a href="http://example.com/path/((((foo)/bar)" rel="external">http://example.com/path/((((foo)/bar)</a>?'),
176
                     array('Unbalanced too (e.g. http://example.com/path/(foo))))/bar)?',
177
                           'Unbalanced too (e.g. <a href="http://example.com/path/(foo))))/bar" rel="external">http://example.com/path/(foo))))/bar</a>)?'),
178
                     array('Unbalanced too (e.g. http://example.com/path/foo/((((bar)?',
179
                           'Unbalanced too (e.g. <a href="http://example.com/path/foo/((((bar)" rel="external">http://example.com/path/foo/((((bar)</a>?'),
180
                     array('Unbalanced too (e.g. http://example.com/path/foo/(bar))))?',
181
                           'Unbalanced too (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">http://example.com/path/foo/(bar)</a>)))?'),
182
                     array('example.com',
183
                           '<a href="http://example.com/" rel="external">example.com</a>'),
184
                     array('example.org',
185
                           '<a href="http://example.org/" rel="external">example.org</a>'),
186
                     array('example.co.uk',
187
                           '<a href="http://example.co.uk/" rel="external">example.co.uk</a>'),
188
                     array('www.example.co.uk',
189
                           '<a href="http://www.example.co.uk/" rel="external">www.example.co.uk</a>'),
190
                     array('farm1.images.example.co.uk',
191
                           '<a href="http://farm1.images.example.co.uk/" rel="external">farm1.images.example.co.uk</a>'),
192
                     array('example.museum',
193
                           '<a href="http://example.museum/" rel="external">example.museum</a>'),
194
                     array('example.travel',
195
                           '<a href="http://example.travel/" rel="external">example.travel</a>'),
196
                     array('example.com.',
197
                           '<a href="http://example.com/" rel="external">example.com</a>.'),
198
                     array('example.com?',
199
                           '<a href="http://example.com/" rel="external">example.com</a>?'),
200
                     array('example.com!',
201
                           '<a href="http://example.com/" rel="external">example.com</a>!'),
202
                     array('example.com,',
203
                           '<a href="http://example.com/" rel="external">example.com</a>,'),
204
                     array('example.com;',
205
                           '<a href="http://example.com/" rel="external">example.com</a>;'),
206
                     array('example.com:',
207
                           '<a href="http://example.com/" rel="external">example.com</a>:'),
208
                     array('\'example.com\'',
209
                           '\'<a href="http://example.com/" rel="external">example.com</a>\''),
210
                     array('"example.com"',
211 212 213
                           '&quot;<a href="http://example.com/" rel="external">example.com</a>&quot;'),
                     array('example.com',
                           '<a href="http://example.com/" rel="external">example.com</a>'),
214
                     array('(example.com)',
215
                           '(<a href="http://example.com/" rel="external">example.com</a>)'),
216
                     array('[example.com]',
217
                           '[<a href="http://example.com/" rel="external">example.com</a>]'),
218
                     array('<example.com>',
219
                           '&lt;<a href="http://example.com/" rel="external">example.com</a>&gt;'),
220
                     array('Hey, check out my cool site example.com okay?',
221
                           'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a> okay?'),
222
                     array('Hey, check out my cool site example.com.I made it.',
223
                           'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a>.I made it.'),
224
                     array('Hey, check out my cool site example.com.Funny thing...',
225
                           'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a>.Funny thing...'),
226
                     array('Hey, check out my cool site example.com.You will love it.',
227
                           'Hey, check out my cool site <a href="http://example.com/" rel="external">example.com</a>.You will love it.'),
228
                     array('What about parens (e.g. example.com/path/foo/(bar))?',
229
                           'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">example.com/path/foo/(bar)</a>)?'),
230
                     array('What about parens (e.g. example.com/path/foo/(bar)?',
231
                           'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">example.com/path/foo/(bar)</a>?'),
232
                     array('What about parens (e.g. example.com/path/foo/(bar).)?',
233
                           'What about parens (e.g. <a href="http://example.com/path/foo/(bar)" rel="external">example.com/path/foo/(bar)</a>.)?'),
234
                     array('What about parens (e.g. example.com/path/(foo,bar)?',
235
                           'What about parens (e.g. <a href="http://example.com/path/(foo,bar)" rel="external">example.com/path/(foo,bar)</a>?'),
236 237 238 239 240 241 242 243 244 245
                     array('file.ext',
                           'file.ext'),
                     array('file.html',
                           'file.html'),
                     array('file.php',
                           'file.php')
                     );
    }
}