Merge "Clarify that gender-unknown option is gender neutral"
[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( ResourceLoader::makeInlineScript(
172 Xml::encodeJsCall( 'mw.loader.using', array(
173 array( 'jquery.qunit', 'jquery.qunit.completenessTest' ),
174 new XmlJsCode(
175 'function () {' . Xml::encodeJsCall( 'mw.loader.load', array( $modules ) ) . '}'
176 )
177 ) )
178 ) );
179 }
180
181 /**
182 * Generate self-sufficient JavaScript payload to run the tests elsewhere.
183 *
184 * Includes startup module to request modules from ResourceLoader.
185 *
186 * Note: This modifies the registry to replace 'jquery.qunit' with an
187 * empty module to allow external environment to preload QUnit with any
188 * neccecary framework adapters (e.g. Karma). Loading it again would
189 * re-define QUnit and dereference event handlers from Karma.
190 */
191 private function exportQUnit() {
192 $out = $this->getOutput();
193 $out->disable();
194
195 $rl = $out->getResourceLoader();
196
197 $query = array(
198 'lang' => $this->getLanguage()->getCode(),
199 'skin' => $this->getSkin()->getSkinName(),
200 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
201 );
202 $embedContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
203 $query['only'] = 'scripts';
204 $startupContext = new ResourceLoaderContext( $rl, new FauxRequest( $query ) );
205
206 $modules = $rl->getTestModuleNames( 'qunit' );
207
208 // The below is essentially a pure-javascript version of OutputPage::getHeadScripts.
209 $startup = $rl->makeModuleResponse( $startupContext, array(
210 'startup' => $rl->getModule( 'startup' ),
211 ) );
212 // Embed page-specific mw.config variables.
213 // The current Special page shouldn't be relevant to tests, but various modules (which
214 // are loaded before the test suites), reference mw.config while initialising.
215 $code = ResourceLoader::makeConfigSetScript( $out->getJSVars() );
216 // Embed private modules as they're not allowed to be loaded dynamically
217 $code .= $rl->makeModuleResponse( $embedContext, array(
218 'user.options' => $rl->getModule( 'user.options' ),
219 'user.tokens' => $rl->getModule( 'user.tokens' ),
220 ) );
221 $code .= Xml::encodeJsCall( 'mw.loader.load', array( $modules ) );
222
223 header( 'Content-Type: text/javascript; charset=utf-8' );
224 header( 'Cache-Control: private, no-cache, must-revalidate' );
225 header( 'Pragma: no-cache' );
226 echo $startup;
227 echo "\n";
228 // Note: The following has to be wrapped in a script tag because the startup module also
229 // writes a script tag (the one loading mediawiki.js). Script tags are synchronous, block
230 // each other, and run in order. But they don't nest. The code appended after the startup
231 // module runs before the added script tag is parsed and executed.
232 echo Xml::encodeJsCall( 'document.write', array( Html::inlineScript( $code ) ) );
233 }
234
235 private function plainQUnit() {
236 $out = $this->getOutput();
237 $out->disable();
238
239 $url = $this->getPageTitle( 'qunit/export' )->getFullURL( array(
240 'debug' => ResourceLoader::inDebugMode() ? 'true' : 'false',
241 ) );
242
243 $styles = $out->makeResourceLoaderLink(
244 'jquery.qunit', ResourceLoaderModule::TYPE_STYLES, false
245 );
246 // Use 'raw' since this is a plain HTML page without ResourceLoader
247 $scripts = $out->makeResourceLoaderLink(
248 'jquery.qunit', ResourceLoaderModule::TYPE_SCRIPTS, false, array( 'raw' => 'true' )
249 );
250
251 $head = trim( $styles['html'] . $scripts['html'] );
252 $html = <<<HTML
253 <!DOCTYPE html>
254 <title>QUnit</title>
255 $head
256 <div id="qunit"></div>
257 HTML;
258 $html .= "\n" . Html::linkedScript( $url );
259
260 header( 'Content-Type: text/html; charset=utf-8' );
261 echo $html;
262 }
263
264 /**
265 * Return an array of subpages that this special page will accept.
266 *
267 * @return string[] subpages
268 */
269 public function getSubpagesForPrefixSearch() {
270 return self::$frameworks;
271 }
272
273 protected function getGroupName() {
274 return 'other';
275 }
276 }