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 $out->setStatusCode( 404 );
48 $out->setPageTitle( $this->msg( 'javascripttest' ) );
50 $this->msg( 'javascripttest-pagetext-noframework' )->parseAsBlock()
51 . $this->getFrameworkListHtml()
56 // Determine framework and mode
57 $pars = explode( '/', $par, 2 );
59 $framework = $pars[0];
60 if ( !in_array( $framework, self
::$frameworks ) ) {
61 // Framework not found
62 $out->setStatusCode( 404 );
65 . $this->msg( 'javascripttest-pagetext-unknownframework' )
66 ->plaintextParams( $par )->parseAsBlock()
68 . $this->getFrameworkListHtml()
73 // This special page is disabled by default ($wgEnableJavaScriptTest), and contains
74 // no sensitive data. In order to allow TestSwarm to embed it into a test client window,
75 // we need to allow iframing of this page.
76 $out->allowClickjacking();
78 $this->msg( 'javascripttest-backlink' )
79 ->rawParams( Linker
::linkKnown( $this->getPageTitle() ) )
83 if ( isset( $pars[1] ) ) {
85 if ( !in_array( $action, array( 'export', 'plain' ) ) ) {
86 $out->setStatusCode( 404 );
89 . $this->msg( 'javascripttest-pagetext-unknownaction' )
90 ->plaintextParams( $action )->parseAsBlock()
95 $method = $action . ucfirst( $framework );
100 $out->addModules( 'mediawiki.special.javaScriptTest' );
102 $method = 'view' . ucfirst( $framework );
104 $out->setPageTitle( $this->msg(
105 'javascripttest-title',
106 // Messages: javascripttest-qunit-name
107 $this->msg( "javascripttest-$framework-name" )->plain()
112 * Get a list of frameworks (including introduction paragraph and links
113 * to the framework run pages)
115 * @return string HTML
117 private function getFrameworkListHtml() {
119 foreach ( self
::$frameworks as $framework ) {
120 $list .= Html
::rawElement(
124 $this->getPageTitle( $framework ),
125 // Message: javascripttest-qunit-name
126 $this->msg( "javascripttest-$framework-name" )->escaped()
132 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
137 * Wrap HTML contents in a summary container.
139 * @param string $html HTML contents to be wrapped
140 * @return string HTML
142 private function wrapSummaryHtml( $html ) {
143 return "<div id=\"mw-javascripttest-summary\">$html</div>";
147 * Run the test suite on the Special page.
149 * Rendered by OutputPage and Skin.
151 private function viewQUnit() {
152 $out = $this->getOutput();
154 $modules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
156 $summary = $this->msg( 'javascripttest-qunit-intro' )
157 ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
161 <div class="mw-content-ltr">
162 <div id="qunit"></div>
166 $out->addHtml( $this->wrapSummaryHtml( $summary ) . $baseHtml );
168 // The testrunner configures QUnit and essentially depends on it. However, test suites
169 // are reusable in environments that preload QUnit (or a compatibility interface to
170 // another framework). Therefore we have to load it ourselves.
171 $out->addHtml( Html
::inlineScript(
172 ResourceLoader
::makeLoaderConditionalScript(
173 Xml
::encodeJsCall( 'mw.loader.using', array(
174 array( 'jquery.qunit', 'jquery.qunit.completenessTest' ),
176 'function () {' . Xml
::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}'
184 * Generate self-sufficient JavaScript payload to run the tests elsewhere.
186 * Includes startup module to request modules from ResourceLoader.
188 * Note: This modifies the registry to replace 'jquery.qunit' with an
189 * empty module to allow external environment to preload QUnit with any
190 * neccecary framework adapters (e.g. Karma). Loading it again would
191 * re-define QUnit and dereference event handlers from Karma.
193 private function exportQUnit() {
194 $out = $this->getOutput();
198 $rl = $out->getResourceLoader();
201 'lang' => $this->getLanguage()->getCode(),
202 'skin' => $this->getSkin()->getSkinName(),
203 'debug' => ResourceLoader
::inDebugMode() ?
'true' : 'false',
205 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
206 $query['only'] = 'scripts';
207 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
209 $modules = $rl->getTestModuleNames( 'qunit' );
211 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
212 $startup = $rl->makeModuleResponse( $startupContext, array(
213 'startup' => $rl->getModule( 'startup' ),
215 // Embed page-specific mw.config variables.
216 // The current Special page shouldn't be relevant to tests, but various modules (which
217 // are loaded before the test suites), reference mw.config while initialising.
218 $code = ResourceLoader
::makeConfigSetScript( $out->getJSVars() );
219 // Embed private modules as they're not allowed to be loaded dynamically
220 $code .= $rl->makeModuleResponse( $embedContext, array(
221 'user.options' => $rl->getModule( 'user.options' ),
222 'user.tokens' => $rl->getModule( 'user.tokens' ),
224 $code .= Xml
::encodeJsCall( 'mw.loader.load', array( $modules ) );
226 header( 'Content-Type: text/javascript; charset=utf-8' );
227 header( 'Cache-Control: private, no-cache, must-revalidate' );
228 header( 'Pragma: no-cache' );
231 // Note: The following has to be wrapped in a script tag because the startup module also
232 // writes a script tag (the one loading mediawiki.js). Script tags are synchronous, block
233 // each other, and run in order. But they don't nest. The code appended after the startup
234 // module runs before the added script tag is parsed and executed.
235 echo Xml
::encodeJsCall( 'document.write', array( Html
::inlineScript( $code ) ) );
238 private function plainQUnit() {
239 $out = $this->getOutput();
242 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
243 'debug' => ResourceLoader
::inDebugMode() ?
'true' : 'false',
246 $styles = $out->makeResourceLoaderLink( 'jquery.qunit', ResourceLoaderModule
::TYPE_STYLES
, false );
247 // Use 'raw' since this is a plain HTML page without ResourceLoader
248 $scripts = $out->makeResourceLoaderLink( 'jquery.qunit', ResourceLoaderModule
::TYPE_SCRIPTS
, false, array( 'raw' => 'true' ) );
250 $head = trim( $styles['html'] . $scripts['html'] );
255 <div id="qunit"></div>
257 $html .= "\n" . Html
::linkedScript( $url );
259 header( 'Content-Type: text/html; charset=utf-8' );
264 * Return an array of subpages that this special page will accept.
266 * @return string[] subpages
268 public function getSubpagesForPrefixSearch() {
269 return self
::$frameworks;
272 protected function getGroupName() {