+ /**
+ * @dataProvider provideMakeIndexes()
+ * @covers LinkFilter::makeIndexes
+ */
+ public function testMakeIndexes( $url, $expected ) {
+ // Set global so file:// tests can work
+ $this->setMwGlobals( [
+ 'wgUrlProtocols' => [
+ 'http://',
+ 'https://',
+ 'mailto:',
+ '//',
+ 'file://', # Non-default
+ ],
+ ] );
+
+ $index = LinkFilter::makeIndexes( $url );
+ $this->assertEquals( $expected, $index, "LinkFilter::makeIndexes(\"$url\")" );
+ }
+
+ public static function provideMakeIndexes() {
+ return [
+ // Testcase for T30627
+ [
+ 'https://example.org/test.cgi?id=12345',
+ [ 'https://org.example./test.cgi?id=12345' ]
+ ],
+ [
+ // mailtos are handled special
+ 'mailto:wiki@wikimedia.org',
+ [ 'mailto:org.wikimedia.@wiki' ]
+ ],
+ [
+ // mailtos are handled special
+ 'mailto:wiki',
+ [ 'mailto:@wiki' ]
+ ],
+
+ // file URL cases per T30627...
+ [
+ // three slashes: local filesystem path Unix-style
+ 'file:///whatever/you/like.txt',
+ [ 'file://./whatever/you/like.txt' ]
+ ],
+ [
+ // three slashes: local filesystem path Windows-style
+ 'file:///c:/whatever/you/like.txt',
+ [ 'file://./c:/whatever/you/like.txt' ]
+ ],
+ [
+ // two slashes: UNC filesystem path Windows-style
+ 'file://intranet/whatever/you/like.txt',
+ [ 'file://intranet./whatever/you/like.txt' ]
+ ],
+ // Multiple-slash cases that can sorta work on Mozilla
+ // if you hack it just right are kinda pathological,
+ // and unreliable cross-platform or on IE which means they're
+ // unlikely to appear on intranets.
+ // Those will survive the algorithm but with results that
+ // are less consistent.
+
+ // protocol-relative URL cases per T31854...
+ [
+ '//example.org/test.cgi?id=12345',
+ [
+ 'http://org.example./test.cgi?id=12345',
+ 'https://org.example./test.cgi?id=12345'
+ ]
+ ],
+
+ // IP addresses
+ [
+ 'http://192.0.2.0/foo',
+ [ 'http://V4.192.0.2.0./foo' ]
+ ],
+ [
+ 'http://192.0.0002.0/foo',
+ [ 'http://V4.192.0.2.0./foo' ]
+ ],
+ [
+ 'http://[2001:db8::1]/foo',
+ [ 'http://V6.2001.DB8.0.0.0.0.0.1./foo' ]
+ ],
+
+ // Explicit specification of the DNS root
+ [
+ 'http://example.com./foo',
+ [ 'http://com.example./foo' ]
+ ],
+ [
+ 'http://192.0.2.0./foo',
+ [ 'http://V4.192.0.2.0./foo' ]
+ ],
+
+ // Weird edge case
+ [
+ 'http://.example.com/foo',
+ [ 'http://com.example../foo' ]
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetQueryConditions
+ * @covers LinkFilter::getQueryConditions
+ */
+ public function testGetQueryConditions( $query, $options, $expected ) {
+ $conds = LinkFilter::getQueryConditions( $query, $options );
+ $this->assertEquals( $expected, $conds );
+ }
+
+ public static function provideGetQueryConditions() {
+ return [
+ 'Basic example' => [
+ 'example.com',
+ [],
+ [
+ 'el_index_60 LIKE \'http://com.example./%\' ESCAPE \'`\' ',
+ 'el_index LIKE \'http://com.example./%\' ESCAPE \'`\' ',
+ ],
+ ],
+ 'Basic example with path' => [
+ 'example.com/foobar',
+ [],
+ [
+ 'el_index_60 LIKE \'http://com.example./foobar%\' ESCAPE \'`\' ',
+ 'el_index LIKE \'http://com.example./foobar%\' ESCAPE \'`\' ',
+ ],
+ ],
+ 'Wildcard domain' => [
+ '*.example.com',
+ [],
+ [
+ 'el_index_60 LIKE \'http://com.example.%\' ESCAPE \'`\' ',
+ 'el_index LIKE \'http://com.example.%\' ESCAPE \'`\' ',
+ ],
+ ],
+ 'Wildcard domain with path' => [
+ '*.example.com/foobar',
+ [],
+ [
+ 'el_index_60 LIKE \'http://com.example.%\' ESCAPE \'`\' ',
+ 'el_index LIKE \'http://com.example.%/foobar%\' ESCAPE \'`\' ',
+ ],
+ ],
+ 'Wildcard domain with path, oneWildcard=true' => [
+ '*.example.com/foobar',
+ [ 'oneWildcard' => true ],
+ [
+ 'el_index_60 LIKE \'http://com.example.%\' ESCAPE \'`\' ',
+ 'el_index LIKE \'http://com.example.%\' ESCAPE \'`\' ',
+ ],
+ ],
+ 'Constant prefix' => [
+ 'example.com/blah/blah/blah/blah/blah/blah/blah/blah/blah/blah?foo=',
+ [],
+ [
+ 'el_index_60' => 'http://com.example./blah/blah/blah/blah/blah/blah/blah/blah/',
+ 'el_index LIKE ' .
+ '\'http://com.example./blah/blah/blah/blah/blah/blah/blah/blah/blah/blah?foo=%\' ' .
+ 'ESCAPE \'`\' ',
+ ],
+ ],
+ 'Bad protocol' => [
+ 'test/',
+ [ 'protocol' => 'invalid://' ],
+ false
+ ],
+ 'Various options' => [
+ 'example.com',
+ [ 'protocol' => 'https://', 'prefix' => 'xx' ],
+ [
+ 'xx_index_60 LIKE \'https://com.example./%\' ESCAPE \'`\' ',
+ 'xx_index LIKE \'https://com.example./%\' ESCAPE \'`\' ',
+ ],
+ ],
+ ];
+ }
+