2013-01-14 03:26:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Matthew Flaschen
|
|
|
|
|
*
|
|
|
|
|
* @group Output
|
|
|
|
|
*
|
2013-10-24 19:35:04 +00:00
|
|
|
* @todo factor tests in this class into providers and test methods
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
class OutputPageTest extends MediaWikiTestCase {
|
|
|
|
|
const SCREEN_MEDIA_QUERY = 'screen and (min-width: 982px)';
|
|
|
|
|
const SCREEN_ONLY_MEDIA_QUERY = 'only screen and (min-width: 982px)';
|
|
|
|
|
|
2017-01-24 17:30:33 +00:00
|
|
|
/**
|
|
|
|
|
* @covers OutputPage::addMeta
|
|
|
|
|
* @covers OutputPage::getMetaTags
|
|
|
|
|
* @covers OutputPage::getHeadLinksArray
|
|
|
|
|
*/
|
|
|
|
|
public function testMetaTags() {
|
|
|
|
|
$outputPage = $this->newInstance();
|
|
|
|
|
$outputPage->addMeta( 'http:expires', '0' );
|
|
|
|
|
$outputPage->addMeta( 'keywords', 'first' );
|
|
|
|
|
$outputPage->addMeta( 'keywords', 'second' );
|
|
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
|
[ 'http:expires', '0' ],
|
|
|
|
|
[ 'keywords', 'first' ],
|
|
|
|
|
[ 'keywords', 'second' ],
|
|
|
|
|
];
|
|
|
|
|
$this->assertSame( $expected, $outputPage->getMetaTags() );
|
|
|
|
|
|
|
|
|
|
$links = $outputPage->getHeadLinksArray();
|
|
|
|
|
$this->assertContains( '<meta http-equiv="expires" content="0"/>', $links );
|
|
|
|
|
$this->assertContains( '<meta name="keywords" content="first"/>', $links );
|
|
|
|
|
$this->assertContains( '<meta name="keywords" content="second"/>', $links );
|
|
|
|
|
$this->assertArrayNotHasKey( 'meta-robots', $links );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers OutputPage::setIndexPolicy
|
|
|
|
|
* @covers OutputPage::setFollowPolicy
|
|
|
|
|
* @covers OutputPage::getHeadLinksArray
|
|
|
|
|
*/
|
|
|
|
|
public function testRobotsPolicies() {
|
|
|
|
|
$outputPage = $this->newInstance();
|
|
|
|
|
$outputPage->setIndexPolicy( 'noindex' );
|
|
|
|
|
$outputPage->setFollowPolicy( 'nofollow' );
|
|
|
|
|
|
|
|
|
|
$links = $outputPage->getHeadLinksArray();
|
|
|
|
|
$this->assertContains( '<meta name="robots" content="noindex,nofollow"/>', $links );
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-14 03:26:15 +00:00
|
|
|
/**
|
|
|
|
|
* Tests a particular case of transformCssMedia, using the given input, globals,
|
|
|
|
|
* expected return, and message
|
|
|
|
|
*
|
|
|
|
|
* Asserts that $expectedReturn is returned.
|
|
|
|
|
*
|
|
|
|
|
* options['printableQuery'] - value of query string for printable, or omitted for none
|
|
|
|
|
* options['handheldQuery'] - value of query string for handheld, or omitted for none
|
|
|
|
|
* options['media'] - passed into the method under the same name
|
|
|
|
|
* options['expectedReturn'] - expected return value
|
|
|
|
|
* options['message'] - PHPUnit message for assertion
|
|
|
|
|
*
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param array $args Key-value array of arguments as shown above
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
2013-02-14 11:22:13 +00:00
|
|
|
protected function assertTransformCssMediaCase( $args ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$queryData = [];
|
2013-01-14 03:26:15 +00:00
|
|
|
if ( isset( $args['printableQuery'] ) ) {
|
|
|
|
|
$queryData['printable'] = $args['printableQuery'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $args['handheldQuery'] ) ) {
|
|
|
|
|
$queryData['handheld'] = $args['handheldQuery'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fauxRequest = new FauxRequest( $queryData, false );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2013-02-14 11:22:13 +00:00
|
|
|
'wgRequest' => $fauxRequest,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
|
|
|
|
$actualReturn = OutputPage::transformCssMedia( $args['media'] );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertSame( $args['expectedReturn'], $actualReturn, $args['message'] );
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests print requests
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testPrintRequests() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen returns null'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen media query returns null'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On printable request, screen media query with only returns null'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'printableQuery' => '1',
|
|
|
|
|
'media' => 'print',
|
|
|
|
|
'expectedReturn' => '',
|
|
|
|
|
'message' => 'On printable request, media print returns empty string'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests screen requests, without either query parameter set
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testScreenRequests() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => 'screen',
|
2013-05-11 19:05:43 +00:00
|
|
|
'message' => 'On screen request, screen media type is preserved'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-05-11 19:05:43 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-05-11 19:05:43 +00:00
|
|
|
'media' => 'handheld',
|
|
|
|
|
'expectedReturn' => 'handheld',
|
|
|
|
|
'message' => 'On screen request, handheld media type is preserved'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => self::SCREEN_MEDIA_QUERY,
|
|
|
|
|
'message' => 'On screen request, screen media query is preserved.'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'expectedReturn' => self::SCREEN_ONLY_MEDIA_QUERY,
|
|
|
|
|
'message' => 'On screen request, screen media query with only is preserved.'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'media' => 'print',
|
|
|
|
|
'expectedReturn' => 'print',
|
|
|
|
|
'message' => 'On screen request, print media type is preserved'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-05-11 19:05:43 +00:00
|
|
|
* Tests handheld behavior
|
2013-10-24 19:35:04 +00:00
|
|
|
* @covers OutputPage::transformCssMedia
|
2013-01-14 03:26:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testHandheld() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'handheldQuery' => '1',
|
|
|
|
|
'media' => 'handheld',
|
|
|
|
|
'expectedReturn' => '',
|
|
|
|
|
'message' => 'On request with handheld querystring and media is handheld, returns empty string'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTransformCssMediaCase( [
|
2013-01-14 03:26:15 +00:00
|
|
|
'handheldQuery' => '1',
|
|
|
|
|
'media' => 'screen',
|
|
|
|
|
'expectedReturn' => null,
|
|
|
|
|
'message' => 'On request with handheld querystring and media is screen, returns null'
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
2014-06-28 20:40:22 +00:00
|
|
|
|
2017-01-18 05:58:46 +00:00
|
|
|
public static function provideTransformFilePath() {
|
|
|
|
|
$baseDir = dirname( __DIR__ ) . '/data/media';
|
|
|
|
|
return [
|
|
|
|
|
// File that matches basePath, and exists. Hash found and appended.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/test.jpg', '/w/test.jpg?edcf2' ],
|
|
|
|
|
// File that matches basePath, but not found on disk. Empty query.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', '/w/unknown.png', '/w/unknown.png?' ],
|
|
|
|
|
// File not matching basePath. Ignored.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', '/files/test.jpg' ],
|
|
|
|
|
// Empty string. Ignored.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', '', '' ],
|
|
|
|
|
// Similar path, but with domain component. Ignored.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/w/test.jpg' ],
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/w/test.jpg' ],
|
|
|
|
|
// Unrelated path with domain component. Ignored.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', 'https://example.org/files/test.jpg' ],
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '/w', '//example.org/files/test.jpg' ],
|
2017-01-18 06:07:05 +00:00
|
|
|
// Unrelated path with domain, and empty base path (root mw install). Ignored.
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '', 'https://example.org/files/test.jpg' ],
|
|
|
|
|
[ 'baseDir' => $baseDir, 'basePath' => '', '//example.org/files/test.jpg' ], // T155310
|
2017-01-18 05:58:46 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTransformFilePath
|
|
|
|
|
* @covers OutputPage::transformFilePath
|
|
|
|
|
* @covers OutputPage::transformResourcePath
|
|
|
|
|
*/
|
|
|
|
|
public function testTransformResourcePath( $baseDir, $basePath, $path, $expected = null ) {
|
|
|
|
|
$this->setMwGlobals( 'IP', $baseDir );
|
|
|
|
|
$conf = new HashConfig( [ 'ResourceBasePath' => $basePath ] );
|
|
|
|
|
|
|
|
|
|
MediaWiki\suppressWarnings();
|
|
|
|
|
$actual = OutputPage::transformResourcePath( $conf, $path );
|
|
|
|
|
MediaWiki\restoreWarnings();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected ?: $path, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-28 20:40:22 +00:00
|
|
|
public static function provideMakeResourceLoaderLink() {
|
2015-10-01 20:13:34 +00:00
|
|
|
// @codingStandardsIgnoreStart Generic.Files.LineLength
|
2016-03-19 01:05:19 +00:00
|
|
|
return [
|
resourceloader: Move queue formatting out of OutputPage
HTML formatting of the queue was distributed over several OutputPage methods.
Each method demanding a snippet of HTML by calling makeResourceLoaderLink()
with a limited amount of information. As such, makeResourceLoaderLink() was
unable to provide the client with the proper state information.
Centralising it also allows it to better reduce duplication in HTML output
and maintain a more accurate state.
Problems fixed by centralising:
1. The 'user' module is special (due to per-user 'version' and 'user' params).
It is manually requested via script-src. To avoid a separate (and wrong)
request from something that requires it, we set state=loading directly.
However, because the module is in the bottom, the old HTML formatter could
only put state=loading in the bottom also. This sometimes caused a wrong
request to be fired for modules=user if something in the top queue
triggered a requirement for it.
2. Since a464d1d4 (T87871) we track states of page-style modules, with purpose
of allowing dependencies on style modules without risking duplicate loading
on pages where the styles are loaded already. This didn't work, because the
state information about page-style modules is output near the stylesheet,
which is after the script tag with mw.loader.load(). That runs first, and
mw.loader would still make a duplicate request before it learns the state.
Changes:
* Document reasons for style/script tag order in getHeadHtml (per 09537e83).
* Pass $type from getModuleStyles() to getAllowedModules(). This wasn't needed
before since a duplicate check in makeResourceLoaderLink() verified the
origin a second time.
* Declare explicit position 'top' on 'user.options' and 'user.tokens' module.
Previously, OutputPage hardcoded them in the top. The new formatter doesn't.
* Remove getHeadScripts().
* Remove getInlineHeadScripts().
* Remove getExternalHeadScripts().
* Remove buildCssLinks().
* Remove getScriptsForBottomQueue().
* Change where Skin::setupSkinUserCss() is called. This methods lets the skin
add modules to the queue. Previously it was called from buildCssLinks(),
via headElement(), via prepareQuickTemplate(), via OutputPage::output().
It's now in OutputPage::output() directly (slightly earlier). This is needed
because prepareQuickTemplate() calls bottomScripts() before headElement().
And bottomScript() would lazy-initialise the queue and lock it before
setupSkinUserCss() is called from headElement().
This makes execution order more predictable instead of being dependent on
the arbitrary order of data extraction in prepareQuickTemplate (which varies
from one skin to another).
* Compute isUserModulePreview() and isKnownEmpty() for the 'user' module early
on so. This avoids wrongful loading and fixes problem 1.
Effective changes in output:
* mw.loader.state() is now before mw.loader.load(). This fixes problem 2.
* mw.loader.state() now sets 'user.options' and 'user.tokens' to "loading".
* mw.loader.state() now sets 'user' (as "loading" or "ready"). Fixes problem 1.
* The <script async src> tag for 'startup' changed position (slightly).
Previously it was after all inline scripts and stylesheets. It's still after
all inline scripts and after most stylesheets, but before any user styles.
Since the queue is now formatted outside OutputPage, it can't inject the
meta-ResourceLoaderDynamicStyles tag and user-stylesheet hack in the middle
of existing output. This shouldn't have any noticable impact.
Bug: T87871
Change-Id: I605b8cd1e1fc009b4662a0edbc54d09dd65ee1df
2016-07-15 14:13:09 +00:00
|
|
|
// Single only=scripts load
|
2016-03-19 01:05:19 +00:00
|
|
|
[
|
|
|
|
|
[ 'test.foo', ResourceLoaderModule::TYPE_SCRIPTS ],
|
2016-02-18 16:33:15 +00:00
|
|
|
"<script>(window.RLQ=window.RLQ||[]).push(function(){"
|
2015-08-28 12:00:58 +00:00
|
|
|
. 'mw.loader.load("http://127.0.0.1:8080/w/load.php?debug=false\u0026lang=en\u0026modules=test.foo\u0026only=scripts\u0026skin=fallback");'
|
2016-02-18 16:33:15 +00:00
|
|
|
. "});</script>"
|
2016-03-19 01:05:19 +00:00
|
|
|
],
|
resourceloader: Move queue formatting out of OutputPage
HTML formatting of the queue was distributed over several OutputPage methods.
Each method demanding a snippet of HTML by calling makeResourceLoaderLink()
with a limited amount of information. As such, makeResourceLoaderLink() was
unable to provide the client with the proper state information.
Centralising it also allows it to better reduce duplication in HTML output
and maintain a more accurate state.
Problems fixed by centralising:
1. The 'user' module is special (due to per-user 'version' and 'user' params).
It is manually requested via script-src. To avoid a separate (and wrong)
request from something that requires it, we set state=loading directly.
However, because the module is in the bottom, the old HTML formatter could
only put state=loading in the bottom also. This sometimes caused a wrong
request to be fired for modules=user if something in the top queue
triggered a requirement for it.
2. Since a464d1d4 (T87871) we track states of page-style modules, with purpose
of allowing dependencies on style modules without risking duplicate loading
on pages where the styles are loaded already. This didn't work, because the
state information about page-style modules is output near the stylesheet,
which is after the script tag with mw.loader.load(). That runs first, and
mw.loader would still make a duplicate request before it learns the state.
Changes:
* Document reasons for style/script tag order in getHeadHtml (per 09537e83).
* Pass $type from getModuleStyles() to getAllowedModules(). This wasn't needed
before since a duplicate check in makeResourceLoaderLink() verified the
origin a second time.
* Declare explicit position 'top' on 'user.options' and 'user.tokens' module.
Previously, OutputPage hardcoded them in the top. The new formatter doesn't.
* Remove getHeadScripts().
* Remove getInlineHeadScripts().
* Remove getExternalHeadScripts().
* Remove buildCssLinks().
* Remove getScriptsForBottomQueue().
* Change where Skin::setupSkinUserCss() is called. This methods lets the skin
add modules to the queue. Previously it was called from buildCssLinks(),
via headElement(), via prepareQuickTemplate(), via OutputPage::output().
It's now in OutputPage::output() directly (slightly earlier). This is needed
because prepareQuickTemplate() calls bottomScripts() before headElement().
And bottomScript() would lazy-initialise the queue and lock it before
setupSkinUserCss() is called from headElement().
This makes execution order more predictable instead of being dependent on
the arbitrary order of data extraction in prepareQuickTemplate (which varies
from one skin to another).
* Compute isUserModulePreview() and isKnownEmpty() for the 'user' module early
on so. This avoids wrongful loading and fixes problem 1.
Effective changes in output:
* mw.loader.state() is now before mw.loader.load(). This fixes problem 2.
* mw.loader.state() now sets 'user.options' and 'user.tokens' to "loading".
* mw.loader.state() now sets 'user' (as "loading" or "ready"). Fixes problem 1.
* The <script async src> tag for 'startup' changed position (slightly).
Previously it was after all inline scripts and stylesheets. It's still after
all inline scripts and after most stylesheets, but before any user styles.
Since the queue is now formatted outside OutputPage, it can't inject the
meta-ResourceLoaderDynamicStyles tag and user-stylesheet hack in the middle
of existing output. This shouldn't have any noticable impact.
Bug: T87871
Change-Id: I605b8cd1e1fc009b4662a0edbc54d09dd65ee1df
2016-07-15 14:13:09 +00:00
|
|
|
// Multiple only=styles load
|
2016-03-19 01:05:19 +00:00
|
|
|
[
|
|
|
|
|
[ [ 'test.baz', 'test.foo', 'test.bar' ], ResourceLoaderModule::TYPE_STYLES ],
|
2015-07-27 23:40:52 +00:00
|
|
|
|
2016-04-20 17:22:51 +00:00
|
|
|
'<link rel="stylesheet" href="http://127.0.0.1:8080/w/load.php?debug=false&lang=en&modules=test.bar%2Cbaz%2Cfoo&only=styles&skin=fallback"/>'
|
2016-03-19 01:05:19 +00:00
|
|
|
],
|
resourceloader: Move queue formatting out of OutputPage
HTML formatting of the queue was distributed over several OutputPage methods.
Each method demanding a snippet of HTML by calling makeResourceLoaderLink()
with a limited amount of information. As such, makeResourceLoaderLink() was
unable to provide the client with the proper state information.
Centralising it also allows it to better reduce duplication in HTML output
and maintain a more accurate state.
Problems fixed by centralising:
1. The 'user' module is special (due to per-user 'version' and 'user' params).
It is manually requested via script-src. To avoid a separate (and wrong)
request from something that requires it, we set state=loading directly.
However, because the module is in the bottom, the old HTML formatter could
only put state=loading in the bottom also. This sometimes caused a wrong
request to be fired for modules=user if something in the top queue
triggered a requirement for it.
2. Since a464d1d4 (T87871) we track states of page-style modules, with purpose
of allowing dependencies on style modules without risking duplicate loading
on pages where the styles are loaded already. This didn't work, because the
state information about page-style modules is output near the stylesheet,
which is after the script tag with mw.loader.load(). That runs first, and
mw.loader would still make a duplicate request before it learns the state.
Changes:
* Document reasons for style/script tag order in getHeadHtml (per 09537e83).
* Pass $type from getModuleStyles() to getAllowedModules(). This wasn't needed
before since a duplicate check in makeResourceLoaderLink() verified the
origin a second time.
* Declare explicit position 'top' on 'user.options' and 'user.tokens' module.
Previously, OutputPage hardcoded them in the top. The new formatter doesn't.
* Remove getHeadScripts().
* Remove getInlineHeadScripts().
* Remove getExternalHeadScripts().
* Remove buildCssLinks().
* Remove getScriptsForBottomQueue().
* Change where Skin::setupSkinUserCss() is called. This methods lets the skin
add modules to the queue. Previously it was called from buildCssLinks(),
via headElement(), via prepareQuickTemplate(), via OutputPage::output().
It's now in OutputPage::output() directly (slightly earlier). This is needed
because prepareQuickTemplate() calls bottomScripts() before headElement().
And bottomScript() would lazy-initialise the queue and lock it before
setupSkinUserCss() is called from headElement().
This makes execution order more predictable instead of being dependent on
the arbitrary order of data extraction in prepareQuickTemplate (which varies
from one skin to another).
* Compute isUserModulePreview() and isKnownEmpty() for the 'user' module early
on so. This avoids wrongful loading and fixes problem 1.
Effective changes in output:
* mw.loader.state() is now before mw.loader.load(). This fixes problem 2.
* mw.loader.state() now sets 'user.options' and 'user.tokens' to "loading".
* mw.loader.state() now sets 'user' (as "loading" or "ready"). Fixes problem 1.
* The <script async src> tag for 'startup' changed position (slightly).
Previously it was after all inline scripts and stylesheets. It's still after
all inline scripts and after most stylesheets, but before any user styles.
Since the queue is now formatted outside OutputPage, it can't inject the
meta-ResourceLoaderDynamicStyles tag and user-stylesheet hack in the middle
of existing output. This shouldn't have any noticable impact.
Bug: T87871
Change-Id: I605b8cd1e1fc009b4662a0edbc54d09dd65ee1df
2016-07-15 14:13:09 +00:00
|
|
|
// Private embed (only=scripts)
|
2016-03-19 01:05:19 +00:00
|
|
|
[
|
|
|
|
|
[ 'test.quux', ResourceLoaderModule::TYPE_SCRIPTS ],
|
2016-02-18 16:33:15 +00:00
|
|
|
"<script>(window.RLQ=window.RLQ||[]).push(function(){"
|
|
|
|
|
. "mw.test.baz({token:123});mw.loader.state({\"test.quux\":\"ready\"});"
|
|
|
|
|
. "});</script>"
|
2016-03-19 01:05:19 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-10-01 20:13:34 +00:00
|
|
|
// @codingStandardsIgnoreEnd
|
2014-06-28 20:40:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
resourceloader: Move queue formatting out of OutputPage
HTML formatting of the queue was distributed over several OutputPage methods.
Each method demanding a snippet of HTML by calling makeResourceLoaderLink()
with a limited amount of information. As such, makeResourceLoaderLink() was
unable to provide the client with the proper state information.
Centralising it also allows it to better reduce duplication in HTML output
and maintain a more accurate state.
Problems fixed by centralising:
1. The 'user' module is special (due to per-user 'version' and 'user' params).
It is manually requested via script-src. To avoid a separate (and wrong)
request from something that requires it, we set state=loading directly.
However, because the module is in the bottom, the old HTML formatter could
only put state=loading in the bottom also. This sometimes caused a wrong
request to be fired for modules=user if something in the top queue
triggered a requirement for it.
2. Since a464d1d4 (T87871) we track states of page-style modules, with purpose
of allowing dependencies on style modules without risking duplicate loading
on pages where the styles are loaded already. This didn't work, because the
state information about page-style modules is output near the stylesheet,
which is after the script tag with mw.loader.load(). That runs first, and
mw.loader would still make a duplicate request before it learns the state.
Changes:
* Document reasons for style/script tag order in getHeadHtml (per 09537e83).
* Pass $type from getModuleStyles() to getAllowedModules(). This wasn't needed
before since a duplicate check in makeResourceLoaderLink() verified the
origin a second time.
* Declare explicit position 'top' on 'user.options' and 'user.tokens' module.
Previously, OutputPage hardcoded them in the top. The new formatter doesn't.
* Remove getHeadScripts().
* Remove getInlineHeadScripts().
* Remove getExternalHeadScripts().
* Remove buildCssLinks().
* Remove getScriptsForBottomQueue().
* Change where Skin::setupSkinUserCss() is called. This methods lets the skin
add modules to the queue. Previously it was called from buildCssLinks(),
via headElement(), via prepareQuickTemplate(), via OutputPage::output().
It's now in OutputPage::output() directly (slightly earlier). This is needed
because prepareQuickTemplate() calls bottomScripts() before headElement().
And bottomScript() would lazy-initialise the queue and lock it before
setupSkinUserCss() is called from headElement().
This makes execution order more predictable instead of being dependent on
the arbitrary order of data extraction in prepareQuickTemplate (which varies
from one skin to another).
* Compute isUserModulePreview() and isKnownEmpty() for the 'user' module early
on so. This avoids wrongful loading and fixes problem 1.
Effective changes in output:
* mw.loader.state() is now before mw.loader.load(). This fixes problem 2.
* mw.loader.state() now sets 'user.options' and 'user.tokens' to "loading".
* mw.loader.state() now sets 'user' (as "loading" or "ready"). Fixes problem 1.
* The <script async src> tag for 'startup' changed position (slightly).
Previously it was after all inline scripts and stylesheets. It's still after
all inline scripts and after most stylesheets, but before any user styles.
Since the queue is now formatted outside OutputPage, it can't inject the
meta-ResourceLoaderDynamicStyles tag and user-stylesheet hack in the middle
of existing output. This shouldn't have any noticable impact.
Bug: T87871
Change-Id: I605b8cd1e1fc009b4662a0edbc54d09dd65ee1df
2016-07-15 14:13:09 +00:00
|
|
|
* See ResourceLoaderClientHtmlTest for full coverage.
|
|
|
|
|
*
|
2014-06-28 20:40:22 +00:00
|
|
|
* @dataProvider provideMakeResourceLoaderLink
|
|
|
|
|
* @covers OutputPage::makeResourceLoaderLink
|
|
|
|
|
*/
|
2014-07-19 21:12:10 +00:00
|
|
|
public function testMakeResourceLoaderLink( $args, $expectedHtml ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
2014-07-11 14:35:14 +00:00
|
|
|
'wgResourceLoaderDebug' => false,
|
2014-06-28 20:40:22 +00:00
|
|
|
'wgLoadScript' => 'http://127.0.0.1:8080/w/load.php',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-06-28 20:40:22 +00:00
|
|
|
$class = new ReflectionClass( 'OutputPage' );
|
|
|
|
|
$method = $class->getMethod( 'makeResourceLoaderLink' );
|
|
|
|
|
$method->setAccessible( true );
|
|
|
|
|
$ctx = new RequestContext();
|
2014-08-09 12:36:35 +00:00
|
|
|
$ctx->setSkin( SkinFactory::getDefaultInstance()->makeSkin( 'fallback' ) );
|
2014-07-11 14:35:14 +00:00
|
|
|
$ctx->setLanguage( 'en' );
|
2014-06-28 20:40:22 +00:00
|
|
|
$out = new OutputPage( $ctx );
|
|
|
|
|
$rl = $out->getResourceLoader();
|
2015-03-29 04:24:31 +00:00
|
|
|
$rl->setMessageBlobStore( new NullMessageBlobStore() );
|
2016-02-17 09:09:32 +00:00
|
|
|
$rl->register( [
|
|
|
|
|
'test.foo' => new ResourceLoaderTestModule( [
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.foo( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-foo { content: "style"; }',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
|
|
|
|
'test.bar' => new ResourceLoaderTestModule( [
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.bar( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-bar { content: "style"; }',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
|
|
|
|
'test.baz' => new ResourceLoaderTestModule( [
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.baz( { a: true } );',
|
|
|
|
|
'styles' => '.mw-test-baz { content: "style"; }',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
|
|
|
|
'test.quux' => new ResourceLoaderTestModule( [
|
2014-06-28 20:40:22 +00:00
|
|
|
'script' => 'mw.test.baz( { token: 123 } );',
|
|
|
|
|
'styles' => '/* pref-animate=off */ .mw-icon { transition: none; }',
|
|
|
|
|
'group' => 'private',
|
2016-02-17 09:09:32 +00:00
|
|
|
] ),
|
|
|
|
|
] );
|
2014-06-28 20:40:22 +00:00
|
|
|
$links = $method->invokeArgs( $out, $args );
|
resourceloader: Move queue formatting out of OutputPage
HTML formatting of the queue was distributed over several OutputPage methods.
Each method demanding a snippet of HTML by calling makeResourceLoaderLink()
with a limited amount of information. As such, makeResourceLoaderLink() was
unable to provide the client with the proper state information.
Centralising it also allows it to better reduce duplication in HTML output
and maintain a more accurate state.
Problems fixed by centralising:
1. The 'user' module is special (due to per-user 'version' and 'user' params).
It is manually requested via script-src. To avoid a separate (and wrong)
request from something that requires it, we set state=loading directly.
However, because the module is in the bottom, the old HTML formatter could
only put state=loading in the bottom also. This sometimes caused a wrong
request to be fired for modules=user if something in the top queue
triggered a requirement for it.
2. Since a464d1d4 (T87871) we track states of page-style modules, with purpose
of allowing dependencies on style modules without risking duplicate loading
on pages where the styles are loaded already. This didn't work, because the
state information about page-style modules is output near the stylesheet,
which is after the script tag with mw.loader.load(). That runs first, and
mw.loader would still make a duplicate request before it learns the state.
Changes:
* Document reasons for style/script tag order in getHeadHtml (per 09537e83).
* Pass $type from getModuleStyles() to getAllowedModules(). This wasn't needed
before since a duplicate check in makeResourceLoaderLink() verified the
origin a second time.
* Declare explicit position 'top' on 'user.options' and 'user.tokens' module.
Previously, OutputPage hardcoded them in the top. The new formatter doesn't.
* Remove getHeadScripts().
* Remove getInlineHeadScripts().
* Remove getExternalHeadScripts().
* Remove buildCssLinks().
* Remove getScriptsForBottomQueue().
* Change where Skin::setupSkinUserCss() is called. This methods lets the skin
add modules to the queue. Previously it was called from buildCssLinks(),
via headElement(), via prepareQuickTemplate(), via OutputPage::output().
It's now in OutputPage::output() directly (slightly earlier). This is needed
because prepareQuickTemplate() calls bottomScripts() before headElement().
And bottomScript() would lazy-initialise the queue and lock it before
setupSkinUserCss() is called from headElement().
This makes execution order more predictable instead of being dependent on
the arbitrary order of data extraction in prepareQuickTemplate (which varies
from one skin to another).
* Compute isUserModulePreview() and isKnownEmpty() for the 'user' module early
on so. This avoids wrongful loading and fixes problem 1.
Effective changes in output:
* mw.loader.state() is now before mw.loader.load(). This fixes problem 2.
* mw.loader.state() now sets 'user.options' and 'user.tokens' to "loading".
* mw.loader.state() now sets 'user' (as "loading" or "ready"). Fixes problem 1.
* The <script async src> tag for 'startup' changed position (slightly).
Previously it was after all inline scripts and stylesheets. It's still after
all inline scripts and after most stylesheets, but before any user styles.
Since the queue is now formatted outside OutputPage, it can't inject the
meta-ResourceLoaderDynamicStyles tag and user-stylesheet hack in the middle
of existing output. This shouldn't have any noticable impact.
Bug: T87871
Change-Id: I605b8cd1e1fc009b4662a0edbc54d09dd65ee1df
2016-07-15 14:13:09 +00:00
|
|
|
$actualHtml = strval( $links );
|
2014-08-06 15:54:22 +00:00
|
|
|
$this->assertEquals( $expectedHtml, $actualHtml );
|
2014-06-28 20:40:22 +00:00
|
|
|
}
|
2015-09-08 21:59:45 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideVaryHeaders
|
|
|
|
|
* @covers OutputPage::addVaryHeader
|
|
|
|
|
* @covers OutputPage::getVaryHeader
|
2015-10-06 07:01:10 +00:00
|
|
|
* @covers OutputPage::getKeyHeader
|
2015-09-08 21:59:45 +00:00
|
|
|
*/
|
2015-10-06 07:01:10 +00:00
|
|
|
public function testVaryHeaders( $calls, $vary, $key ) {
|
2015-09-08 21:59:45 +00:00
|
|
|
// get rid of default Vary fields
|
|
|
|
|
$outputPage = $this->getMockBuilder( 'OutputPage' )
|
2016-02-17 09:09:32 +00:00
|
|
|
->setConstructorArgs( [ new RequestContext() ] )
|
|
|
|
|
->setMethods( [ 'getCacheVaryCookies' ] )
|
2015-09-08 21:59:45 +00:00
|
|
|
->getMock();
|
|
|
|
|
$outputPage->expects( $this->any() )
|
|
|
|
|
->method( 'getCacheVaryCookies' )
|
2016-02-17 09:09:32 +00:00
|
|
|
->will( $this->returnValue( [] ) );
|
|
|
|
|
TestingAccessWrapper::newFromObject( $outputPage )->mVaryHeader = [];
|
2015-09-08 21:59:45 +00:00
|
|
|
|
|
|
|
|
foreach ( $calls as $call ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
call_user_func_array( [ $outputPage, 'addVaryHeader' ], $call );
|
2015-09-08 21:59:45 +00:00
|
|
|
}
|
|
|
|
|
$this->assertEquals( $vary, $outputPage->getVaryHeader(), 'Vary:' );
|
2015-10-06 07:01:10 +00:00
|
|
|
$this->assertEquals( $key, $outputPage->getKeyHeader(), 'Key:' );
|
2015-09-08 21:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideVaryHeaders() {
|
2015-10-06 07:01:10 +00:00
|
|
|
// note: getKeyHeader() automatically adds Vary: Cookie
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ // single header
|
|
|
|
|
[
|
|
|
|
|
[ 'Cookie' ],
|
|
|
|
|
],
|
2015-09-08 21:59:45 +00:00
|
|
|
'Vary: Cookie',
|
2015-10-06 07:01:10 +00:00
|
|
|
'Key: Cookie',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ // non-unique headers
|
|
|
|
|
[
|
|
|
|
|
[ 'Cookie' ],
|
|
|
|
|
[ 'Accept-Language' ],
|
|
|
|
|
[ 'Cookie' ],
|
|
|
|
|
],
|
2015-09-08 21:59:45 +00:00
|
|
|
'Vary: Cookie, Accept-Language',
|
2015-10-06 07:01:10 +00:00
|
|
|
'Key: Cookie,Accept-Language',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ // two headers with single options
|
|
|
|
|
[
|
|
|
|
|
[ 'Cookie', [ 'param=phpsessid' ] ],
|
|
|
|
|
[ 'Accept-Language', [ 'substr=en' ] ],
|
|
|
|
|
],
|
2015-09-08 21:59:45 +00:00
|
|
|
'Vary: Cookie, Accept-Language',
|
2015-10-06 07:01:10 +00:00
|
|
|
'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ // one header with multiple options
|
|
|
|
|
[
|
|
|
|
|
[ 'Cookie', [ 'param=phpsessid', 'param=userId' ] ],
|
|
|
|
|
],
|
2015-09-08 21:59:45 +00:00
|
|
|
'Vary: Cookie',
|
2015-10-06 07:01:10 +00:00
|
|
|
'Key: Cookie;param=phpsessid;param=userId',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ // Duplicate option
|
|
|
|
|
[
|
|
|
|
|
[ 'Cookie', [ 'param=phpsessid' ] ],
|
|
|
|
|
[ 'Cookie', [ 'param=phpsessid' ] ],
|
|
|
|
|
[ 'Accept-Language', [ 'substr=en', 'substr=en' ] ],
|
|
|
|
|
],
|
2015-09-08 21:59:45 +00:00
|
|
|
'Vary: Cookie, Accept-Language',
|
2015-10-06 07:01:10 +00:00
|
|
|
'Key: Cookie;param=phpsessid,Accept-Language;substr=en',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ // Same header, different options
|
|
|
|
|
[
|
|
|
|
|
[ 'Cookie', [ 'param=phpsessid' ] ],
|
|
|
|
|
[ 'Cookie', [ 'param=userId' ] ],
|
|
|
|
|
],
|
2015-09-08 21:59:45 +00:00
|
|
|
'Vary: Cookie',
|
2015-10-06 07:01:10 +00:00
|
|
|
'Key: Cookie;param=phpsessid;param=userId',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2015-09-08 21:59:45 +00:00
|
|
|
}
|
2015-10-08 04:45:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers OutputPage::haveCacheVaryCookies
|
|
|
|
|
*/
|
2017-01-18 05:58:46 +00:00
|
|
|
public function testHaveCacheVaryCookies() {
|
2015-10-08 04:45:26 +00:00
|
|
|
$request = new FauxRequest();
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
$context->setRequest( $request );
|
|
|
|
|
$outputPage = new OutputPage( $context );
|
|
|
|
|
|
|
|
|
|
// No cookies are set.
|
|
|
|
|
$this->assertFalse( $outputPage->haveCacheVaryCookies() );
|
|
|
|
|
|
|
|
|
|
// 'Token' is present but empty, so it shouldn't count.
|
|
|
|
|
$request->setCookie( 'Token', '' );
|
|
|
|
|
$this->assertFalse( $outputPage->haveCacheVaryCookies() );
|
|
|
|
|
|
|
|
|
|
// 'Token' present and nonempty.
|
|
|
|
|
$request->setCookie( 'Token', '123' );
|
|
|
|
|
$this->assertTrue( $outputPage->haveCacheVaryCookies() );
|
|
|
|
|
}
|
2016-07-08 19:11:53 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @covers OutputPage::addCategoryLinks
|
|
|
|
|
* @covers OutputPage::getCategories
|
|
|
|
|
*/
|
2017-01-18 05:58:46 +00:00
|
|
|
public function testGetCategories() {
|
2016-07-08 19:11:53 +00:00
|
|
|
$fakeResultWrapper = new FakeResultWrapper( [
|
|
|
|
|
(object) [
|
|
|
|
|
'pp_value' => 1,
|
|
|
|
|
'page_title' => 'Test'
|
|
|
|
|
],
|
|
|
|
|
(object) [
|
|
|
|
|
'page_title' => 'Test2'
|
|
|
|
|
]
|
|
|
|
|
] );
|
|
|
|
|
$outputPage = $this->getMockBuilder( 'OutputPage' )
|
|
|
|
|
->setConstructorArgs( [ new RequestContext() ] )
|
|
|
|
|
->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$outputPage->expects( $this->any() )
|
|
|
|
|
->method( 'addCategoryLinksToLBAndGetResult' )
|
|
|
|
|
->will( $this->returnValue( $fakeResultWrapper ) );
|
|
|
|
|
|
|
|
|
|
$outputPage->addCategoryLinks( [
|
|
|
|
|
'Test' => 'Test',
|
|
|
|
|
'Test2' => 'Test2',
|
|
|
|
|
] );
|
|
|
|
|
$this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
|
|
|
|
|
$this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
|
|
|
|
|
$this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
|
|
|
|
|
}
|
2017-01-24 17:30:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return OutputPage
|
|
|
|
|
*/
|
|
|
|
|
private function newInstance() {
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
|
|
|
|
|
$context->setConfig( new HashConfig( [
|
|
|
|
|
'AppleTouchIcon' => false,
|
|
|
|
|
'DisableLangConversion' => true,
|
|
|
|
|
'EnableAPI' => false,
|
|
|
|
|
'EnableCanonicalServerLink' => false,
|
|
|
|
|
'Favicon' => false,
|
|
|
|
|
'Feed' => false,
|
|
|
|
|
'LanguageCode' => false,
|
|
|
|
|
'ReferrerPolicy' => false,
|
|
|
|
|
'RightsPage' => false,
|
|
|
|
|
'RightsUrl' => false,
|
|
|
|
|
'UniversalEditButton' => false,
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
return new OutputPage( $context );
|
|
|
|
|
}
|
2013-01-14 03:26:15 +00:00
|
|
|
}
|
2015-03-29 04:24:31 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MessageBlobStore that doesn't do anything
|
|
|
|
|
*/
|
|
|
|
|
class NullMessageBlobStore extends MessageBlobStore {
|
2015-06-16 19:06:19 +00:00
|
|
|
public function get( ResourceLoader $resourceLoader, $modules, $lang ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2015-03-29 04:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function updateModule( $name, ResourceLoaderModule $module, $lang ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function updateMessage( $key ) {
|
2015-03-29 04:24:31 +00:00
|
|
|
}
|
2015-09-26 15:00:56 +00:00
|
|
|
|
2015-03-29 04:24:31 +00:00
|
|
|
public function clear() {
|
|
|
|
|
}
|
|
|
|
|
}
|