Correct behaviour of ConfigHelper, add tests

- Cherry-picked for back-port purposes 

Bug: T365084
Change-Id: I220990554d0a07642dd03a0c7bd53f9bd7ff55ec
This commit is contained in:
Jon Robson 2024-05-15 21:23:41 -07:00 committed by Mabualruz
parent 9995bcdf12
commit fb73253c85
3 changed files with 47 additions and 14 deletions

View file

@ -38,6 +38,18 @@ class ConfigHelper {
$exclusions = $options[ 'exclude' ] ?? [];
$inclusions = $options['include'] ?? [];
$excludeQueryString = $exclusions[ 'querystring' ] ?? [];
foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
$paramValue = $request->getRawVal( $param );
if ( $paramValue !== null ) {
if ( $excludedParamPattern === '*' ) {
// Backwards compatibility for the '*' wildcard.
$excludedParamPattern = '.+';
}
return (bool)preg_match( "/$excludedParamPattern/", $paramValue );
}
}
if ( $title != null && $title->isMainPage() ) {
// only one check to make
return $exclusions[ 'mainpage' ] ?? false;
@ -70,7 +82,6 @@ class ConfigHelper {
foreach ( $pageTitles as $titleText ) {
// use strtolower to make sure the config passed for special pages
// is case insensitive, so it does not generate a wrong special page title
$titleText = $canonicalTitle->isSpecialPage() ? strtolower( $titleText ) : $titleText;
$excludedTitle = Title::newFromText( $titleText );
if ( $canonicalTitle != null && $canonicalTitle->equals( $excludedTitle ) ) {
@ -87,18 +98,6 @@ class ConfigHelper {
return true;
}
$excludeQueryString = $exclusions[ 'querystring' ] ?? [];
foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
$paramValue = $request->getRawVal( $param );
if ( $paramValue !== null ) {
if ( $excludedParamPattern === '*' ) {
// Backwards compatibility for the '*' wildcard.
$excludedParamPattern = '.+';
}
return (bool)preg_match( "/$excludedParamPattern/", $paramValue );
}
}
return false;
}
}

View file

@ -2,6 +2,7 @@
namespace MediaWiki\Skins\Vector\Tests\Integration;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Skins\Vector\ConfigHelper;
use RequestContext;
use Title;
@ -37,6 +38,39 @@ class ConfigHelperTest extends \MediaWikiIntegrationTestCase {
$this->assertSame( ConfigHelper::shouldDisable( $config, $request, $title ), $disable );
}
/**
* @covers ::shouldDisable for the main page
*/
public function testShouldDisableMainPageWithQueryString() {
$config = [
'exclude' => [
'mainpage' => false,
'querystring' => [
'diff' => '*',
]
],
];
$request = new FauxRequest( [
'title' => 'Main_Page',
'diff' => '1223300368',
'oldid' => '1212457119',
] );
$title = Title::makeTitle( NS_MAIN, 'Main_Page' );
$this->assertSame( true, ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable page title exclusion
*/
public function testShouldDisablePageTitlesRespectCase() {
$config = [ 'exclude' => [ 'pagetitles' => [ 'Special:AbuseLog' ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'Special:AbuseLog' );
$this->assertTrue( ConfigHelper::shouldDisable( $config, $request, $title ), true );
}
/**
* @covers ::shouldDisable for the main page when mainpage is not present in the config
*/

View file

@ -283,7 +283,7 @@ class VectorHooksTest extends MediaWikiIntegrationTestCase {
),
Title::makeTitle( NS_SPECIAL, 'Specialpages' ),
[ 'action' => 'history' ],
false
true
],
[
'Max width can be disabled on talk pages',