Merge "Improve messages related to $wgAllowCategorizedRecentChanges"
[lhc/web/wiklou.git] / includes / specials / SpecialJavaScriptTest.php
1 <?php
2 /**
3 * Implements Special:JavaScriptTest
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * @ingroup SpecialPage
26 */
27 class SpecialJavaScriptTest extends SpecialPage {
28 /**
29 * @var array Supported frameworks.
30 */
31 private static $frameworks = array(
32 'qunit',
33 );
34
35 public function __construct() {
36 parent::__construct( 'JavaScriptTest' );
37 }
38
39 public function execute( $par ) {
40 $out = $this->getOutput();
41
42 $this->setHeaders();
43 $out->disallowUserJs();
44
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() );
50 return;
51 }
52 $out->setStatusCode( 404 );
53 $out->setPageTitle( $this->msg( 'javascripttest' ) );
54 $out->addHTML(
55 $this->msg( 'javascripttest-pagetext-noframework' )->parseAsBlock()
56 . $this->getFrameworkListHtml()
57 );
58 return;
59 }
60
61 // Determine framework and mode
62 $pars = explode( '/', $par, 2 );
63
64 $framework = $pars[0];
65 if ( !in_array( $framework, self::$frameworks ) ) {
66 // Framework not found
67 $out->setStatusCode( 404 );
68 $out->addHTML(
69 '<div class="error">'
70 . $this->msg( 'javascripttest-pagetext-unknownframework' )
71 ->plaintextParams( $par )->parseAsBlock()
72 . '</div>'
73 . $this->getFrameworkListHtml()
74 );
75 return;
76 }
77
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
85 $out->setSubtitle(
86 $this->msg( 'javascripttest-backlink' )
87 ->rawParams( Linker::linkKnown( $this->getPageTitle() ) )
88 );
89 }
90
91 // Custom actions
92 if ( isset( $pars[1] ) ) {
93 $action = $pars[1];
94 if ( !in_array( $action, array( 'export', 'plain' ) ) ) {
95 $out->setStatusCode( 404 );
96 $out->addHTML(
97 '<div class="error">'
98 . $this->msg( 'javascripttest-pagetext-unknownaction' )
99 ->plaintextParams( $action )->parseAsBlock()
100 . '</div>'
101 );
102 return;
103 }
104 $method = $action . ucfirst( $framework );
105 $this->$method();
106 return;
107 }
108
109 $out->addModules( 'mediawiki.special.javaScriptTest' );
110
111 $method = 'view' . ucfirst( $framework );
112 $this->$method();
113 $out->setPageTitle( $this->msg(
114 'javascripttest-title',
115 // Messages: javascripttest-qunit-name
116 $this->msg( "javascripttest-$framework-name" )->plain()
117 ) );
118 }
119
120 /**
121 * Get a list of frameworks (including introduction paragraph and links
122 * to the framework run pages)
123 *
124 * @return string HTML
125 */
126 private function getFrameworkListHtml() {
127 $list = '<ul>';
128 foreach ( self::$frameworks as $framework ) {
129 $list .= Html::rawElement(
130 'li',
131 array(),
132 Linker::link(
133 $this->getPageTitle( $framework ),
134 // Message: javascripttest-qunit-name
135 $this->msg( "javascripttest-$framework-name" )->escaped()
136 )
137 );
138 }
139 $list .= '</ul>';
140
141 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
142 ->parseAsBlock();
143 }
144
145 /**
146 * Get summary text wrapped in a container
147 *
148 * @return string HTML
149 */
150 private function getSummaryHtml() {
151 $summary = $this->msg( 'javascripttest-qunit-intro' )
152 ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
153 ->parseAsBlock();
154 return "<div id=\"mw-javascripttest-summary\">$summary</div>";
155 }
156
157 /**
158 * Run the test suite on the Special page.
159 *
160 * Rendered by OutputPage and Skin.
161 */
162 private function viewQUnit() {
163 $out = $this->getOutput();
164
165 $modules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
166
167 $baseHtml = <<<HTML
168 <div class="mw-content-ltr">
169 <div id="qunit"></div>
170 </div>
171 HTML;
172
173 $out->addHtml( $this->getSummaryHtml() . $baseHtml );
174
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' ),
181 new XmlJsCode(
182 'function () {' . Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}'
183 )
184 ) )
185 ) );
186 }
187
188 /**
189 * Generate self-sufficient JavaScript payload to run the tests elsewhere.
190 *
191 * Includes startup module to request modules from ResourceLoader.
192 *
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.
197 */
198 private function exportQUnit() {
199 $out = $this->getOutput();
200 $out->disable();
201
202 $rl = $out->getResourceLoader();
203
204 $query = array(
205 'lang' => $this->getLanguage()->getCode(),
206 'skin' => $this->getSkin()->getSkinName(),
207 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
208 );
209 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
210 $query['only'] = 'scripts';
211 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
212
213 $modules = $rl->getTestModuleNames( 'qunit' );
214
215 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
216 $startup = $rl->makeModuleResponse( $startupContext, array(
217 'startup' => $rl->getModule( 'startup' ),
218 ) );
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' ),
227 ) );
228 $code .= Xml::encodeJsCall( 'mw.loader.load', array( $modules ) );
229
230 header( 'Content-Type: text/javascript; charset=utf-8' );
231 header( 'Cache-Control: private, no-cache, must-revalidate' );
232 header( 'Pragma: no-cache' );
233 echo $startup;
234 echo "\n";
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 ) ) );
240 }
241
242 private function plainQUnit() {
243 $out = $this->getOutput();
244 $out->disable();
245
246 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
247 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
248 ) );
249
250 $styles = $out->makeResourceLoaderLink( 'jquery.qunit',
251 ResourceLoaderModule::TYPE_STYLES
252 );
253 // Use 'raw' since this is a plain HTML page without ResourceLoader
254 $scripts = $out->makeResourceLoaderLink( 'jquery.qunit',
255 ResourceLoaderModule::TYPE_SCRIPTS,
256 array( 'raw' => 'true' )
257 );
258
259 $head = implode( "\n", array_merge( $styles['html'], $scripts['html'] ) );
260 $summary = $this->getSummaryHtml();
261 $html = <<<HTML
262 <!DOCTYPE html>
263 <title>QUnit</title>
264 $head
265 $summary
266 <div id="qunit"></div>
267 HTML;
268 $html .= "\n" . Html::linkedScript( $url );
269
270 header( 'Content-Type: text/html; charset=utf-8' );
271 echo $html;
272 }
273
274 /**
275 * Return an array of subpages that this special page will accept.
276 *
277 * @return string[] subpages
278 */
279 public function getSubpagesForPrefixSearch() {
280 return self::$frameworks;
281 }
282
283 protected function getGroupName() {
284 return 'other';
285 }
286 }