26 parent::__construct(
'JavaScriptTest' );
33 if ( $par ===
'qunit/export' ) {
36 } elseif ( $par ===
null || $par ===
'' || $par ===
'qunit' || $par ===
'qunit/plain' ) {
41 wfHttpError( 404,
'Unknown action',
"Unknown action \"$par\"." );
45 private function getComponents(): array {
48 $rl = $this->
getOutput()->getResourceLoader();
49 foreach ( $rl->getTestSuiteModuleNames() as $module ) {
50 if ( str_starts_with( $module,
'test.' ) ) {
51 $components[] = substr( $module, 5 );
61 private function getModulesForComponentOrThrow( ?
string $component ): array {
62 if ( $component !== null ) {
63 if ( !in_array( $component, $this->getComponents() ) ) {
66 "No test module found for the '$component' component.\n"
67 .
"Make sure the extension is enabled via wfLoadExtension(),\n"
68 .
"and register a test module via the QUnitTestModule attribute in extension.json.",
72 return [
'test.' . $component ];
74 $rl = $this->getOutput()->getResourceLoader();
75 return $rl->getTestSuiteModuleNames();
84 private function exportJS() {
85 $out = $this->getOutput();
87 $rl = $out->getResourceLoader();
91 $out->getMetadata()->setPreventClickjacking(
false );
96 'debug' => $req->getRawVal(
'debug' ),
99 $embedContext =
new RL\Context( $rl,
new FauxRequest( $query ) );
100 $query[
'only'] =
'scripts';
101 $startupContext =
new RL\Context( $rl,
new FauxRequest( $query ) );
103 $component = $req->getRawVal(
'component' );
104 $modules = $this->getModulesForComponentOrThrow( $component );
108 $config =
new MultiConfig( [
109 new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled =>
false ] ),
114 $startupModule = $rl->getModule(
'startup' );
115 $startupModule->setConfig( $config );
116 $code = $rl->makeModuleResponse( $startupContext, [
'startup' => $startupModule ] );
118 $code .= ResourceLoader::makeLoaderConditionalScript(
132 ResourceLoader::makeConfigSetScript( [
134 'wgPageName' =>
'Special:Badtitle/JavaScriptTest',
136 'wgRelevantPageName' =>
'Special:Badtitle/JavaScriptTest',
138 'wgTestModuleComponents' => $this->getComponents(),
141 . $rl->makeModuleResponse( $embedContext, [
142 'user.options' => $rl->getModule(
'user.options' ),
145 . Html::encodeJsCall(
'mw.loader.load', [ $modules ] )
147 $encModules = Html::encodeJsVar( $modules );
148 $code .= ResourceLoader::makeInlineCodeWithModule(
'mediawiki.base', <<<JAVASCRIPT
151 var promises = $encModules.map(
function( module ) {
152 return mw.loader.using( module ).promise();
154 Promise.allSettled( promises ).then( QUnit.start );
158 header(
'Content-Type: text/javascript; charset=utf-8' );
159 header(
'Cache-Control: private, no-cache, must-revalidate' );
163 private function renderPage() {
165 $component = $req->getRawVal(
'component' );
167 $this->getModulesForComponentOrThrow( $component );
169 $basePath = $this->getConfig()->get( MainConfigNames::ResourceBasePath );
170 $headHtml = implode(
"\n", [
171 Html::linkedStyle(
"$basePath/resources/lib/qunitjs/qunit.css" ),
172 Html::linkedStyle(
"$basePath/resources/src/qunitjs/qunit-local.css" ),
175 $scriptUrl = $this->getPageTitle(
'qunit/export' )->getFullURL( [
176 'debug' => $req->getRawVal(
'debug' ) ??
'0',
177 'component' => $component,
179 $script = implode(
"\n", [
180 Html::linkedScript(
"$basePath/resources/lib/qunitjs/qunit.js" ),
181 Html::inlineScript(
'QUnit.config.autostart = false;' ),
182 Html::linkedScript( $scriptUrl ),
185 header(
'Content-Type: text/html; charset=utf-8' );
190<div
id=
"qunit"></div>
191<div
id=
"qunit-fixture"></div>