b5f38159af59fb96518ba350d866566f9fef3187
3 * Implements Special:JavaScriptTest
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * @ingroup SpecialPage
27 class SpecialJavaScriptTest
extends SpecialPage
{
29 * @var array Supported frameworks.
31 private static $frameworks = array(
35 public function __construct() {
36 parent
::__construct( 'JavaScriptTest' );
39 public function execute( $par ) {
40 $out = $this->getOutput();
43 $out->disallowUserJs();
45 if ( $par === null ) {
46 // No framework specified
47 // If only one framework is configured, redirect to it. Otherwise display a list.
48 if ( count( self
::$frameworks ) === 1 ) {
49 $out->redirect( $this->getPageTitle( self
::$frameworks[0] . '/plain' )->getLocalURL() );
52 $out->setStatusCode( 404 );
53 $out->setPageTitle( $this->msg( 'javascripttest' ) );
55 $this->msg( 'javascripttest-pagetext-noframework' )->parseAsBlock()
56 . $this->getFrameworkListHtml()
61 // Determine framework and mode
62 $pars = explode( '/', $par, 2 );
64 $framework = $pars[0];
65 if ( !in_array( $framework, self
::$frameworks ) ) {
66 // Framework not found
67 $out->setStatusCode( 404 );
70 . $this->msg( 'javascripttest-pagetext-unknownframework' )
71 ->plaintextParams( $par )->parseAsBlock()
73 . $this->getFrameworkListHtml()
78 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
79 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
80 // we need to allow iframing of this page.
81 $out->allowClickjacking();
82 if ( count( self
::$frameworks ) !== 1 ) {
83 // If there's only one framework, don't set the subtitle since it
84 // is going to redirect back to this page
86 $this->msg( 'javascripttest-backlink' )
87 ->rawParams( Linker
::linkKnown( $this->getPageTitle() ) )
92 if ( isset( $pars[1] ) ) {
94 if ( !in_array( $action, array( 'export', 'plain' ) ) ) {
95 $out->setStatusCode( 404 );
98 . $this->msg( 'javascripttest-pagetext-unknownaction' )
99 ->plaintextParams( $action )->parseAsBlock()
104 $method = $action . ucfirst( $framework );
109 $out->addModules( 'mediawiki.special.javaScriptTest' );
111 $method = 'view' . ucfirst( $framework );
113 $out->setPageTitle( $this->msg(
114 'javascripttest-title',
115 // Messages: javascripttest-qunit-name
116 $this->msg( "javascripttest-$framework-name" )->plain()
121 * Get a list of frameworks (including introduction paragraph and links
122 * to the framework run pages)
124 * @return string HTML
126 private function getFrameworkListHtml() {
128 foreach ( self
::$frameworks as $framework ) {
129 $list .= Html
::rawElement(
133 $this->getPageTitle( $framework ),
134 // Message: javascripttest-qunit-name
135 $this->msg( "javascripttest-$framework-name" )->escaped()
141 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
146 * Get summary text wrapped in a container
148 * @return string HTML
150 private function getSummaryHtml() {
151 $summary = $this->msg( 'javascripttest-qunit-intro' )
152 ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
154 return "<div id=\"mw-javascripttest-summary\">$summary</div>";
158 * Run the test suite on the Special page.
160 * Rendered by OutputPage and Skin.
162 private function viewQUnit() {
163 $out = $this->getOutput();
165 $modules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
168 <div class="mw-content-ltr">
169 <div id="qunit"></div>
173 $out->addHtml( $this->getSummaryHtml() . $baseHtml );
175 // The testrunner configures QUnit and essentially depends on it. However, test suites
176 // are reusable in environments that preload QUnit (or a compatibility interface to
177 // another framework). Therefore we have to load it ourselves.
178 $out->addHtml( ResourceLoader
::makeInlineScript(
179 Xml
::encodeJsCall( 'mw.loader.using', array(
180 array( 'jquery.qunit', 'jquery.qunit.completenessTest' ),
182 'function () {' . Xml
::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}'
189 * Generate self-sufficient JavaScript payload to run the tests elsewhere.
191 * Includes startup module to request modules from ResourceLoader.
193 * Note: This modifies the registry to replace 'jquery.qunit' with an
194 * empty module to allow external environment to preload QUnit with any
195 * neccecary framework adapters (e.g. Karma). Loading it again would
196 * re-define QUnit and dereference event handlers from Karma.
198 private function exportQUnit() {
199 $out = $this->getOutput();
202 $rl = $out->getResourceLoader();
205 'lang' => $this->getLanguage()->getCode(),
206 'skin' => $this->getSkin()->getSkinName(),
207 'debug' => ResourceLoader
::inDebugMode() ?
'true' : 'false',
209 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
210 $query['only'] = 'scripts';
211 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
213 $query['raw'] = true;
215 $modules = $rl->getTestModuleNames( 'qunit' );
217 // Disable autostart because we load modules asynchronously. By default, QUnit would start
218 // at domready when there are no tests loaded and also fire 'QUnit.done' which then instructs
219 // Karma to end the run before the tests even started.
220 $qunitConfig = 'QUnit.config.autostart = false;'
221 . 'if (window.__karma__) {'
222 // karma-qunit's use of autostart=false and QUnit.start conflicts with ours.
223 // Hack around this by replacing 'karma.loaded' with a no-op and call it ourselves later.
224 // See <https://github.com/karma-runner/karma-qunit/issues/27>.
225 . 'window.__karma__.loaded = function () {};'
228 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
229 $startup = $rl->makeModuleResponse( $startupContext, array(
230 'startup' => $rl->getModule( 'startup' ),
232 // Embed page-specific mw.config variables.
233 // The current Special page shouldn't be relevant to tests, but various modules (which
234 // are loaded before the test suites), reference mw.config while initialising.
235 $code = ResourceLoader
::makeConfigSetScript( $out->getJSVars() );
236 // Embed private modules as they're not allowed to be loaded dynamically
237 $code .= $rl->makeModuleResponse( $embedContext, array(
238 'user.options' => $rl->getModule( 'user.options' ),
239 'user.tokens' => $rl->getModule( 'user.tokens' ),
241 // Catch exceptions (such as "dependency missing" or "unknown module") so that we
242 // always start QUnit. Re-throw so that they are caught and reported as global exceptions
243 // by QUnit and Karma.
244 $code .= '(function () {'
245 . 'var start = window.__karma__ ? window.__karma__.start : QUnit.start;'
247 . 'mw.loader.using( ' . Xml
::encodeJsVar( $modules ) . ' ).always( start );'
248 . '} catch ( e ) { start(); throw e; }'
251 header( 'Content-Type: text/javascript; charset=utf-8' );
252 header( 'Cache-Control: private, no-cache, must-revalidate' );
253 header( 'Pragma: no-cache' );
256 // The following has to be deferred via RLQ because the startup module is asynchronous.
257 echo ResourceLoader
::makeLoaderConditionalScript( $code );
260 private function plainQUnit() {
261 $out = $this->getOutput();
264 $styles = $out->makeResourceLoaderLink( 'jquery.qunit',
265 ResourceLoaderModule
::TYPE_STYLES
268 // Use 'raw' because QUnit loads before ResourceLoader initialises (omit mw.loader.state call)
269 // Use 'test' to ensure OutputPage doesn't use the "async" attribute because QUnit must
270 // load before qunit/export.
271 $scripts = $out->makeResourceLoaderLink( 'jquery.qunit',
272 ResourceLoaderModule
::TYPE_SCRIPTS
,
273 array( 'raw' => true, 'sync' => true )
276 $head = implode( "\n", array_merge( $styles['html'], $scripts['html'] ) );
277 $summary = $this->getSummaryHtml();
283 <div id="qunit"></div>
286 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
287 'debug' => ResourceLoader
::inDebugMode() ?
'true' : 'false',
289 $html .= "\n" . Html
::linkedScript( $url );
291 header( 'Content-Type: text/html; charset=utf-8' );
296 * Return an array of subpages that this special page will accept.
298 * @return string[] subpages
300 public function getSubpagesForPrefixSearch() {
301 return self
::$frameworks;
304 protected function getGroupName() {