26 parent::__construct(
'JavaScriptTest' );
33if ( $par ===
'qunit/export' ) {
34// Send the JavaScript payload. 36 } elseif ( $par ===
null || $par ===
'' || $par ===
'qunit' || $par ===
'qunit/plain' ) {
38// (Support "/qunit" and "/qunit/plain" for backwards-compatibility) 41wfHttpError( 404,
'Unknown action',
"Unknown action \"$par\"." );
45privatefunction getComponents(): array {
48 $rl = $this->
getOutput()->getResourceLoader();
49foreach ( $rl->getTestSuiteModuleNames() as $module ) {
50if ( str_starts_with( $module,
'test.' ) ) {
51 $components[] = substr( $module, 5 );
61privatefunction getModulesForComponentOrThrow( ?
string $component ): array {
62if ( $component !== null ) {
63if ( !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.",
72return [
'test.' . $component ];
74 $rl = $this->getOutput()->getResourceLoader();
75return $rl->getTestSuiteModuleNames();
84privatefunction exportJS() {
85 $out = $this->getOutput();
87 $rl = $out->getResourceLoader();
89// Allow framing (disabling wgBreakFrames). Otherwise, mediawiki.page.ready 90// will close this tab when running from CLI using karma-qunit. 91 $out->getMetadata()->setPreventClickjacking(
false );
96'debug' => $req->getRawVal(
'debug' ),
99 $embedContext =
newRL\Context( $rl,
new FauxRequest( $query ) );
100 $query[
'only'] =
'scripts';
101 $startupContext =
newRL\Context( $rl,
new FauxRequest( $query ) );
103 $component = $req->getRawVal(
'component' );
104$modules = $this->getModulesForComponentOrThrow( $component );
106// Disable module storage. 107// The unit test for mw.loader.store will enable it (with a mock timers). 108 $config =
new MultiConfig( [
109new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled =>
false ] ),
113// The below is essentially a pure-javascript version of OutputPage::headElement(). 114 $startupModule = $rl->getModule(
'startup' );
115 $startupModule->setConfig( $config );
116 $code = $rl->makeModuleResponse( $startupContext, [
'startup' => $startupModule ] );
117// The following has to be deferred via RLQ because the startup module is asynchronous. 118 $code .= ResourceLoader::makeLoaderConditionalScript(
119// Embed page-specific mw.config variables. 121// For compatibility with older tests, these will come from the user 122// action "viewing Special:JavaScripTest". 124// This is deprecated since MediaWiki 1.25 and slowly being phased out in favour of: 125// 1. tests explicitly mocking the configuration they depend on. 126// 2. tests explicitly skipping or not loading code that is only meant 127// for real page views (e.g. not loading as dependency, or using a QUnit 130// See https://phabricator.wikimedia.org/T89434. 131// Keep a select few that are commonly referenced. 132 ResourceLoader::makeConfigSetScript( [
133// used by mediawiki.util 134'wgPageName' =>
'Special:Badtitle/JavaScriptTest',
135// used as input for mw.Title 136'wgRelevantPageName' =>
'Special:Badtitle/JavaScriptTest',
137// used by testrunner.js for QUnit toolbar 138'wgTestModuleComponents' => $this->getComponents(),
140// Embed private modules as they're not allowed to be loaded dynamically 141 . $rl->makeModuleResponse( $embedContext, [
142'user.options' => $rl->getModule(
'user.options' ),
144// Load all the test modules 145 . Html::encodeJsCall(
'mw.loader.load', [ $modules ] )
147 $encModules = Html::encodeJsVar( $modules );
148 $code .= ResourceLoader::makeInlineCodeWithModule(
'mediawiki.base', <<<JAVASCRIPT
149// Wait for each module individually, so that partial failures wont break the page 150// completely by rejecting the promise before all/ any modules are loaded. 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' );
163privatefunction 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' );
191<div
id=
"qunit-fixture"></div>