40 parent::__construct(
'JavaScriptTest' );
46 if ( $par ===
'qunit/export' ) {
49 } elseif ( $par ===
null || $par ===
'' || $par ===
'qunit' || $par ===
'qunit/plain' ) {
54 wfHttpError( 404,
'Unknown action',
"Unknown action \"$par\"." );
58 private function getComponents(): array {
61 $rl = $this->
getOutput()->getResourceLoader();
62 foreach ( $rl->getTestSuiteModuleNames() as $module ) {
63 if ( str_starts_with( $module,
'test.' ) ) {
64 $components[] = substr( $module, 5 );
74 private function getModulesForComponentOrThrow( ?
string $component ): array {
75 if ( $component !== null ) {
76 if ( !in_array( $component, $this->getComponents() ) ) {
79 "No test module found for the '$component' component.\n"
80 .
"Make sure the extension is enabled via wfLoadExtension(),\n"
81 .
"and register a test module via the QUnitTestModules attribute in extension.json.",
85 return [
'test.' . $component ];
87 $rl = $this->getOutput()->getResourceLoader();
88 return $rl->getTestSuiteModuleNames();
97 private function exportJS() {
98 $out = $this->getOutput();
100 $rl = $out->getResourceLoader();
104 $out->getMetadata()->setPreventClickjacking(
false );
108 'skin' =>
'fallback',
109 'debug' => $req->getRawVal(
'debug' ),
112 $embedContext =
new RL\Context( $rl,
new FauxRequest( $query ) );
113 $query[
'only'] =
'scripts';
114 $startupContext =
new RL\Context( $rl,
new FauxRequest( $query ) );
116 $component = $req->getRawVal(
'component' );
117 $modules = $this->getModulesForComponentOrThrow( $component );
121 $config =
new MultiConfig( [
122 new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled =>
false ] ),
127 $startupModule = $rl->getModule(
'startup' );
128 $startupModule->setConfig( $config );
129 $code = $rl->makeModuleResponse( $startupContext, [
'startup' => $startupModule ] );
131 $code .= ResourceLoader::makeLoaderConditionalScript(
145 ResourceLoader::makeConfigSetScript( [
147 'wgPageName' =>
'Special:Badtitle/JavaScriptTest',
149 'wgRelevantPageName' =>
'Special:Badtitle/JavaScriptTest',
151 'wgTestModuleComponents' => $this->getComponents(),
154 . $rl->makeModuleResponse( $embedContext, [
155 'user.options' => $rl->getModule(
'user.options' ),
158 . Html::encodeJsCall(
'mw.loader.load', [ $modules ] )
160 $encModules = Html::encodeJsVar( $modules );
161 $code .= ResourceLoader::makeInlineCodeWithModule(
'mediawiki.base', <<<JAVASCRIPT
164 var promises = $encModules.map(
function( module ) {
165 return mw.loader.using( module ).promise();
167 Promise.allSettled( promises ).then( QUnit.start );
171 header(
'Content-Type: text/javascript; charset=utf-8' );
172 header(
'Cache-Control: private, no-cache, must-revalidate' );
176 private function renderPage() {
178 $component = $req->getRawVal(
'component' );
180 $this->getModulesForComponentOrThrow( $component );
182 $basePath = $this->getConfig()->get( MainConfigNames::ResourceBasePath );
183 $headHtml = implode(
"\n", [
184 Html::linkedStyle(
"$basePath/resources/lib/qunitjs/qunit.css" ),
185 Html::linkedStyle(
"$basePath/resources/src/qunitjs/qunit-local.css" ),
188 $scriptUrl = $this->getPageTitle(
'qunit/export' )->getFullURL( [
189 'debug' => $req->getRawVal(
'debug' ) ??
'0',
190 'component' => $component,
192 $script = implode(
"\n", [
193 Html::linkedScript(
"$basePath/resources/lib/qunitjs/qunit.js" ),
194 Html::inlineScript(
'QUnit.config.autostart = false;' ),
195 Html::linkedScript( $scriptUrl ),
198 header(
'Content-Type: text/html; charset=utf-8' );
203<div
id=
"qunit"></div>
204<div
id=
"qunit-fixture"></div>