Merge "Remove double escaping of group member name on Special:ListUsers"
[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 $out->setStatusCode( 404 );
48 $out->setPageTitle( $this->msg( 'javascripttest' ) );
49 $out->addHTML(
50 $this->msg( 'javascripttest-pagetext-noframework' )->parseAsBlock()
51 . $this->getFrameworkListHtml()
52 );
53 return;
54 }
55
56 // Determine framework and mode
57 $pars = explode( '/', $par, 2 );
58
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()
69 );
70 return;
71 }
72
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() ) )
80 );
81
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>'
92 );
93 return;
94 }
95 $method = $action . ucfirst( $framework );
96 $this->$method();
97 return;
98 }
99
100 $out->addModules( 'mediawiki.special.javaScriptTest' );
101
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 ) );
109 }
110
111 /**
112 * Get a list of frameworks (including introduction paragraph and links
113 * to the framework run pages)
114 *
115 * @return string HTML
116 */
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()
127 )
128 );
129 }
130 $list .= '</ul>';
131
132 return $this->msg( 'javascripttest-pagetext-frameworks' )->rawParams( $list )
133 ->parseAsBlock();
134 }
135
136 /**
137 * Wrap HTML contents in a summary container.
138 *
139 * @param string $html HTML contents to be wrapped
140 * @return string HTML
141 */
142 private function wrapSummaryHtml( $html ) {
143 return "<div id=\"mw-javascripttest-summary\">$html</div>";
144 }
145
146 /**
147 * Run the test suite on the Special page.
148 *
149 * Rendered by OutputPage and Skin.
150 */
151 private function viewQUnit() {
152 $out = $this->getOutput();
153
154 $modules = $out->getResourceLoader()->getTestModuleNames( 'qunit' );
155
156 $summary = $this->msg( 'javascripttest-qunit-intro' )
157 ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
158 ->parseAsBlock();
159
160 $baseHtml = <<<HTML
161 <div class="mw-content-ltr">
162 <div id="qunit"></div>
163 </div>
164 HTML;
165
166 $out->addHtml( $this->wrapSummaryHtml( $summary ) . $baseHtml );
167
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 ) ) . '}'
177 )
178 ) )
179 )
180 ) );
181 }
182
183 /**
184 * Generate self-sufficient JavaScript payload to run the tests elsewhere.
185 *
186 * Includes startup module to request modules from ResourceLoader.
187 *
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.
192 */
193 private function exportQUnit() {
194 $out = $this->getOutput();
195
196 $out->disable();
197
198 $rl = $out->getResourceLoader();
199
200 $query = array(
201 'lang' => $this->getLanguage()->getCode(),
202 'skin' => $this->getSkin()->getSkinName(),
203 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
204 );
205 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
206 $query['only'] = 'scripts';
207 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
208
209 $modules = $rl->getTestModuleNames( 'qunit' );
210
211 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
212 $startup = $rl->makeModuleResponse( $startupContext, array(
213 'startup' => $rl->getModule( 'startup' ),
214 ) );
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' ),
223 ) );
224 $code .= Xml::encodeJsCall( 'mw.loader.load', array( $modules ) );
225
226 header( 'Content-Type: text/javascript; charset=utf-8' );
227 header( 'Cache-Control: private, no-cache, must-revalidate' );
228 header( 'Pragma: no-cache' );
229 echo $startup;
230 echo "\n";
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 ) ) );
236 }
237
238 private function plainQUnit() {
239 $out = $this->getOutput();
240 $out->disable();
241
242 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
243 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
244 ) );
245
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' ) );
249
250 $head = trim( $styles['html'] . $scripts['html'] );
251 $html = <<<HTML
252 <!DOCTYPE html>
253 <title>QUnit</title>
254 $head
255 <div id="qunit"></div>
256 HTML;
257 $html .= "\n" . Html::linkedScript( $url );
258
259 header( 'Content-Type: text/html; charset=utf-8' );
260 echo $html;
261 }
262
263 /**
264 * Return an array of subpages that this special page will accept.
265 *
266 * @return string[] subpages
267 */
268 public function getSubpagesForPrefixSearch() {
269 return self::$frameworks;
270 }
271
272 protected function getGroupName() {
273 return 'other';
274 }
275 }