] );
}
+ public static function provideCdnCacheEpoch() {
+ $base = [
+ 'pageTime' => '2011-04-01T12:00:00+00:00',
+ 'maxAge' => 24 * 3600,
+ ];
+ return [
+ 'after 1s' => [ $base + [
+ 'reqTime' => '2011-04-01T12:00:01+00:00',
+ 'expect' => '2011-04-01T12:00:00+00:00',
+ ] ],
+ 'after 23h' => [ $base + [
+ 'reqTime' => '2011-04-02T11:00:00+00:00',
+ 'expect' => '2011-04-01T12:00:00+00:00',
+ ] ],
+ 'after 24h and a bit' => [ $base + [
+ 'reqTime' => '2011-04-02T12:34:56+00:00',
+ 'expect' => '2011-04-01T12:34:56+00:00',
+ ] ],
+ 'after a year' => [ $base + [
+ 'reqTime' => '2012-05-06T00:12:07+00:00',
+ 'expect' => '2012-05-05T00:12:07+00:00',
+ ] ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideCdnCacheEpoch
+ * @covers OutputPage::getCdnCacheEpoch
+ */
+ public function testCdnCacheEpoch( $params ) {
+ $out = TestingAccessWrapper::newFromObject( $this->newInstance() );
+ $reqTime = strtotime( $params['reqTime'] );
+ $pageTime = strtotime( $params['pageTime'] );
+ $actual = max( $pageTime, $out->getCdnCacheEpoch( $reqTime, $params['maxAge'] ) );
+
+ $this->assertEquals(
+ $params['expect'],
+ gmdate( DateTime::ATOM, $actual ),
+ 'cdn epoch'
+ );
+ }
+
/**
* Tests screen requests, without either query parameter set
* @covers OutputPage::transformCssMedia
'UploadPath' => $uploadPath,
] );
- MediaWiki\suppressWarnings();
+ Wikimedia\suppressWarnings();
$actual = OutputPage::transformResourcePath( $conf, $path );
- MediaWiki\restoreWarnings();
+ Wikimedia\restoreWarnings();
$this->assertEquals( $expected ?: $path, $actual );
}
public static function provideMakeResourceLoaderLink() {
- // @codingStandardsIgnoreStart Generic.Files.LineLength
+ // phpcs:disable Generic.Files.LineLength
return [
// Single only=scripts load
[
[ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
- "<script>(window.RLQ=window.RLQ||[]).push(function(){"
+ "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
. 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
. "});</script>"
],
// Private embed (only=scripts)
[
[ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
- "<script>(window.RLQ=window.RLQ||[]).push(function(){"
+ "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
. "mw.test.baz({token:123});\nmw.loader.state({\"test.quux\":\"ready\"});"
. "});</script>"
],
+ // Load private module (combined)
+ [
+ [ 'test.quux', ResourceLoaderModule::TYPE_COMBINED ],
+ "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
+ . "mw.loader.implement(\"test.quux@1ev0ijv\",function($,jQuery,require,module){"
+ . "mw.test.baz({token:123});},{\"css\":[\".mw-icon{transition:none}"
+ . "\"]});});</script>"
+ ],
+ // Load no modules
+ [
+ [ [], ResourceLoaderModule::TYPE_COMBINED ],
+ '',
+ ],
+ // noscript group
+ [
+ [ 'test.noscript', ResourceLoaderModule::TYPE_STYLES ],
+ '<noscript><link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.noscript&only=styles&skin=fallback"/></noscript>'
+ ],
+ // Load two modules in separate groups
+ [
+ [ [ 'test.group.foo', 'test.group.bar' ], ResourceLoaderModule::TYPE_COMBINED ],
+ "<script nonce=\"secret\">(window.RLQ=window.RLQ||[]).push(function(){"
+ . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.bar\u0026skin=fallback");'
+ . 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.group.foo\u0026skin=fallback");'
+ . "});</script>"
+ ],
];
- // @codingStandardsIgnoreEnd
+ // phpcs:enable
}
/**
$this->setMwGlobals( [
'wgResourceLoaderDebug' => false,
'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
+ 'wgCSPReportOnlyHeader' => true,
] );
- $class = new ReflectionClass( 'OutputPage' );
+ $class = new ReflectionClass( OutputPage::class );
$method = $class->getMethod( 'makeResourceLoaderLink' );
$method->setAccessible( true );
$ctx = new RequestContext();
$ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
$ctx->setLanguage( 'en' );
$out = new OutputPage( $ctx );
+ $nonce = $class->getProperty( 'CSPNonce' );
+ $nonce->setAccessible( true );
+ $nonce->setValue( $out, 'secret' );
$rl = $out->getResourceLoader();
$rl->setMessageBlobStore( new NullMessageBlobStore() );
$rl->register( [
'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
'group' => 'private',
] ),
+ 'test.noscript' => new ResourceLoaderTestModule( [
+ 'styles' => '.stuff { color: red; }',
+ 'group' => 'noscript',
+ ] ),
+ 'test.group.foo' => new ResourceLoaderTestModule( [
+ 'script' => 'mw.doStuff( "foo" );',
+ 'group' => 'foo',
+ ] ),
+ 'test.group.bar' => new ResourceLoaderTestModule( [
+ 'script' => 'mw.doStuff( "bar" );',
+ 'group' => 'bar',
+ ] ),
] );
$links = $method->invokeArgs( $out, $args );
$actualHtml = strval( $links );
}
public static function provideBuildExemptModules() {
+ // phpcs:disable Generic.Files.LineLength
return [
'empty' => [
'exemptStyleModules' => [],
'exemptStyleModules' => [ 'site' => [], 'noscript' => [], 'private' => [], 'user' => [] ],
'<meta name="ResourceLoaderDynamicStyles" content=""/>',
],
- // @codingStandardsIgnoreStart Generic.Files.LineLength
'default logged-out' => [
'exemptStyleModules' => [ 'site' => [ 'site.styles' ] ],
'<meta name="ResourceLoaderDynamicStyles" content=""/>' . "\n" .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=example.user&only=styles&skin=fallback&version=0a56zyi"/>' . "\n" .
'<link rel="stylesheet" href="/w/load.php?debug=false&lang=en&modules=user.styles&only=styles&skin=fallback&version=1e9z0ox"/>',
],
- // @codingStandardsIgnoreEnd Generic.Files.LineLength
];
+ // phpcs:enable
}
/**
$ctx = new RequestContext();
$ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
$ctx->setLanguage( 'en' );
- $outputPage = $this->getMockBuilder( 'OutputPage' )
+ $outputPage = $this->getMockBuilder( OutputPage::class )
->setConstructorArgs( [ $ctx ] )
- ->setMethods( [ 'isUserCssPreview', 'buildCssLinksArray' ] )
+ ->setMethods( [ 'buildCssLinksArray' ] )
->getMock();
- $outputPage->expects( $this->any() )
- ->method( 'isUserCssPreview' )
- ->willReturn( false );
$outputPage->expects( $this->any() )
->method( 'buildCssLinksArray' )
->willReturn( [] );
*/
public function testVaryHeaders( $calls, $vary, $key ) {
// get rid of default Vary fields
- $outputPage = $this->getMockBuilder( 'OutputPage' )
+ $outputPage = $this->getMockBuilder( OutputPage::class )
->setConstructorArgs( [ new RequestContext() ] )
->setMethods( [ 'getCacheVaryCookies' ] )
->getMock();
$this->assertTrue( $outputPage->haveCacheVaryCookies() );
}
- /*
+ /**
* @covers OutputPage::addCategoryLinks
* @covers OutputPage::getCategories
*/
'page_title' => 'Test2'
]
] );
- $outputPage = $this->getMockBuilder( 'OutputPage' )
+ $outputPage = $this->getMockBuilder( OutputPage::class )
->setConstructorArgs( [ new RequestContext() ] )
->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
->getMock();
'not all and (min-resolution: 2dppx),' .
'</img/two-x.png>;rel=preload;as=image;media=(min-resolution: 2dppx)'
],
+ [
+ [
+ 'ResourceBasePath' => '/w',
+ 'Logo' => '/img/default.png',
+ 'LogoHD' => [
+ 'svg' => '/img/vector.svg',
+ ],
+ ],
+ 'Link: </img/vector.svg>;rel=preload;as=image'
+
+ ],
[
[
'ResourceBasePath' => '/w',
$context->setConfig( new HashConfig( $config + [
'AppleTouchIcon' => false,
'DisableLangConversion' => true,
- 'EnableAPI' => false,
'EnableCanonicalServerLink' => false,
'Favicon' => false,
'Feed' => false,