2012-01-03 18:33:26 +00:00
|
|
|
<?php
|
2012-02-09 09:34:47 +00:00
|
|
|
/**
|
2012-04-28 18:41:55 +00:00
|
|
|
* Implements Special:JavaScriptTest
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-02-09 09:34:47 +00:00
|
|
|
* @file
|
2012-04-28 18:41:55 +00:00
|
|
|
* @ingroup SpecialPage
|
2012-02-09 09:34:47 +00:00
|
|
|
*/
|
2012-01-03 18:33:26 +00:00
|
|
|
|
2012-02-09 09:34:47 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup SpecialPage
|
|
|
|
|
*/
|
2012-01-03 18:33:26 +00:00
|
|
|
class SpecialJavaScriptTest extends SpecialPage {
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct( 'JavaScriptTest' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute( $par ) {
|
|
|
|
|
$out = $this->getOutput();
|
|
|
|
|
|
|
|
|
|
$this->setHeaders();
|
|
|
|
|
$out->disallowUserJs();
|
|
|
|
|
|
2014-12-02 21:48:21 +00:00
|
|
|
// This special page is disabled by default ($wgEnableJavaScriptTest), and contains
|
|
|
|
|
// no sensitive data. In order to allow TestSwarm to embed it into a test client window,
|
|
|
|
|
// we need to allow iframing of this page.
|
|
|
|
|
$out->allowClickjacking();
|
|
|
|
|
|
2016-03-31 20:18:14 +00:00
|
|
|
// Sub resource: Internal JavaScript export bundle for QUnit
|
|
|
|
|
if ( $par === 'qunit/export' ) {
|
|
|
|
|
$this->exportQUnit();
|
2014-12-02 21:48:21 +00:00
|
|
|
return;
|
2012-01-03 18:33:26 +00:00
|
|
|
}
|
2014-12-02 21:48:21 +00:00
|
|
|
|
2016-03-31 20:18:14 +00:00
|
|
|
// Regular view: QUnit test runner
|
|
|
|
|
// (Support "/qunit" and "/qunit/plain" for backwards compatibility)
|
|
|
|
|
if ( $par === null || $par === '' || $par === 'qunit' || $par === 'qunit/plain' ) {
|
|
|
|
|
$this->plainQUnit();
|
|
|
|
|
return;
|
2012-01-03 18:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 20:18:14 +00:00
|
|
|
// Unknown action
|
|
|
|
|
$out->setStatusCode( 404 );
|
|
|
|
|
$out->setPageTitle( $this->msg( 'javascripttest' ) );
|
|
|
|
|
$out->addHTML(
|
|
|
|
|
'<div class="error">'
|
|
|
|
|
. $this->msg( 'javascripttest-pagetext-unknownaction' )
|
|
|
|
|
->plaintextParams( $par )->parseAsBlock()
|
|
|
|
|
. '</div>'
|
|
|
|
|
);
|
2012-01-03 18:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-07-10 03:40:27 +00:00
|
|
|
* Get summary text wrapped in a container
|
2014-12-02 21:48:21 +00:00
|
|
|
*
|
|
|
|
|
* @return string HTML
|
2012-01-03 18:33:26 +00:00
|
|
|
*/
|
2015-07-10 03:40:27 +00:00
|
|
|
private function getSummaryHtml() {
|
|
|
|
|
$summary = $this->msg( 'javascripttest-qunit-intro' )
|
|
|
|
|
->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
|
|
|
|
|
->parseAsBlock();
|
|
|
|
|
return "<div id=\"mw-javascripttest-summary\">$summary</div>";
|
2012-01-03 18:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-02 21:48:21 +00:00
|
|
|
/**
|
|
|
|
|
* Generate self-sufficient JavaScript payload to run the tests elsewhere.
|
|
|
|
|
*
|
|
|
|
|
* Includes startup module to request modules from ResourceLoader.
|
|
|
|
|
*
|
|
|
|
|
* Note: This modifies the registry to replace 'jquery.qunit' with an
|
|
|
|
|
* empty module to allow external environment to preload QUnit with any
|
|
|
|
|
* neccecary framework adapters (e.g. Karma). Loading it again would
|
|
|
|
|
* re-define QUnit and dereference event handlers from Karma.
|
|
|
|
|
*/
|
|
|
|
|
private function exportQUnit() {
|
|
|
|
|
$out = $this->getOutput();
|
|
|
|
|
$out->disable();
|
|
|
|
|
|
|
|
|
|
$rl = $out->getResourceLoader();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$query = [
|
2014-12-02 21:48:21 +00:00
|
|
|
'lang' => $this->getLanguage()->getCode(),
|
|
|
|
|
'skin' => $this->getSkin()->getSkinName(),
|
|
|
|
|
'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
|
2015-06-04 20:12:23 +00:00
|
|
|
'target' => 'test',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-12-02 21:48:21 +00:00
|
|
|
$embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
|
|
|
|
|
$query['only'] = 'scripts';
|
|
|
|
|
$startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
|
|
|
|
|
|
2015-07-28 02:46:00 +00:00
|
|
|
$query['raw'] = true;
|
|
|
|
|
|
2014-12-02 21:48:21 +00:00
|
|
|
$modules = $rl->getTestModuleNames( 'qunit' );
|
|
|
|
|
|
2015-07-28 02:46:00 +00:00
|
|
|
// 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 <https://github.com/karma-runner/karma-qunit/issues/27>.
|
|
|
|
|
. 'window.__karma__.loaded = function () {};'
|
|
|
|
|
. '}';
|
|
|
|
|
|
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
|
|
|
// The below is essentially a pure-javascript version of OutputPage::headElement().
|
2016-02-17 09:09:32 +00:00
|
|
|
$startup = $rl->makeModuleResponse( $startupContext, [
|
2014-12-02 21:48:21 +00:00
|
|
|
'startup' => $rl->getModule( 'startup' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-12-02 21:48:21 +00:00
|
|
|
// Embed page-specific mw.config variables.
|
|
|
|
|
// The current Special page shouldn't be relevant to tests, but various modules (which
|
|
|
|
|
// are loaded before the test suites), reference mw.config while initialising.
|
|
|
|
|
$code = ResourceLoader::makeConfigSetScript( $out->getJSVars() );
|
|
|
|
|
// Embed private modules as they're not allowed to be loaded dynamically
|
2016-02-17 09:09:32 +00:00
|
|
|
$code .= $rl->makeModuleResponse( $embedContext, [
|
2014-12-02 21:48:21 +00:00
|
|
|
'user.options' => $rl->getModule( 'user.options' ),
|
|
|
|
|
'user.tokens' => $rl->getModule( 'user.tokens' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2015-07-28 02:46:00 +00:00
|
|
|
// 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.
|
2017-04-27 18:05:25 +00:00
|
|
|
$modules = Xml::encodeJsVar( $modules );
|
|
|
|
|
$code .= <<<CODE
|
|
|
|
|
(function () {
|
|
|
|
|
var start = window.__karma__ ? window.__karma__.start : QUnit.start;
|
|
|
|
|
try {
|
|
|
|
|
mw.loader.using( $modules )
|
2017-04-27 18:19:32 +00:00
|
|
|
.always( function () {
|
|
|
|
|
start();
|
|
|
|
|
} )
|
2017-04-27 18:22:41 +00:00
|
|
|
.fail( function ( e ) {
|
|
|
|
|
setTimeout( function () {
|
|
|
|
|
throw e;
|
|
|
|
|
} );
|
|
|
|
|
} );
|
2017-04-27 18:05:25 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
|
start();
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}());
|
|
|
|
|
CODE;
|
2014-12-02 21:48:21 +00:00
|
|
|
|
|
|
|
|
header( 'Content-Type: text/javascript; charset=utf-8' );
|
|
|
|
|
header( 'Cache-Control: private, no-cache, must-revalidate' );
|
|
|
|
|
header( 'Pragma: no-cache' );
|
2015-07-28 02:46:00 +00:00
|
|
|
echo $qunitConfig;
|
2014-12-02 21:48:21 +00:00
|
|
|
echo $startup;
|
2015-07-28 02:46:00 +00:00
|
|
|
// The following has to be deferred via RLQ because the startup module is asynchronous.
|
|
|
|
|
echo ResourceLoader::makeLoaderConditionalScript( $code );
|
2014-12-02 21:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function plainQUnit() {
|
|
|
|
|
$out = $this->getOutput();
|
|
|
|
|
$out->disable();
|
|
|
|
|
|
2015-07-31 19:34:10 +00:00
|
|
|
$styles = $out->makeResourceLoaderLink( 'jquery.qunit',
|
|
|
|
|
ResourceLoaderModule::TYPE_STYLES
|
2015-03-15 02:50:37 +00:00
|
|
|
);
|
2015-07-28 02:46:00 +00:00
|
|
|
|
|
|
|
|
// 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.
|
2015-07-31 19:34:10 +00:00
|
|
|
$scripts = $out->makeResourceLoaderLink( 'jquery.qunit',
|
|
|
|
|
ResourceLoaderModule::TYPE_SCRIPTS,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'raw' => true, 'sync' => true ]
|
2015-03-15 02:50:37 +00:00
|
|
|
);
|
2014-12-02 21:48:21 +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
|
|
|
$head = implode( "\n", [ $styles, $scripts ] );
|
2015-07-10 03:40:27 +00:00
|
|
|
$summary = $this->getSummaryHtml();
|
2014-12-02 21:48:21 +00:00
|
|
|
$html = <<<HTML
|
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<title>QUnit</title>
|
|
|
|
|
$head
|
2015-07-10 03:40:27 +00:00
|
|
|
$summary
|
2014-12-02 21:48:21 +00:00
|
|
|
<div id="qunit"></div>
|
|
|
|
|
HTML;
|
2015-07-28 02:46:00 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$url = $this->getPageTitle( 'qunit/export' )->getFullURL( [
|
2015-07-28 02:46:00 +00:00
|
|
|
'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2014-12-02 21:48:21 +00:00
|
|
|
$html .= "\n" . Html::linkedScript( $url );
|
|
|
|
|
|
|
|
|
|
header( 'Content-Type: text/html; charset=utf-8' );
|
|
|
|
|
echo $html;
|
2012-01-03 18:33:26 +00:00
|
|
|
}
|
2013-03-07 20:15:54 +00:00
|
|
|
|
|
|
|
|
protected function getGroupName() {
|
|
|
|
|
return 'other';
|
|
|
|
|
}
|
2012-01-03 18:33:26 +00:00
|
|
|
}
|