Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / SpecialJavaScriptTest.php
blob2fe08f9a8ac87ea0319dc89647d4a718d8fcd118
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use HttpError;
24 use MediaWiki\Config\HashConfig;
25 use MediaWiki\Config\MultiConfig;
26 use MediaWiki\Html\Html;
27 use MediaWiki\MainConfigNames;
28 use MediaWiki\Request\FauxRequest;
29 use MediaWiki\ResourceLoader as RL;
30 use MediaWiki\ResourceLoader\ResourceLoader;
31 use MediaWiki\SpecialPage\SpecialPage;
33 /**
34 * @ingroup SpecialPage
35 * @ingroup ResourceLoader
37 class SpecialJavaScriptTest extends SpecialPage {
39 public function __construct() {
40 parent::__construct( 'JavaScriptTest' );
43 public function execute( $par ) {
44 $this->getOutput()->disable();
46 if ( $par === 'qunit/export' ) {
47 // Send the JavaScript payload.
48 $this->exportJS();
49 } elseif ( $par === null || $par === '' || $par === 'qunit' || $par === 'qunit/plain' ) {
50 // Render the page
51 // (Support "/qunit" and "/qunit/plain" for backwards-compatibility)
52 $this->renderPage();
53 } else {
54 wfHttpError( 404, 'Unknown action', "Unknown action \"$par\"." );
58 /**
59 * Used on both GUI (Special:JavaScriptTest) and the exportJS route
61 private function getModulesForComponentOrThrow( ?string $component ): array {
62 $out = $this->getOutput();
63 $rl = $out->getResourceLoader();
64 $req = $this->getContext()->getRequest();
66 $modules = $rl->getTestSuiteModuleNames();
67 if ( $component !== null ) {
68 $module = 'test.' . $component;
69 if ( !in_array( 'test.' . $component, $modules ) ) {
70 throw new HttpError(
71 404,
72 "No test module found for the '$component' component.\n"
73 . "Make sure the extension is enabled via wfLoadExtension(),\n"
74 . "and register a test module via the QUnitTestModules attribute in extension.json.",
75 'Unknown test module',
78 $modules = [ 'test.' . $component ];
81 return $modules;
84 /**
85 * Send the standalone JavaScript payload.
87 * Loaded by the GUI (on Special:JavaScriptTest), and by the CLI (via grunt-karma).
89 private function exportJS() {
90 $out = $this->getOutput();
91 $req = $this->getContext()->getRequest();
92 $rl = $out->getResourceLoader();
94 // Allow framing (disabling wgBreakFrames). Otherwise, mediawiki.page.ready
95 // will close this tab when running from CLI using karma-qunit.
96 $out->getMetadata()->setPreventClickjacking( false );
98 $query = [
99 'lang' => 'qqx',
100 'skin' => 'fallback',
101 'debug' => $req->getRawVal( 'debug' ),
102 'target' => 'test',
104 $embedContext = new RL\Context( $rl, new FauxRequest( $query ) );
105 $query['only'] = 'scripts';
106 $startupContext = new RL\Context( $rl, new FauxRequest( $query ) );
108 $component = $req->getRawVal( 'component' );
109 $modules = $this->getModulesForComponentOrThrow( $component );
111 // Disable module storage.
112 // The unit test for mw.loader.store will enable it (with a mock timers).
113 $config = new MultiConfig( [
114 new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled => false ] ),
115 $rl->getConfig(),
116 ] );
118 // The below is essentially a pure-javascript version of OutputPage::headElement().
119 $startupModule = $rl->getModule( 'startup' );
120 $startupModule->setConfig( $config );
121 $code = $rl->makeModuleResponse( $startupContext, [ 'startup' => $startupModule ] );
122 // The following has to be deferred via RLQ because the startup module is asynchronous.
123 $code .= ResourceLoader::makeLoaderConditionalScript(
124 // Embed page-specific mw.config variables.
126 // For compatibility with older tests, these will come from the user
127 // action "viewing Special:JavaScripTest".
129 // This is deprecated since MediaWiki 1.25 and slowly being phased out in favour of:
130 // 1. tests explicitly mocking the configuration they depend on.
131 // 2. tests explicitly skipping or not loading code that is only meant
132 // for real page views (e.g. not loading as dependency, or using a QUnit
133 // conditional).
135 // See https://phabricator.wikimedia.org/T89434.
136 // Keep a select few that are commonly referenced.
137 ResourceLoader::makeConfigSetScript( [
138 // used by mediawiki.util
139 'wgPageName' => 'Special:Badtitle/JavaScriptTest',
140 // used as input for mw.Title
141 'wgRelevantPageName' => 'Special:Badtitle/JavaScriptTest',
143 // Embed private modules as they're not allowed to be loaded dynamically
144 . $rl->makeModuleResponse( $embedContext, [
145 'user.options' => $rl->getModule( 'user.options' ),
147 // Load all the test modules
148 . Html::encodeJsCall( 'mw.loader.load', [ $modules ] )
150 $encModules = Html::encodeJsVar( $modules );
151 $code .= ResourceLoader::makeInlineCodeWithModule( 'mediawiki.base', <<<JAVASCRIPT
152 // Wait for each module individually, so that partial failures wont break the page
153 // completely by rejecting the promise before all/ any modules are loaded.
154 var promises = $encModules.map( function( module ) {
155 return mw.loader.using( module ).promise();
156 } );
157 Promise.allSettled( promises ).then( QUnit.start );
158 JAVASCRIPT
161 header( 'Content-Type: text/javascript; charset=utf-8' );
162 header( 'Cache-Control: private, no-cache, must-revalidate' );
163 echo $code;
166 private function renderPage() {
167 $req = $this->getContext()->getRequest();
168 $component = $req->getRawVal( 'component' );
169 // If set, validate
170 $this->getModulesForComponentOrThrow( $component );
172 $basePath = $this->getConfig()->get( MainConfigNames::ResourceBasePath );
173 $headHtml = implode( "\n", [
174 Html::linkedStyle( "$basePath/resources/lib/qunitjs/qunit.css" ),
175 Html::linkedStyle( "$basePath/resources/src/qunitjs/qunit-local.css" ),
176 ] );
178 $scriptUrl = $this->getPageTitle( 'qunit/export' )->getFullURL( [
179 'debug' => $req->getRawVal( 'debug' ) ?? '0',
180 'component' => $component,
181 ] );
182 $script = implode( "\n", [
183 Html::linkedScript( "$basePath/resources/lib/qunitjs/qunit.js" ),
184 Html::inlineScript( 'QUnit.config.autostart = false;' ),
185 Html::linkedScript( $scriptUrl ),
186 ] );
188 header( 'Content-Type: text/html; charset=utf-8' );
189 echo <<<HTML
190 <!DOCTYPE html>
191 <title>QUnit</title>
192 $headHtml
193 <div id="qunit"></div>
194 <div id="qunit-fixture"></div>
195 $script
196 HTML;
199 protected function getGroupName() {
200 return 'other';
204 /** @deprecated class alias since 1.41 */
205 class_alias( SpecialJavaScriptTest::class, 'SpecialJavaScriptTest' );