Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / tests / phpunit / ResourceLoaderTestCase.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Psr\Log\LoggerInterface;
5 use Psr\Log\NullLogger;
6
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';
12
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 * @param ResourceLoader|null $rl
21 * @return ResourceLoaderContext
22 */
23 protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
24 if ( is_string( $options ) ) {
25 // Back-compat for extension tests
26 $options = [ 'lang' => $options ];
27 }
28 $options += [
29 'lang' => 'en',
30 'dir' => 'ltr',
31 'skin' => 'vector',
32 'modules' => 'startup',
33 'only' => 'scripts',
34 ];
35 $resourceLoader = $rl ?: new ResourceLoader();
36 $request = new FauxRequest( [
37 'lang' => $options['lang'],
38 'modules' => $options['modules'],
39 'only' => $options['only'],
40 'skin' => $options['skin'],
41 'target' => 'phpunit',
42 ] );
43 $ctx = $this->getMockBuilder( ResourceLoaderContext::class )
44 ->setConstructorArgs( [ $resourceLoader, $request ] )
45 ->setMethods( [ 'getDirection' ] )
46 ->getMock();
47 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
48 return $ctx;
49 }
50
51 public static function getSettings() {
52 return [
53 // For ResourceLoader::inDebugMode since it doesn't have context
54 'ResourceLoaderDebug' => true,
55
56 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
57 'CacheEpoch' => '20140101000000',
58
59 // For ResourceLoader::__construct()
60 'ResourceLoaderSources' => [],
61
62 // For wfScript()
63 'ScriptPath' => '/w',
64 'ScriptExtension' => '.php',
65 'Script' => '/w/index.php',
66 'LoadScript' => '/w/load.php',
67 ];
68 }
69
70 protected function setUp() {
71 parent::setUp();
72
73 ResourceLoader::clearCache();
74
75 $globals = [];
76 foreach ( self::getSettings() as $key => $value ) {
77 $globals['wg' . $key] = $value;
78 }
79 $this->setMwGlobals( $globals );
80 }
81 }
82
83 /* Stubs */
84
85 class ResourceLoaderTestModule extends ResourceLoaderModule {
86 protected $messages = [];
87 protected $dependencies = [];
88 protected $group = null;
89 protected $source = 'local';
90 protected $script = '';
91 protected $styles = '';
92 protected $skipFunction = null;
93 protected $isRaw = false;
94 protected $isKnownEmpty = false;
95 protected $type = ResourceLoaderModule::LOAD_GENERAL;
96 protected $targets = [ 'phpunit' ];
97 protected $shouldEmbed = null;
98
99 public function __construct( $options = [] ) {
100 foreach ( $options as $key => $value ) {
101 $this->$key = $value;
102 }
103 }
104
105 public function getScript( ResourceLoaderContext $context ) {
106 return $this->validateScriptFile( 'input', $this->script );
107 }
108
109 public function getStyles( ResourceLoaderContext $context ) {
110 return [ '' => $this->styles ];
111 }
112
113 public function getMessages() {
114 return $this->messages;
115 }
116
117 public function getDependencies( ResourceLoaderContext $context = null ) {
118 return $this->dependencies;
119 }
120
121 public function getGroup() {
122 return $this->group;
123 }
124
125 public function getSource() {
126 return $this->source;
127 }
128
129 public function getType() {
130 return $this->type;
131 }
132
133 public function getSkipFunction() {
134 return $this->skipFunction;
135 }
136
137 public function isRaw() {
138 return $this->isRaw;
139 }
140 public function isKnownEmpty( ResourceLoaderContext $context ) {
141 return $this->isKnownEmpty;
142 }
143
144 public function shouldEmbedModule( ResourceLoaderContext $context ) {
145 return $this->shouldEmbed !== null ? $this->shouldEmbed : parent::shouldEmbedModule( $context );
146 }
147
148 public function enableModuleContentVersion() {
149 return true;
150 }
151 }
152
153 class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
154 protected $lessVars = [];
155
156 public function __construct( $options = [], $test = [] ) {
157 parent::__construct( $options );
158
159 foreach ( $test as $key => $value ) {
160 $this->$key = $value;
161 }
162 }
163
164 public function getLessVars( ResourceLoaderContext $context ) {
165 return $this->lessVars;
166 }
167 }
168
169 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
170 }
171
172 class EmptyResourceLoader extends ResourceLoader {
173 // TODO: This won't be needed once ResourceLoader is empty by default
174 // and default registrations are done from ServiceWiring instead.
175 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
176 $this->setLogger( $logger ?: new NullLogger() );
177 $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
178 // Source "local" is required by StartupModule
179 $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
180 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
181 }
182
183 public function getErrors() {
184 return $this->errors;
185 }
186 }