Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / ResourceLoaderTestCase.php
blob68b91bf3a419e14afc3fe080acc232a3027d4fc1
1 <?php
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5 use Psr\Log\NullLogger;
7 abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
8 // Version hash for a blank file module.
9 // Result of ResourceLoader::makeHash(), ResourceLoaderTestModule
10 // and ResourceLoaderFileModule::getDefinitionSummary().
11 const BLANK_VERSION = '09p30q0';
13 /**
14 * @param array|string $options Language code or options array
15 * - string 'lang' Language code
16 * - string 'dir' Language direction (ltr or rtl)
17 * - string 'modules' Pipe-separated list of module names
18 * - string|null 'only' "scripts" (unwrapped script), "styles" (stylesheet), or null
19 * (mw.loader.implement).
20 * @return ResourceLoaderContext
22 protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
23 if ( is_string( $options ) ) {
24 // Back-compat for extension tests
25 $options = [ 'lang' => $options ];
27 $options += [
28 'lang' => 'en',
29 'dir' => 'ltr',
30 'modules' => 'startup',
31 'only' => 'scripts',
33 $resourceLoader = $rl ?: new ResourceLoader();
34 $request = new FauxRequest( [
35 'lang' => $options['lang'],
36 'modules' => $options['modules'],
37 'only' => $options['only'],
38 'skin' => 'vector',
39 'target' => 'phpunit',
40 ] );
41 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
42 ->setConstructorArgs( [ $resourceLoader, $request ] )
43 ->setMethods( [ 'getDirection' ] )
44 ->getMock();
45 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
46 return $ctx;
49 public static function getSettings() {
50 return [
51 // For ResourceLoader::inDebugMode since it doesn't have context
52 'ResourceLoaderDebug' => true,
54 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
55 'CacheEpoch' => '20140101000000',
57 // For ResourceLoader::__construct()
58 'ResourceLoaderSources' => [],
60 // For wfScript()
61 'ScriptPath' => '/w',
62 'ScriptExtension' => '.php',
63 'Script' => '/w/index.php',
64 'LoadScript' => '/w/load.php',
68 protected function setUp() {
69 parent::setUp();
71 ResourceLoader::clearCache();
73 $globals = [];
74 foreach ( self::getSettings() as $key => $value ) {
75 $globals['wg' . $key] = $value;
77 $this->setMwGlobals( $globals );
81 /* Stubs */
83 class ResourceLoaderTestModule extends ResourceLoaderModule {
84 protected $messages = [];
85 protected $dependencies = [];
86 protected $group = null;
87 protected $source = 'local';
88 protected $position = 'bottom';
89 protected $script = '';
90 protected $styles = '';
91 protected $skipFunction = null;
92 protected $isRaw = false;
93 protected $isKnownEmpty = false;
94 protected $type = ResourceLoaderModule::LOAD_GENERAL;
95 protected $targets = [ 'phpunit' ];
97 public function __construct( $options = [] ) {
98 foreach ( $options as $key => $value ) {
99 $this->$key = $value;
103 public function getScript( ResourceLoaderContext $context ) {
104 return $this->validateScriptFile( 'input', $this->script );
107 public function getStyles( ResourceLoaderContext $context ) {
108 return [ '' => $this->styles ];
111 public function getMessages() {
112 return $this->messages;
115 public function getDependencies( ResourceLoaderContext $context = null ) {
116 return $this->dependencies;
119 public function getGroup() {
120 return $this->group;
123 public function getSource() {
124 return $this->source;
126 public function getPosition() {
127 return $this->position;
130 public function getType() {
131 return $this->type;
134 public function getSkipFunction() {
135 return $this->skipFunction;
138 public function isRaw() {
139 return $this->isRaw;
141 public function isKnownEmpty( ResourceLoaderContext $context ) {
142 return $this->isKnownEmpty;
145 public function enableModuleContentVersion() {
146 return true;
150 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
153 class EmptyResourceLoader extends ResourceLoader {
154 // TODO: This won't be needed once ResourceLoader is empty by default
155 // and default registrations are done from ServiceWiring instead.
156 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
157 $this->setLogger( $logger ?: new NullLogger() );
158 $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
159 // Source "local" is required by StartupModule
160 $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
161 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
164 public function getErrors() {
165 return $this->errors;