Add tests for ConfigHelper

This class is being used in both vector and minerva, and we are
currently depending on it working for our night mode rollout, so let's
add some test coverage!  Also fixed a small spacing issue in production
code that was bothering me lol

Note: these are not totally exhaustive - next steps would be to ensure
that the exclusions/inclusions work with arrays and not only single
elements, but hopefully this helps build some confidence ☺️

Change-Id: Iebea43eae6b1517fd778763db4f5db3b0fd9c30f
This commit is contained in:
Steph Toyofuku 2024-03-27 16:49:39 -07:00
parent 6d9e3abe9c
commit 3387ef3ee4
2 changed files with 123 additions and 1 deletions

View file

@ -86,8 +86,8 @@ class ConfigHelper {
if ( $title != null && $title->inNamespaces( $excludeNamespaces ) ) {
return true;
}
$excludeQueryString = $exclusions[ 'querystring' ] ?? [];
$excludeQueryString = $exclusions[ 'querystring' ] ?? [];
foreach ( $excludeQueryString as $param => $excludedParamPattern ) {
$paramValue = $request->getRawVal( $param );
if ( $paramValue !== null ) {

View file

@ -0,0 +1,122 @@
<?php
namespace MediaWiki\Skins\Vector\Tests\Integration;
use MediaWiki\Skins\Vector\ConfigHelper;
use RequestContext;
use Title;
/**
* @coversDefaultClass \MediaWiki\Skins\Vector\ConfigHelper
*/
class ConfigHelperTest extends \MediaWikiIntegrationTestCase {
/**
* @covers ::shouldDisable when config is empty
*/
public function testShouldDisableEmpty() {
$request = RequestContext::getMain()->getRequest();
$this->assertFalse( ConfigHelper::shouldDisable( [], $request ) );
}
public static function provideShouldDisableMainPage() {
return [
[ true ], [ false ]
];
}
/**
* @dataProvider provideShouldDisableMainPage
* @covers ::shouldDisable for the main page
*/
public function testShouldDisableMainPage( $disable ) {
$config = [ 'exclude' => [ 'mainpage' => $disable ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'Main Page' );
$this->assertSame( ConfigHelper::shouldDisable( $config, $request, $title ), $disable );
}
/**
* @covers ::shouldDisable for the main page when mainpage is not present in the config
*/
public function testShouldDisableMainPageImplicit() {
$config = [ 'exclude' => [ 'pagetitles' => [ 'Main Page' ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'Main Page' );
$this->assertFalse( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable inclusion
*/
public function testShouldDisableInclude() {
$config = [ 'exclude' => [ 'pagetitles' => [ 'test' ] ], 'include' => [ 'test' ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'test' );
$this->assertFalse( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable page title exclusion
*/
public function testShouldDisablePageTitles() {
$config = [ 'exclude' => [ 'pagetitles' => [ 'test' ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'test' );
$this->assertTrue( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable namespace exclusion
*/
public function testShouldDisableNamespaces() {
$config = [ 'exclude' => [ 'namespaces' => [ NS_SPECIAL ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_SPECIAL, 'test' );
$this->assertTrue( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable query string exclusion
*/
public function testShouldDisableQueryString() {
$config = [ 'exclude' => [ 'querystring' => [ 'action' => 'test' ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'test' );
$request->setVal( 'action', 'aaatestaaa' );
$this->assertTrue( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable query string exclusion using regex
*/
public function testShouldDisableQueryStringRegex() {
$config = [ 'exclude' => [ 'querystring' => [ 'action' => 'a+b.c' ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'test' );
$request->setVal( 'action', 'aaaabbc' );
$this->assertTrue( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
/**
* @covers ::shouldDisable query string exclusion using wildcard
*/
public function testShouldDisableQueryStringWildcard() {
$config = [ 'exclude' => [ 'querystring' => [ 'action' => '*' ] ] ];
$request = RequestContext::getMain()->getRequest();
$title = Title::makeTitle( NS_MAIN, 'test' );
$request->setVal( 'action', 'test' );
$this->assertTrue( ConfigHelper::shouldDisable( $config, $request, $title ) );
}
}