MediaWiki master
SpecialJavaScriptTest.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Specials;
25
34
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
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 ) ) {
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
192class_alias( SpecialJavaScriptTest::class, 'SpecialJavaScriptTest' );
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
A Config instance which stores all settings as a member variable.
Provides a fallback sequence for Config objects.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
A class containing constants representing the names of configuration variables.
const ResourceBasePath
Name constant for the ResourceBasePath setting, for use with Config::get()
const ResourceLoaderStorageEnabled
Name constant for the ResourceLoaderStorageEnabled setting, for use with Config::get()
WebRequest clone which takes values from a provided array.
Context object that contains information about the state of a specific ResourceLoader web request.
Definition Context.php:45
ResourceLoader is a loading system for JavaScript and CSS resources.
Parent class for all special pages.
getPageTitle( $subpage=false)
Get a self-referential title object.
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
execute( $par)
Default execute method Checks user permissions.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...