2017-02-20 04:33:24 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 14:39:49 +00:00
|
|
|
use MediaWiki\Extension\TemplateStyles\TemplateStylesMatcherFactory;
|
2017-02-20 04:33:24 +00:00
|
|
|
use Wikimedia\CSS\Objects\ComponentValueList;
|
|
|
|
use Wikimedia\CSS\Objects\Token;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group TemplateStyles
|
2022-02-06 14:39:49 +00:00
|
|
|
* @covers \MediaWiki\Extension\TemplateStyles\TemplateStylesMatcherFactory
|
2017-02-20 04:33:24 +00:00
|
|
|
*/
|
2019-07-21 21:04:39 +00:00
|
|
|
class TemplateStylesMatcherFactoryTest extends MediaWikiUnitTestCase {
|
2017-02-20 04:33:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideUrls
|
|
|
|
* @param string $type
|
|
|
|
* @param string $url
|
|
|
|
* @param bool $expect
|
|
|
|
*/
|
|
|
|
public function testUrls( $type, $url, $expect ) {
|
|
|
|
$factory = new TemplateStylesMatcherFactory( [
|
|
|
|
'test1' => [
|
|
|
|
'<^http://example\.com/test1/>',
|
|
|
|
],
|
|
|
|
'test2' => [
|
|
|
|
'<^http://example\.com/test2/A/>',
|
|
|
|
'<^http://example\.com/test2/B/>',
|
|
|
|
],
|
|
|
|
'anything' => [
|
|
|
|
'<.>',
|
|
|
|
],
|
|
|
|
] );
|
|
|
|
|
|
|
|
$list = new ComponentValueList( [
|
|
|
|
new Token( Token::T_STRING, $url )
|
|
|
|
] );
|
2021-01-08 03:11:35 +00:00
|
|
|
$this->assertSame( $expect, (bool)$factory->urlstring( $type )->matchAgainst( $list ) );
|
2017-02-20 04:33:24 +00:00
|
|
|
|
|
|
|
$list = new ComponentValueList( [
|
|
|
|
new Token( Token::T_URL, $url )
|
|
|
|
] );
|
2021-01-08 03:11:35 +00:00
|
|
|
$this->assertSame( $expect, (bool)$factory->url( $type )->matchAgainst( $list ) );
|
2017-02-20 04:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideUrls() {
|
|
|
|
return [
|
|
|
|
[ 'test1', 'http://example.com/test1/foobar', true ],
|
|
|
|
[ 'test2', 'http://example.com/test1/foobar', false ],
|
|
|
|
[ 'test2', 'http://example.com/test2/A/foobar', true ],
|
|
|
|
[ 'test2', 'http://example.com/test2/B/foobar', true ],
|
|
|
|
[ 'test2', 'http://example.com/test2/C/foobar', false ],
|
|
|
|
[ 'test3', 'http://example.com/test3/foobar', false ],
|
|
|
|
[ 'test1', 'http://example.com/test1/../../etc/password', false ],
|
|
|
|
[ 'test1', 'http://example.com/test1/..%2F..%2Fetc%2Fpassword', false ],
|
|
|
|
[ 'test1', 'http://example.com/test1/etc\\password', false ],
|
|
|
|
[ 'test1', 'http://example.com/test%31/foobar', true ],
|
|
|
|
[ 'test1', 'http://example.com/test1/x=/%2E/foobar', false ],
|
|
|
|
[ 'test1', 'http://example.com/test1/?x=/%2E/foobar', true ],
|
|
|
|
[ 'test1', 'http://example.com/test1/%3Fx=/%2E/foobar', false ],
|
|
|
|
[ 'test1', 'http://example.com/test1/#x=/%2E/foobar', true ],
|
|
|
|
[ 'test1', 'http://example.com/test1/%23x=/%2E/foobar', false ],
|
|
|
|
[ 'anything', 'totally bogus', true ],
|
|
|
|
[ 'anything', '/dotdot/../still/fails/though', false ],
|
|
|
|
[ 'anything', '../still/fails/though', false ],
|
|
|
|
[ 'anything', 'still/fails/..', false ],
|
|
|
|
[ 'anything', '..', false ],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|