X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fresourceloader%2FResourceLoaderWikiModuleTest.php;h=78eec6a6f7e33d1bad099fae1b38522b5a12f5b3;hb=63752d25bfc28159f5f9b6a2e9a6a6d784959fd0;hp=5cab8e2ff6d3e05f1c8e8978d9aef58fcce61bba;hpb=10e119b56c32aa99491e676f0208e4c2b402fabc;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php index 5cab8e2ff6..78eec6a6f7 100644 --- a/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php @@ -2,6 +2,7 @@ use MediaWiki\MediaWikiServices; use Wikimedia\Rdbms\IDatabase; +use Wikimedia\TestingAccessWrapper; class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase { @@ -218,6 +219,112 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase { $this->assertEquals( $expected, $module->getTitleInfo( $context ), 'Title info' ); } + /** + * @covers ResourceLoaderWikiModule::preloadTitleInfo + */ + public function testGetPreloadedBadTitle() { + // Mock values + $pages = [ + // Covers else branch for invalid page name + '[x]' => [ 'type' => 'styles' ], + ]; + $titleInfo = []; + + // Set up objects + $module = $this->getMockBuilder( 'TestResourceLoaderWikiModule' ) + ->setMethods( [ 'getPages' ] ) ->getMock(); + $module->method( 'getPages' )->willReturn( $pages ); + $module::$returnFetchTitleInfo = $titleInfo; + $rl = new EmptyResourceLoader(); + $rl->register( 'testmodule', $module ); + $context = new ResourceLoaderContext( $rl, new FauxRequest() ); + + // Act + TestResourceLoaderWikiModule::preloadTitleInfo( + $context, + wfGetDB( DB_REPLICA ), + [ 'testmodule' ] + ); + + // Assert + $module = TestingAccessWrapper::newFromObject( $module ); + $this->assertEquals( $titleInfo, $module->getTitleInfo( $context ), 'Title info' ); + } + + /** + * @covers ResourceLoaderWikiModule::preloadTitleInfo + */ + public function testGetPreloadedTitleInfoEmpty() { + $context = new ResourceLoaderContext( new EmptyResourceLoader(), new FauxRequest() ); + // Covers early return + $this->assertSame( + null, + ResourceLoaderWikiModule::preloadTitleInfo( + $context, + wfGetDB( DB_REPLICA ), + [] + ) + ); + } + + public static function provideGetContent() { + return [ + 'Bad title' => [ null, '[x]' ], + 'Dead redirect' => [ null, [ + 'text' => 'Dead redirect', + 'title' => 'Dead_redirect', + 'redirect' => 1, + ] ], + 'Bad content model' => [ null, [ + 'text' => 'MediaWiki:Wikitext', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Wikitext', + ] ], + 'No JS content found' => [ null, [ + 'text' => 'MediaWiki:Script.js', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Script.js', + ] ], + 'No CSS content found' => [ null, [ + 'text' => 'MediaWiki:Styles.css', + 'ns' => NS_MEDIAWIKI, + 'title' => 'Script.css', + ] ], + ]; + } + + /** + * @covers ResourceLoaderWikiModule::getContent + * @dataProvider provideGetContent + */ + public function testGetContent( $expected, $title ) { + $context = $this->getResourceLoaderContext( [], new EmptyResourceLoader ); + $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' ) + ->setMethods( [ 'getContentObj' ] ) ->getMock(); + $module->expects( $this->any() ) + ->method( 'getContentObj' )->willReturn( null ); + + if ( is_array( $title ) ) { + $title += [ 'ns' => NS_MAIN, 'id' => 1, 'len' => 1, 'redirect' => 0 ]; + $titleText = $title['text']; + // Mock Title db access via LinkCache + MediaWikiServices::getInstance()->getLinkCache()->addGoodLinkObj( + $title['id'], + new TitleValue( $title['ns'], $title['title'] ), + $title['len'], + $title['redirect'] + ); + } else { + $titleText = $title; + } + + $module = TestingAccessWrapper::newFromObject( $module ); + $this->assertEquals( + $expected, + $module->getContent( $titleText ) + ); + } + /** * @covers ResourceLoaderWikiModule::getContent */