Merge "Show protection log on creation-protected pages"
[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 * @return ResourceLoaderContext
21 */
22 protected function getResourceLoaderContext( $options = [], ResourceLoader $rl = null ) {
23 if ( is_string( $options ) ) {
24 // Back-compat for extension tests
25 $options = [ 'lang' => $options ];
26 }
27 $options += [
28 'lang' => 'en',
29 'dir' => 'ltr',
30 'skin' => 'vector',
31 'modules' => 'startup',
32 'only' => 'scripts',
33 ];
34 $resourceLoader = $rl ?: new ResourceLoader();
35 $request = new FauxRequest( [
36 'lang' => $options['lang'],
37 'modules' => $options['modules'],
38 'only' => $options['only'],
39 'skin' => $options['skin'],
40 'target' => 'phpunit',
41 ] );
42 $ctx = $this->getMockBuilder( 'ResourceLoaderContext' )
43 ->setConstructorArgs( [ $resourceLoader, $request ] )
44 ->setMethods( [ 'getDirection' ] )
45 ->getMock();
46 $ctx->method( 'getDirection' )->willReturn( $options['dir'] );
47 return $ctx;
48 }
49
50 public static function getSettings() {
51 return [
52 // For ResourceLoader::inDebugMode since it doesn't have context
53 'ResourceLoaderDebug' => true,
54
55 // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
56 'CacheEpoch' => '20140101000000',
57
58 // For ResourceLoader::__construct()
59 'ResourceLoaderSources' => [],
60
61 // For wfScript()
62 'ScriptPath' => '/w',
63 'ScriptExtension' => '.php',
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 $position = 'bottom';
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 public function getPosition() {
129 return $this->position;
130 }
131
132 public function getType() {
133 return $this->type;
134 }
135
136 public function getSkipFunction() {
137 return $this->skipFunction;
138 }
139
140 public function isRaw() {
141 return $this->isRaw;
142 }
143 public function isKnownEmpty( ResourceLoaderContext $context ) {
144 return $this->isKnownEmpty;
145 }
146
147 public function shouldEmbedModule( ResourceLoaderContext $context ) {
148 return $this->shouldEmbed !== null ? $this->shouldEmbed : parent::shouldEmbedModule( $context );
149 }
150
151 public function enableModuleContentVersion() {
152 return true;
153 }
154 }
155
156 class ResourceLoaderFileTestModule extends ResourceLoaderFileModule {
157 protected $lessVars = [];
158
159 public function __construct( $options = [], $test = [] ) {
160 parent::__construct( $options );
161
162 foreach ( $test as $key => $value ) {
163 $this->$key = $value;
164 }
165 }
166
167 public function getLessVars( ResourceLoaderContext $context ) {
168 return $this->lessVars;
169 }
170 }
171
172 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
173 }
174
175 class EmptyResourceLoader extends ResourceLoader {
176 // TODO: This won't be needed once ResourceLoader is empty by default
177 // and default registrations are done from ServiceWiring instead.
178 public function __construct( Config $config = null, LoggerInterface $logger = null ) {
179 $this->setLogger( $logger ?: new NullLogger() );
180 $this->config = $config ?: MediaWikiServices::getInstance()->getMainConfig();
181 // Source "local" is required by StartupModule
182 $this->addSource( 'local', $this->config->get( 'LoadScript' ) );
183 $this->setMessageBlobStore( new MessageBlobStore( $this, $this->getLogger() ) );
184 }
185
186 public function getErrors() {
187 return $this->errors;
188 }
189 }