Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 94 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| SpecialJavaScriptTest | |
0.00% |
0 / 93 |
|
0.00% |
0 / 7 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
42 | |||
| getComponents | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getModulesForComponentOrThrow | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| exportJS | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
2 | |||
| renderPage | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
2 | |||
| getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Specials; |
| 8 | |
| 9 | use MediaWiki\Config\HashConfig; |
| 10 | use MediaWiki\Config\MultiConfig; |
| 11 | use MediaWiki\Exception\HttpError; |
| 12 | use MediaWiki\Html\Html; |
| 13 | use MediaWiki\MainConfigNames; |
| 14 | use MediaWiki\Request\FauxRequest; |
| 15 | use MediaWiki\ResourceLoader as RL; |
| 16 | use MediaWiki\ResourceLoader\ResourceLoader; |
| 17 | use MediaWiki\SpecialPage\SpecialPage; |
| 18 | |
| 19 | /** |
| 20 | * @ingroup SpecialPage |
| 21 | * @ingroup ResourceLoader |
| 22 | */ |
| 23 | class SpecialJavaScriptTest extends SpecialPage { |
| 24 | |
| 25 | public function __construct() { |
| 26 | parent::__construct( 'JavaScriptTest' ); |
| 27 | } |
| 28 | |
| 29 | /** @inheritDoc */ |
| 30 | public function execute( $par ) { |
| 31 | $this->getOutput()->disable(); |
| 32 | |
| 33 | if ( $par === 'qunit/export' ) { |
| 34 | // Send the JavaScript payload. |
| 35 | $this->exportJS(); |
| 36 | } elseif ( $par === null || $par === '' || $par === 'qunit' || $par === 'qunit/plain' ) { |
| 37 | // Render the page |
| 38 | // (Support "/qunit" and "/qunit/plain" for backwards-compatibility) |
| 39 | $this->renderPage(); |
| 40 | } else { |
| 41 | wfHttpError( 404, 'Unknown action', "Unknown action \"$par\"." ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private function getComponents(): array { |
| 46 | $components = []; |
| 47 | |
| 48 | $rl = $this->getOutput()->getResourceLoader(); |
| 49 | foreach ( $rl->getTestSuiteModuleNames() as $module ) { |
| 50 | if ( str_starts_with( $module, 'test.' ) ) { |
| 51 | $components[] = substr( $module, 5 ); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $components; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Used on both GUI (renderPage, Special:JavaScriptTest) and CLI (exposeJS, via Gruntfile.js). |
| 60 | */ |
| 61 | private function getModulesForComponentOrThrow( ?string $component ): array { |
| 62 | if ( $component !== null ) { |
| 63 | if ( !in_array( $component, $this->getComponents() ) ) { |
| 64 | throw new HttpError( |
| 65 | 404, |
| 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.", |
| 69 | 'Unknown component', |
| 70 | ); |
| 71 | } |
| 72 | return [ 'test.' . $component ]; |
| 73 | } else { |
| 74 | $rl = $this->getOutput()->getResourceLoader(); |
| 75 | return $rl->getTestSuiteModuleNames(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Send the standalone JavaScript payload. |
| 81 | * |
| 82 | * Loaded by the GUI (on Special:JavaScriptTest), and by the CLI (via grunt-karma). |
| 83 | */ |
| 84 | private function exportJS() { |
| 85 | $out = $this->getOutput(); |
| 86 | $req = $this->getContext()->getRequest(); |
| 87 | $rl = $out->getResourceLoader(); |
| 88 | |
| 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 ); |
| 92 | |
| 93 | $query = [ |
| 94 | 'lang' => 'qqx', |
| 95 | 'skin' => 'fallback', |
| 96 | 'debug' => $req->getRawVal( 'debug' ), |
| 97 | 'target' => 'test', |
| 98 | ]; |
| 99 | $embedContext = new RL\Context( $rl, new FauxRequest( $query ) ); |
| 100 | $query['only'] = 'scripts'; |
| 101 | $startupContext = new RL\Context( $rl, new FauxRequest( $query ) ); |
| 102 | |
| 103 | $component = $req->getRawVal( 'component' ); |
| 104 | $modules = $this->getModulesForComponentOrThrow( $component ); |
| 105 | |
| 106 | // Disable module storage. |
| 107 | // The unit test for mw.loader.store will enable it (with a mock timers). |
| 108 | $config = new MultiConfig( [ |
| 109 | new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled => false ] ), |
| 110 | $rl->getConfig(), |
| 111 | ] ); |
| 112 | |
| 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. |
| 120 | // |
| 121 | // For compatibility with older tests, these will come from the user |
| 122 | // action "viewing Special:JavaScripTest". |
| 123 | // |
| 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 |
| 128 | // conditional). |
| 129 | // |
| 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(), |
| 139 | ] ) |
| 140 | // Embed private modules as they're not allowed to be loaded dynamically |
| 141 | . $rl->makeModuleResponse( $embedContext, [ |
| 142 | 'user.options' => $rl->getModule( 'user.options' ), |
| 143 | ] ) |
| 144 | // Load all the test modules |
| 145 | . Html::encodeJsCall( 'mw.loader.load', [ $modules ] ) |
| 146 | ); |
| 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(); |
| 153 | } ); |
| 154 | Promise.allSettled( promises ).then( QUnit.start ); |
| 155 | JAVASCRIPT |
| 156 | ); |
| 157 | |
| 158 | header( 'Content-Type: text/javascript; charset=utf-8' ); |
| 159 | header( 'Cache-Control: private, no-cache, must-revalidate' ); |
| 160 | echo $code; |
| 161 | } |
| 162 | |
| 163 | private function renderPage() { |
| 164 | $req = $this->getContext()->getRequest(); |
| 165 | $component = $req->getRawVal( 'component' ); |
| 166 | // If set, validate |
| 167 | $this->getModulesForComponentOrThrow( $component ); |
| 168 | |
| 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" ), |
| 173 | ] ); |
| 174 | |
| 175 | $scriptUrl = $this->getPageTitle( 'qunit/export' )->getFullURL( [ |
| 176 | 'debug' => $req->getRawVal( 'debug' ) ?? '0', |
| 177 | 'component' => $component, |
| 178 | ] ); |
| 179 | $script = implode( "\n", [ |
| 180 | Html::linkedScript( "$basePath/resources/lib/qunitjs/qunit.js" ), |
| 181 | Html::inlineScript( 'QUnit.config.autostart = false;' ), |
| 182 | Html::linkedScript( $scriptUrl ), |
| 183 | ] ); |
| 184 | |
| 185 | header( 'Content-Type: text/html; charset=utf-8' ); |
| 186 | echo <<<HTML |
| 187 | <!DOCTYPE html> |
| 188 | <title>QUnit</title> |
| 189 | $headHtml |
| 190 | <div id="qunit"></div> |
| 191 | <div id="qunit-fixture"></div> |
| 192 | $script |
| 193 | HTML; |
| 194 | } |
| 195 | |
| 196 | /** @inheritDoc */ |
| 197 | protected function getGroupName() { |
| 198 | return 'other'; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** @deprecated class alias since 1.41 */ |
| 203 | class_alias( SpecialJavaScriptTest::class, 'SpecialJavaScriptTest' ); |