ResourceLoaderTestCase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. use Psr\Log\LoggerInterface;
  4. use Psr\Log\NullLogger;
  5. abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
  6. // Version hash for a blank file module.
  7. // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
  8. // and ResourceLoaderFileModule::getDefinitionSummary().
  9. const BLANK_VERSION = '09p30q0';
  10. /**
  11. * @param array|string $options Language code or options array
  12. * - string 'lang' Language code
  13. * - string 'dir' Language direction (ltr or rtl)
  14. * - string 'modules' Pipe-separated list of module names
  15. * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
  16. * (mw.loader.implement).
  17. * @param ResourceLoader|null $rl
  18. * @return ResourceLoaderContext
  19. */
  20. protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
  21. if ( is_string( $options ) ) {
  22. // Back-compat for extension tests
  23. $options = [ 'lang' => $options ];
  24. }
  25. $options += [
  26. 'lang' => 'en',
  27. 'dir' => 'ltr',
  28. 'skin' => 'vector',
  29. 'modules' => 'startup',
  30. 'only' => 'scripts',
  31. 'safemode' => null,
  32. ];
  33. $resourceLoader = $rl ?: new ResourceLoader();
  34. $request = new FauxRequest( [
  35. 'lang' => $options['lang'],
  36. 'modules' => $options['modules'],
  37. 'only' => $options['only'],
  38. 'safemode' => $options['safemode'],
  39. 'skin' => $options['skin'],
  40. 'target' => 'phpunit',
  41. ] );
  42. $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
  43. ->setConstructorArgs( [ $resourceLoader, $request ] )
  44. ->setMethods( [ 'getDirection' ] )
  45. ->getMock();
  46. $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
  47. return $ctx;
  48. }
  49. public static function getSettings() {
  50. return [
  51. // For ResourceLoader::inDebugMode since it doesn't have context
  52. 'ResourceLoaderDebug' => true,
  53. // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
  54. 'CacheEpoch' => '20140101000000',
  55. // For ResourceLoader::__construct()
  56. 'ResourceLoaderSources' => [],
  57. // For wfScript()
  58. 'ScriptPath' => '/w',
  59. 'Script' => '/w/index.php',
  60. 'LoadScript' => '/w/load.php',
  61. ];
  62. }
  63. protected function setUp() {
  64. parent::setUp();
  65. ResourceLoader::clearCache();
  66. $globals = [];
  67. foreach ( self::getSettings() as $key => $value ) {
  68. $globals['wg' . $key] = $value;
  69. }
  70. $this->setMwGlobals( $globals );
  71. }
  72. }
  73. /* Stubs */
  74. class ResourceLoaderTestModule extends ResourceLoaderModule {
  75. protected $messages = [];
  76. protected $dependencies = [];
  77. protected $group = null;
  78. protected $source = 'local';
  79. protected $script = '';
  80. protected $styles = '';
  81. protected $skipFunction = null;
  82. protected $isRaw = false;
  83. protected $isKnownEmpty = false;
  84. protected $type = ResourceLoaderModule::LOAD_GENERAL;
  85. protected $targets = [ 'phpunit' ];
  86. protected $shouldEmbed = null;
  87. public function __construct( $options = [] ) {
  88. foreach ( $options as $key => $value ) {
  89. $this->$key = $value;
  90. }
  91. }
  92. public function getScript( ResourceLoaderContext $context ) {
  93. return $this->validateScriptFile( 'input', $this->script );
  94. }
  95. public function getStyles( ResourceLoaderContext $context ) {
  96. return [ '' => $this->styles ];
  97. }
  98. public function getMessages() {
  99. return $this->messages;
  100. }
  101. public function getDependencies( ResourceLoaderContext $context = null ) {
  102. return $this->dependencies;
  103. }
  104. public function getGroup() {
  105. return $this->group;
  106. }
  107. public function getSource() {
  108. return $this->source;
  109. }
  110. public function getType() {
  111. return $this->type;
  112. }
  113. public function getSkipFunction() {
  114. return $this->skipFunction;
  115. }
  116. public function isRaw() {
  117. return $this->isRaw;
  118. }
  119. public function isKnownEmpty( ResourceLoaderContext $context ) {
  120. return $this->isKnownEmpty;
  121. }
  122. public function shouldEmbedModule( ResourceLoaderContext $context ) {
  123. return $this->shouldEmbed ?? parent::shouldEmbedModule( $context );
  124. }
  125. public function enableModuleContentVersion() {
  126. return true;
  127. }
  128. }
  129. class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
  130. protected $lessVars = [];
  131. public function __construct( $options = [], $test = [] ) {
  132. parent::__construct( $options );
  133. foreach ( $test as $key => $value ) {
  134. $this->$key = $value;
  135. }
  136. }
  137. public function getLessVars( ResourceLoaderContext $context ) {
  138. return $this->lessVars;
  139. }
  140. }
  141. class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
  142. }
  143. class EmptyResourceLoader extends ResourceLoader {
  144. // TODO: This won't be needed once ResourceLoader is empty by default
  145. // and default registrations are done from ServiceWiring instead.
  146. public function __construct( Config $config = null, LoggerInterface $logger = null ) {
  147. $this->setLogger( $logger ?: new NullLogger() );
  148. $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
  149. // Source "local" is required by StartupModule
  150. $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
  151. $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
  152. }
  153. public function getErrors() {
  154. return $this->errors;
  155. }
  156. }