40 parent::__construct(
'JavaScriptTest' );
47 if ( $par ===
'qunit/export' ) {
50 } elseif ( $par ===
null || $par ===
'' || $par ===
'qunit' || $par ===
'qunit/plain' ) {
55 wfHttpError( 404,
'Unknown action',
"Unknown action \"$par\"." );
59 private function getComponents(): array {
62 $rl = $this->
getOutput()->getResourceLoader();
63 foreach ( $rl->getTestSuiteModuleNames() as $module ) {
64 if ( str_starts_with( $module,
'test.' ) ) {
65 $components[] = substr( $module, 5 );
75 private function getModulesForComponentOrThrow( ?
string $component ): array {
76 if ( $component !== null ) {
77 if ( !in_array( $component, $this->getComponents() ) ) {
80 "No test module found for the '$component' component.\n"
81 .
"Make sure the extension is enabled via wfLoadExtension(),\n"
82 .
"and register a test module via the QUnitTestModule attribute in extension.json.",
86 return [
'test.' . $component ];
88 $rl = $this->getOutput()->getResourceLoader();
89 return $rl->getTestSuiteModuleNames();
98 private function exportJS() {
99 $out = $this->getOutput();
101 $rl = $out->getResourceLoader();
105 $out->getMetadata()->setPreventClickjacking(
false );
109 'skin' =>
'fallback',
110 'debug' => $req->getRawVal(
'debug' ),
113 $embedContext =
new RL\Context( $rl,
new FauxRequest( $query ) );
114 $query[
'only'] =
'scripts';
115 $startupContext =
new RL\Context( $rl,
new FauxRequest( $query ) );
117 $component = $req->getRawVal(
'component' );
118 $modules = $this->getModulesForComponentOrThrow( $component );
122 $config =
new MultiConfig( [
123 new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled =>
false ] ),
128 $startupModule = $rl->getModule(
'startup' );
129 $startupModule->setConfig( $config );
130 $code = $rl->makeModuleResponse( $startupContext, [
'startup' => $startupModule ] );
132 $code .= ResourceLoader::makeLoaderConditionalScript(
146 ResourceLoader::makeConfigSetScript( [
148 'wgPageName' =>
'Special:Badtitle/JavaScriptTest',
150 'wgRelevantPageName' =>
'Special:Badtitle/JavaScriptTest',
152 'wgTestModuleComponents' => $this->getComponents(),
155 . $rl->makeModuleResponse( $embedContext, [
156 'user.options' => $rl->getModule(
'user.options' ),
159 . Html::encodeJsCall(
'mw.loader.load', [ $modules ] )
161 $encModules = Html::encodeJsVar( $modules );
162 $code .= ResourceLoader::makeInlineCodeWithModule(
'mediawiki.base', <<<JAVASCRIPT
165 var promises = $encModules.map(
function( module ) {
166 return mw.loader.using( module ).promise();
168 Promise.allSettled( promises ).then( QUnit.start );
172 header(
'Content-Type: text/javascript; charset=utf-8' );
173 header(
'Cache-Control: private, no-cache, must-revalidate' );
177 private function renderPage() {
179 $component = $req->getRawVal(
'component' );
181 $this->getModulesForComponentOrThrow( $component );
183 $basePath = $this->getConfig()->get( MainConfigNames::ResourceBasePath );
184 $headHtml = implode(
"\n", [
185 Html::linkedStyle(
"$basePath/resources/lib/qunitjs/qunit.css" ),
186 Html::linkedStyle(
"$basePath/resources/src/qunitjs/qunit-local.css" ),
189 $scriptUrl = $this->getPageTitle(
'qunit/export' )->getFullURL( [
190 'debug' => $req->getRawVal(
'debug' ) ??
'0',
191 'component' => $component,
193 $script = implode(
"\n", [
194 Html::linkedScript(
"$basePath/resources/lib/qunitjs/qunit.js" ),
195 Html::inlineScript(
'QUnit.config.autostart = false;' ),
196 Html::linkedScript( $scriptUrl ),
199 header(
'Content-Type: text/html; charset=utf-8' );
204<div
id=
"qunit"></div>
205<div
id=
"qunit-fixture"></div>