Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / specials / SpecialJavaScriptTest.php
blob492105d942a4cde3e73512b34522be2efb42a1e0
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 {
28 /**
29 * @var array Supported frameworks.
31 private static $frameworks = array(
32 'qunit',
35 public function __construct() {
36 parent::__construct( 'JavaScriptTest' );
39 public function execute( $par ) {
40 $out = $this->getOutput();
42 $this->setHeaders();
43 $out->disallowUserJs();
45 if ( $par === null ) {
46 // No framework specified
47 $out->setStatusCode( 404 );
48 $out->setPageTitle( $this->msg( 'javascripttest' ) );
49 $out->addHTML(
50 $this->msg( 'javascripttest-pagetext-noframework' )->parseAsBlock()
51 . $this->getFrameworkListHtml()
53 return;
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 );
63 $out->addHTML(
64 '<div class="error">'
65 . $this->msg( 'javascripttest-pagetext-unknownframework' )
66 ->plaintextParams( $par )->parseAsBlock()
67 . '</div>'
68 . $this->getFrameworkListHtml()
70 return;
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();
77 $out->setSubtitle(
78 $this->msg( 'javascripttest-backlink' )
79 ->rawParams( Linker::linkKnown( $this->getPageTitle() ) )
82 // Custom actions
83 if ( isset( $pars[1] ) ) {
84 $action = $pars[1];
85 if ( !in_array( $action, array( 'export', 'plain' ) ) ) {
86 $out->setStatusCode( 404 );
87 $out->addHTML(
88 '<div class="error">'
89 . $this->msg( 'javascripttest-pagetext-unknownaction' )
90 ->plaintextParams( $action )->parseAsBlock()
91 . '</div>'
93 return;
95 $method = $action . ucfirst( $framework );
96 $this->$method();
97 return;
100 $out->addModules( 'mediawiki.special.javaScriptTest' );
102 $method = 'view' . ucfirst( $framework );
103 $this->$method();
104 $out->setPageTitle( $this->msg(
105 'javascripttest-title',
106 // Messages: javascripttest-qunit-name
107 $this->msg( "javascripttest-$framework-name" )->plain()
108 ) );
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() {
118 $list = '<ul>';
119 foreach ( self::$frameworks as $framework ) {
120 $list .= Html::rawElement(
121 'li',
122 array(),
123 Linker::link(
124 $this->getPageTitle( $framework ),
125 // Message: javascripttest-qunit-name
126 $this->msg( "javascripttest-$framework-name" )->escaped()
130 $list .= '</ul>';
132 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
133 ->parseAsBlock();
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' )
158 ->parseAsBlock();
160 $baseHtml = <<<HTML
161 <div class="mw-content-ltr">
162 <div id="qunit"></div>
163 </div>
164 HTML;
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' ),
175 new XmlJsCode(
176 'function () {' . Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}'
180 ) );
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();
195 $out->disable();
197 $rl = $out->getResourceLoader();
199 $query = array(
200 'lang' => $this->getLanguage()->getCode(),
201 'skin' => $this->getSkin()->getSkinName(),
202 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
204 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
205 $query['only'] = 'scripts';
206 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
208 $modules = $rl->getTestModuleNames( 'qunit' );
210 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
211 $startup = $rl->makeModuleResponse( $startupContext, array(
212 'startup' => $rl->getModule( 'startup' ),
213 ) );
214 // Embed page-specific mw.config variables.
215 // The current Special page shouldn't be relevant to tests, but various modules (which
216 // are loaded before the test suites), reference mw.config while initialising.
217 $code = ResourceLoader::makeConfigSetScript( $out->getJSVars() );
218 // Embed private modules as they're not allowed to be loaded dynamically
219 $code .= $rl->makeModuleResponse( $embedContext, array(
220 'user.options' => $rl->getModule( 'user.options' ),
221 'user.tokens' => $rl->getModule( 'user.tokens' ),
222 ) );
223 $code .= Xml::encodeJsCall( 'mw.loader.load', array( $modules ) );
225 header( 'Content-Type: text/javascript; charset=utf-8' );
226 header( 'Cache-Control: private, no-cache, must-revalidate' );
227 header( 'Pragma: no-cache' );
228 echo $startup;
229 echo "\n";
230 // Note: The following has to be wrapped in a script tag because the startup module also
231 // writes a script tag (the one loading mediawiki.js). Script tags are synchronous, block
232 // each other, and run in order. But they don't nest. The code appended after the startup
233 // module runs before the added script tag is parsed and executed.
234 echo Xml::encodeJsCall( 'document.write', array( Html::inlineScript( $code ) ) );
237 private function plainQUnit() {
238 $out = $this->getOutput();
239 $out->disable();
241 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
242 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
243 ) );
245 $styles = $out->makeResourceLoaderLink( 'jquery.qunit', ResourceLoaderModule::TYPE_STYLES, false );
246 // Use 'raw' since this is a plain HTML page without ResourceLoader
247 $scripts = $out->makeResourceLoaderLink( 'jquery.qunit', ResourceLoaderModule::TYPE_SCRIPTS, false, array( 'raw' => 'true' ) );
249 $head = trim( $styles['html'] . $scripts['html'] );
250 $html = <<<HTML
251 <!DOCTYPE html>
252 <title>QUnit</title>
253 $head
254 <div id="qunit"></div>
255 HTML;
256 $html .= "\n" . Html::linkedScript( $url );
258 header( 'Content-Type: text/html; charset=utf-8' );
259 echo $html;
263 * Return an array of subpages that this special page will accept.
265 * @return string[] subpages
267 public function getSubpagesForPrefixSearch() {
268 return self::$frameworks;
271 protected function getGroupName() {
272 return 'other';