Debian Codegraph @

tests/phpunit/includes/ExternalLinks/LinkFilterTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\ExternalLinks\LinkFilter;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
6 use Wikimedia\Rdbms\IExpression;
7 use Wikimedia\Rdbms\LikeMatch;
8
9 /**
10 * @covers \MediaWiki\ExternalLinks\LinkFilter
11 * @group Database
12 */
13 class LinkFilterTest extends MediaWikiLangTestCase {
14
15 protected function setUp(): void {
16 parent::setUp();
17
18 $this->overrideConfigValue( MainConfigNames::UrlProtocols, [
19 'http://',
20 'https://',
21 'ftp://',
22 'irc://',
23 'ircs://',
24 'gopher://',
25 'telnet://',
26 'nntp://',
27 'worldwind://',
28 'mailto:',
29 'news:',
30 'svn://',
31 'git://',
32 'mms://',
33 '//',
34 ] );
35 }
36
37 /**
38 * Take an array from LinkFilter::makeLikeArray(), and create a regex from it
39 *
40 * @param array $like Array as created by LinkFilter::makeLikeArray()
41 * @return string Regex
42 */
43 private function createRegexFromLIKE( $like ) {
44 $regex = '!^';
45
46 foreach ( $like as $item ) {
47 if ( $item instanceof LikeMatch ) {
48 if ( $item->toString() == '%' ) {
49 $regex .= '.*';
50 } elseif ( $item->toString() == '_' ) {
51 $regex .= '.';
52 }
53 } else {
54 $regex .= preg_quote( $item, '!' );
55 }
56
57 }
58
59 $regex .= '$!';
60
61 return $regex;
62 }
63
64 public static function provideValidPatterns() {
65 return [
66 // Protocol, Search pattern, URL which matches the pattern
67 [ 'http://', '*.test.com', 'http://www.test.com' ],
68 [ 'http://', 'test.com:8080/dir/file', 'http://name:[email protected]:8080/dir/file' ],
69 [ 'https://', '*.com', 'https://s.s.test..com:88/dir/file?a=1&b=2' ],
70 [ 'https://', '*.com', 'https://name:[email protected]/index.html' ],
71 [ 'http://', 'name:[email protected]', 'http://test.com' ],
72 [ 'http://', 'test.com', 'http://name:[email protected]' ],
73 [ 'http://', '*.test.com', 'http://a.b.c.test.com/dir/dir/file?a=6' ],
74 [ null, 'http://*.test.com', 'http://www.test.com' ],
75 [ 'http://', '.test.com', 'http://.test.com' ],
76 [ 'http://', '*..test.com', 'http://foo..test.com' ],
77 [ 'mailto:', '[email protected]', 'mailto:[email protected]' ],
78 [ 'mailto:', '*@mail.test123.com', 'mailto:[email protected]' ],
79 [ '',
80 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg',
81 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg'
82 ],
83 [ '', 'http://name:pass@*.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg',
84 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
85 [ '', 'http://name:wrongpass@*.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]',
86 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
87 [ 'http://', 'name:pass@*.test.com:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg',
88 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
89 [ '', 'http://name:[email protected]:12345',
90 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg' ],
91 [ 'ftp://', 'user:[email protected]:1233/home/user/file;type=efw',
92 'ftp://user:[email protected]:1233/home/user/file;type=efw' ],
93 [ null, 'ftp://otheruser:[email protected]:1233/home/user/file;type=',
94 'ftp://user:[email protected]:1233/home/user/file;type=efw' ],
95 [ null, 'ftp://@ftp.test.com:1233/home/user/file;type=',
96 'ftp://user:[email protected]:1233/home/user/file;type=efw' ],
97 [ null, 'ftp://ftp.test.com/',
98 'ftp://user:[email protected]/home/user/file;type=efw' ],
99 [ null, 'ftp://ftp.test.com/',
100 'ftp://user:[email protected]/home/user/file;type=efw' ],
101 [ null, 'ftp://*.test.com:222/',
102 'ftp://user:[email protected]:222/home' ],
103 [ 'irc://', '*.myserver:6667/', 'irc://test.myserver:6667/' ],
104 [ 'irc://', 'name:pass@*.myserver/', 'irc://test.myserver:6667/' ],
105 [ 'irc://', 'name:pass@*.myserver/', 'irc://other:@test.myserver:6667/' ],
106 [ '', 'irc://test/name,string,abc?msg=t', 'irc://test/name,string,abc?msg=test' ],
107 [ '', 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z',
108 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z' ],
109 [ '', 'https://gerrit.wikimedia.org',
110 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z' ],
111 [ 'mailto:', '*.test.com', 'mailto:[email protected]' ],
112 [ 'mailto:', 'test.com', 'mailto:[email protected]' ],
113 [ 'news:', '[email protected]', 'news:[email protected]' ],
114 [ 'news:', '*.test.com', 'news:[email protected]' ],
115 [ '', 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com',
116 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com' ],
117 [ '', 'news:*.aol.com',
118 'news:4df8kh$iagfewewf(at)newsbf02aaa.news.aol.com' ],
119
120 // (T347574) Only set host if it's not already set (if // is used)
121 [ 'news:', 'comp.compression', 'news://comp.compression' ],
122 [ 'news:', 'comp.compression', 'news:comp.compression' ],
123 // (T364743) Also only set host if it's not already set, but in a different code path
124 [ '', 'news://*.example.com', 'news://example.org', [ 'found' => false ] ],
125
126 [ '', 'git://github.com/prwef/abc-def.git', 'git://github.com/prwef/abc-def.git' ],
127 [ 'git://', 'github.com/', 'git://github.com/prwef/abc-def.git' ],
128 [ 'git://', '*.github.com/', 'git://a.b.c.d.e.f.github.com/prwef/abc-def.git' ],
129 [ '', 'gopher://*.test.com/', 'gopher://gopher.test.com/0/v2/vstat' ],
130 [ 'telnet://', '*.test.com', 'telnet://shell.test.com/~home/' ],
131 [ '', 'http://test.com', 'http://test.com/index?arg=1' ],
132 [ 'http://', '*.test.com', 'http://www.test.com/index?arg=1' ],
133 [ '',
134 'http://xx23124:[email protected]:12345/dir',
135 'http://name:[email protected]:12345/dir/dir/file.xyz.php#__se__?arg1=_&arg2[]=4rtg'
136 ],
137 [ 'http://', '127.0.0.1', 'http://127.000.000.001' ],
138 [ 'http://', '127.0.0.*', 'http://127.000.000.010' ],
139 [ 'http://', '127.0.*', 'http://127.000.123.010' ],
140 [ 'http://', '127.*', 'http://127.127.127.127' ],
141 [ 'http://', '[0:0:0:0:0:0:0:0001]', 'http://[::1]' ],
142 [ 'http://', '[2001:db8:0:0:*]', 'http://[2001:0DB8::]' ],
143 [ 'http://', '[2001:db8:0:0:*]', 'http://[2001:0DB8::123]' ],
144 [ 'http://', '[2001:db8:0:0:*]', 'http://[2001:0DB8::123:456]' ],
145 [ 'http://', 'xn--f-vgaa.example.com', 'http://fóó.example.com' ],
146 [ 'http://', 'xn--f-vgaa.example.com', 'http://f%c3%b3%C3%B3.example.com' ],
147 [ 'http://', 'fóó.example.com', 'http://xn--f-vgaa.example.com' ],
148 [ 'http://', 'f%c3%b3%C3%B3.example.com', 'http://xn--f-vgaa.example.com' ],
149 [ 'http://', 'f%c3%b3%C3%B3.example.com', 'http://fóó.example.com' ],
150 [ 'http://', 'fóó.example.com', 'http://f%c3%b3%C3%B3.example.com' ],
151
152 [ 'http://', 'example.com./foo', 'http://example.com/foo' ],
153 [ 'http://', 'example.com/foo', 'http://example.com./foo' ],
154 [ 'http://', '127.0.0.1./foo', 'http://127.0.0.1/foo' ],
155 [ 'http://', '127.0.0.1/foo', 'http://127.0.0.1./foo' ],
156
157 // Tests for false positives
158 [ 'http://', 'test.com', 'http://www.test.com', [ 'found' => false ] ],
159 [ 'http://', 'www1.test.com', 'http://www.test.com', [ 'found' => false ] ],
160 [ 'http://', '*.test.com', 'http://www.test.t.com', [ 'found' => false ] ],
161 [ 'http://', 'test.com', 'http://xtest.com', [ 'found' => false ] ],
162 [ 'http://', '*.test.com', 'http://xtest.com', [ 'found' => false ] ],
163 [ 'http://', '.test.com', 'http://test.com', [ 'found' => false ] ],
164 [ 'http://', '.test.com', 'http://www.test.com', [ 'found' => false ] ],
165 [ 'http://', '*..test.com', 'http://test.com', [ 'found' => false ] ],
166 [ 'http://', '*..test.com', 'http://www.test.com', [ 'found' => false ] ],
167 [ '', 'http://test.com:8080', 'http://www.test.com:8080', [ 'found' => false ] ],
168 [ '', 'https://test.com', 'http://test.com', [ 'found' => false ] ],
169 [ '', 'http://test.com', 'https://test.com', [ 'found' => false ] ],
170 [ 'http://', 'http://test.com', 'http://test.com', [ 'found' => false ] ],
171 [ null, 'http://www.test.com', 'http://www.test.com:80', [ 'found' => false ] ],
172 [ null, 'http://www.test.com:80', 'http://www.test.com', [ 'found' => false ] ],
173 [ null, 'http://*.test.com:80', 'http://www.test.com', [ 'found' => false ] ],
174 [ '', 'https://gerrit.wikimedia.org/r/#/XXX/status:open,n,z',
175 'https://gerrit.wikimedia.org/r/#/q/status:open,n,z', [ 'found' => false ] ],
176 [ '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
177 'https://gerrit.wikimedia.org/r/#/XXX/status:open,n,z', [ 'found' => false ] ],
178 [ 'mailto:', '@test.com', '@abc.test.com', [ 'found' => false ] ],
179 [ 'mailto:', '[email protected]', '[email protected]', [ 'found' => false ] ],
180 [ '', 'mailto:[email protected]', '[email protected]', [ 'found' => false ] ],
181 [ '', 'mailto:@test.com', '@abc.test.com', [ 'found' => false ] ],
182 [ 'ftp://', '*.co', 'ftp://www.co.uk', [ 'found' => false ] ],
183 [ 'ftp://', '*.co', 'ftp://www.co.m', [ 'found' => false ] ],
184 [ 'ftp://', '*.co/dir/', 'ftp://www.co/dir2/', [ 'found' => false ] ],
185 [ 'ftp://', 'www.co/dir/', 'ftp://www.co/dir2/', [ 'found' => false ] ],
186 [ 'ftp://', 'test.com/dir/', 'ftp://test.com/', [ 'found' => false ] ],
187 [ '', 'http://test.com:8080/dir/', 'http://test.com:808/dir/', [ 'found' => false ] ],
188 [ '', 'http://test.com/dir/index.html', 'http://test.com/dir/index.php', [ 'found' => false ] ],
189 [ 'http://', '127.0.0.*', 'http://127.0.1.0', [ 'found' => false ] ],
190 [ 'http://', '[2001:db8::*]', 'http://[2001:0DB8::123:456]', [ 'found' => false ] ],
191
192 // These are false positives too and ideally shouldn't match, but that
193 // would require using regexes and RLIKE instead of LIKE
194 // [ null, 'http://*.test.com', 'http://www.test.com:80', [ 'found' => false ] ],
195 // [ '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
196 // 'https://gerrit.wikimedia.org/XXX/r/#/q/status:open,n,z', [ 'found' => false ] ],
197 ];
198 }
199
200 /**
201 * Tests whether the LIKE clause produced by LinkFilter::makeLikeArray($pattern, $protocol)
202 * will find one of the URL indexes produced by LinkFilter::makeIndexes($url)
203 *
204 * @dataProvider provideValidPatterns
205 *
206 * @param string $protocol Protocol, e.g. 'http://' or 'mailto:'
207 * @param string $pattern Search pattern to feed to LinkFilter::makeLikeArray
208 * @param string $url URL to feed to LinkFilter::makeIndexes
209 * @param array $options
210 * - found: (bool) Should the URL be found? (defaults true)
211 */
212 public function testMakeLikeArrayWithValidPatterns( $protocol, $pattern, $url, $options = [] ) {
213 $options += [ 'found' => true ];
214
215 $indexes = LinkFilter::makeIndexes( $url );
216 $likeArrays = LinkFilter::makeLikeArray( $pattern, $protocol );
217 $likeArray = array_merge( $likeArrays[0], $likeArrays[1] );
218
219 $this->assertIsArray( $likeArrays,
220 "LinkFilter::makeLikeArray('$pattern', '$protocol') returned false on a valid pattern"
221 );
222
223 $regex = $this->createRegexFromLIKE( $likeArray );
224 $debugmsg = "Regex: '" . $regex . "'\n";
225 $debugmsg .= count( $indexes ) . " index(es) created by LinkFilter::makeIndexes():\n";
226
227 $matches = 0;
228
229 foreach ( $indexes as $index ) {
230 $indexString = implode( '', $index );
231 $matches += preg_match( $regex, $indexString );
232 $debugmsg .= "\t'$indexString'\n";
233 }
234
235 if ( $options['found'] ) {
236 $this->assertTrue(
237 $matches > 0,
238 "Search pattern '$protocol$pattern' does not find url '$url' \n$debugmsg"
239 );
240 } else {
241 $this->assertFalse(
242 $matches > 0,
243 "Search pattern '$protocol$pattern' should not find url '$url' \n$debugmsg"
244 );
245 }
246 }
247
248 public static function provideInvalidPatterns() {
249 return [
250 [ '' ],
251 [ '*' ],
252 [ 'http://*' ],
253 [ 'http://*/' ],
254 [ 'http://*/dir/file' ],
255 [ 'test.*.com' ],
256 [ 'http://test.*.com' ],
257 [ 'test.*.com' ],
258 [ 'http://*.test.*' ],
259 [ 'http://*test.com' ],
260 [ 'https://*' ],
261 [ '*://test.com' ],
262 [ 'mailto:name:pass@t*est.com' ],
263 [ 'http://*:888/' ],
264 [ '*http://' ],
265 [ 'test.com/*/index' ],
266 [ 'test.com/dir/index?arg=*' ],
267 ];
268 }
269
270 /**
271 * testMakeLikeArrayWithInvalidPatterns()
272 *
273 * Tests whether LinkFilter::makeLikeArray($pattern) will reject invalid search patterns
274 *
275 * @dataProvider provideInvalidPatterns
276 *
277 * @param string $pattern Invalid search pattern
278 */
279 public function testMakeLikeArrayWithInvalidPatterns( $pattern ) {
280 $this->assertFalse(
281 LinkFilter::makeLikeArray( $pattern ),
282 "'$pattern' is not a valid pattern and should be rejected"
283 );
284 }
285
286 /**
287 * @dataProvider provideMakeIndexes()
288 */
289 public function testMakeIndexes( $url, $expected ) {
290 // Set global so file:// tests can work
291 $this->overrideConfigValue(
292 MainConfigNames::UrlProtocols, [
293 'http://',
294 'https://',
295 'mailto:',
296 '//',
297 'file://', # Non-default
298 ] );
299
300 $index = LinkFilter::makeIndexes( $url );
301 $this->assertEquals( $expected, $index, "LinkFilter::makeIndexes(\"$url\")" );
302 }
303
304 public static function provideMakeIndexes() {
305 return [
306 // Testcase for T30627
307 [
308 'https://example.org/test.cgi?id=12345',
309 [ [ 'https://org.example.', '/test.cgi?id=12345' ] ]
310 ],
311 [
312 // mailtos are handled special
313 'mailto:[email protected]',
314 [ [ 'mailto:org.wikimedia.@wiki', '' ] ]
315 ],
316 [
317 // mailtos are handled special
318 'mailto:wiki',
319 [ [ 'mailto:@wiki', '' ] ]
320 ],
321
322 // file URL cases per T30627...
323 [
324 // three slashes: local filesystem path Unix-style
325 'file:///whatever/you/like.txt',
326 [ [ 'file://.', '/whatever/you/like.txt' ] ]
327 ],
328 [
329 // three slashes: local filesystem path Windows-style
330 'file:///c:/whatever/you/like.txt',
331 [ [ 'file://.', '/c:/whatever/you/like.txt' ] ]
332 ],
333 [
334 // two slashes: UNC filesystem path Windows-style
335 'file://intranet/whatever/you/like.txt',
336 [ [ 'file://intranet.', '/whatever/you/like.txt' ] ]
337 ],
338 // Multiple-slash cases that can sorta work on Mozilla
339 // if you hack it just right are kinda pathological,
340 // and unreliable cross-platform or on IE which means they're
341 // unlikely to appear on intranets.
342 // Those will survive the algorithm but with results that
343 // are less consistent.
344
345 // protocol-relative URL cases per T31854...
346 [
347 '//example.org/test.cgi?id=12345',
348 [ [ 'https://org.example.', '/test.cgi?id=12345' ] ]
349 ],
350
351 // IP addresses
352 [
353 'http://192.0.2.0/foo',
354 [ [ 'http://V4.192.0.2.0.', '/foo' ] ]
355 ],
356 [
357 'http://192.0.0002.0/foo',
358 [ [ 'http://V4.192.0.2.0.', '/foo' ] ]
359 ],
360 [
361 'http://[2001:db8::1]/foo',
362 [ [ 'http://V6.2001.DB8.0.0.0.0.0.1.', '/foo' ] ]
363 ],
364
365 // Explicit specification of the DNS root
366 [
367 'http://example.com./foo',
368 [ [ 'http://com.example.', '/foo' ] ]
369 ],
370 [
371 'http://192.0.2.0./foo',
372 [ [ 'http://V4.192.0.2.0.', '/foo' ] ]
373 ],
374
375 // Weird edge case
376 [
377 'http://.example.com/foo',
378 [ [ 'http://com.example..', '/foo' ] ]
379 ],
380 ];
381 }
382
383 /**
384 * @dataProvider provideReverseIndexes()
385 */
386 public function testReverseIndex( $url, $expected ) {
387 // Set global so file:// tests can work
388 $this->overrideConfigValue(
389 MainConfigNames::UrlProtocols, [
390 'http://',
391 'https://',
392 'mailto:',
393 '//',
394 'file://', # Non-default
395 ] );
396
397 $index = LinkFilter::reverseIndexes( $url );
398 $this->assertEquals( $expected, $index, "LinkFilter::reverseIndexe(\"$url\")" );
399 }
400
401 public static function provideReverseIndexes() {
402 return [
403 // Testcase for T30627
404 [
405 'https://org.example./test.cgi?id=12345',
406 'https://example.org'
407 ],
408 [
409 // mailtos are handled special
410 'mailto:org.wikimedia.@wiki',
411 'mailto:[email protected]'
412 ],
413 [
414 // mailtos are handled special
415 'mailto:@wiki',
416 'mailto:wiki@'
417 ],
418 [
419 // mailtos are handled special
420 'mailto:wiki',
421 'mailto:wiki'
422 ],
423
424 // file URL cases per T30627...
425 [
426 // three slashes: local filesystem path Unix-style
427 'file:///whatever/you/like.txt',
428 'file://'
429 ],
430 [
431 // three slashes: local filesystem path Windows-style
432 'file:///c:/whatever/you/like.txt',
433 'file://'
434 ],
435 [
436 // two slashes: UNC filesystem path Windows-style
437 'file://intranet/whatever/you/like.txt',
438 'file://intranet'
439 ],
440 // Multiple-slash cases that can sorta work on Mozilla
441 // if you hack it just right are kinda pathological,
442 // and unreliable cross-platform or on IE which means they're
443 // unlikely to appear on intranets.
444 // Those will survive the algorithm but with results that
445 // are less consistent.
446
447 // protocol-relative URL cases per T31854...
448 [
449 '//org.example/test.cgi?id=12345',
450 '//example.org'
451 ],
452
453 // IP addresses
454 [
455 'http://V4.192.0.2.0./foo',
456 'http://192.0.2.0'
457 ],
458 [
459 'http://V4.192.0.2.0./foo',
460 'http://192.0.2.0'
461 ],
462 [
463 'http://V6.2001.DB8.0.0.0.0.0.1./foo',
464 'http://[2001:DB8:0:0:0:0:0:1]'
465 ],
466
467 // Explicit specification of the DNS root
468 [
469 'http://com.example./foo',
470 'http://example.com'
471 ],
472 [
473 'http://192.0.2.0./foo',
474 'http://V4.192.0.2.0'
475 ],
476
477 // Weird edge case
478 [
479 'http://com.example../foo',
480 'http://.example.com'
481 ],
482
483 // port
484 [
485 'http://com.example.:8000/foo',
486 'http://example.com:8000'
487 ],
488 ];
489 }
490
491 /**
492 * @dataProvider provideGetQueryConditions
493 */
494 public function testGetQueryConditions( $query, $options, $expected ) {
495 $this->overrideConfigValues( [
496 MainConfigNames::ExternalLinksDomainGaps => [
497 'http://example.gaps.' => [
498 10 => 20,
499 100 => 200,
500 1000 => 2000,
501 ],
502 'https://example.gaps.' => [
503 30 => 40,
504 5000 => 60000,
505 ],
506 ],
507 ] );
508 $conds = LinkFilter::getQueryConditions( $query, $options );
509 if ( !$conds ) {
510 $this->assertEquals( $expected, $conds );
511 return;
512 }
513 $sqlConds = [];
514 foreach ( $conds as $cond ) {
515 if ( $cond instanceof IExpression ) {
516 $sqlConds[] = $cond->toSql( new AddQuoterMock() );
517 } else {
518 $sqlConds[] = $cond;
519 }
520 }
521 $this->assertEquals( $expected, $sqlConds );
522 }
523
524 public static function provideGetQueryConditions() {
525 return [
526 'Basic example' => [
527 'example.com',
528 [],
529 [
530 '(el_to_domain_index LIKE \'http://com.example.%\' ESCAPE \'`\' OR ' .
531 'el_to_domain_index LIKE \'https://com.example.%\' ESCAPE \'`\')',
532 'el_to_path LIKE \'/%\' ESCAPE \'`\'',
533 ],
534 ],
535 'Basic example with path' => [
536 'example.com/foobar',
537 [],
538 [
539 '(el_to_domain_index LIKE \'http://com.example.%\' ESCAPE \'`\' OR ' .
540 'el_to_domain_index LIKE \'https://com.example.%\' ESCAPE \'`\')',
541 'el_to_path LIKE \'/foobar%\' ESCAPE \'`\'',
542 ],
543 ],
544 'Wildcard domain' => [
545 '*.example.com',
546 [],
547 [
548 '(el_to_domain_index LIKE \'http://com.example.%\' ESCAPE \'`\' OR ' .
549 'el_to_domain_index LIKE \'https://com.example.%\' ESCAPE \'`\')',
550 'el_to_path LIKE \'/%\' ESCAPE \'`\'',
551 ],
552 ],
553 'Wildcard domain with path' => [
554 '*.example.com/foobar',
555 [],
556 [
557 '(el_to_domain_index LIKE \'http://com.example.%\' ESCAPE \'`\' OR ' .
558 'el_to_domain_index LIKE \'https://com.example.%\' ESCAPE \'`\')',
559 'el_to_path LIKE \'/foobar%\' ESCAPE \'`\'',
560 ],
561 ],
562 'Wildcard domain with path, oneWildcard=true' => [
563 '*.example.com/foobar',
564 [ 'oneWildcard' => true ],
565 [
566 '(el_to_domain_index = \'http://com.example.\' OR ' .
567 'el_to_domain_index = \'https://com.example.\')',
568 'el_to_path LIKE \'/foobar%\' ESCAPE \'`\'',
569 ],
570 ],
571 'Constant prefix' => [
572 'example.com/blah/blah/blah/blah/blah/blah/blah/blah/blah/blah?foo=',
573 [],
574 [
575 '(el_to_domain_index LIKE \'http://com.example.%\' ESCAPE \'`\' OR ' .
576 'el_to_domain_index LIKE \'https://com.example.%\' ESCAPE \'`\')',
577 'el_to_path LIKE ' .
578 '\'/blah/blah/blah/blah/blah/blah/blah/blah/blah/blah?foo=%\' ' .
579 'ESCAPE \'`\'',
580 ],
581 ],
582 'Bad protocol' => [
583 'test/',
584 [ 'protocol' => 'invalid://' ],
585 false
586 ],
587 'domains with gaps' => [
588 'gaps.example',
589 [],
590 [
591 '((el_to_domain_index LIKE \'http://example.gaps.%\' ESCAPE \'`\' AND ' .
592 '(el_id < 10 OR el_id > 20) AND ' .
593 '(el_id < 100 OR el_id > 200) AND ' .
594 '(el_id < 1000 OR el_id > 2000)) OR ' .
595 '(el_to_domain_index LIKE \'https://example.gaps.%\' ESCAPE \'`\' AND ' .
596 '(el_id < 30 OR el_id > 40) AND ' .
597 '(el_id < 5000 OR el_id > 60000)))',
598 'el_to_path LIKE \'/%\' ESCAPE \'`\'',
599 ],
600 ],
601 'Various options' => [
602 'example.com',
603 [ 'protocol' => 'https://' ],
604 [
605 "(el_to_domain_index LIKE 'https://com.example.%' ESCAPE '`')",
606 "el_to_path LIKE '/%' ESCAPE '`'",
607 ],
608 ],
609 ];
610 }
611
612 /**
613 * @dataProvider provideGetIndexedUrlsNonReversed
614 */
615 public function testGetIndexedUrlsNonReversed( $urls, $expected ) {
616 $list = LinkFilter::getIndexedUrlsNonReversed( $urls );
617 $this->assertEquals( $expected, $list );
618 }
619
620 public static function provideGetIndexedUrlsNonReversed() {
621 return [
622 'Basic example' => [
623 [ 'https://example.com' ],
624 [ 'https://example.com/' ],
625 ],
626 'Basic example with path' => [
627 [ 'https://example.com/foobar' ],
628 [ 'https://example.com/foobar' ],
629 ],
630 'Proto-relative' => [
631 [ '//example.com/foobar' ],
632 [ 'https://example.com/foobar' ],
633 ],
634 'Links with port' => [
635 [ 'https://spam.com/index.html:8000' ],
636 [ 'https://spam.com/index.html:8000' ],
637 ],
638 'Links with user' => [
639 [ 'https://[email protected]/bar.html' ],
640 [ 'https://spam.com/bar.html' ],
641 ],
642 'Mailto' => [
643 [ 'mailto:[email protected]' ],
644 [ 'mailto:[email protected]' ],
645 ],
646 'IPv6 (gosh I hate ipv6)' => [
647 [ 'http://[::1]/' ],
648 [ 'http://[0:0:0:0:0:0:0:1]/' ]
649 ],
650 'IPv4' => [
651 [ 'http://0.0.0.1/' ],
652 [ 'http://0.0.0.1/' ]
653 ]
654 ];
655 }
656 }