Debian Codegraph @

tests/phpunit/includes/MediaWikiServicesTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Config\Config;
4 use MediaWiki\Config\GlobalVarConfig;
5 use MediaWiki\Config\HashConfig;
6 use MediaWiki\Hook\MediaWikiServicesHook;
7 use MediaWiki\HookContainer\HookContainer;
8 use MediaWiki\HookContainer\StaticHookRegistry;
9 use MediaWiki\MainConfigNames;
10 use MediaWiki\MediaWikiServices;
11 use Wikimedia\Services\DestructibleService;
12 use Wikimedia\Services\SalvageableService;
13
14 /**
15 * @covers \MediaWiki\MediaWikiServices
16 * @group Database
17 * This test doesn't really make queries, but needs to be in the Database test to make sure
18 * that storage isn't disabled on the original instance.
19 */
20 class MediaWikiServicesTest extends MediaWikiIntegrationTestCase {
21 private const DEPRECATED_SERVICES = [
22 'BlockErrorFormatter',
23 'ConfigRepository',
24 'ConfiguredReadOnlyMode',
25 ];
26
27 /** @var array */
28 public static $mockServiceWiring = [];
29
30 /**
31 * @return Config
32 */
33 private function newTestConfig() {
34 $globalConfig = new GlobalVarConfig();
35
36 $testConfig = new HashConfig();
37 $testConfig->set( MainConfigNames::ServiceWiringFiles, $globalConfig->get( MainConfigNames::ServiceWiringFiles ) );
38 $testConfig->set( MainConfigNames::ConfigRegistry, $globalConfig->get( MainConfigNames::ConfigRegistry ) );
39 $testConfig->set( MainConfigNames::Hooks, [] );
40
41 return $testConfig;
42 }
43
44 /**
45 * @return MediaWikiServices
46 */
47 private function newMediaWikiServices() {
48 $config = $this->newTestConfig();
49 $instance = new MediaWikiServices( $config );
50
51 // Load the default wiring from the specified files.
52 $wiringFiles = $config->get( MainConfigNames::ServiceWiringFiles );
53 $instance->loadWiringFiles( $wiringFiles );
54
55 return $instance;
56 }
57
58 private function newConfigWithMockWiring() {
59 $config = new HashConfig;
60 $config->set( MainConfigNames::ServiceWiringFiles, [ __DIR__ . '/MockServiceWiring.php' ] );
61 return $config;
62 }
63
64 public function testGetInstance() {
65 $services = MediaWikiServices::getInstance();
66 $this->assertInstanceOf( MediaWikiServices::class, $services );
67 }
68
69 public function testForceGlobalInstance() {
70 $newServices = $this->newMediaWikiServices();
71 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
72
73 $this->assertInstanceOf( MediaWikiServices::class, $oldServices );
74 $this->assertNotSame( $oldServices, $newServices );
75
76 $theServices = MediaWikiServices::getInstance();
77 $this->assertSame( $theServices, $newServices );
78
79 MediaWikiServices::forceGlobalInstance( $oldServices );
80
81 $theServices = MediaWikiServices::getInstance();
82 $this->assertSame( $theServices, $oldServices );
83 }
84
85 public function testResetGlobalInstance() {
86 $newServices = $this->newMediaWikiServices();
87 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
88
89 $service1 = $this->createMock( SalvageableService::class );
90 $service1->expects( $this->never() )
91 ->method( 'salvage' );
92
93 $newServices->defineService(
94 'Test',
95 static function () use ( $service1 ) {
96 return $service1;
97 }
98 );
99
100 // force instantiation
101 $newServices->getService( 'Test' );
102
103 MediaWikiServices::resetGlobalInstance( $this->newTestConfig() );
104 $theServices = MediaWikiServices::getInstance();
105
106 $this->assertSame(
107 $service1,
108 $theServices->getService( 'Test' ),
109 'service definition should survive reset'
110 );
111
112 $this->assertNotSame( $theServices, $newServices );
113 $this->assertNotSame( $theServices, $oldServices );
114
115 MediaWikiServices::forceGlobalInstance( $oldServices );
116 }
117
118 public function testResetGlobalInstance_quick() {
119 $newServices = $this->newMediaWikiServices();
120 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
121
122 $service1 = $this->createMock( SalvageableService::class );
123 $service1->expects( $this->never() )
124 ->method( 'salvage' );
125
126 $service2 = $this->createMock( SalvageableService::class );
127 $service2->expects( $this->once() )
128 ->method( 'salvage' )
129 ->with( $service1 );
130
131 // sequence of values the instantiator will return
132 $instantiatorReturnValues = [
133 $service1,
134 $service2,
135 ];
136
137 $newServices->defineService(
138 'Test',
139 static function () use ( &$instantiatorReturnValues ) {
140 return array_shift( $instantiatorReturnValues );
141 }
142 );
143
144 // force instantiation
145 $newServices->getService( 'Test' );
146
147 MediaWikiServices::resetGlobalInstance( $this->newTestConfig(), 'quick' );
148 $theServices = MediaWikiServices::getInstance();
149
150 $this->assertSame( $service2, $theServices->getService( 'Test' ) );
151
152 $this->assertNotSame( $theServices, $newServices );
153 $this->assertNotSame( $theServices, $oldServices );
154
155 MediaWikiServices::forceGlobalInstance( $oldServices );
156 }
157
158 public function testResetGlobalInstance_T263925() {
159 $newServices = $this->newMediaWikiServices();
160 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
161 self::$mockServiceWiring = [
162 'HookContainer' => function ( MediaWikiServices $services ) {
163 return new HookContainer(
164 new StaticHookRegistry(
165 [],
166 [
167 'MediaWikiServices' => [
168 [
169 'handler' => [
170 'name' => 'test',
171 'factory' => static function () {
172 return new class implements MediaWikiServicesHook {
173 public function onMediaWikiServices( $services ) {
174 }
175 };
176 }
177 ],
178 'deprecated' => false,
179 'extensionPath' => 'path'
180 ],
181 ]
182 ],
183 []
184 ),
185 $this->createSimpleObjectFactory()
186 );
187 }
188 ];
189 $newServices->redefineService( 'HookContainer',
190 self::$mockServiceWiring['HookContainer'] );
191
192 $newServices->getHookContainer()->run( 'MediaWikiServices', [ $newServices ] );
193 MediaWikiServices::resetGlobalInstance( $this->newConfigWithMockWiring(), 'quick' );
194 $this->assertTrue( true, 'expected no exception from above' );
195
196 self::$mockServiceWiring = [];
197 MediaWikiServices::forceGlobalInstance( $oldServices );
198 }
199
200 public function testDisableStorage() {
201 $newServices = $this->newMediaWikiServices();
202 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
203
204 $lbFactory = $this->createMock( \Wikimedia\Rdbms\LBFactorySimple::class );
205
206 $newServices->redefineService(
207 'DBLoadBalancerFactory',
208 static function () use ( $lbFactory ) {
209 return $lbFactory;
210 }
211 );
212
213 $this->assertFalse( $newServices->isStorageDisabled() );
214
215 $newServices->disableStorage(); // should destroy DBLoadBalancerFactory
216
217 $this->assertTrue( $newServices->isStorageDisabled() );
218
219 try {
220 $newServices->getDBLoadBalancer()->getConnection( DB_REPLICA );
221 } catch ( RuntimeException $ex ) {
222 // ok, as expected
223 }
224
225 MediaWikiServices::forceGlobalInstance( $oldServices );
226 $newServices->destroy();
227
228 // This should work now.
229 MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_REPLICA );
230
231 // No exception was thrown, avoid being risky
232 $this->assertTrue( true );
233 }
234
235 public function testResetChildProcessServices() {
236 $newServices = $this->newMediaWikiServices();
237 $oldServices = MediaWikiServices::forceGlobalInstance( $newServices );
238
239 $service1 = $this->createMock( DestructibleService::class );
240 $service1->expects( $this->once() )
241 ->method( 'destroy' );
242
243 $service2 = $this->createMock( DestructibleService::class );
244 $service2->expects( $this->never() )
245 ->method( 'destroy' );
246
247 // sequence of values the instantiator will return
248 $instantiatorReturnValues = [
249 $service1,
250 $service2,
251 ];
252
253 $newServices->defineService(
254 'Test',
255 static function () use ( &$instantiatorReturnValues ) {
256 return array_shift( $instantiatorReturnValues );
257 }
258 );
259
260 // force the service to become active, so we can check that it does get destroyed
261 $oldTestService = $newServices->getService( 'Test' );
262
263 MediaWikiServices::resetChildProcessServices();
264 $finalServices = MediaWikiServices::getInstance();
265
266 $newTestService = $finalServices->getService( 'Test' );
267 $this->assertNotSame( $oldTestService, $newTestService );
268
269 MediaWikiServices::forceGlobalInstance( $oldServices );
270 }
271
272 public function testResetServiceForTesting() {
273 $services = $this->newMediaWikiServices();
274 $serviceCounter = 0;
275
276 $services->defineService(
277 'Test',
278 function () use ( &$serviceCounter ) {
279 $serviceCounter++;
280 $service = $this->createMock( Wikimedia\Services\DestructibleService::class );
281 $service->expects( $this->once() )->method( 'destroy' );
282 return $service;
283 }
284 );
285
286 // This should do nothing. In particular, it should not create a service instance.
287 $services->resetServiceForTesting( 'Test' );
288 $this->assertSame( 0, $serviceCounter, 'No service instance should be created yet.' );
289
290 $oldInstance = $services->getService( 'Test' );
291 $this->assertSame( 1, $serviceCounter, 'A service instance should exit now.' );
292
293 // The old instance should be detached, and destroy() called.
294 $services->resetServiceForTesting( 'Test' );
295 $newInstance = $services->getService( 'Test' );
296
297 $this->assertNotSame( $oldInstance, $newInstance );
298
299 // Satisfy the expectation that destroy() is called also for the second service instance.
300 $newInstance->destroy();
301 }
302
303 public function testResetServiceForTesting_noDestroy() {
304 $services = $this->newMediaWikiServices();
305
306 $services->defineService(
307 'Test',
308 function () {
309 $service = $this->createMock( Wikimedia\Services\DestructibleService::class );
310 $service->expects( $this->never() )->method( 'destroy' );
311 return $service;
312 }
313 );
314
315 $oldInstance = $services->getService( 'Test' );
316
317 // The old instance should be detached, but destroy() not called.
318 $services->resetServiceForTesting( 'Test', false );
319 $newInstance = $services->getService( 'Test' );
320
321 $this->assertNotSame( $oldInstance, $newInstance );
322 }
323
324 public function provideGetters() {
325 $getServiceCases = self::provideGetService();
326 $getterCases = [];
327
328 // All getters should be named just like the service, with "get" added.
329 foreach ( $getServiceCases as $name => $case ) {
330 if ( $name[0] === '_' ) {
331 // Internal service, no getter
332 continue;
333 }
334 [ $service, $class ] = $case;
335 $getterCases[$name] = [
336 'get' . $service,
337 $class,
338 in_array( $service, self::DEPRECATED_SERVICES )
339 ];
340 }
341
342 return $getterCases;
343 }
344
345 /**
346 * @dataProvider provideGetters
347 */
348 public function testGetters( $getter, $type, $isDeprecated = false ) {
349 if ( $isDeprecated ) {
350 $this->hideDeprecated( MediaWikiServices::class . "::$getter" );
351 }
352
353 // Test against the default instance, since the dummy will not know the default services.
354 $services = MediaWikiServices::getInstance();
355 $service = $services->$getter();
356 $this->assertInstanceOf( $type, $service );
357 }
358
359 public static function provideGetService() {
360 global $IP;
361 $serviceList = require "$IP/includes/ServiceWiring.php";
362 $ret = [];
363 foreach ( $serviceList as $name => $callback ) {
364 $fun = new ReflectionFunction( $callback );
365 if ( !$fun->hasReturnType() ) {
366 throw new LogicException( 'All service callbacks must have a return type defined, ' .
367 "none found for $name" );
368 }
369
370 $returnType = $fun->getReturnType();
371 $ret[$name] = [ $name, $returnType->getName() ];
372 }
373 return $ret;
374 }
375
376 /**
377 * @dataProvider provideGetService
378 */
379 public function testGetService( $name, $type ) {
380 // Test against the default instance, since the dummy will not know the default services.
381 $services = MediaWikiServices::getInstance();
382
383 $service = $services->getService( $name );
384 $this->assertInstanceOf( $type, $service );
385 }
386
387 public function testDefaultServiceInstantiation() {
388 // Check all services in the default instance, not a dummy instance!
389 // Note that we instantiate all services here, including any that
390 // were registered by extensions.
391 $services = MediaWikiServices::getInstance();
392 $names = $services->getServiceNames();
393
394 foreach ( $names as $name ) {
395 $this->assertTrue( $services->hasService( $name ) );
396 $service = $services->getService( $name );
397 $this->assertIsObject( $service );
398 }
399 }
400
401 public function testDefaultServiceWiringServicesHaveTests() {
402 global $IP;
403 $testedServices = array_keys( self::provideGetService() );
404 $allServices = array_keys( require "$IP/includes/ServiceWiring.php" );
405 $this->assertEquals(
406 [],
407 array_diff( $allServices, $testedServices ),
408 'The following services have not been added to MediaWikiServicesTest::provideGetService'
409 );
410 }
411
412 public function testGettersAreSorted() {
413 $methods = ( new ReflectionClass( MediaWikiServices::class ) )
414 ->getMethods( ReflectionMethod::IS_STATIC | ReflectionMethod::IS_PUBLIC );
415
416 $names = array_map( static function ( $method ) {
417 return $method->getName();
418 }, $methods );
419 $serviceNames = array_map( static function ( $name ) {
420 return "get$name";
421 }, array_keys( self::provideGetService() ) );
422 $names = array_values( array_filter( $names, static function ( $name ) use ( $serviceNames ) {
423 return in_array( $name, $serviceNames );
424 } ) );
425
426 $sortedNames = $names;
427 natcasesort( $sortedNames );
428
429 $this->assertSame( $sortedNames, $names,
430 'Please keep service getters sorted alphabetically' );
431 }
432 }