| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use HttpError; |
| 24 |
use MediaWiki\Config\HashConfig; |
| 25 |
use MediaWiki\Config\MultiConfig; |
| 26 |
use MediaWiki\Html\Html; |
| 27 |
use MediaWiki\MainConfigNames; |
| 28 |
use MediaWiki\Request\FauxRequest; |
| 29 |
use MediaWiki\ResourceLoader as RL; |
| 30 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 31 |
use MediaWiki\SpecialPage\SpecialPage; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class SpecialJavaScriptTest extends SpecialPage { |
| 38 |
|
| 39 |
public function __construct() { |
| 40 |
parent::__construct( 'JavaScriptTest' ); |
| 41 |
} |
| 42 |
|
| 43 |
public function execute( $par ) { |
| 44 |
$this->getOutput()->disable(); |
| 45 |
|
| 46 |
if ( $par === 'qunit/export' ) { |
| 47 |
|
| 48 |
$this->exportJS(); |
| 49 |
} elseif ( $par === null || $par === '' || $par === 'qunit' || $par === 'qunit/plain' ) { |
| 50 |
|
| 51 |
|
| 52 |
$this->renderPage(); |
| 53 |
} else { |
| 54 |
wfHttpError( 404, 'Unknown action', "Unknown action \"$par\"." ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private function getModulesForComponentOrThrow( ?string $component ): array { |
| 62 |
$out = $this->getOutput(); |
| 63 |
$rl = $out->getResourceLoader(); |
| 64 |
$req = $this->getContext()->getRequest(); |
| 65 |
|
| 66 |
$modules = $rl->getTestSuiteModuleNames(); |
| 67 |
if ( $component !== null ) { |
| 68 |
$module = 'test.' . $component; |
| 69 |
if ( !in_array( 'test.' . $component, $modules ) ) { |
| 70 |
throw new HttpError( |
| 71 |
404, |
| 72 |
"No test module found for the '$component' component.\n" |
| 73 |
. "Make sure the extension is enabled via wfLoadExtension(),\n" |
| 74 |
. "and register a test module via the QUnitTestModules attribute in extension.json.", |
| 75 |
'Unknown test module', |
| 76 |
); |
| 77 |
} |
| 78 |
$modules = [ 'test.' . $component ]; |
| 79 |
} |
| 80 |
|
| 81 |
return $modules; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private function exportJS() { |
| 90 |
$out = $this->getOutput(); |
| 91 |
$req = $this->getContext()->getRequest(); |
| 92 |
$rl = $out->getResourceLoader(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
$out->getMetadata()->setPreventClickjacking( false ); |
| 97 |
|
| 98 |
$query = [ |
| 99 |
'lang' => 'qqx', |
| 100 |
'skin' => 'fallback', |
| 101 |
'debug' => $req->getRawVal( 'debug' ), |
| 102 |
'target' => 'test', |
| 103 |
]; |
| 104 |
$embedContext = new RL\Context( $rl, new FauxRequest( $query ) ); |
| 105 |
$query['only'] = 'scripts'; |
| 106 |
$startupContext = new RL\Context( $rl, new FauxRequest( $query ) ); |
| 107 |
|
| 108 |
$component = $req->getRawVal( 'component' ); |
| 109 |
$modules = $this->getModulesForComponentOrThrow( $component ); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
$config = new MultiConfig( [ |
| 114 |
new HashConfig( [ MainConfigNames::ResourceLoaderStorageEnabled => false ] ), |
| 115 |
$rl->getConfig(), |
| 116 |
] ); |
| 117 |
|
| 118 |
|
| 119 |
$startupModule = $rl->getModule( 'startup' ); |
| 120 |
$startupModule->setConfig( $config ); |
| 121 |
$code = $rl->makeModuleResponse( $startupContext, [ 'startup' => $startupModule ] ); |
| 122 |
|
| 123 |
$code .= ResourceLoader::makeLoaderConditionalScript( |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
ResourceLoader::makeConfigSetScript( [ |
| 138 |
|
| 139 |
'wgPageName' => 'Special:Badtitle/JavaScriptTest', |
| 140 |
|
| 141 |
'wgRelevantPageName' => 'Special:Badtitle/JavaScriptTest', |
| 142 |
] ) |
| 143 |
|
| 144 |
. $rl->makeModuleResponse( $embedContext, [ |
| 145 |
'user.options' => $rl->getModule( 'user.options' ), |
| 146 |
] ) |
| 147 |
|
| 148 |
. Html::encodeJsCall( 'mw.loader.load', [ $modules ] ) |
| 149 |
); |
| 150 |
$encModules = Html::encodeJsVar( $modules ); |
| 151 |
$code .= ResourceLoader::makeInlineCodeWithModule( 'mediawiki.base', <<<JAVASCRIPT |
| 152 |
// Wait for each module individually, so that partial failures wont break the page |
| 153 |
// completely by rejecting the promise before all/ any modules are loaded. |
| 154 |
var promises = $encModules.map( function( module ) { |
| 155 |
return mw.loader.using( module ).promise(); |
| 156 |
} ); |
| 157 |
Promise.allSettled( promises ).then( QUnit.start ); |
| 158 |
JAVASCRIPT |
| 159 |
); |
| 160 |
|
| 161 |
header( 'Content-Type: text/javascript; charset=utf-8' ); |
| 162 |
header( 'Cache-Control: private, no-cache, must-revalidate' ); |
| 163 |
echo $code; |
| 164 |
} |
| 165 |
|
| 166 |
private function renderPage() { |
| 167 |
$req = $this->getContext()->getRequest(); |
| 168 |
$component = $req->getRawVal( 'component' ); |
| 169 |
|
| 170 |
$this->getModulesForComponentOrThrow( $component ); |
| 171 |
|
| 172 |
$basePath = $this->getConfig()->get( MainConfigNames::ResourceBasePath ); |
| 173 |
$headHtml = implode( "\n", [ |
| 174 |
Html::linkedStyle( "$basePath/resources/lib/qunitjs/qunit.css" ), |
| 175 |
Html::linkedStyle( "$basePath/resources/src/qunitjs/qunit-local.css" ), |
| 176 |
] ); |
| 177 |
|
| 178 |
$scriptUrl = $this->getPageTitle( 'qunit/export' )->getFullURL( [ |
| 179 |
'debug' => (string)ResourceLoader::inDebugMode(), |
| 180 |
'component' => $component, |
| 181 |
] ); |
| 182 |
$script = implode( "\n", [ |
| 183 |
Html::linkedScript( "$basePath/resources/lib/qunitjs/qunit.js" ), |
| 184 |
Html::inlineScript( 'QUnit.config.autostart = false;' ), |
| 185 |
Html::linkedScript( $scriptUrl ), |
| 186 |
] ); |
| 187 |
|
| 188 |
header( 'Content-Type: text/html; charset=utf-8' ); |
| 189 |
echo <<<HTML |
| 190 |
<!DOCTYPE html> |
| 191 |
<title>QUnit</title> |
| 192 |
$headHtml |
| 193 |
<div id="qunit"></div> |
| 194 |
<div id="qunit-fixture"></div> |
| 195 |
$script |
| 196 |
HTML; |
| 197 |
} |
| 198 |
|
| 199 |
protected function getGroupName() { |
| 200 |
return 'other'; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
class_alias( SpecialJavaScriptTest::class, 'SpecialJavaScriptTest' ); |