4c73f1676e087382adde5ff4f0a712b17f788a5a
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 $modules = $rl->getTestModuleNames( 'qunit' );
215 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
216 $startup = $rl->makeModuleResponse( $startupContext, array(
217 'startup' => $rl->getModule( 'startup' ),
219 // Embed page-specific mw.config variables.
220 // The current Special page shouldn't be relevant to tests, but various modules (which
221 // are loaded before the test suites), reference mw.config while initialising.
222 $code = ResourceLoader
::makeConfigSetScript( $out->getJSVars() );
223 // Embed private modules as they're not allowed to be loaded dynamically
224 $code .= $rl->makeModuleResponse( $embedContext, array(
225 'user.options' => $rl->getModule( 'user.options' ),
226 'user.tokens' => $rl->getModule( 'user.tokens' ),
228 $code .= Xml
::encodeJsCall( 'mw.loader.load', array( $modules ) );
230 header( 'Content-Type: text/javascript; charset=utf-8' );
231 header( 'Cache-Control: private, no-cache, must-revalidate' );
232 header( 'Pragma: no-cache' );
235 // Note: The following has to be wrapped in a script tag because the startup module also
236 // writes a script tag (the one loading mediawiki.js). Script tags are synchronous, block
237 // each other, and run in order. But they don't nest. The code appended after the startup
238 // module runs before the added script tag is parsed and executed.
239 echo Xml
::encodeJsCall( 'document.write', array( Html
::inlineScript( $code ) ) );
242 private function plainQUnit() {
243 $out = $this->getOutput();
246 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
247 'debug' => ResourceLoader
::inDebugMode() ?
'true' : 'false',
250 $styles = $out->makeResourceLoaderLink(
251 'jquery.qunit', ResourceLoaderModule
::TYPE_STYLES
, false
253 // Use 'raw' since this is a plain HTML page without ResourceLoader
254 $scripts = $out->makeResourceLoaderLink(
255 'jquery.qunit', ResourceLoaderModule
::TYPE_SCRIPTS
, false, array( 'raw' => 'true' )
258 $head = trim( $styles['html'] . $scripts['html'] );
259 $summary = $this->getSummaryHtml();
265 <div id="qunit"></div>
267 $html .= "\n" . Html
::linkedScript( $url );
269 header( 'Content-Type: text/html; charset=utf-8' );
274 * Return an array of subpages that this special page will accept.
276 * @return string[] subpages
278 public function getSubpagesForPrefixSearch() {
279 return self
::$frameworks;
282 protected function getGroupName() {