Type check the APC value in LoadBalancer::doWait()
[mediawiki.git] / includes / specials / SpecialJavaScriptTest.php
blobdc6a61975066192df2bdf5e80b45de14d9e124c1
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * @ingroup SpecialPage
27 class SpecialJavaScriptTest extends SpecialPage {
29 public function __construct() {
30 parent::__construct( 'JavaScriptTest' );
33 public function execute( $par ) {
34 $out = $this->getOutput();
36 $this->setHeaders();
37 $out->disallowUserJs();
39 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
40 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
41 // we need to allow iframing of this page.
42 $out->allowClickjacking();
44 // Sub resource: Internal JavaScript export bundle for QUnit
45 if ( $par === 'qunit/export' ) {
46 $this->exportQUnit();
47 return;
50 // Regular view: QUnit test runner
51 // (Support "/qunit" and "/qunit/plain" for backwards compatibility)
52 if ( $par === null || $par === '' || $par === 'qunit' || $par === 'qunit/plain' ) {
53 $this->plainQUnit();
54 return;
57 // Unknown action
58 $out->setStatusCode( 404 );
59 $out->setPageTitle( $this->msg( 'javascripttest' ) );
60 $out->addHTML(
61 '<div class="error">'
62 . $this->msg( 'javascripttest-pagetext-unknownaction' )
63 ->plaintextParams( $par )->parseAsBlock()
64 . '</div>'
68 /**
69 * Get summary text wrapped in a container
71 * @return string HTML
73 private function getSummaryHtml() {
74 $summary = $this->msg( 'javascripttest-qunit-intro' )
75 ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
76 ->parseAsBlock();
77 return "<div id=\"mw-javascripttest-summary\">$summary</div>";
80 /**
81 * Generate self-sufficient JavaScript payload to run the tests elsewhere.
83 * Includes startup module to request modules from ResourceLoader.
85 * Note: This modifies the registry to replace 'jquery.qunit' with an
86 * empty module to allow external environment to preload QUnit with any
87 * neccecary framework adapters (e.g. Karma). Loading it again would
88 * re-define QUnit and dereference event handlers from Karma.
90 private function exportQUnit() {
91 $out = $this->getOutput();
92 $out->disable();
94 $rl = $out->getResourceLoader();
96 $query = [
97 'lang' => $this->getLanguage()->getCode(),
98 'skin' => $this->getSkin()->getSkinName(),
99 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
100 'target' => 'test',
102 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
103 $query['only'] = 'scripts';
104 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
106 $query['raw'] = true;
108 $modules = $rl->getTestModuleNames( 'qunit' );
110 // Disable autostart because we load modules asynchronously. By default, QUnit would start
111 // at domready when there are no tests loaded and also fire 'QUnit.done' which then instructs
112 // Karma to end the run before the tests even started.
113 $qunitConfig = 'QUnit.config.autostart = false;'
114 . 'if (window.__karma__) {'
115 // karma-qunit's use of autostart=false and QUnit.start conflicts with ours.
116 // Hack around this by replacing 'karma.loaded' with a no-op and call it ourselves later.
117 // See <https://github.com/karma-runner/karma-qunit/issues/27>.
118 . 'window.__karma__.loaded = function () {};'
119 . '}';
121 // The below is essentially a pure-javascript version of OutputPage::headElement().
122 $startup = $rl->makeModuleResponse( $startupContext, [
123 'startup' => $rl->getModule( 'startup' ),
124 ] );
125 // Embed page-specific mw.config variables.
126 // The current Special page shouldn't be relevant to tests, but various modules (which
127 // are loaded before the test suites), reference mw.config while initialising.
128 $code = ResourceLoader::makeConfigSetScript( $out->getJSVars() );
129 // Embed private modules as they're not allowed to be loaded dynamically
130 $code .= $rl->makeModuleResponse( $embedContext, [
131 'user.options' => $rl->getModule( 'user.options' ),
132 'user.tokens' => $rl->getModule( 'user.tokens' ),
133 ] );
134 // Catch exceptions (such as "dependency missing" or "unknown module") so that we
135 // always start QUnit. Re-throw so that they are caught and reported as global exceptions
136 // by QUnit and Karma.
137 $code .= '(function () {'
138 . 'var start = window.__karma__ ? window.__karma__.start : QUnit.start;'
139 . 'try {'
140 . 'mw.loader.using( ' . Xml::encodeJsVar( $modules ) . ' )'
141 . '.always( start )'
142 . '.fail( function ( e ) { throw e; } );'
143 . '} catch ( e ) { start(); throw e; }'
144 . '}());';
146 header( 'Content-Type: text/javascript; charset=utf-8' );
147 header( 'Cache-Control: private, no-cache, must-revalidate' );
148 header( 'Pragma: no-cache' );
149 echo $qunitConfig;
150 echo $startup;
151 // The following has to be deferred via RLQ because the startup module is asynchronous.
152 echo ResourceLoader::makeLoaderConditionalScript( $code );
155 private function plainQUnit() {
156 $out = $this->getOutput();
157 $out->disable();
159 $styles = $out->makeResourceLoaderLink( 'jquery.qunit',
160 ResourceLoaderModule::TYPE_STYLES
163 // Use 'raw' because QUnit loads before ResourceLoader initialises (omit mw.loader.state call)
164 // Use 'test' to ensure OutputPage doesn't use the "async" attribute because QUnit must
165 // load before qunit/export.
166 $scripts = $out->makeResourceLoaderLink( 'jquery.qunit',
167 ResourceLoaderModule::TYPE_SCRIPTS,
168 [ 'raw' => true, 'sync' => true ]
171 $head = implode( "\n", [ $styles, $scripts ] );
172 $summary = $this->getSummaryHtml();
173 $html = <<<HTML
174 <!DOCTYPE html>
175 <title>QUnit</title>
176 $head
177 $summary
178 <div id="qunit"></div>
179 HTML;
181 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( [
182 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
183 ] );
184 $html .= "\n" . Html::linkedScript( $url );
186 header( 'Content-Type: text/html; charset=utf-8' );
187 echo $html;
191 * Return an array of subpages that this special page will accept.
193 * @return string[] subpages
195 public function getSubpagesForPrefixSearch() {
196 return self::$frameworks;
199 protected function getGroupName() {
200 return 'other';