Output only to stderr in unit tests
[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 'Script' => '/w/index.php',
65 'LoadScript' => '/w/load.php',
66 ];
67 }
68
69 protected function setUp() {
70 parent::setUp();
71
72 ResourceLoader::clearCache();
73
74 $globals = [];
75 foreach ( self::getSettings() as $key => $value ) {
76 $globals['wg' . $key] = $value;
77 }
78 $this->setMwGlobals( $globals );
79 }
80 }
81
82 /* Stubs */
83
84 class ResourceLoaderTestModule extends ResourceLoaderModule {
85 protected $messages = [];
86 protected $dependencies = [];
87 protected $group = null;
88 protected $source = 'local';
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' ];
96 protected $shouldEmbed = null;
97
98 public function __construct( $options = [] ) {
99 foreach ( $options as $key => $value ) {
100 $this->$key = $value;
101 }
102 }
103
104 public function getScript( ResourceLoaderContext $context ) {
105 return $this->validateScriptFile( 'input', $this->script );
106 }
107
108 public function getStyles( ResourceLoaderContext $context ) {
109 return [ '' => $this->styles ];
110 }
111
112 public function getMessages() {
113 return $this->messages;
114 }
115
116 public function getDependencies( ResourceLoaderContext $context = null ) {
117 return $this->dependencies;
118 }
119
120 public function getGroup() {
121 return $this->group;
122 }
123
124 public function getSource() {
125 return $this->source;
126 }
127
128 public function getType() {
129 return $this->type;
130 }
131
132 public function getSkipFunction() {
133 return $this->skipFunction;
134 }
135
136 public function isRaw() {
137 return $this->isRaw;
138 }
139 public function isKnownEmpty( ResourceLoaderContext $context ) {
140 return $this->isKnownEmpty;
141 }
142
143 public function shouldEmbedModule( ResourceLoaderContext $context ) {
144 return $this->shouldEmbed !== null ? $this->shouldEmbed : parent::shouldEmbedModule( $context );
145 }
146
147 public function enableModuleContentVersion() {
148 return true;
149 }
150 }
151
152 class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
153 protected $lessVars = [];
154
155 public function __construct( $options = [], $test = [] ) {
156 parent::__construct( $options );
157
158 foreach ( $test as $key => $value ) {
159 $this->$key = $value;
160 }
161 }
162
163 public function getLessVars( ResourceLoaderContext $context ) {
164 return $this->lessVars;
165 }
166 }
167
168 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
169 }
170
171 class EmptyResourceLoader extends ResourceLoader {
172 // TODO: This won't be needed once ResourceLoader is empty by default
173 // and default registrations are done from ServiceWiring instead.
174 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
175 $this->setLogger( $logger ?: new NullLogger() );
176 $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
177 // Source "local" is required by StartupModule
178 $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
179 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
180 }
181
182 public function getErrors() {
183 return $this->errors;
184 }
185 }