| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\ResourceLoader; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\ResourceLoader\Module; |
| 7 |
use MediaWiki\ResourceLoader\StartUpModule; |
| 8 |
use Psr\Log\NullLogger; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class StartUpModuleTest extends ResourceLoaderTestCase { |
| 15 |
|
| 16 |
protected static function expandPlaceholders( $text ) { |
| 17 |
return strtr( $text, [ |
| 18 |
'{blankVer}' => self::BLANK_VERSION |
| 19 |
] ); |
| 20 |
} |
| 21 |
|
| 22 |
public static function provideGetModuleRegistrations() { |
| 23 |
return [ |
| 24 |
[ [ |
| 25 |
'msg' => 'Empty registry', |
| 26 |
'modules' => [], |
| 27 |
'out' => ' |
| 28 |
mw.loader.addSource({ |
| 29 |
"local": "/w/load.php" |
| 30 |
}); |
| 31 |
mw.loader.register([]);' |
| 32 |
] ], |
| 33 |
[ [ |
| 34 |
'msg' => 'Basic registry', |
| 35 |
'modules' => [ |
| 36 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 37 |
], |
| 38 |
'out' => ' |
| 39 |
mw.loader.addSource({ |
| 40 |
"local": "/w/load.php" |
| 41 |
}); |
| 42 |
mw.loader.register([ |
| 43 |
[ |
| 44 |
"test.blank", |
| 45 |
"" |
| 46 |
] |
| 47 |
]);', |
| 48 |
] ], |
| 49 |
[ [ |
| 50 |
'msg' => 'Optimise the dependency tree (basic case)', |
| 51 |
'modules' => [ |
| 52 |
'a' => [ |
| 53 |
'class' => ResourceLoaderTestModule::class, |
| 54 |
'dependencies' => [ 'b', 'c', 'd' ], |
| 55 |
], |
| 56 |
'b' => [ |
| 57 |
'class' => ResourceLoaderTestModule::class, |
| 58 |
'dependencies' => [ 'c' ], |
| 59 |
], |
| 60 |
'c' => [ |
| 61 |
'class' => ResourceLoaderTestModule::class, |
| 62 |
'dependencies' => [], |
| 63 |
], |
| 64 |
'd' => [ |
| 65 |
'class' => ResourceLoaderTestModule::class, |
| 66 |
'dependencies' => [], |
| 67 |
], |
| 68 |
], |
| 69 |
'out' => ' |
| 70 |
mw.loader.addSource({ |
| 71 |
"local": "/w/load.php" |
| 72 |
}); |
| 73 |
mw.loader.register([ |
| 74 |
[ |
| 75 |
"a", |
| 76 |
"", |
| 77 |
[ |
| 78 |
1, |
| 79 |
3 |
| 80 |
] |
| 81 |
], |
| 82 |
[ |
| 83 |
"b", |
| 84 |
"", |
| 85 |
[ |
| 86 |
2 |
| 87 |
] |
| 88 |
], |
| 89 |
[ |
| 90 |
"c", |
| 91 |
"" |
| 92 |
], |
| 93 |
[ |
| 94 |
"d", |
| 95 |
"" |
| 96 |
] |
| 97 |
]);', |
| 98 |
] ], |
| 99 |
[ [ |
| 100 |
'msg' => 'Optimise the dependency tree (tolerate unknown deps)', |
| 101 |
'modules' => [ |
| 102 |
'a' => [ |
| 103 |
'class' => ResourceLoaderTestModule::class, |
| 104 |
'dependencies' => [ 'b', 'c', 'x' ] |
| 105 |
], |
| 106 |
'b' => [ |
| 107 |
'class' => ResourceLoaderTestModule::class, |
| 108 |
'dependencies' => [ 'c', 'x' ] |
| 109 |
], |
| 110 |
'c' => [ |
| 111 |
'class' => ResourceLoaderTestModule::class, |
| 112 |
'dependencies' => [] |
| 113 |
], |
| 114 |
], |
| 115 |
'out' => ' |
| 116 |
mw.loader.addSource({ |
| 117 |
"local": "/w/load.php" |
| 118 |
}); |
| 119 |
mw.loader.register([ |
| 120 |
[ |
| 121 |
"a", |
| 122 |
"", |
| 123 |
[ |
| 124 |
1, |
| 125 |
"x" |
| 126 |
] |
| 127 |
], |
| 128 |
[ |
| 129 |
"b", |
| 130 |
"", |
| 131 |
[ |
| 132 |
2, |
| 133 |
"x" |
| 134 |
] |
| 135 |
], |
| 136 |
[ |
| 137 |
"c", |
| 138 |
"" |
| 139 |
] |
| 140 |
]);', |
| 141 |
] ], |
| 142 |
[ [ |
| 143 |
|
| 144 |
'msg' => 'Optimise the dependency tree (indirect circular dependency)', |
| 145 |
'modules' => [ |
| 146 |
'top' => [ |
| 147 |
'class' => ResourceLoaderTestModule::class, |
| 148 |
'dependencies' => [ 'middle1', 'util' ], |
| 149 |
], |
| 150 |
'middle1' => [ |
| 151 |
'class' => ResourceLoaderTestModule::class, |
| 152 |
'dependencies' => [ 'middle2', 'util' ], |
| 153 |
], |
| 154 |
'middle2' => [ |
| 155 |
'class' => ResourceLoaderTestModule::class, |
| 156 |
'dependencies' => [ 'bottom' ], |
| 157 |
], |
| 158 |
'bottom' => [ |
| 159 |
'class' => ResourceLoaderTestModule::class, |
| 160 |
'dependencies' => [ 'top' ], |
| 161 |
], |
| 162 |
'util' => [ |
| 163 |
'class' => ResourceLoaderTestModule::class, |
| 164 |
'dependencies' => [], |
| 165 |
], |
| 166 |
], |
| 167 |
'out' => ' |
| 168 |
mw.loader.addSource({ |
| 169 |
"local": "/w/load.php" |
| 170 |
}); |
| 171 |
mw.loader.register([ |
| 172 |
[ |
| 173 |
"top", |
| 174 |
"", |
| 175 |
[ |
| 176 |
1, |
| 177 |
4 |
| 178 |
] |
| 179 |
], |
| 180 |
[ |
| 181 |
"middle1", |
| 182 |
"", |
| 183 |
[ |
| 184 |
2, |
| 185 |
4 |
| 186 |
] |
| 187 |
], |
| 188 |
[ |
| 189 |
"middle2", |
| 190 |
"", |
| 191 |
[ |
| 192 |
3 |
| 193 |
] |
| 194 |
], |
| 195 |
[ |
| 196 |
"bottom", |
| 197 |
"", |
| 198 |
[ |
| 199 |
0 |
| 200 |
] |
| 201 |
], |
| 202 |
[ |
| 203 |
"util", |
| 204 |
"" |
| 205 |
] |
| 206 |
]);', |
| 207 |
] ], |
| 208 |
[ [ |
| 209 |
|
| 210 |
'msg' => 'Optimise the dependency tree (direct circular dependency)', |
| 211 |
'modules' => [ |
| 212 |
'top' => [ |
| 213 |
'class' => ResourceLoaderTestModule::class, |
| 214 |
'dependencies' => [ 'util', 'top' ], |
| 215 |
], |
| 216 |
'util' => [ |
| 217 |
'class' => ResourceLoaderTestModule::class, |
| 218 |
'dependencies' => [], |
| 219 |
], |
| 220 |
], |
| 221 |
'out' => ' |
| 222 |
mw.loader.addSource({ |
| 223 |
"local": "/w/load.php" |
| 224 |
}); |
| 225 |
mw.loader.register([ |
| 226 |
[ |
| 227 |
"top", |
| 228 |
"", |
| 229 |
[ |
| 230 |
1, |
| 231 |
0 |
| 232 |
] |
| 233 |
], |
| 234 |
[ |
| 235 |
"util", |
| 236 |
"" |
| 237 |
] |
| 238 |
]);', |
| 239 |
] ], |
| 240 |
[ [ |
| 241 |
'msg' => 'Group signature', |
| 242 |
'modules' => [ |
| 243 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 244 |
'test.group.foo' => [ |
| 245 |
'class' => ResourceLoaderTestModule::class, |
| 246 |
'group' => 'x-foo', |
| 247 |
], |
| 248 |
'test.group.bar' => [ |
| 249 |
'class' => ResourceLoaderTestModule::class, |
| 250 |
'group' => 'x-bar', |
| 251 |
], |
| 252 |
], |
| 253 |
'out' => ' |
| 254 |
mw.loader.addSource({ |
| 255 |
"local": "/w/load.php" |
| 256 |
}); |
| 257 |
mw.loader.register([ |
| 258 |
[ |
| 259 |
"test.blank", |
| 260 |
"" |
| 261 |
], |
| 262 |
[ |
| 263 |
"test.group.foo", |
| 264 |
"", |
| 265 |
[], |
| 266 |
2 |
| 267 |
], |
| 268 |
[ |
| 269 |
"test.group.bar", |
| 270 |
"", |
| 271 |
[], |
| 272 |
3 |
| 273 |
] |
| 274 |
]);' |
| 275 |
] ], |
| 276 |
[ [ |
| 277 |
'msg' => 'Different skin (irrelevant skin modules should not be registered)', |
| 278 |
'modules' => [ |
| 279 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 280 |
'test.skin.fallback' => [ |
| 281 |
'class' => ResourceLoaderTestModule::class, |
| 282 |
'skins' => [ 'fallback' ], |
| 283 |
], |
| 284 |
'test.skin.foo' => [ |
| 285 |
'class' => ResourceLoaderTestModule::class, |
| 286 |
'skins' => [ 'foo' ], |
| 287 |
], |
| 288 |
], |
| 289 |
'out' => ' |
| 290 |
mw.loader.addSource({ |
| 291 |
"local": "/w/load.php" |
| 292 |
}); |
| 293 |
mw.loader.register([ |
| 294 |
[ |
| 295 |
"test.blank", |
| 296 |
"" |
| 297 |
], |
| 298 |
[ |
| 299 |
"test.skin.fallback", |
| 300 |
"" |
| 301 |
] |
| 302 |
]);' |
| 303 |
] ], |
| 304 |
[ [ |
| 305 |
'msg' => 'Safemode disabled (default; register all modules)', |
| 306 |
'modules' => [ |
| 307 |
|
| 308 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 309 |
'test.core-generated' => [ |
| 310 |
'class' => ResourceLoaderTestModule::class, |
| 311 |
'origin' => Module::ORIGIN_CORE_INDIVIDUAL |
| 312 |
], |
| 313 |
'test.sitewide' => [ |
| 314 |
'class' => ResourceLoaderTestModule::class, |
| 315 |
'origin' => Module::ORIGIN_USER_SITEWIDE |
| 316 |
], |
| 317 |
'test.user' => [ |
| 318 |
'class' => ResourceLoaderTestModule::class, |
| 319 |
'origin' => Module::ORIGIN_USER_INDIVIDUAL |
| 320 |
], |
| 321 |
], |
| 322 |
'out' => ' |
| 323 |
mw.loader.addSource({ |
| 324 |
"local": "/w/load.php" |
| 325 |
}); |
| 326 |
mw.loader.register([ |
| 327 |
[ |
| 328 |
"test.blank", |
| 329 |
"" |
| 330 |
], |
| 331 |
[ |
| 332 |
"test.core-generated", |
| 333 |
"" |
| 334 |
], |
| 335 |
[ |
| 336 |
"test.sitewide", |
| 337 |
"" |
| 338 |
], |
| 339 |
[ |
| 340 |
"test.user", |
| 341 |
"" |
| 342 |
] |
| 343 |
]);' |
| 344 |
] ], |
| 345 |
[ [ |
| 346 |
'msg' => 'Safemode enabled (filter modules with user/site origin)', |
| 347 |
'extraQuery' => [ 'safemode' => '1' ], |
| 348 |
'modules' => [ |
| 349 |
|
| 350 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 351 |
'test.core-generated' => [ |
| 352 |
'class' => ResourceLoaderTestModule::class, |
| 353 |
'origin' => Module::ORIGIN_CORE_INDIVIDUAL |
| 354 |
], |
| 355 |
'test.sitewide' => [ |
| 356 |
'class' => ResourceLoaderTestModule::class, |
| 357 |
'origin' => Module::ORIGIN_USER_SITEWIDE |
| 358 |
], |
| 359 |
'test.user' => [ |
| 360 |
'class' => ResourceLoaderTestModule::class, |
| 361 |
'origin' => Module::ORIGIN_USER_INDIVIDUAL |
| 362 |
], |
| 363 |
], |
| 364 |
'out' => ' |
| 365 |
mw.loader.addSource({ |
| 366 |
"local": "/w/load.php" |
| 367 |
}); |
| 368 |
mw.loader.register([ |
| 369 |
[ |
| 370 |
"test.blank", |
| 371 |
"" |
| 372 |
], |
| 373 |
[ |
| 374 |
"test.core-generated", |
| 375 |
"" |
| 376 |
] |
| 377 |
]);' |
| 378 |
] ], |
| 379 |
[ [ |
| 380 |
'msg' => 'Foreign source', |
| 381 |
'sources' => [ |
| 382 |
'example' => [ |
| 383 |
'loadScript' => 'http://example.org/w/load.php', |
| 384 |
'apiScript' => 'http://example.org/w/api.php', |
| 385 |
], |
| 386 |
], |
| 387 |
'modules' => [ |
| 388 |
'test.blank' => [ |
| 389 |
'class' => ResourceLoaderTestModule::class, |
| 390 |
'source' => 'example' |
| 391 |
], |
| 392 |
], |
| 393 |
'out' => ' |
| 394 |
mw.loader.addSource({ |
| 395 |
"local": "/w/load.php", |
| 396 |
"example": "http://example.org/w/load.php" |
| 397 |
}); |
| 398 |
mw.loader.register([ |
| 399 |
[ |
| 400 |
"test.blank", |
| 401 |
"", |
| 402 |
[], |
| 403 |
null, |
| 404 |
"example" |
| 405 |
] |
| 406 |
]);' |
| 407 |
] ], |
| 408 |
[ [ |
| 409 |
'msg' => 'Conditional dependency function', |
| 410 |
'modules' => [ |
| 411 |
'test.x.core' => [ 'class' => ResourceLoaderTestModule::class ], |
| 412 |
'test.x.polyfill' => [ |
| 413 |
'class' => ResourceLoaderTestModule::class, |
| 414 |
'skipFunction' => 'return true;' |
| 415 |
], |
| 416 |
'test.y.polyfill' => [ |
| 417 |
'class' => ResourceLoaderTestModule::class, |
| 418 |
'skipFunction' => |
| 419 |
'return !!(' . |
| 420 |
' window.JSON &&' . |
| 421 |
' JSON.parse &&' . |
| 422 |
' JSON.stringify' . |
| 423 |
');' |
| 424 |
], |
| 425 |
'test.z.foo' => [ |
| 426 |
'class' => ResourceLoaderTestModule::class, |
| 427 |
'dependencies' => [ |
| 428 |
'test.x.core', |
| 429 |
'test.x.polyfill', |
| 430 |
'test.y.polyfill', |
| 431 |
], |
| 432 |
], |
| 433 |
], |
| 434 |
'out' => ' |
| 435 |
mw.loader.addSource({ |
| 436 |
"local": "/w/load.php" |
| 437 |
}); |
| 438 |
mw.loader.register([ |
| 439 |
[ |
| 440 |
"test.x.core", |
| 441 |
"" |
| 442 |
], |
| 443 |
[ |
| 444 |
"test.x.polyfill", |
| 445 |
"", |
| 446 |
[], |
| 447 |
null, |
| 448 |
null, |
| 449 |
"return true;" |
| 450 |
], |
| 451 |
[ |
| 452 |
"test.y.polyfill", |
| 453 |
"", |
| 454 |
[], |
| 455 |
null, |
| 456 |
null, |
| 457 |
"return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);" |
| 458 |
], |
| 459 |
[ |
| 460 |
"test.z.foo", |
| 461 |
"", |
| 462 |
[ |
| 463 |
0, |
| 464 |
1, |
| 465 |
2 |
| 466 |
] |
| 467 |
] |
| 468 |
]);', |
| 469 |
] ], |
| 470 |
[ [ |
| 471 |
'msg' => 'ES6-only module', |
| 472 |
'modules' => [ |
| 473 |
'test.es6' => [ |
| 474 |
'class' => ResourceLoaderTestModule::class, |
| 475 |
], |
| 476 |
], |
| 477 |
'out' => ' |
| 478 |
mw.loader.addSource({ |
| 479 |
"local": "/w/load.php" |
| 480 |
}); |
| 481 |
mw.loader.register([ |
| 482 |
[ |
| 483 |
"test.es6", |
| 484 |
"" |
| 485 |
] |
| 486 |
]);', |
| 487 |
] ], |
| 488 |
[ [ |
| 489 |
'msg' => 'noscript group omitted (T291735)', |
| 490 |
'modules' => [ |
| 491 |
'test.not-noscript' => [ |
| 492 |
'class' => ResourceLoaderTestModule::class, |
| 493 |
], |
| 494 |
'test.noscript' => [ |
| 495 |
'class' => ResourceLoaderTestModule::class, |
| 496 |
'group' => 'noscript', |
| 497 |
], |
| 498 |
'test.also-noscript' => [ |
| 499 |
'class' => ResourceLoaderTestModule::class, |
| 500 |
'group' => 'noscript', |
| 501 |
], |
| 502 |
], |
| 503 |
'out' => ' |
| 504 |
mw.loader.addSource({ |
| 505 |
"local": "/w/load.php" |
| 506 |
}); |
| 507 |
mw.loader.register([ |
| 508 |
[ |
| 509 |
"test.not-noscript", |
| 510 |
"" |
| 511 |
] |
| 512 |
]);', |
| 513 |
] ], |
| 514 |
[ [ |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
'msg' => 'Advanced (everything combined)', |
| 520 |
'sources' => [ |
| 521 |
'example' => [ |
| 522 |
'loadScript' => 'http://example.org/w/load.php', |
| 523 |
'apiScript' => 'http://example.org/w/api.php', |
| 524 |
], |
| 525 |
], |
| 526 |
'modules' => [ |
| 527 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 528 |
'test.x.core' => [ 'class' => ResourceLoaderTestModule::class ], |
| 529 |
'test.x.util' => [ |
| 530 |
'class' => ResourceLoaderTestModule::class, |
| 531 |
'dependencies' => [ |
| 532 |
'test.x.core', |
| 533 |
], |
| 534 |
], |
| 535 |
'test.x.foo' => [ |
| 536 |
'class' => ResourceLoaderTestModule::class, |
| 537 |
'dependencies' => [ |
| 538 |
'test.x.core', |
| 539 |
], |
| 540 |
], |
| 541 |
'test.x.bar' => [ |
| 542 |
'class' => ResourceLoaderTestModule::class, |
| 543 |
'dependencies' => [ |
| 544 |
'test.x.core', |
| 545 |
'test.x.util', |
| 546 |
], |
| 547 |
], |
| 548 |
'test.x.quux' => [ |
| 549 |
'class' => ResourceLoaderTestModule::class, |
| 550 |
'dependencies' => [ |
| 551 |
'test.x.foo', |
| 552 |
'test.x.bar', |
| 553 |
'test.x.util', |
| 554 |
'test.x.unknown', |
| 555 |
], |
| 556 |
], |
| 557 |
'test.group.foo.1' => [ |
| 558 |
'class' => ResourceLoaderTestModule::class, |
| 559 |
'group' => 'x-foo', |
| 560 |
], |
| 561 |
'test.group.foo.2' => [ |
| 562 |
'class' => ResourceLoaderTestModule::class, |
| 563 |
'group' => 'x-foo', |
| 564 |
], |
| 565 |
'test.group.bar.1' => [ |
| 566 |
'class' => ResourceLoaderTestModule::class, |
| 567 |
'group' => 'x-bar', |
| 568 |
], |
| 569 |
'test.group.bar.2' => [ |
| 570 |
'class' => ResourceLoaderTestModule::class, |
| 571 |
'group' => 'x-bar', |
| 572 |
'source' => 'example', |
| 573 |
], |
| 574 |
'test.es6' => [ |
| 575 |
'class' => ResourceLoaderTestModule::class, |
| 576 |
] |
| 577 |
], |
| 578 |
'out' => ' |
| 579 |
mw.loader.addSource({ |
| 580 |
"local": "/w/load.php", |
| 581 |
"example": "http://example.org/w/load.php" |
| 582 |
}); |
| 583 |
mw.loader.register([ |
| 584 |
[ |
| 585 |
"test.blank", |
| 586 |
"" |
| 587 |
], |
| 588 |
[ |
| 589 |
"test.x.core", |
| 590 |
"" |
| 591 |
], |
| 592 |
[ |
| 593 |
"test.x.util", |
| 594 |
"", |
| 595 |
[ |
| 596 |
1 |
| 597 |
] |
| 598 |
], |
| 599 |
[ |
| 600 |
"test.x.foo", |
| 601 |
"", |
| 602 |
[ |
| 603 |
1 |
| 604 |
] |
| 605 |
], |
| 606 |
[ |
| 607 |
"test.x.bar", |
| 608 |
"", |
| 609 |
[ |
| 610 |
2 |
| 611 |
] |
| 612 |
], |
| 613 |
[ |
| 614 |
"test.x.quux", |
| 615 |
"", |
| 616 |
[ |
| 617 |
3, |
| 618 |
4, |
| 619 |
"test.x.unknown" |
| 620 |
] |
| 621 |
], |
| 622 |
[ |
| 623 |
"test.group.foo.1", |
| 624 |
"", |
| 625 |
[], |
| 626 |
2 |
| 627 |
], |
| 628 |
[ |
| 629 |
"test.group.foo.2", |
| 630 |
"", |
| 631 |
[], |
| 632 |
2 |
| 633 |
], |
| 634 |
[ |
| 635 |
"test.group.bar.1", |
| 636 |
"", |
| 637 |
[], |
| 638 |
3 |
| 639 |
], |
| 640 |
[ |
| 641 |
"test.group.bar.2", |
| 642 |
"", |
| 643 |
[], |
| 644 |
3, |
| 645 |
"example" |
| 646 |
], |
| 647 |
[ |
| 648 |
"test.es6", |
| 649 |
"" |
| 650 |
] |
| 651 |
]);' |
| 652 |
] ], |
| 653 |
]; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
public function testGetModuleRegistrations( $case ) { |
| 660 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 661 |
|
| 662 |
$extraQuery = $case['extraQuery'] ?? []; |
| 663 |
$context = $this->getResourceLoaderContext( $extraQuery ); |
| 664 |
$rl = $context->getResourceLoader(); |
| 665 |
if ( isset( $case['sources'] ) ) { |
| 666 |
$rl->addSource( $case['sources'] ); |
| 667 |
} |
| 668 |
$rl->register( $case['modules'] ); |
| 669 |
$module = new StartUpModule(); |
| 670 |
$module->setConfig( $rl->getConfig() ); |
| 671 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 672 |
$out = ltrim( $case['out'], "\n" ); |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
$this->setLogger( 'exception', new NullLogger() ); |
| 677 |
|
| 678 |
$this->assertEquals( |
| 679 |
self::expandPlaceholders( $out ), |
| 680 |
$module->getModuleRegistrations( $context ), |
| 681 |
$case['msg'] |
| 682 |
); |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public function provideGetModuleRegistrationsProduction() { |
| 691 |
yield 'Version falls back gracefully if getModuleContent throws' => [ [ |
| 692 |
'modules' => [ |
| 693 |
'test.fail' => [ |
| 694 |
'factory' => function () { |
| 695 |
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 696 |
->onlyMethods( [ 'getModuleContent' ] )->getMock(); |
| 697 |
$mock->method( 'getModuleContent' )->willThrowException( new Exception ); |
| 698 |
return $mock; |
| 699 |
} |
| 700 |
] |
| 701 |
], |
| 702 |
'out' => 'mw.loader.addSource({"local":"/w/load.php"});' . "\n" |
| 703 |
. 'mw.loader.register([["test.fail",""]]);' . "\n" |
| 704 |
. 'mw.loader.state({"test.fail":"error"});', |
| 705 |
] ]; |
| 706 |
yield 'Version falls back gracefully if getDefinitionSummary throws' => [ [ |
| 707 |
'modules' => [ |
| 708 |
'test.fail' => [ |
| 709 |
'factory' => function () { |
| 710 |
$mock = $this->getMockBuilder( ResourceLoaderTestModule::class ) |
| 711 |
->onlyMethods( [ |
| 712 |
'enableModuleContentVersion', |
| 713 |
'getDefinitionSummary' |
| 714 |
] ) |
| 715 |
->getMock(); |
| 716 |
$mock->method( 'enableModuleContentVersion' )->willReturn( false ); |
| 717 |
$mock->method( 'getDefinitionSummary' )->willThrowException( new Exception ); |
| 718 |
return $mock; |
| 719 |
} |
| 720 |
] |
| 721 |
], |
| 722 |
'out' => 'mw.loader.addSource({"local":"/w/load.php"});' . "\n" |
| 723 |
. 'mw.loader.register([["test.fail",""]]);' . "\n" |
| 724 |
. 'mw.loader.state({"test.fail":"error"});', |
| 725 |
] ]; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
public function testGetModuleRegistrationsProduction( array $case ) { |
| 732 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 733 |
|
| 734 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ] ); |
| 735 |
$rl = $context->getResourceLoader(); |
| 736 |
$rl->register( $case['modules'] ); |
| 737 |
$module = new StartUpModule(); |
| 738 |
$module->setConfig( $rl->getConfig() ); |
| 739 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 740 |
$out = ltrim( $case['out'], "\n" ); |
| 741 |
|
| 742 |
|
| 743 |
$this->setLogger( 'exception', new NullLogger() ); |
| 744 |
|
| 745 |
$this->assertEquals( |
| 746 |
self::expandPlaceholders( $out ), |
| 747 |
$module->getModuleRegistrations( $context ) |
| 748 |
); |
| 749 |
} |
| 750 |
|
| 751 |
public function testGetModuleRegistrations_hook() { |
| 752 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 753 |
$this->setTemporaryHook( 'ResourceLoaderModifyEmbeddedSourceUrls', function ( &$urls ) { |
| 754 |
$urlUtils = $this->getServiceContainer()->getUrlUtils(); |
| 755 |
$urls['local'] = $urlUtils->expand( $urls['local'] ); |
| 756 |
} ); |
| 757 |
|
| 758 |
$context = $this->getResourceLoaderContext(); |
| 759 |
$rl = $context->getResourceLoader(); |
| 760 |
$module = new StartUpModule(); |
| 761 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 762 |
$module->setConfig( $rl->getConfig() ); |
| 763 |
$out = 'mw.loader.addSource({ |
| 764 |
"local": "https://example.org/w/load.php" |
| 765 |
}); |
| 766 |
mw.loader.register([]);'; |
| 767 |
$this->assertEquals( |
| 768 |
$out, |
| 769 |
$module->getModuleRegistrations( $context ) |
| 770 |
); |
| 771 |
} |
| 772 |
|
| 773 |
public static function provideRegistrations() { |
| 774 |
return [ |
| 775 |
[ [ |
| 776 |
'test.blank' => [ 'class' => ResourceLoaderTestModule::class ], |
| 777 |
'test.min' => [ |
| 778 |
'class' => ResourceLoaderTestModule::class, |
| 779 |
'skipFunction' => |
| 780 |
'return !!(' . |
| 781 |
' window.JSON &&' . |
| 782 |
' JSON.parse &&' . |
| 783 |
' JSON.stringify' . |
| 784 |
');', |
| 785 |
'dependencies' => [ |
| 786 |
'test.blank', |
| 787 |
], |
| 788 |
], |
| 789 |
] ] |
| 790 |
]; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
public function testRegistrationsMinified( $modules ) { |
| 797 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 798 |
|
| 799 |
$context = $this->getResourceLoaderContext( [ |
| 800 |
'debug' => 'false', |
| 801 |
] ); |
| 802 |
$rl = $context->getResourceLoader(); |
| 803 |
$rl->register( $modules ); |
| 804 |
$module = new StartUpModule(); |
| 805 |
$module->setConfig( $rl->getConfig() ); |
| 806 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 807 |
$out = 'mw.loader.addSource({"local":"/w/load.php"});' . "\n" |
| 808 |
. 'mw.loader.register([' |
| 809 |
. '["test.blank","{blankVer}"],' |
| 810 |
. '["test.min","{blankVer}",[0],null,null,' |
| 811 |
. '"return!!(window.JSON\u0026\u0026JSON.parse\u0026\u0026JSON.stringify);"' |
| 812 |
. ']]);'; |
| 813 |
|
| 814 |
$this->assertEquals( |
| 815 |
self::expandPlaceholders( $out ), |
| 816 |
$module->getModuleRegistrations( $context ), |
| 817 |
'Minified output' |
| 818 |
); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
public function testRegistrationsUnminified( $modules ) { |
| 825 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 826 |
|
| 827 |
$context = $this->getResourceLoaderContext( [ |
| 828 |
'debug' => 'true', |
| 829 |
] ); |
| 830 |
$rl = $context->getResourceLoader(); |
| 831 |
$rl->register( $modules ); |
| 832 |
$module = new StartUpModule(); |
| 833 |
$module->setConfig( $rl->getConfig() ); |
| 834 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 835 |
$out = |
| 836 |
'mw.loader.addSource({ |
| 837 |
"local": "/w/load.php" |
| 838 |
}); |
| 839 |
mw.loader.register([ |
| 840 |
[ |
| 841 |
"test.blank", |
| 842 |
"" |
| 843 |
], |
| 844 |
[ |
| 845 |
"test.min", |
| 846 |
"", |
| 847 |
[ |
| 848 |
0 |
| 849 |
], |
| 850 |
null, |
| 851 |
null, |
| 852 |
"return !!( window.JSON \u0026\u0026 JSON.parse \u0026\u0026 JSON.stringify);" |
| 853 |
] |
| 854 |
]);'; |
| 855 |
|
| 856 |
$this->assertEquals( |
| 857 |
self::expandPlaceholders( $out ), |
| 858 |
$module->getModuleRegistrations( $context ), |
| 859 |
'Unminified output' |
| 860 |
); |
| 861 |
} |
| 862 |
|
| 863 |
public function testGetVersionHash_varyConfig() { |
| 864 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 865 |
$context = $this->getResourceLoaderContext(); |
| 866 |
|
| 867 |
$module = new StartUpModule(); |
| 868 |
$module->setConfig( $context->getResourceLoader()->getConfig() ); |
| 869 |
$version1 = $module->getVersionHash( $context ); |
| 870 |
|
| 871 |
$module = new StartUpModule(); |
| 872 |
$module->setConfig( $context->getResourceLoader()->getConfig() ); |
| 873 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 874 |
$version2 = $module->getVersionHash( $context ); |
| 875 |
|
| 876 |
$this->assertEquals( |
| 877 |
$version1, |
| 878 |
$version2, |
| 879 |
'Deterministic version hash' |
| 880 |
); |
| 881 |
} |
| 882 |
|
| 883 |
public function testGetVersionHash_varyModule() { |
| 884 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 885 |
|
| 886 |
$context1 = $this->getResourceLoaderContext( [ |
| 887 |
'debug' => 'false', |
| 888 |
] ); |
| 889 |
$rl1 = $context1->getResourceLoader(); |
| 890 |
$rl1->register( [ |
| 891 |
'test.a' => [ 'class' => ResourceLoaderTestModule::class ], |
| 892 |
'test.b' => [ 'class' => ResourceLoaderTestModule::class ], |
| 893 |
] ); |
| 894 |
$module = new StartUpModule(); |
| 895 |
$module->setConfig( $rl1->getConfig() ); |
| 896 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 897 |
$module->setName( 'test' ); |
| 898 |
$version1 = $module->getVersionHash( $context1 ); |
| 899 |
|
| 900 |
$context2 = $this->getResourceLoaderContext(); |
| 901 |
$rl2 = $context2->getResourceLoader(); |
| 902 |
$rl2->register( [ |
| 903 |
'test.b' => [ 'class' => ResourceLoaderTestModule::class ], |
| 904 |
'test.c' => [ 'class' => ResourceLoaderTestModule::class ], |
| 905 |
] ); |
| 906 |
$module = new StartUpModule(); |
| 907 |
$module->setConfig( $rl2->getConfig() ); |
| 908 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 909 |
$module->setName( 'test' ); |
| 910 |
$version2 = $module->getVersionHash( $context2 ); |
| 911 |
|
| 912 |
$context3 = $this->getResourceLoaderContext(); |
| 913 |
$rl3 = $context3->getResourceLoader(); |
| 914 |
$rl3->register( [ |
| 915 |
'test.a' => [ 'class' => ResourceLoaderTestModule::class ], |
| 916 |
'test.b' => [ |
| 917 |
'class' => ResourceLoaderTestModule::class, |
| 918 |
'script' => 'different', |
| 919 |
], |
| 920 |
] ); |
| 921 |
$module = new StartUpModule(); |
| 922 |
$module->setConfig( $rl3->getConfig() ); |
| 923 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 924 |
$module->setName( 'test' ); |
| 925 |
$version3 = $module->getVersionHash( $context3 ); |
| 926 |
|
| 927 |
|
| 928 |
$this->assertNotEquals( |
| 929 |
$version1, |
| 930 |
$version2, |
| 931 |
'Module name is significant' |
| 932 |
); |
| 933 |
|
| 934 |
$this->assertNotEquals( |
| 935 |
$version1, |
| 936 |
$version3, |
| 937 |
'Hash change of any module impacts startup hash' |
| 938 |
); |
| 939 |
} |
| 940 |
|
| 941 |
public function testGetVersionHash_varyDeps() { |
| 942 |
$this->clearHook( 'ResourceLoaderModifyEmbeddedSourceUrls' ); |
| 943 |
|
| 944 |
$context = $this->getResourceLoaderContext( [ 'debug' => 'false' ] ); |
| 945 |
$rl = $context->getResourceLoader(); |
| 946 |
$rl->register( [ |
| 947 |
'test.a' => [ |
| 948 |
'class' => ResourceLoaderTestModule::class, |
| 949 |
'dependencies' => [ 'x', 'y' ], |
| 950 |
], |
| 951 |
] ); |
| 952 |
$module = new StartUpModule(); |
| 953 |
$module->setConfig( $rl->getConfig() ); |
| 954 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 955 |
$module->setName( 'test' ); |
| 956 |
$version1 = $module->getVersionHash( $context ); |
| 957 |
|
| 958 |
$context = $this->getResourceLoaderContext(); |
| 959 |
$rl = $context->getResourceLoader(); |
| 960 |
$rl->register( [ |
| 961 |
'test.a' => [ |
| 962 |
'class' => ResourceLoaderTestModule::class, |
| 963 |
'dependencies' => [ 'x', 'z' ], |
| 964 |
], |
| 965 |
] ); |
| 966 |
$module = new StartUpModule(); |
| 967 |
$module->setConfig( $rl->getConfig() ); |
| 968 |
$module->setHookContainer( $this->getServiceContainer()->getHookContainer() ); |
| 969 |
$module->setName( 'test' ); |
| 970 |
$version2 = $module->getVersionHash( $context ); |
| 971 |
|
| 972 |
|
| 973 |
$this->assertNotEquals( |
| 974 |
$version1, |
| 975 |
$version2, |
| 976 |
'Dependencies are significant' |
| 977 |
); |
| 978 |
} |
| 979 |
|
| 980 |
} |