@@ -51,10 +51,31 @@ protected function setUp(): void {
51
51
* @return \Drupal\Core\Extension\ModuleHandler
52
52
* The module handler to test.
53
53
*/
54
- protected function getModuleHandler ($ implementations = []) {
55
- $ module_handler = new ModuleHandler ($ this ->root , [], $ this ->eventDispatcher , $ implementations );
56
- $ module_handler ->addModule ('module_handler_test ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test ' );
57
- return $ module_handler ;
54
+ protected function getModuleHandler ($ modules = [], $ implementations = [], $ loadAll = TRUE ) {
55
+ // This only works if there's a single $hook.
56
+ if ($ implementations ) {
57
+ $ listeners = array_map (fn ($ function ) => [new ProceduralCall ([]), $ function ], array_keys ($ implementations ));
58
+ $ this ->eventDispatcher ->expects ($ this ->once ())
59
+ ->method ('getListeners ' )
60
+ ->with ("drupal_hook.hook " )
61
+ ->willReturn ($ listeners );
62
+ $ implementations = ['hook ' => [ProceduralCall::class => $ implementations ]];
63
+ }
64
+ $ modules ['module_handler_test ' ] = 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test ' ;
65
+ $ moduleList = [];
66
+ foreach ($ modules as $ module => $ path ) {
67
+ $ filename = "$ module.module " ;
68
+ $ moduleList [$ module ] = [
69
+ 'type ' => 'module ' ,
70
+ 'pathname ' => "$ path/ $ module.info.yml " ,
71
+ 'filename ' => file_exists ("$ this ->root / $ path/ $ filename " ) ? $ filename : NULL ,
72
+ ];
73
+ }
74
+ $ moduleHandler = new ModuleHandler ($ this ->root , $ moduleList , $ this ->eventDispatcher , $ implementations );
75
+ if ($ loadAll ) {
76
+ $ moduleHandler ->loadAll ();
77
+ }
78
+ return $ moduleHandler ;
58
79
}
59
80
60
81
/**
@@ -63,11 +84,13 @@ protected function getModuleHandler($implementations = []) {
63
84
* @covers ::load
64
85
*/
65
86
public function testLoadModule (): void {
66
- $ module_handler = $ this ->getModuleHandler ();
87
+ $ moduleList = [
88
+ 'module_handler_test_added ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added ' ,
89
+ ];
90
+ $ module_handler = $ this ->getModuleHandler ($ moduleList );
67
91
$ this ->assertTrue ($ module_handler ->load ('module_handler_test ' ));
68
92
$ this ->assertTrue (function_exists ('module_handler_test_hook ' ));
69
93
70
- $ module_handler ->addModule ('module_handler_test_added ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added ' );
71
94
$ this ->assertTrue ($ module_handler ->load ('module_handler_test_added ' ));
72
95
$ this ->assertTrue (function_exists ('module_handler_test_added_helper ' ), 'Function exists after being loaded. ' );
73
96
$ this ->assertTrue ($ module_handler ->load ('module_handler_test_added ' ));
@@ -81,9 +104,11 @@ public function testLoadModule(): void {
81
104
* @covers ::loadAll
82
105
*/
83
106
public function testLoadAllModules (): void {
84
- $ module_handler = $ this ->getModuleHandler ();
85
- $ module_handler ->addModule ('module_handler_test_all1 ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1 ' );
86
- $ module_handler ->addModule ('module_handler_test_all2 ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2 ' );
107
+ $ moduleList = [
108
+ 'module_handler_test_all1 ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1 ' ,
109
+ 'module_handler_test_all2 ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2 ' ,
110
+ ];
111
+ $ module_handler = $ this ->getModuleHandler ($ moduleList );
87
112
$ module_handler ->loadAll ();
88
113
$ this ->assertTrue (function_exists ('module_handler_test_all1_hook ' ), 'Function exists after being loaded. ' );
89
114
$ this ->assertTrue (function_exists ('module_handler_test_all2_hook ' ), 'Function exists after being loaded. ' );
@@ -109,20 +134,14 @@ public function testModuleReloading(): void {
109
134
->onlyMethods (['load ' ])
110
135
->getMock ();
111
136
$ calls = [
112
- // First reload.
113
- 'module_handler_test ' ,
114
- // Second reload.
115
137
'module_handler_test ' ,
116
- 'module_handler_test_added ' ,
117
138
];
118
- $ module_handler ->expects ($ this ->exactly ( count ( $ calls ) ))
139
+ $ module_handler ->expects ($ this ->once ( ))
119
140
->method ('load ' )
120
141
->with ($ this ->callback (function (string $ module ) use (&$ calls ): bool {
121
142
return $ module === array_shift ($ calls );
122
143
}));
123
144
$ module_handler ->reload ();
124
- $ module_handler ->addModule ('module_handler_test_added ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added ' );
125
- $ module_handler ->reload ();
126
145
}
127
146
128
147
/**
@@ -131,7 +150,7 @@ public function testModuleReloading(): void {
131
150
* @covers ::isLoaded
132
151
*/
133
152
public function testIsLoaded (): void {
134
- $ module_handler = $ this ->getModuleHandler ();
153
+ $ module_handler = $ this ->getModuleHandler (loadAll: FALSE );
135
154
$ this ->assertFalse ($ module_handler ->isLoaded ());
136
155
$ module_handler ->loadAll ();
137
156
$ this ->assertTrue ($ module_handler ->isLoaded ());
@@ -197,9 +216,11 @@ public function testSetModuleList(): void {
197
216
*
198
217
* @covers ::addModule
199
218
* @covers ::add
219
+ *
220
+ * @group legacy
200
221
*/
201
222
public function testAddModule (): void {
202
-
223
+ $ this -> expectDeprecation ( ' Drupal\Core\Extension\ModuleHandler::addModule() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3491200 ' );
203
224
$ module_handler = $ this ->getMockBuilder (ModuleHandler::class)
204
225
->setConstructorArgs ([
205
226
$ this ->root , [], $ this ->eventDispatcher , [],
@@ -219,9 +240,11 @@ public function testAddModule(): void {
219
240
*
220
241
* @covers ::addProfile
221
242
* @covers ::add
243
+ *
244
+ * @group legacy
222
245
*/
223
246
public function testAddProfile (): void {
224
-
247
+ $ this -> expectDeprecation ( ' Drupal\Core\Extension\ModuleHandler::addProfile() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. There is no direct replacement. See https://www.drupal.org/node/3491200 ' );
225
248
$ module_handler = $ this ->getMockBuilder (ModuleHandler::class)
226
249
->setConstructorArgs ([
227
250
$ this ->root , [], $ this ->eventDispatcher , [],
@@ -310,15 +333,18 @@ public function testInvokeModuleEnabled(): void {
310
333
* @covers ::loadAllIncludes
311
334
*/
312
335
public function testImplementsHookModuleEnabled (): void {
313
- $ implementations ['hook ' ][ProceduralCall::class]['module_handler_test_hook ' ] = 'module_handler_test ' ;
314
- $ module_handler = $ this ->getModuleHandler ($ implementations );
336
+ $ implementations = [
337
+ 'module_handler_test_hook ' => 'module_handler_test ' ,
338
+ 'module_handler_test_added_hook ' => 'module_handler_test_added ' ,
339
+ ];
340
+ $ moduleList = [
341
+ 'module_handler_test_added ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added ' ,
342
+ 'module_handler_test_no_hook ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_no_hook ' ,
343
+ ];
344
+ $ module_handler = $ this ->getModuleHandler ($ moduleList , $ implementations );
315
345
316
346
$ this ->assertTrue ($ module_handler ->hasImplementations ('hook ' , 'module_handler_test ' ), 'Installed module implementation found. ' );
317
-
318
- $ module_handler ->addModule ('module_handler_test_added ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added ' );
319
347
$ this ->assertTrue ($ module_handler ->hasImplementations ('hook ' , 'module_handler_test_added ' ), 'Runtime added module with implementation in include found. ' );
320
-
321
- $ module_handler ->addModule ('module_handler_test_no_hook ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_no_hook ' );
322
348
$ this ->assertFalse ($ module_handler ->hasImplementations ('hook ' , 'module_handler_test_no_hook ' ), 'Missing implementation not found. ' );
323
349
}
324
350
@@ -328,9 +354,16 @@ public function testImplementsHookModuleEnabled(): void {
328
354
* @covers ::invokeAll
329
355
*/
330
356
public function testInvokeAll (): void {
331
- $ module_handler = $ this ->getModuleHandler ();
332
- $ module_handler ->addModule ('module_handler_test_all1 ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1 ' );
333
- $ module_handler ->addModule ('module_handler_test_all2 ' , 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2 ' );
357
+ $ implementations = [
358
+ 'module_handler_test_hook ' => 'module_handler_test ' ,
359
+ 'module_handler_test_all1_hook ' => 'module_handler_test_all1 ' ,
360
+ 'module_handler_test_all2_hook ' => 'module_handler_test_all2 ' ,
361
+ ];
362
+ $ moduleList = [
363
+ 'module_handler_test_all1 ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1 ' ,
364
+ 'module_handler_test_all2 ' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2 ' ,
365
+ ];
366
+ $ module_handler = $ this ->getModuleHandler ($ moduleList , $ implementations );
334
367
$ this ->assertEquals ([TRUE , TRUE , TRUE ], $ module_handler ->invokeAll ('hook ' , [TRUE ]));
335
368
}
336
369
@@ -349,7 +382,7 @@ function some_method(): void {
349
382
350
383
};
351
384
$ implementations ['some_hook ' ][get_class ($ c )]['some_method ' ] = 'some_module ' ;
352
- $ module_handler = $ this ->getModuleHandler ( $ implementations );
385
+ $ module_handler = new ModuleHandler ( $ this ->root , [], $ this -> eventDispatcher , $ implementations, [] );
353
386
$ module_handler ->setModuleList (['some_module ' => TRUE ]);
354
387
$ r = new \ReflectionObject ($ module_handler );
355
388
@@ -376,10 +409,15 @@ function some_method(): void {
376
409
* @covers ::getModuleDirectories
377
410
*/
378
411
public function testGetModuleDirectories (): void {
379
- $ module_handler = $ this ->getModuleHandler ();
380
- $ module_handler ->setModuleList ([]);
381
- $ module_handler ->addModule ('node ' , 'core/modules/node ' );
382
- $ this ->assertEquals (['node ' => $ this ->root . '/core/modules/node ' ], $ module_handler ->getModuleDirectories ());
412
+ $ moduleList = [
413
+ 'node ' => 'core/modules/node ' ,
414
+ ];
415
+ $ module_handler = $ this ->getModuleHandler ($ moduleList );
416
+ $ moduleDirectories = [
417
+ 'node ' => $ this ->root . '/core/modules/node ' ,
418
+ 'module_handler_test ' => $ this ->root . '/core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test ' ,
419
+ ];
420
+ $ this ->assertEquals ($ moduleDirectories , $ module_handler ->getModuleDirectories ());
383
421
}
384
422
385
423
/**
0 commit comments