diff options
author | Timo Tijhof <krinklemail@gmail.com> | 2016-07-15 15:13:09 +0100 |
---|---|---|
committer | Timo Tijhof <krinklemail@gmail.com> | 2016-08-08 12:23:09 -0700 |
commit | 80e5b160e0985304a540a32002ff356aae886e8f (patch) | |
tree | 10cf2102ab45362f7c42f724dd5e5e022cabfd95 /tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php | |
parent | ce3a2f56216256aa22d709e810107c592655034b (diff) | |
download | mediawikicore-80e5b160e0985304a540a32002ff356aae886e8f.tar.gz mediawikicore-80e5b160e0985304a540a32002ff356aae886e8f.zip |
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
Diffstat (limited to 'tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php')
-rw-r--r-- | tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php | 278 |
1 files changed, 278 insertions, 0 deletions
diff --git a/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php new file mode 100644 index 000000000000..0965b9f9147a --- /dev/null +++ b/tests/phpunit/includes/resourceloader/ResourceLoaderClientHtmlTest.php @@ -0,0 +1,278 @@ +<?php + +/** + * @group ResourceLoader + */ +class ResourceLoaderClientHtmlTest extends PHPUnit_Framework_TestCase { + + protected static function makeContext( $extraQuery = [] ) { + $conf = new HashConfig( [ + 'ResourceLoaderSources' => [], + 'ResourceModuleSkinStyles' => [], + 'ResourceModules' => [], + 'EnableJavaScriptTest' => false, + 'ResourceLoaderDebug' => false, + 'LoadScript' => '/w/load.php', + ] ); + return new ResourceLoaderContext( + new ResourceLoader( $conf ), + new FauxRequest( array_merge( [ + 'lang' => 'nl', + 'skin' => 'fallback', + 'user' => 'Example', + 'target' => 'phpunit', + ], $extraQuery ) ) + ); + } + + protected static function makeModule( array $options = [] ) { + return new ResourceLoaderTestModule( $options ); + } + + protected static function makeSampleModules() { + $modules = [ + 'test' => [], + 'test.top' => [ 'position' => 'top' ], + 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ], + 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ], + + 'test.styles.pure' => [ 'type' => ResourceLoaderModule::LOAD_STYLES ], + 'test.styles.mixed' => [], + 'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule::LOAD_STYLES ], + 'test.styles.mixed.user' => [ 'group' => 'user' ], + 'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ], + 'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ], + + 'test.scripts' => [], + 'test.scripts.top' => [ 'position' => 'top' ], + 'test.scripts.mixed.user' => [ 'group' => 'user' ], + 'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ], + 'test.scripts.raw' => [ 'isRaw' => true ], + ]; + return array_map( function ( $options ) { + return self::makeModule( $options ); + }, $modules ); + } + + /** + * @covers ResourceLoaderClientHtml::getDocumentAttributes + */ + public function testGetDocumentAttributes() { + $client = new ResourceLoaderClientHtml( self::makeContext() ); + $this->assertInternalType( 'array', $client->getDocumentAttributes() ); + } + + /** + * @covers ResourceLoaderClientHtml::__construct + * @covers ResourceLoaderClientHtml::setModules + * @covers ResourceLoaderClientHtml::setModuleStyles + * @covers ResourceLoaderClientHtml::setModuleScripts + * @covers ResourceLoaderClientHtml::getData + * @covers ResourceLoaderClientHtml::getContext + */ + public function testGetData() { + $context = self::makeContext(); + $context->getResourceLoader()->register( self::makeSampleModules() ); + + $client = new ResourceLoaderClientHtml( $context ); + $client->setModules( [ + 'test', + 'test.private.bottom', + 'test.private.top', + 'test.top', + 'test.unregistered', + ] ); + $client->setModuleStyles( [ + 'test.styles.mixed', + 'test.styles.mixed.user.empty', + 'test.styles.private', + 'test.styles.pure', + 'test.unregistered.styles', + ] ); + $client->setModuleScripts( [ + 'test.scripts', + 'test.scripts.mixed.user.empty', + 'test.scripts.top', + 'test.unregistered.scripts', + ] ); + + $expected = [ + 'states' => [ + 'test.private.top' => 'loading', + 'test.private.bottom' => 'loading', + 'test.styles.pure' => 'ready', + 'test.styles.mixed.user.empty' => 'ready', + 'test.styles.private' => 'ready', + 'test.scripts' => 'loading', + 'test.scripts.top' => 'loading', + 'test.scripts.mixed.user.empty' => 'ready', + ], + 'general' => [ + 'top' => [ 'test.top' ], + 'bottom' => [ 'test' ], + ], + 'styles' => [ + 'test.styles.mixed', + 'test.styles.pure', + ], + 'scripts' => [ + 'top' => [ 'test.scripts.top' ], + 'bottom' => [ 'test.scripts' ], + ], + 'embed' => [ + 'styles' => [ 'test.styles.private' ], + 'general' => [ + 'top' => [ 'test.private.top' ], + 'bottom' => [ 'test.private.bottom' ], + ], + ], + ]; + + $access = TestingAccessWrapper::newFromObject( $client ); + $this->assertEquals( $expected, $access->getData() ); + } + + /** + * @covers ResourceLoaderClientHtml::setConfig + * @covers ResourceLoaderClientHtml::setExemptStates + * @covers ResourceLoaderClientHtml::getHeadHtml + * @covers ResourceLoaderClientHtml::getLoad + * @covers ResourceLoader::makeLoaderStateScript + */ + public function testGetHeadHtml() { + $context = self::makeContext(); + $context->getResourceLoader()->register( self::makeSampleModules() ); + + $client = new ResourceLoaderClientHtml( $context ); + $client->setConfig( [ 'key' => 'value' ] ); + $client->setModules( [ + 'test.top', + 'test.private.top', + ] ); + $client->setModuleStyles( [ + 'test.styles.pure', + 'test.styles.private', + ] ); + $client->setModuleScripts( [ + 'test.scripts.top', + ] ); + $client->setExemptStates( [ + 'test.exempt' => 'ready', + ] ); + + // @codingStandardsIgnoreStart Generic.Files.LineLength + $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n" + . '<script>(window.RLQ=window.RLQ||[]).push(function(){' + . 'mw.config.set({"key":"value"});' + . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});' + . 'mw.loader.implement("test.private.top",function($,jQuery,require,module){},{"css":[]});' + . 'mw.loader.load(["test.top"]);' + . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");' + . '});</script>' . "\n" + . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n" + . '<style>.private{}</style>' . "\n" + . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>'; + // @codingStandardsIgnoreEnd + + $this->assertEquals( $expected, $client->getHeadHtml() ); + } + + /** + * @covers ResourceLoaderClientHtml::getBodyHtml + * @covers ResourceLoaderClientHtml::getLoad + */ + public function testGetBodyHtml() { + $context = self::makeContext(); + $context->getResourceLoader()->register( self::makeSampleModules() ); + + $client = new ResourceLoaderClientHtml( $context ); + $client->setConfig( [ 'key' => 'value' ] ); + $client->setModules( [ + 'test', + 'test.private.bottom', + ] ); + $client->setModuleScripts( [ + 'test.scripts', + ] ); + + // @codingStandardsIgnoreStart Generic.Files.LineLength + $expected = '<script>(window.RLQ=window.RLQ||[]).push(function(){' + . 'mw.loader.implement("test.private.bottom",function($,jQuery,require,module){},{"css":[]});' + . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts\u0026only=scripts\u0026skin=fallback");' + . 'mw.loader.load(["test"]);' + . '});</script>'; + // @codingStandardsIgnoreEnd + + $this->assertEquals( $expected, $client->getBodyHtml() ); + } + + public static function provideMakeLoad() { + return [ + // @codingStandardsIgnoreStart Generic.Files.LineLength + [ + 'context' => [], + 'modules' => [ 'test.unknown' ], + 'only' => ResourceLoaderModule::TYPE_STYLES, + 'output' => '', + ], + [ + 'context' => [], + 'modules' => [ 'test.styles.private' ], + 'only' => ResourceLoaderModule::TYPE_STYLES, + 'output' => '<style>.private{}</style>', + ], + [ + 'context' => [], + 'modules' => [ 'test.private.top' ], + 'only' => ResourceLoaderModule::TYPE_COMBINED, + 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top",function($,jQuery,require,module){},{"css":[]});});</script>', + ], + [ + 'context' => [], + // Eg. startup module + 'modules' => [ 'test.scripts.raw' ], + 'only' => ResourceLoaderModule::TYPE_SCRIPTS, + 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>', + ], + [ + 'context' => [], + 'modules' => [ 'test.scripts.mixed.user' ], + 'only' => ResourceLoaderModule::TYPE_SCRIPTS, + 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.mixed.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>', + ], + [ + 'context' => [ 'debug' => true ], + 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ], + 'only' => ResourceLoaderModule::TYPE_STYLES, + 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n" + . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>', + ], + [ + 'context' => [], + 'modules' => [ 'test.styles.noscript' ], + 'only' => ResourceLoaderModule::TYPE_STYLES, + 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>', + ], + // @codingStandardsIgnoreEnd + ]; + } + + /** + * @dataProvider provideMakeLoad + * @covers ResourceLoaderClientHtml::makeLoad + * @covers ResourceLoaderClientHtml::makeContext + * @covers ResourceLoader::makeModuleResponse + * @covers ResourceLoaderModule::getModuleContent + * @covers ResourceLoader::getCombinedVersion + * @covers ResourceLoader::createLoaderURL + * @covers ResourceLoader::createLoaderQuery + * @covers ResourceLoader::makeLoaderQuery + * @covers ResourceLoader::makeInlineScript + */ + public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) { + $context = self::makeContext( $extraQuery ); + $context->getResourceLoader()->register( self::makeSampleModules() ); + $actual = ResourceLoaderClientHtml::makeLoad( $context, $modules, $type ); + $this->assertEquals( $expected, (string)$actual ); + } +} |