Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / tests / phpunit / includes / MediaWikiServicesTest.php
1 <?php
2
3 use Mediawiki\Http\HttpRequestFactory;
4 use MediaWiki\Interwiki\InterwikiLookup;
5 use MediaWiki\Linker\LinkRenderer;
6 use MediaWiki\Linker\LinkRendererFactory;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\Services\DestructibleService;
9 use MediaWiki\Services\SalvageableService;
10 use MediaWiki\Services\ServiceDisabledException;
11 use MediaWiki\Shell\CommandFactory;
12 use MediaWiki\Storage\BlobStore;
13 use MediaWiki\Storage\BlobStoreFactory;
14 use MediaWiki\Storage\RevisionLookup;
15 use MediaWiki\Storage\RevisionStore;
16 use MediaWiki\Storage\SqlBlobStore;
17
18 /**
19 * @covers MediaWiki\MediaWikiServices
20 *
21 * @group MediaWiki
22 */
23 class MediaWikiServicesTest extends MediaWikiTestCase {
24
25 /**
26 * @return Config
27 */
28 private function newTestConfig() {
29 $globalConfig = new GlobalVarConfig();
30
31 $testConfig = new HashConfig();
32 $testConfig->set( 'ServiceWiringFiles', $globalConfig->get( 'ServiceWiringFiles' ) );
33 $testConfig->set( 'ConfigRegistry', $globalConfig->get( 'ConfigRegistry' ) );
34
35 return $testConfig;
36 }
37
38 /**
39 * @return MediaWikiServices
40 */
41 private function newMediaWikiServices( Config $config = null ) {
42 if ( $config === null ) {
43 $config = $this->newTestConfig();
44 }
45
46 $instance = new MediaWikiServices( $config );
47
48 // Load the default wiring from the specified files.
49 $wiringFiles = $config->get( 'ServiceWiringFiles' );
50 $instance->loadWiringFiles( $wiringFiles );
51
52 return $instance;
53 }
54
55 public function testGetInstance() {
56 $services = MediaWikiServices::getInstance();
57 $this->assertInstanceOf( MediaWikiServices::class, $services );
58 }
59
60 public function testForceGlobalInstance() {
61 $newServices = $this->newMediaWikiServices();
62 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
63
64 $this->assertInstanceOf( MediaWikiServices::class, $oldServices );
65 $this->assertNotSame( $oldServices, $newServices );
66
67 $theServices = MediaWikiServices::getInstance();
68 $this->assertSame( $theServices, $newServices );
69
70 MediaWikiServices::forceGlobalInstance( $oldServices );
71
72 $theServices = MediaWikiServices::getInstance();
73 $this->assertSame( $theServices, $oldServices );
74 }
75
76 public function testResetGlobalInstance() {
77 $newServices = $this->newMediaWikiServices();
78 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
79
80 $service1 = $this->createMock( SalvageableService::class );
81 $service1->expects( $this->never() )
82 ->method( 'salvage' );
83
84 $newServices->defineService(
85 'Test',
86 function () use ( $service1 ) {
87 return $service1;
88 }
89 );
90
91 // force instantiation
92 $newServices->getService( 'Test' );
93
94 MediaWikiServices::resetGlobalInstance( $this->newTestConfig() );
95 $theServices = MediaWikiServices::getInstance();
96
97 $this->assertSame(
98 $service1,
99 $theServices->getService( 'Test' ),
100 'service definition should survive reset'
101 );
102
103 $this->assertNotSame( $theServices, $newServices );
104 $this->assertNotSame( $theServices, $oldServices );
105
106 MediaWikiServices::forceGlobalInstance( $oldServices );
107 }
108
109 public function testResetGlobalInstance_quick() {
110 $newServices = $this->newMediaWikiServices();
111 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
112
113 $service1 = $this->createMock( SalvageableService::class );
114 $service1->expects( $this->never() )
115 ->method( 'salvage' );
116
117 $service2 = $this->createMock( SalvageableService::class );
118 $service2->expects( $this->once() )
119 ->method( 'salvage' )
120 ->with( $service1 );
121
122 // sequence of values the instantiator will return
123 $instantiatorReturnValues = [
124 $service1,
125 $service2,
126 ];
127
128 $newServices->defineService(
129 'Test',
130 function () use ( &$instantiatorReturnValues ) {
131 return array_shift( $instantiatorReturnValues );
132 }
133 );
134
135 // force instantiation
136 $newServices->getService( 'Test' );
137
138 MediaWikiServices::resetGlobalInstance( $this->newTestConfig(), 'quick' );
139 $theServices = MediaWikiServices::getInstance();
140
141 $this->assertSame( $service2, $theServices->getService( 'Test' ) );
142
143 $this->assertNotSame( $theServices, $newServices );
144 $this->assertNotSame( $theServices, $oldServices );
145
146 MediaWikiServices::forceGlobalInstance( $oldServices );
147 }
148
149 public function testDisableStorageBackend() {
150 $newServices = $this->newMediaWikiServices();
151 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
152
153 $lbFactory = $this->getMockBuilder( \Wikimedia\Rdbms\LBFactorySimple::class )
154 ->disableOriginalConstructor()
155 ->getMock();
156
157 $newServices->redefineService(
158 'DBLoadBalancerFactory',
159 function () use ( $lbFactory ) {
160 return $lbFactory;
161 }
162 );
163
164 // force the service to become active, so we can check that it does get destroyed
165 $newServices->getService( 'DBLoadBalancerFactory' );
166
167 MediaWikiServices::disableStorageBackend(); // should destroy DBLoadBalancerFactory
168
169 try {
170 MediaWikiServices::getInstance()->getService( 'DBLoadBalancerFactory' );
171 $this->fail( 'DBLoadBalancerFactory should have been disabled' );
172 }
173 catch ( ServiceDisabledException $ex ) {
174 // ok, as expected
175 } catch ( Throwable $ex ) {
176 $this->fail( 'ServiceDisabledException expected, caught ' . get_class( $ex ) );
177 }
178
179 MediaWikiServices::forceGlobalInstance( $oldServices );
180 $newServices->destroy();
181 }
182
183 public function testResetChildProcessServices() {
184 $newServices = $this->newMediaWikiServices();
185 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
186
187 $service1 = $this->createMock( DestructibleService::class );
188 $service1->expects( $this->once() )
189 ->method( 'destroy' );
190
191 $service2 = $this->createMock( DestructibleService::class );
192 $service2->expects( $this->never() )
193 ->method( 'destroy' );
194
195 // sequence of values the instantiator will return
196 $instantiatorReturnValues = [
197 $service1,
198 $service2,
199 ];
200
201 $newServices->defineService(
202 'Test',
203 function () use ( &$instantiatorReturnValues ) {
204 return array_shift( $instantiatorReturnValues );
205 }
206 );
207
208 // force the service to become active, so we can check that it does get destroyed
209 $oldTestService = $newServices->getService( 'Test' );
210
211 MediaWikiServices::resetChildProcessServices();
212 $finalServices = MediaWikiServices::getInstance();
213
214 $newTestService = $finalServices->getService( 'Test' );
215 $this->assertNotSame( $oldTestService, $newTestService );
216
217 MediaWikiServices::forceGlobalInstance( $oldServices );
218 }
219
220 public function testResetServiceForTesting() {
221 $services = $this->newMediaWikiServices();
222 $serviceCounter = 0;
223
224 $services->defineService(
225 'Test',
226 function () use ( &$serviceCounter ) {
227 $serviceCounter++;
228 $service = $this->createMock( MediaWiki\Services\DestructibleService::class );
229 $service->expects( $this->once() )->method( 'destroy' );
230 return $service;
231 }
232 );
233
234 // This should do nothing. In particular, it should not create a service instance.
235 $services->resetServiceForTesting( 'Test' );
236 $this->assertEquals( 0, $serviceCounter, 'No service instance should be created yet.' );
237
238 $oldInstance = $services->getService( 'Test' );
239 $this->assertEquals( 1, $serviceCounter, 'A service instance should exit now.' );
240
241 // The old instance should be detached, and destroy() called.
242 $services->resetServiceForTesting( 'Test' );
243 $newInstance = $services->getService( 'Test' );
244
245 $this->assertNotSame( $oldInstance, $newInstance );
246
247 // Satisfy the expectation that destroy() is called also for the second service instance.
248 $newInstance->destroy();
249 }
250
251 public function testResetServiceForTesting_noDestroy() {
252 $services = $this->newMediaWikiServices();
253
254 $services->defineService(
255 'Test',
256 function () {
257 $service = $this->createMock( MediaWiki\Services\DestructibleService::class );
258 $service->expects( $this->never() )->method( 'destroy' );
259 return $service;
260 }
261 );
262
263 $oldInstance = $services->getService( 'Test' );
264
265 // The old instance should be detached, but destroy() not called.
266 $services->resetServiceForTesting( 'Test', false );
267 $newInstance = $services->getService( 'Test' );
268
269 $this->assertNotSame( $oldInstance, $newInstance );
270 }
271
272 public function provideGetters() {
273 $getServiceCases = $this->provideGetService();
274 $getterCases = [];
275
276 // All getters should be named just like the service, with "get" added.
277 foreach ( $getServiceCases as $name => $case ) {
278 if ( $name[0] === '_' ) {
279 // Internal service, no getter
280 continue;
281 }
282 list( $service, $class ) = $case;
283 $getterCases[$name] = [
284 'get' . $service,
285 $class,
286 ];
287 }
288
289 return $getterCases;
290 }
291
292 /**
293 * @dataProvider provideGetters
294 */
295 public function testGetters( $getter, $type ) {
296 // Test against the default instance, since the dummy will not know the default services.
297 $services = MediaWikiServices::getInstance();
298 $service = $services->$getter();
299 $this->assertInstanceOf( $type, $service );
300 }
301
302 public function provideGetService() {
303 // NOTE: This should list all service getters defined in ServiceWiring.php.
304 // NOTE: For every test case defined here there should be a corresponding
305 // test case defined in provideGetters().
306 return [
307 'BootstrapConfig' => [ 'BootstrapConfig', Config::class ],
308 'ConfigFactory' => [ 'ConfigFactory', ConfigFactory::class ],
309 'MainConfig' => [ 'MainConfig', Config::class ],
310 'SiteStore' => [ 'SiteStore', SiteStore::class ],
311 'SiteLookup' => [ 'SiteLookup', SiteLookup::class ],
312 'StatsdDataFactory' => [ 'StatsdDataFactory', IBufferingStatsdDataFactory::class ],
313 'InterwikiLookup' => [ 'InterwikiLookup', InterwikiLookup::class ],
314 'EventRelayerGroup' => [ 'EventRelayerGroup', EventRelayerGroup::class ],
315 'SearchEngineFactory' => [ 'SearchEngineFactory', SearchEngineFactory::class ],
316 'SearchEngineConfig' => [ 'SearchEngineConfig', SearchEngineConfig::class ],
317 'SkinFactory' => [ 'SkinFactory', SkinFactory::class ],
318 'DBLoadBalancerFactory' => [ 'DBLoadBalancerFactory', Wikimedia\Rdbms\LBFactory::class ],
319 'DBLoadBalancer' => [ 'DBLoadBalancer', Wikimedia\Rdbms\LoadBalancer::class ],
320 'WatchedItemStore' => [ 'WatchedItemStore', WatchedItemStore::class ],
321 'WatchedItemQueryService' => [ 'WatchedItemQueryService', WatchedItemQueryService::class ],
322 'CryptRand' => [ 'CryptRand', CryptRand::class ],
323 'CryptHKDF' => [ 'CryptHKDF', CryptHKDF::class ],
324 'MediaHandlerFactory' => [ 'MediaHandlerFactory', MediaHandlerFactory::class ],
325 'Parser' => [ 'Parser', Parser::class ],
326 'ParserCache' => [ 'ParserCache', ParserCache::class ],
327 'GenderCache' => [ 'GenderCache', GenderCache::class ],
328 'LinkCache' => [ 'LinkCache', LinkCache::class ],
329 'LinkRenderer' => [ 'LinkRenderer', LinkRenderer::class ],
330 'LinkRendererFactory' => [ 'LinkRendererFactory', LinkRendererFactory::class ],
331 '_MediaWikiTitleCodec' => [ '_MediaWikiTitleCodec', MediaWikiTitleCodec::class ],
332 'MimeAnalyzer' => [ 'MimeAnalyzer', MimeAnalyzer::class ],
333 'TitleFormatter' => [ 'TitleFormatter', TitleFormatter::class ],
334 'TitleParser' => [ 'TitleParser', TitleParser::class ],
335 'ProxyLookup' => [ 'ProxyLookup', ProxyLookup::class ],
336 'MainObjectStash' => [ 'MainObjectStash', BagOStuff::class ],
337 'MainWANObjectCache' => [ 'MainWANObjectCache', WANObjectCache::class ],
338 'LocalServerObjectCache' => [ 'LocalServerObjectCache', BagOStuff::class ],
339 'VirtualRESTServiceClient' => [ 'VirtualRESTServiceClient', VirtualRESTServiceClient::class ],
340 'ShellCommandFactory' => [ 'ShellCommandFactory', CommandFactory::class ],
341 'BlobStoreFactory' => [ 'BlobStoreFactory', BlobStoreFactory::class ],
342 'BlobStore' => [ 'BlobStore', BlobStore::class ],
343 '_SqlBlobStore' => [ '_SqlBlobStore', SqlBlobStore::class ],
344 'RevisionStore' => [ 'RevisionStore', RevisionStore::class ],
345 'RevisionLookup' => [ 'RevisionLookup', RevisionLookup::class ],
346 'HttpRequestFactory' => [ 'HttpRequestFactory', HttpRequestFactory::class ],
347 ];
348 }
349
350 /**
351 * @dataProvider provideGetService
352 */
353 public function testGetService( $name, $type ) {
354 // Test against the default instance, since the dummy will not know the default services.
355 $services = MediaWikiServices::getInstance();
356
357 $service = $services->getService( $name );
358 $this->assertInstanceOf( $type, $service );
359 }
360
361 public function testDefaultServiceInstantiation() {
362 // Check all services in the default instance, not a dummy instance!
363 // Note that we instantiate all services here, including any that
364 // were registered by extensions.
365 $services = MediaWikiServices::getInstance();
366 $names = $services->getServiceNames();
367
368 foreach ( $names as $name ) {
369 $this->assertTrue( $services->hasService( $name ) );
370 $service = $services->getService( $name );
371 $this->assertInternalType( 'object', $service );
372 }
373 }
374
375 }