<?php
+use Wikimedia\Rdbms\LikeMatch;
+
/**
* @group Database
*/
class LinkFilterTest extends MediaWikiLangTestCase {
protected function setUp() {
-
parent::setUp();
$this->setMwGlobals( 'wgUrlProtocols', [
'mms://',
'//',
] );
-
}
/**
* @return string Regex
*/
function createRegexFromLIKE( $like ) {
-
$regex = '!^';
foreach ( $like as $item ) {
-
if ( $item instanceof LikeMatch ) {
if ( $item->toString() == '%' ) {
$regex .= '.*';
$regex .= '$!';
return $regex;
-
}
/**
* @return array
*/
public static function provideValidPatterns() {
-
return [
// Protocol, Search pattern, URL which matches the pattern
[ 'http://', '*.test.com', 'http://www.test.com' ],
// [ '', 'https://*.wikimedia.org/r/#/q/status:open,n,z',
// 'https://gerrit.wikimedia.org/XXX/r/#/q/status:open,n,z', false ],
];
-
}
/**
* @param bool $shouldBeFound Should the URL be found? (defaults true)
*/
function testMakeLikeArrayWithValidPatterns( $protocol, $pattern, $url, $shouldBeFound = true ) {
-
$indexes = wfMakeUrlIndexes( $url );
$likeArray = LinkFilter::makeLikeArray( $pattern, $protocol );
"Search pattern '$protocol$pattern' should not find url '$url' \n$debugmsg"
);
}
-
}
/**
* @return array
*/
public static function provideInvalidPatterns() {
-
return [
[ '' ],
[ '*' ],
[ 'test.com/*/index' ],
[ 'test.com/dir/index?arg=*' ],
];
-
}
/**
* @param string $pattern Invalid search pattern
*/
function testMakeLikeArrayWithInvalidPatterns( $pattern ) {
-
$this->assertFalse(
LinkFilter::makeLikeArray( $pattern ),
"'$pattern' is not a valid pattern and should be rejected"
);
-
}
}