X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FOutputPageTest.php;h=32fa46887debe526f2bd84ae5332fd1d66724c3c;hb=3cecf7e6f1b6b30e86c3b08372ecf200bd5a9018;hp=59441ce08a0f1caa45c36f9193d3b9f78003dc19;hpb=bf9c2b0cb30cbe994f3a1859cf46224dd52039e1;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index 59441ce08a..32fa46887d 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -1,5 +1,7 @@ assertEquals( $expectedHtml, $actualHtml ); } + public static function provideBuildExemptModules() { + return [ + 'empty' => [ + 'exemptStyleModules' => [], + '', + ], + 'empty sets' => [ + 'exemptStyleModules' => [ 'site' => [], 'noscript' => [], 'private' => [], 'user' => [] ], + '', + ], + // @codingStandardsIgnoreStart Generic.Files.LineLength + 'default logged-out' => [ + 'exemptStyleModules' => [ 'site' => [ 'site.styles' ] ], + '' . "\n" . + '', + ], + 'default logged-in' => [ + 'exemptStyleModules' => [ 'site' => [ 'site.styles' ], 'user' => [ 'user.styles' ] ], + '' . "\n" . + '' . "\n" . + '', + ], + 'custom modules' => [ + 'exemptStyleModules' => [ + 'site' => [ 'site.styles', 'example.site.a', 'example.site.b' ], + 'user' => [ 'user.styles', 'example.user' ], + ], + '' . "\n" . + '' . "\n" . + '' . "\n" . + '' . "\n" . + '', + ], + // @codingStandardsIgnoreEnd Generic.Files.LineLength + ]; + } + + /** + * @dataProvider provideBuildExemptModules + * @covers OutputPage::buildExemptModules + */ + public function testBuildExemptModules( array $exemptStyleModules, $expect ) { + $this->setMwGlobals( [ + 'wgResourceLoaderDebug' => false, + 'wgLoadScript' => '/w/load.php', + // Stub wgCacheEpoch as it influences getVersionHash used for the + // urls in the expected HTML + 'wgCacheEpoch' => '20140101000000', + ] ); + + // Set up stubs + $ctx = new RequestContext(); + $ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) ); + $ctx->setLanguage( 'en' ); + $outputPage = $this->getMockBuilder( 'OutputPage' ) + ->setConstructorArgs( [ $ctx ] ) + ->setMethods( [ 'isUserCssPreview', 'buildCssLinksArray' ] ) + ->getMock(); + $outputPage->expects( $this->any() ) + ->method( 'isUserCssPreview' ) + ->willReturn( false ); + $outputPage->expects( $this->any() ) + ->method( 'buildCssLinksArray' ) + ->willReturn( [] ); + $rl = $outputPage->getResourceLoader(); + $rl->setMessageBlobStore( new NullMessageBlobStore() ); + + // Register custom modules + $rl->register( [ + 'example.site.a' => new ResourceLoaderTestModule( [ 'group' => 'site' ] ), + 'example.site.b' => new ResourceLoaderTestModule( [ 'group' => 'site' ] ), + 'example.user' => new ResourceLoaderTestModule( [ 'group' => 'user' ] ), + ] ); + + $outputPage = TestingAccessWrapper::newFromObject( $outputPage ); + $outputPage->rlExemptStyleModules = $exemptStyleModules; + $this->assertEquals( + $expect, + strval( $outputPage->buildExemptModules() ) + ); + } + /** * @dataProvider provideVaryHeaders * @covers OutputPage::addVaryHeader @@ -472,13 +556,110 @@ class OutputPageTest extends MediaWikiTestCase { $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) ); } + /** + * @dataProvider provideLinkHeaders + * @covers OutputPage::addLinkHeader + * @covers OutputPage::getLinkHeader + */ + public function testLinkHeaders( $headers, $result ) { + $outputPage = $this->newInstance(); + + foreach ( $headers as $header ) { + $outputPage->addLinkHeader( $header ); + } + + $this->assertEquals( $result, $outputPage->getLinkHeader() ); + } + + public function provideLinkHeaders() { + return [ + [ + [], + false + ], + [ + [ ';rel=preload;as=image' ], + 'Link: ;rel=preload;as=image', + ], + [ + [ ';rel=preload;as=image',';rel=preload;as=image' ], + 'Link: ;rel=preload;as=image,;rel=preload;as=image', + ], + ]; + } + + /** + * @dataProvider providePreloadLinkHeaders + * @covers OutputPage::addLogoPreloadLinkHeaders + * @covers ResourceLoaderSkinModule::getLogo + */ + public function testPreloadLinkHeaders( $config, $result, $baseDir = null ) { + if ( $baseDir ) { + $this->setMwGlobals( 'IP', $baseDir ); + } + $out = TestingAccessWrapper::newFromObject( $this->newInstance( $config ) ); + $out->addLogoPreloadLinkHeaders(); + + $this->assertEquals( $result, $out->getLinkHeader() ); + } + + public function providePreloadLinkHeaders() { + return [ + [ + [ + 'ResourceBasePath' => '/w', + 'Logo' => '/img/default.png', + 'LogoHD' => [ + '1.5x' => '/img/one-point-five.png', + '2x' => '/img/two-x.png', + ], + ], + 'Link: ;rel=preload;as=image;media=' . + 'not all and (min-resolution: 1.5dppx),' . + ';rel=preload;as=image;media=' . + '(min-resolution: 1.5dppx) and (max-resolution: 1.999999dppx),' . + ';rel=preload;as=image;media=(min-resolution: 2dppx)' + ], + [ + [ + 'ResourceBasePath' => '/w', + 'Logo' => '/img/default.png', + 'LogoHD' => false, + ], + 'Link: ;rel=preload;as=image' + ], + [ + [ + 'ResourceBasePath' => '/w', + 'Logo' => '/img/default.png', + 'LogoHD' => [ + '2x' => '/img/two-x.png', + ], + ], + 'Link: ;rel=preload;as=image;media=' . + 'not all and (min-resolution: 2dppx),' . + ';rel=preload;as=image;media=(min-resolution: 2dppx)' + ], + [ + [ + 'ResourceBasePath' => '/w', + 'Logo' => '/w/test.jpg', + 'LogoHD' => false, + 'UploadPath' => '/w/images', + ], + 'Link: ;rel=preload;as=image', + 'baseDir' => dirname( __DIR__ ) . '/data/media', + ], + ]; + } + /** * @return OutputPage */ - private function newInstance() { + private function newInstance( $config = [] ) { $context = new RequestContext(); - $context->setConfig( new HashConfig( [ + $context->setConfig( new HashConfig( $config + [ 'AppleTouchIcon' => false, 'DisableLangConversion' => true, 'EnableAPI' => false,