From b19fba79a9f36cbb5dbd0573cb04eacb7045dc79 Mon Sep 17 00:00:00 2001 From: Jon Robson Date: Wed, 15 May 2024 21:23:41 -0700 Subject: [PATCH] Correct behaviour of ConfigHelper, add tests - Cherry-picked to be back-ported Bug: T365084 Change-Id: I220990554d0a07642dd03a0c7bd53f9bd7ff55ec --- includes/ConfigHelper.php | 25 +++++++------- .../phpunit/integration/ConfigHelperTest.php | 34 +++++++++++++++++++ tests/phpunit/integration/VectorHooksTest.php | 2 +- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/includes/ConfigHelper.php b/includes/ConfigHelper.php index b6503e037..828d581a4 100644 --- a/includes/ConfigHelper.php +++ b/includes/ConfigHelper.php @@ -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; } } diff --git a/tests/phpunit/integration/ConfigHelperTest.php b/tests/phpunit/integration/ConfigHelperTest.php index 285aca951..dd790c9a1 100644 --- a/tests/phpunit/integration/ConfigHelperTest.php +++ b/tests/phpunit/integration/ConfigHelperTest.php @@ -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 */ diff --git a/tests/phpunit/integration/VectorHooksTest.php b/tests/phpunit/integration/VectorHooksTest.php index 23cdc1211..2baebc4fa 100644 --- a/tests/phpunit/integration/VectorHooksTest.php +++ b/tests/phpunit/integration/VectorHooksTest.php @@ -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',