| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\ResourceLoader; |
| 4 |
|
| 5 |
use MediaWiki\Config\HashConfig; |
| 6 |
use MediaWiki\Request\FauxRequest; |
| 7 |
use MediaWiki\ResourceLoader\ClientHtml; |
| 8 |
use MediaWiki\ResourceLoader\Context; |
| 9 |
use MediaWiki\ResourceLoader\Module; |
| 10 |
use MediaWiki\ResourceLoader\ResourceLoader; |
| 11 |
use MediaWikiCoversValidator; |
| 12 |
use PHPUnit\Framework\TestCase; |
| 13 |
use Wikimedia\TestingAccessWrapper; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class ClientHtmlTest extends TestCase { |
| 20 |
|
| 21 |
use MediaWikiCoversValidator; |
| 22 |
|
| 23 |
public function testGetData() { |
| 24 |
$context = self::makeContext(); |
| 25 |
$context->getResourceLoader()->register( self::makeSampleModules() ); |
| 26 |
|
| 27 |
$client = new ClientHtml( $context ); |
| 28 |
$client->setModules( [ |
| 29 |
'test', |
| 30 |
'test.private', |
| 31 |
'test.shouldembed.empty', |
| 32 |
'test.shouldembed', |
| 33 |
'test.user', |
| 34 |
'test.unregistered', |
| 35 |
] ); |
| 36 |
$client->setModuleStyles( [ |
| 37 |
'test.styles.mixed', |
| 38 |
'test.styles.user.empty', |
| 39 |
'test.styles.private', |
| 40 |
'test.styles.pure', |
| 41 |
'test.styles.shouldembed', |
| 42 |
'test.styles.deprecated', |
| 43 |
'test.unregistered.styles', |
| 44 |
] ); |
| 45 |
|
| 46 |
$expected = [ |
| 47 |
'states' => [ |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
'test.private' => 'loading', |
| 59 |
'test.shouldembed' => 'loading', |
| 60 |
'test.user' => 'loading', |
| 61 |
|
| 62 |
|
| 63 |
'test.shouldembed.empty' => 'ready', |
| 64 |
'test.styles.pure' => 'ready', |
| 65 |
'test.styles.user.empty' => 'ready', |
| 66 |
'test.styles.private' => 'ready', |
| 67 |
'test.styles.shouldembed' => 'ready', |
| 68 |
'test.styles.deprecated' => 'ready', |
| 69 |
], |
| 70 |
'general' => [ |
| 71 |
'test', |
| 72 |
], |
| 73 |
'styles' => [ |
| 74 |
'test.styles.pure', |
| 75 |
'test.styles.deprecated', |
| 76 |
], |
| 77 |
'embed' => [ |
| 78 |
'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ], |
| 79 |
'general' => [ |
| 80 |
'test.private', |
| 81 |
'test.shouldembed', |
| 82 |
'test.user', |
| 83 |
], |
| 84 |
], |
| 85 |
'styleDeprecations' => [ |
| 86 |
|
| 87 |
"This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message." |
| 88 |
], |
| 89 |
]; |
| 90 |
|
| 91 |
$access = TestingAccessWrapper::newFromObject( $client ); |
| 92 |
$this->assertEquals( $expected, $access->getData() ); |
| 93 |
} |
| 94 |
|
| 95 |
public function testGetHeadHtml() { |
| 96 |
$context = self::makeContext(); |
| 97 |
$context->getResourceLoader()->register( self::makeSampleModules() ); |
| 98 |
|
| 99 |
$client = new ClientHtml( $context ); |
| 100 |
$client->setConfig( [ 'key' => 'value' ] ); |
| 101 |
$client->setModules( [ |
| 102 |
'test', |
| 103 |
'test.private', |
| 104 |
] ); |
| 105 |
$client->setModuleStyles( [ |
| 106 |
'test.styles.pure', |
| 107 |
'test.styles.private', |
| 108 |
'test.styles.deprecated', |
| 109 |
] ); |
| 110 |
$client->setExemptStates( [ |
| 111 |
'test.exempt' => 'ready', |
| 112 |
] ); |
| 113 |
$expected = '<script>' |
| 114 |
. 'document.documentElement.className="client-js";' |
| 115 |
. 'RLCONF={"key":"value"};' |
| 116 |
. 'RLSTATE={"test.exempt":"ready","test.private":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.styles.deprecated":"ready"};' |
| 117 |
. 'RLPAGEMODULES=["test"];' |
| 118 |
. '</script>' . "\n" |
| 119 |
. '<script>(RLQ=window.RLQ||[]).push(function(){' |
| 120 |
. 'mw.loader.impl(function(){return["test.private@{blankVer}",null,{"css":[]}];});' |
| 121 |
. '});</script>' . "\n" |
| 122 |
. '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.deprecated%2Cpure&only=styles">' . "\n" |
| 123 |
. '<style>.private{}</style>' . "\n" |
| 124 |
. '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&raw=1"></script>'; |
| 125 |
|
| 126 |
$expected = self::expandVariables( $expected ); |
| 127 |
|
| 128 |
$this->assertSame( $expected, (string)$client->getHeadHtml() ); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function testGetHeadHtmlWithTarget() { |
| 135 |
$client = new ClientHtml( |
| 136 |
self::makeContext(), |
| 137 |
[ 'target' => 'example' ] |
| 138 |
); |
| 139 |
$expected = '<script>document.documentElement.className="client-js";</script>' . "\n" |
| 140 |
. '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&raw=1&target=example"></script>'; |
| 141 |
|
| 142 |
|
| 143 |
$this->assertSame( $expected, (string)$client->getHeadHtml() ); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function testGetHeadHtmlWithSafemode() { |
| 150 |
$client = new ClientHtml( |
| 151 |
self::makeContext(), |
| 152 |
[ 'safemode' => '1' ] |
| 153 |
); |
| 154 |
$expected = '<script>document.documentElement.className="client-js";</script>' . "\n" |
| 155 |
. '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&raw=1&safemode=1"></script>'; |
| 156 |
|
| 157 |
|
| 158 |
$this->assertSame( $expected, (string)$client->getHeadHtml() ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function testGetHeadHtmlWithNullTarget() { |
| 165 |
$client = new ClientHtml( |
| 166 |
self::makeContext(), |
| 167 |
[ 'target' => null ] |
| 168 |
); |
| 169 |
$expected = '<script>document.documentElement.className="client-js";</script>' . "\n" |
| 170 |
. '<script async="" src="/w/load.php?lang=nl&modules=startup&only=scripts&raw=1"></script>'; |
| 171 |
|
| 172 |
|
| 173 |
$this->assertSame( $expected, (string)$client->getHeadHtml() ); |
| 174 |
} |
| 175 |
|
| 176 |
public function testGetBodyHtml() { |
| 177 |
$context = self::makeContext(); |
| 178 |
$context->getResourceLoader()->register( self::makeSampleModules() ); |
| 179 |
|
| 180 |
$client = new ClientHtml( $context ); |
| 181 |
$client->setConfig( [ 'key' => 'value' ] ); |
| 182 |
$client->setModules( [ |
| 183 |
'test', |
| 184 |
'test.private.bottom', |
| 185 |
] ); |
| 186 |
$client->setModuleStyles( [ |
| 187 |
'test.styles.deprecated', |
| 188 |
] ); |
| 189 |
$expected = '<script>(RLQ=window.RLQ||[]).push(function(){' |
| 190 |
. 'mw.log.warn("This page is using the deprecated ResourceLoader module \"test.styles.deprecated\".\nDeprecation message.");' |
| 191 |
. '});</script>'; |
| 192 |
|
| 193 |
|
| 194 |
$this->assertSame( $expected, (string)$client->getBodyHtml() ); |
| 195 |
} |
| 196 |
|
| 197 |
public static function provideMakeLoad() { |
| 198 |
return [ |
| 199 |
[ |
| 200 |
'context' => [], |
| 201 |
'modules' => [ 'test.unknown' ], |
| 202 |
'only' => Module::TYPE_STYLES, |
| 203 |
'extra' => [], |
| 204 |
'output' => '', |
| 205 |
], |
| 206 |
[ |
| 207 |
'context' => [], |
| 208 |
'modules' => [ 'test.styles.private' ], |
| 209 |
'only' => Module::TYPE_STYLES, |
| 210 |
'extra' => [], |
| 211 |
'output' => '<style>.private{}</style>', |
| 212 |
], |
| 213 |
[ |
| 214 |
'context' => [], |
| 215 |
'modules' => [ 'test.private' ], |
| 216 |
'only' => Module::TYPE_COMBINED, |
| 217 |
'extra' => [], |
| 218 |
'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["test.private@{blankVer}",null,{"css":[]}];});});</script>', |
| 219 |
], |
| 220 |
[ |
| 221 |
'context' => [], |
| 222 |
'modules' => [ 'test.scripts' ], |
| 223 |
'only' => Module::TYPE_SCRIPTS, |
| 224 |
|
| 225 |
'extra' => [ 'raw' => '1' ], |
| 226 |
'output' => '<script async="" src="/w/load.php?lang=nl&modules=test.scripts&only=scripts&raw=1"></script>', |
| 227 |
], |
| 228 |
[ |
| 229 |
'context' => [], |
| 230 |
'modules' => [ 'test.scripts.user' ], |
| 231 |
'only' => Module::TYPE_SCRIPTS, |
| 232 |
'extra' => [], |
| 233 |
'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026user=Example\u0026version={blankCombi}");});</script>', |
| 234 |
], |
| 235 |
[ |
| 236 |
'context' => [], |
| 237 |
'modules' => [ 'test.user' ], |
| 238 |
'only' => Module::TYPE_COMBINED, |
| 239 |
'extra' => [], |
| 240 |
'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test.user\u0026user=Example\u0026version={blankCombi}");});</script>', |
| 241 |
], |
| 242 |
[ |
| 243 |
'context' => [ 'debug' => 'true' ], |
| 244 |
'modules' => [ 'test.styles.pure', 'test.styles.mixed' ], |
| 245 |
'only' => Module::TYPE_STYLES, |
| 246 |
'extra' => [], |
| 247 |
'output' => '<link rel="stylesheet" href="/w/load.php?debug=1&lang=nl&modules=test.styles.mixed&only=styles">' . "\n" |
| 248 |
. '<link rel="stylesheet" href="/w/load.php?debug=1&lang=nl&modules=test.styles.pure&only=styles">', |
| 249 |
], |
| 250 |
[ |
| 251 |
'context' => [ 'debug' => 'false' ], |
| 252 |
'modules' => [ 'test.styles.pure', 'test.styles.mixed' ], |
| 253 |
'only' => Module::TYPE_STYLES, |
| 254 |
'extra' => [], |
| 255 |
'output' => '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.mixed%2Cpure&only=styles">', |
| 256 |
], |
| 257 |
[ |
| 258 |
'context' => [], |
| 259 |
'modules' => [ 'test.styles.noscript' ], |
| 260 |
'only' => Module::TYPE_STYLES, |
| 261 |
'extra' => [], |
| 262 |
'output' => '<noscript><link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.noscript&only=styles"></noscript>', |
| 263 |
], |
| 264 |
[ |
| 265 |
'context' => [], |
| 266 |
'modules' => [ 'test.shouldembed' ], |
| 267 |
'only' => Module::TYPE_COMBINED, |
| 268 |
'extra' => [], |
| 269 |
'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["test.shouldembed@{blankVer}",null,{"css":[]}];});});</script>', |
| 270 |
], |
| 271 |
[ |
| 272 |
'context' => [], |
| 273 |
'modules' => [ 'test.styles.shouldembed' ], |
| 274 |
'only' => Module::TYPE_STYLES, |
| 275 |
'extra' => [], |
| 276 |
'output' => '<style>.shouldembed{}</style>', |
| 277 |
], |
| 278 |
[ |
| 279 |
'context' => [], |
| 280 |
'modules' => [ 'test.scripts.shouldembed' ], |
| 281 |
'only' => Module::TYPE_SCRIPTS, |
| 282 |
'extra' => [], |
| 283 |
'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>', |
| 284 |
], |
| 285 |
[ |
| 286 |
'context' => [], |
| 287 |
'modules' => [ 'test', 'test.shouldembed' ], |
| 288 |
'only' => Module::TYPE_COMBINED, |
| 289 |
'extra' => [], |
| 290 |
'output' => '<script>(RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?lang=nl\u0026modules=test");mw.loader.impl(function(){return["test.shouldembed@{blankVer}",null,{"css":[]}];});});</script>', |
| 291 |
], |
| 292 |
[ |
| 293 |
'context' => [], |
| 294 |
'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ], |
| 295 |
'only' => Module::TYPE_STYLES, |
| 296 |
'extra' => [], |
| 297 |
'output' => |
| 298 |
'<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.styles.pure&only=styles">' . "\n" |
| 299 |
. '<style>.shouldembed{}</style>' |
| 300 |
], |
| 301 |
[ |
| 302 |
'context' => [], |
| 303 |
'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ], |
| 304 |
'only' => Module::TYPE_STYLES, |
| 305 |
'extra' => [], |
| 306 |
'output' => |
| 307 |
'<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.ordering.a%2Cb&only=styles">' . "\n" |
| 308 |
. '<style>.orderingC{}.orderingD{}</style>' . "\n" |
| 309 |
. '<link rel="stylesheet" href="/w/load.php?lang=nl&modules=test.ordering.e&only=styles">' |
| 310 |
], |
| 311 |
]; |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public function testMakeLoad( |
| 322 |
array $contextQuery, |
| 323 |
array $modules, |
| 324 |
$type, |
| 325 |
array $extraQuery, |
| 326 |
$expected |
| 327 |
) { |
| 328 |
$context = self::makeContext( $contextQuery ); |
| 329 |
$context->getResourceLoader()->register( self::makeSampleModules() ); |
| 330 |
$actual = ClientHtml::makeLoad( $context, $modules, $type, $extraQuery, false ); |
| 331 |
$expected = self::expandVariables( $expected ); |
| 332 |
$this->assertSame( $expected, (string)$actual ); |
| 333 |
} |
| 334 |
|
| 335 |
public function testGetDocumentAttributes() { |
| 336 |
$client = new ClientHtml( self::makeContext() ); |
| 337 |
$this->assertIsArray( $client->getDocumentAttributes() ); |
| 338 |
} |
| 339 |
|
| 340 |
private static function expandVariables( $text ) { |
| 341 |
return strtr( $text, [ |
| 342 |
'{blankCombi}' => ResourceLoaderTestCase::BLANK_COMBI, |
| 343 |
'{blankVer}' => ResourceLoaderTestCase::BLANK_VERSION |
| 344 |
] ); |
| 345 |
} |
| 346 |
|
| 347 |
private static function makeContext( $extraQuery = [] ) { |
| 348 |
$conf = new HashConfig( [] ); |
| 349 |
return new Context( |
| 350 |
new ResourceLoader( $conf, null, null, [ |
| 351 |
'loadScript' => '/w/load.php', |
| 352 |
] ), |
| 353 |
new FauxRequest( array_merge( [ |
| 354 |
'lang' => 'nl', |
| 355 |
'skin' => 'fallback', |
| 356 |
'user' => 'Example', |
| 357 |
'target' => 'phpunit', |
| 358 |
], $extraQuery ) ) |
| 359 |
); |
| 360 |
} |
| 361 |
|
| 362 |
private static function makeModule( array $options = [] ) { |
| 363 |
return $options + [ 'class' => ResourceLoaderTestModule::class ]; |
| 364 |
} |
| 365 |
|
| 366 |
private static function makeSampleModules() { |
| 367 |
$modules = [ |
| 368 |
'test' => [], |
| 369 |
'test.private' => [ 'group' => 'private' ], |
| 370 |
'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ], |
| 371 |
'test.shouldembed' => [ 'shouldEmbed' => true ], |
| 372 |
'test.user' => [ 'group' => 'user' ], |
| 373 |
|
| 374 |
'test.styles.pure' => [ 'type' => Module::LOAD_STYLES ], |
| 375 |
'test.styles.mixed' => [], |
| 376 |
'test.styles.noscript' => [ |
| 377 |
'type' => Module::LOAD_STYLES, |
| 378 |
'group' => 'noscript', |
| 379 |
], |
| 380 |
'test.styles.user' => [ |
| 381 |
'type' => Module::LOAD_STYLES, |
| 382 |
'group' => 'user', |
| 383 |
], |
| 384 |
'test.styles.user.empty' => [ |
| 385 |
'type' => Module::LOAD_STYLES, |
| 386 |
'group' => 'user', |
| 387 |
'isKnownEmpty' => true, |
| 388 |
], |
| 389 |
'test.styles.private' => [ |
| 390 |
'type' => Module::LOAD_STYLES, |
| 391 |
'group' => 'private', |
| 392 |
'styles' => '.private{}', |
| 393 |
], |
| 394 |
'test.styles.shouldembed' => [ |
| 395 |
'type' => Module::LOAD_STYLES, |
| 396 |
'shouldEmbed' => true, |
| 397 |
'styles' => '.shouldembed{}', |
| 398 |
], |
| 399 |
'test.styles.deprecated' => [ |
| 400 |
'type' => Module::LOAD_STYLES, |
| 401 |
'deprecated' => 'Deprecation message.', |
| 402 |
], |
| 403 |
|
| 404 |
'test.scripts' => [], |
| 405 |
'test.scripts.user' => [ 'group' => 'user' ], |
| 406 |
'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ], |
| 407 |
'test.scripts.shouldembed' => [ 'shouldEmbed' => true ], |
| 408 |
|
| 409 |
'test.ordering.a' => [ 'shouldEmbed' => false ], |
| 410 |
'test.ordering.b' => [ 'shouldEmbed' => false ], |
| 411 |
'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ], |
| 412 |
'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ], |
| 413 |
'test.ordering.e' => [ 'shouldEmbed' => false ], |
| 414 |
]; |
| 415 |
return array_map( static function ( $options ) { |
| 416 |
return self::makeModule( $options ); |
| 417 |
}, $modules ); |
| 418 |
} |
| 419 |
} |