X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=includes%2Fspecials%2FSpecialJavaScriptTest.php;h=fbdefea43aba7b79c3829341df058f8204a53233;hp=4c73f1676e087382adde5ff4f0a712b17f788a5a;hb=59ebff658ce912c1b0e7ef8d8f9bfec5a4e17b39;hpb=012248fc5a7d6d0257df1a0feadbda5ae4a477e2 diff --git a/includes/specials/SpecialJavaScriptTest.php b/includes/specials/SpecialJavaScriptTest.php index 4c73f1676e..fbdefea43a 100644 --- a/includes/specials/SpecialJavaScriptTest.php +++ b/includes/specials/SpecialJavaScriptTest.php @@ -205,13 +205,27 @@ HTML; 'lang' => $this->getLanguage()->getCode(), 'skin' => $this->getSkin()->getSkinName(), 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false', + 'target' => 'test', ); $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) ); $query['only'] = 'scripts'; $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) ); + $query['raw'] = true; + $modules = $rl->getTestModuleNames( 'qunit' ); + // Disable autostart because we load modules asynchronously. By default, QUnit would start + // at domready when there are no tests loaded and also fire 'QUnit.done' which then instructs + // Karma to end the run before the tests even started. + $qunitConfig = 'QUnit.config.autostart = false;' + . 'if (window.__karma__) {' + // karma-qunit's use of autostart=false and QUnit.start conflicts with ours. + // Hack around this by replacing 'karma.loaded' with a no-op and call it ourselves later. + // See . + . 'window.__karma__.loaded = function () {};' + . '}'; + // The below is essentially a pure-javascript version of OutputPage::getHeadScripts. $startup = $rl->makeModuleResponse( $startupContext, array( 'startup' => $rl->getModule( 'startup' ), @@ -225,37 +239,42 @@ HTML; 'user.options' => $rl->getModule( 'user.options' ), 'user.tokens' => $rl->getModule( 'user.tokens' ), ) ); - $code .= Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ); + // Catch exceptions (such as "dependency missing" or "unknown module") so that we + // always start QUnit. Re-throw so that they are caught and reported as global exceptions + // by QUnit and Karma. + $code .= '(function () {' + . 'var start = window.__karma__ ? window.__karma__.start : QUnit.start;' + . 'try {' + . 'mw.loader.using( ' . Xml::encodeJsVar( $modules ) . ' ).always( start );' + . '} catch ( e ) { start(); throw e; }' + . '}());'; header( 'Content-Type: text/javascript; charset=utf-8' ); header( 'Cache-Control: private, no-cache, must-revalidate' ); header( 'Pragma: no-cache' ); + echo $qunitConfig; echo $startup; - echo "\n"; - // Note: The following has to be wrapped in a script tag because the startup module also - // writes a script tag (the one loading mediawiki.js). Script tags are synchronous, block - // each other, and run in order. But they don't nest. The code appended after the startup - // module runs before the added script tag is parsed and executed. - echo Xml::encodeJsCall( 'document.write', array( Html::inlineScript( $code ) ) ); + // The following has to be deferred via RLQ because the startup module is asynchronous. + echo ResourceLoader::makeLoaderConditionalScript( $code ); } private function plainQUnit() { $out = $this->getOutput(); $out->disable(); - $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array( - 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false', - ) ); - - $styles = $out->makeResourceLoaderLink( - 'jquery.qunit', ResourceLoaderModule::TYPE_STYLES, false + $styles = $out->makeResourceLoaderLink( 'jquery.qunit', + ResourceLoaderModule::TYPE_STYLES ); - // Use 'raw' since this is a plain HTML page without ResourceLoader - $scripts = $out->makeResourceLoaderLink( - 'jquery.qunit', ResourceLoaderModule::TYPE_SCRIPTS, false, array( 'raw' => 'true' ) + + // Use 'raw' because QUnit loads before ResourceLoader initialises (omit mw.loader.state call) + // Use 'test' to ensure OutputPage doesn't use the "async" attribute because QUnit must + // load before qunit/export. + $scripts = $out->makeResourceLoaderLink( 'jquery.qunit', + ResourceLoaderModule::TYPE_SCRIPTS, + array( 'raw' => true, 'sync' => true ) ); - $head = trim( $styles['html'] . $scripts['html'] ); + $head = implode( "\n", array_merge( $styles['html'], $scripts['html'] ) ); $summary = $this->getSummaryHtml(); $html = << @@ -264,6 +283,10 @@ $head $summary
HTML; + + $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array( + 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false', + ) ); $html .= "\n" . Html::linkedScript( $url ); header( 'Content-Type: text/html; charset=utf-8' );