Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 86
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialJavaScriptTest
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 5
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
42
 exportJS
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 1
12
 renderPage
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
24namespace MediaWiki\Specials;
25
26use MediaWiki\Config\HashConfig;
27use MediaWiki\Config\MultiConfig;
28use MediaWiki\Html\Html;
29use MediaWiki\MainConfigNames;
30use MediaWiki\Request\FauxRequest;
31use MediaWiki\ResourceLoader as RL;
32use MediaWiki\ResourceLoader\ResourceLoader;
33use MediaWiki\SpecialPage\SpecialPage;
34
35/**
36 * @ingroup SpecialPage
37 */
38class SpecialJavaScriptTest extends SpecialPage {
39
40    public function __construct() {
41        parent::__construct( 'JavaScriptTest' );
42    }
43
44    public function execute( $par ) {
45        $this->getOutput()->disable();
46
47        if ( $par === 'qunit/export' ) {
48            // Send the JavaScript payload.
49            $this->exportJS();
50        } elseif ( $par === null || $par === '' || $par === 'qunit' || $par === 'qunit/plain' ) {
51            // Render the page
52            // (Support "/qunit" and "/qunit/plain" for backwards-compatibility)
53            $this->renderPage();
54        } else {
55            wfHttpError( 404, 'Unknown action', "Unknown action \"$par\"." );
56        }
57    }
58
59    /**
60     * Send the standalone JavaScript payload.
61     *
62     * Loaded by the GUI (on Special:JavaScriptTest), and by the CLI (via grunt-karma).
63     */
64    private function exportJS() {
65        $out = $this->getOutput();
66        $req = $this->getContext()->getRequest();
67        $rl = $out->getResourceLoader();
68
69        // Allow framing (disabling wgBreakFrames). Otherwise, mediawiki.page.ready
70        // will close this tab when running from CLI using karma-qunit.
71        $out->setPreventClickjacking( false );
72
73        $query = [
74            'lang' => 'qqx',
75            'skin' => 'fallback',
76            'debug' => $req->getRawVal( 'debug' ),
77            'target' => 'test',
78        ];
79        $embedContext = new RL\Context( $rl, new FauxRequest( $query ) );
80        $query['only'] = 'scripts';
81        $startupContext = new RL\Context( $rl, new FauxRequest( $query ) );
82
83        $modules = $rl->getTestSuiteModuleNames();
84        $component = $req->getRawVal( 'component' );
85        if ( $component ) {
86            $module = 'test.' . $component;
87            if ( !in_array( 'test.' . $component, $modules ) ) {
88                wfHttpError(
89                    404,
90                    'Unknown test module',
91                    "'$module' is not a defined test module. "
92                        . 'Register one via the QUnitTestModules attribute in extension.json.'
93                );
94                return;
95            }
96            $modules = [ 'test.' . $component ];
97        }
98
99        // Disable module storage.
100        // The unit test for mw.loader.store will enable it (with a mock timers).
101        $config = new MultiConfig( [
102            new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled => false ] ),
103            $rl->getConfig(),
104        ] );
105
106        // The below is essentially a pure-javascript version of OutputPage::headElement().
107        $startupModule = $rl->getModule( 'startup' );
108        $startupModule->setConfig( $config );
109        $code = $rl->makeModuleResponse( $startupContext, [ 'startup' => $startupModule ] );
110        // The following has to be deferred via RLQ because the startup module is asynchronous.
111        $code .= ResourceLoader::makeLoaderConditionalScript(
112            // Embed page-specific mw.config variables.
113            //
114            // For compatibility with older tests, these will come from the user
115            // action "viewing Special:JavaScripTest".
116            //
117            // This is deprecated since MediaWiki 1.25 and slowly being phased out in favour of:
118            // 1. tests explicitly mocking the configuration they depend on.
119            // 2. tests explicitly skipping or not loading code that is only meant
120            //    for real page views (e.g. not loading as dependency, or using a QUnit
121            //    conditional).
122            //
123            // See https://phabricator.wikimedia.org/T89434.
124            // Keep a select few that are commonly referenced.
125            ResourceLoader::makeConfigSetScript( [
126                // used by mediawiki.util
127                'wgPageName' => 'Special:Badtitle/JavaScriptTest',
128                // used as input for mw.Title
129                'wgRelevantPageName' => 'Special:Badtitle/JavaScriptTest',
130            ] )
131            // Embed private modules as they're not allowed to be loaded dynamically
132            . $rl->makeModuleResponse( $embedContext, [
133                'user.options' => $rl->getModule( 'user.options' ),
134            ] )
135            // Load all the test modules
136            . Html::encodeJsCall( 'mw.loader.load', [ $modules ] )
137        );
138        $encModules = Html::encodeJsVar( $modules );
139        $code .= ResourceLoader::makeInlineCodeWithModule( 'mediawiki.base', <<<JAVASCRIPT
140    // Wait for each module individually, so that partial failures wont break the page
141    // completely by rejecting the promise before all/ any modules are loaded.
142    var promises = $encModules.map( function( module ) {
143        return mw.loader.using( module ).promise();
144    } );
145    Promise.allSettled( promises ).then( QUnit.start );
146JAVASCRIPT
147        );
148
149        header( 'Content-Type: text/javascript; charset=utf-8' );
150        header( 'Cache-Control: private, no-cache, must-revalidate' );
151        echo $code;
152    }
153
154    private function renderPage() {
155        $basePath = $this->getConfig()->get( MainConfigNames::ResourceBasePath );
156        $headHtml = implode( "\n", [
157            Html::linkedStyle( "$basePath/resources/lib/qunitjs/qunit.css" ),
158            Html::linkedStyle( "$basePath/resources/src/qunitjs/qunit-local.css" ),
159        ] );
160
161        $introHtml = $this->msg( 'javascripttest-qunit-intro' )
162            ->params( 'https://www.mediawiki.org/wiki/Manual:JavaScript_unit_testing' )
163            ->parseAsBlock();
164
165        $scriptUrl = $this->getPageTitle( 'qunit/export' )->getFullURL( [
166            'debug' => (string)ResourceLoader::inDebugMode(),
167        ] );
168        $script = implode( "\n", [
169            Html::linkedScript( "$basePath/resources/lib/qunitjs/qunit.js" ),
170            Html::inlineScript( 'QUnit.config.autostart = false;' ),
171            Html::linkedScript( $scriptUrl ),
172        ] );
173
174        header( 'Content-Type: text/html; charset=utf-8' );
175        echo <<<HTML
176<!DOCTYPE html>
177<title>QUnit</title>
178$headHtml
179$introHtml
180<div id="qunit"></div>
181<div id="qunit-fixture"></div>
182$script
183HTML;
184    }
185
186    protected function getGroupName() {
187        return 'other';
188    }
189}
190
191/** @deprecated class alias since 1.41 */
192class_alias( SpecialJavaScriptTest::class, 'SpecialJavaScriptTest' );