ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilTestBaseTestCaseTrait.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use PHPUnit\Framework\MockObject\MockObject;
24use ILIAS\Data\Factory as DataFactory;
29use ILIAS\HTTP\Services as HTTPServices;
31use ILIAS\Refinery\Factory as RefineryFactory;
32use ILIAS\Refinery\Random\Group as RandomGroup;
33use ILIAS\StaticURL\Services as StaticURLServices;
34use ILIAS\UI\Factory as UIFactory;
35use ILIAS\UI\Renderer as UIRenderer;
36use ILIAS\UI\Implementation\DefaultRenderer as ImplementationDefaultRenderer;
38use ILIAS\UI\Implementation\Factory as ImplementationFactory;
40use GuzzleHttp\Psr7\Uri as GuzzleURI;
41use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
42
44{
45 private array $services = [
46 ilAccess::class => 'ilAccess',
47 ilAccessHandler::class => 'ilAccess',
48 ilOrgUnitPositionAndRBACAccessHandler::class => 'ilAccess',
49 ilOrgUnitPositionAccessHandler::class => 'ilAccess',
50 ilRBACAccessHandler::class => 'ilAccess',
51 DataFactory::class => 'DataFactory',
52 ilGlobalPageTemplate::class => 'tpl',
53 ilGlobalTemplateInterface::class => 'tpl',
54 ilDBInterface::class => 'ilDB',
55 ilBenchmark::class => 'ilBench',
56 ilObjUser::class => 'ilUser',
57 ilErrorHandling::class => 'ilErr',
58 ilTree::class => 'tree',
59 ilLanguage::class => 'lng',
60 Language::class => 'lng',
61 ilAppEventHandler::class => 'ilAppEventHandler',
62 ilObjectDefinition::class => 'objDefinition',
63 RefineryFactory::class => 'refinery',
64 ilRbacSystem::class => 'rbacsystem',
65 ilRbacReview::class => 'rbacreview',
66 ilRbacAdmin::class => 'rbacadmin',
67 HTTPServices::class => 'http',
68 GlobalHttpState::class => 'http',
69 ilComponentFactory::class => 'component.factory',
70 ilComponentRepository::class => 'component.repository',
71 ImplementationFactory::class => 'ui.factory',
72 UIFactory::class => 'ui.factory',
73 ImplementationDefaultRenderer::class => 'ui.renderer',
74 UIRenderer::class => 'ui.renderer',
75 ilUIService::class => 'uiService',
76 StaticURLServices::class => 'static_url',
77 FileUpload::class => 'upload',
78 ilLogger::class => 'ilLog',
79 ilSetting::class => 'ilSetting',
80 Setting::class => 'ilSetting',
81 ilCtrl::class => 'ilCtrl',
82 ilCtrlInterface::class => 'ilCtrl',
83 ilObjectDataCache::class => 'ilObjDataCache',
84 ilHelpGUI::class => 'ilHelp',
85 ilCtrlBaseClassInterface::class => 'ilHelp',
86 ilTabsGUI::class => 'ilTabs',
87 ilLocatorGUI::class => 'ilLocator',
88 ilToolbarGUI::class => 'ilToolbar',
89 ilObjectCustomIconFactory::class => 'object.customicons.factory',
90 Filesystems::class => 'filesystem',
91 ilObjTest::class => 'ilObjTest',
92 \ILIAS\Cache\Services::class => 'global_cache',
93 ];
94
95 protected function defineGlobalConstants(): void
96 {
97 if (!defined('ILIAS_HTTP_PATH')) {
98 define('ILIAS_HTTP_PATH', 'http://localhost');
99 }
100 if (!defined('CLIENT_DATA_DIR')) {
101 define('CLIENT_DATA_DIR', '/var/iliasdata');
102 }
103 if (!defined('IL_INST_ID')) {
104 define('IL_INST_ID', '0');
105 }
106 if (!defined('ANONYMOUS_USER_ID')) {
107 define('ANONYMOUS_USER_ID', 13);
108 }
109 if (!defined('ANONYMOUS_USER_ID')) {
110 define('ANONYMOUS_USER_ID', 13);
111 }
112 if (!defined('ROOT_FOLDER_ID')) {
113 define('ROOT_FOLDER_ID', 8);
114 }
115 if (!defined('ILIAS_LOG_ENABLED')) {
116 define('ILIAS_LOG_ENABLED', true);
117 }
118 if (!defined('ILIAS_LOG_DIR')) {
119 define('ILIAS_LOG_DIR', '/var/log');
120 }
121 if (!defined('ILIAS_LOG_FILE')) {
122 define('ILIAS_LOG_FILE', '/var/log/ilias.log');
123 }
124 }
125
130 protected function setGlobalVariable(string $name, mixed $value): void
131 {
132 global $DIC;
133
134 $GLOBALS[$name] = $value;
135
136 unset($DIC[$name]);
137 $DIC[$name] = static function (Container $c) use ($value) {
138 return $value;
139 };
140 }
141
145 protected function getGlobalTemplateMock()
146 {
147 return $this->createMock(ilTemplate::class);
148 }
149
153 protected function getDatabaseMock()
154 {
155 return $this->createMock(ilDBInterface::class);
156 }
157
161 protected function getIliasMock()
162 {
163 $mock = $this->createMock(ILIAS::class);
164
165 $account = new stdClass();
166 $account->id = 6;
167 $account->fullname = 'Esther Tester';
168
169 $mock->account = $account;
170 $mock->ini_ilias = $this->createMock(ilIniFile::class);
171
172 return $mock;
173 }
174
175 protected function addGlobal_ilAccess(): void
176 {
177 $this->setGlobalVariable('ilAccess', $this->createMock(ilAccess::class));
178 }
179
180 protected function addGlobal_dataFactory(): void
181 {
182 $this->setGlobalVariable('DataFactory', $this->createMock(DataFactory::class));
183 }
184
185 protected function addGlobal_ilUser(): void
186 {
187 $this->setGlobalVariable('ilUser', $this->createMock(ilObjUser::class));
188 }
189
190 protected function addGlobal_objDefinition(): void
191 {
192 $this->setGlobalVariable('objDefinition', $this->createMock(ilObjectDefinition::class));
193 }
194
195 protected function addGlobal_tree(): void
196 {
197 $this->setGlobalVariable('tree', $this->createMock(ilTree::class));
198 }
199
200 protected function addGlobal_ilSetting(): void
201 {
202 $this->setGlobalVariable('ilSetting', $this->createMock(ilSetting::class));
203 }
204
205 protected function addGlobal_rbacsystem(): void
206 {
207 $this->setGlobalVariable('rbacsystem', $this->createMock(ilRbacSystem::class));
208 }
209
210 protected function addGlobal_ilRbacAdmin(): void
211 {
212 $this->setGlobalVariable('rbacadmin', $this->createMock(ilRbacAdmin::class));
213 }
214
215 protected function addGlobal_ilCtrl(): void
216 {
217 $this->setGlobalVariable('ilCtrl', $this->createMock(ilCtrl::class));
218 }
219
220 protected function addGlobal_lng(): void
221 {
222 $this->setGlobalVariable('lng', $this->createMock(ilLanguage::class));
223 }
224
225 protected function addGlobal_filesystem(): void
226 {
227 $this->setGlobalVariable('filesystem', $this->createMock(Filesystems::class));
228 }
229
230 protected function addGlobal_static_url(): void
231 {
232 $this->setGlobalVariable('static_url', $this->createMock(ILIAS\StaticURL\Services::class));
233 }
234
235 protected function addGlobal_upload(): void
236 {
237 $this->setGlobalVariable('upload', $this->createMock(FileUpload::class));
238 }
239
240 protected function addGlobal_ilDB(): void
241 {
242 $db = $this->createMock(ilDBInterface::class);
243 $db
244 ->method('loadModule')
245 ->willReturnCallback(
246 function ($module): ilDBPdoManager|ilDBPdoReverse|null {
247 return match ($module) {
248 ilDBConstants::MODULE_MANAGER => $this->createMock(ilDBPdoManager::class),
249 ilDBConstants::MODULE_REVERSE => $this->createMock(ilDBPdoReverse::class),
250 default => null
251 };
252 }
253 );
254
255 $this->setGlobalVariable('ilDB', $db);
256 }
257
258 protected function addGlobal_ilBench(): void
259 {
260 $this->setGlobalVariable('ilBench', $this->createMock(ilBenchmark::class));
261 }
262
263 protected function addGlobal_ilLog(): void
264 {
265 $this->setGlobalVariable('ilLog', $this->createMock(ilLogger::class));
266 }
267
268 protected function addGlobal_ilias(): void
269 {
270 $this->setGlobalVariable('ilias', $this->getIliasMock());
271 }
272
273 protected function addGlobal_ilErr(): void
274 {
275 $this->setGlobalVariable('ilErr', $this->createMock(ilErrorHandling::class));
276 }
277
278 protected function addGlobal_GlobalScreenService(): void
279 {
280 $this->setGlobalVariable('global_screen', $this->createMock(ILIAS\GlobalScreen\Services::class));
281 }
282
283 protected function addGlobal_ilNavigationHistory(): void
284 {
285 $this->setGlobalVariable('ilNavigationHistory', $this->createMock(ilNavigationHistory::class));
286 }
287
288 protected function addGlobal_ilAppEventHandler(): void
289 {
290 $this->setGlobalVariable('ilAppEventHandler', $this->createMock(ilAppEventHandler::class));
291 }
292
293 protected function addGlobal_tpl(): void
294 {
295 $this->setGlobalVariable('tpl', $this->createMock(ilGlobalPageTemplate::class));
296 }
297
298 protected function addGlobal_ilComponentRepository(): void
299 {
300 $this->setGlobalVariable('component.repository', $this->createMock(ilComponentRepository::class));
301 }
302
303 protected function addGlobal_ilComponentFactory(): void
304 {
305 $this->setGlobalVariable('component.factory', $this->createMock(ilComponentFactory::class));
306 }
307
308 protected function addGlobal_ilTabs(): void
309 {
310 $this->setGlobalVariable('ilTabs', $this->createMock(ilTabsGUI::class));
311 }
312
313 protected function addGlobal_ilObjDataCache(): void
314 {
315 $this->setGlobalVariable('ilObjDataCache', $this->createMock(ilObjectDataCache::class));
316 }
317
318 protected function addGlobal_ilLocator(): void
319 {
320 $this->setGlobalVariable('ilLocator', $this->createMock(ilLocatorGUI::class));
321 }
322
323 protected function addGlobal_rbacreview(): void
324 {
325 $this->setGlobalVariable('rbacreview', $this->createMock(ilRbacReview::class));
326 }
327
328 protected function addGlobal_ilToolbar(): void
329 {
330 $this->setGlobalVariable('ilToolbar', $this->createMock(ilToolbarGUI::class));
331 }
332 protected function addGlobal_http(): void
333 {
334 $request_mock = $this->getMockBuilder(\Psr\Http\Message\ServerRequestInterface::class)
335 ->disableOriginalConstructor()
336 ->getMock();
337 $request_mock->method('getUri')
338 ->willReturn(new GuzzleURI('http://wwww.ilias.de'));
339 $http_mock = $this->getMockBuilder(HTTPServices::class)->disableOriginalConstructor()
340 ->getMock();
341 $http_mock->method('request')
342 ->willReturn($request_mock);
343 $this->setGlobalVariable('http', $http_mock);
344 }
345
346 protected function addGlobal_ilIliasIniFile(): void
347 {
348 $this->setGlobalVariable('ilIliasIniFile', $this->createMock(ilIniFile::class));
349 }
350
351 protected function addGlobal_ilLoggerFactory(): void
352 {
353 $this->setGlobalVariable('ilLoggerFactory', $this->createMock(ilLoggerFactory::class));
354 }
355
356 protected function addGlobal_ilHelp(): void
357 {
358 $this->setGlobalVariable('ilHelp', $this->createMock(ilHelpGUI::class));
359 }
360
361 protected function addGlobal_uiService(): void
362 {
363 $this->setGlobalVariable('uiService', $this->createMock(\ilUIService::class));
364 }
365
366 protected function addGlobal_uiFactory(): void
367 {
368 $this->setGlobalVariable('ui.factory', $this->createMock(FactoryInternal::class));
369 }
370
371 protected function addGlobal_uiRenderer(): void
372 {
373 $this->setGlobalVariable('ui.renderer', $this->createMock(ILIAS\UI\Implementation\DefaultRenderer::class));
374 }
375
376 protected function addGlobal_refinery(): void
377 {
378 $refineryMock = $this->getMockBuilder(RefineryFactory::class)->disableOriginalConstructor()->getMock();
379 $refineryMock->expects(self::any())->method('random')->willReturn($this->getMockBuilder(RandomGroup::class)->getMock());
380 $this->setGlobalVariable('refinery', $refineryMock);
381 }
382
383 protected function addGlobal_skillService(): void
384 {
385 $this->setGlobalVariable('skill', $this->createMock(ILIAS\Skill\Service\SkillService::class));
386 }
387
388 protected function addGlobal_objectMetadata(): void
389 {
390 $this->setGlobalVariable('learning_object_metadata', $this->createMock(ILIAS\MetaData\Services\ServicesInterface::class));
391 }
392
393 protected function addGlobal_objectService(): void
394 {
395 $this->setGlobalVariable('object.customicons.factory', $this->getMockBuilder(ILIAS\ILIASObject\Properties\AdditionalProperties\Icon\Factory::class)->disableOriginalConstructor()->getMock());
396 $object_mock = $this->getMockBuilder(\ilObjectService::class)->disableOriginalConstructor()->getMock();
397
398 $this->setGlobalVariable('object', $object_mock);
399 }
400
401 protected function addGlobal_resourceStorage(): void
402 {
403 $this->setGlobalVariable('resource_storage', $this->createMock(\ILIAS\ResourceStorage\Services::class));
404 }
405
406 protected function addGlobal_fileDelivery(): void
407 {
408 $this->setGlobalVariable(
409 'file_delivery',
410 $this->getFileDelivery()
411 );
412 }
413
414 protected function addGlobal_user(): void
415 {
416 $this->setGlobalVariable(
417 'user',
418 $this->createMock(PublicInterface::class)
419 );
420 }
421
422 protected function addGlobal_uiUploadLimitResolver(): void
423 {
424 $this->setGlobalVariable('ui.upload_limit_resolver', $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class));
425 }
426
427 protected function getFileDelivery(): \ILIAS\FileDelivery\Services
428 {
429 $data_signer = new ILIAS\FileDelivery\Token\DataSigner(
430 new ILIAS\FileDelivery\Token\Signer\Key\Secret\SecretKeyRotation(
431 new ILIAS\FileDelivery\Token\Signer\Key\Secret\SecretKey('blup')
432 )
433 );
434 $http_mock = $this->createMock(HTTPServices::class);
435 $response_builder_mock = $this->createMock(\ILIAS\FileDelivery\Delivery\ResponseBuilder\ResponseBuilder::class);
436 return new \ILIAS\FileDelivery\Services(
437 new ILIAS\FileDelivery\Delivery\StreamDelivery(
438 $data_signer,
439 $http_mock,
440 $response_builder_mock,
441 $response_builder_mock
442 ),
443 new \ILIAS\FileDelivery\Delivery\LegacyDelivery(
444 $http_mock,
445 $response_builder_mock,
446 $response_builder_mock
447 ),
448 $data_signer,
449 $http_mock
450 );
451 }
452
453 protected function getTestObjMock(): ilObjTest
454 {
455 return $this->createConfiguredMock(ilObjTest::class, [
456 'getLocalDIC' => $this->buildLocalDICMock()
457 ]);
458 }
459
460 protected function buildLocalDICMock(): ILIAS\Test\TestDIC
461 {
462 $local_dic_mock = $this->getMockBuilder(ILIAS\Test\TestDIC::class)
463 ->onlyMethods([])->getMock();
464 $local_dic_mock['question.general_properties.repository'] = fn(Pimple\Container $c)
465 => $this->createMock(
466 ILIAS\TestQuestionPool\Questions\GeneralQuestionPropertiesRepository::class
467 );
468 $local_dic_mock['request_data_collector'] = fn(Pimple\Container $c)
469 => $this->createMock(
470 \ILIAS\Test\RequestDataCollector::class
471 );
472 $local_dic_mock['participant.access_filter.factory'] = fn(Pimple\Container $c)
473 => $this->createMock(
474 \ilTestParticipantAccessFilterFactory::class
475 );
476 $local_dic_mock['logging.logger'] = fn(Pimple\Container $c)
477 => $this->createMock(
478 \ILIAS\Test\Logging\TestLogger::class
479 );
480 $local_dic_mock['logging.viewer'] = fn(Pimple\Container $c)
481 => $this->createMock(
482 \ILIAS\Test\Logging\TestLogViewer::class
483 );
484 $local_dic_mock['shuffler'] = fn(Pimple\Container $c)
485 => $this->createMock(
486 \ilTestShuffler::class
487 );
488 $local_dic_mock['results.presentation.factory'] = fn(Pimple\Container $c)
489 => $this->createMock(
490 ILIAS\Test\Results\Presentation\Factory::class
491 );
492 $local_dic_mock['results.data.factory'] = fn(Pimple\Container $c)
493 => $this->createMock(
494 ILIAS\Test\Results\Data\Factory::class
495 );
496 $local_dic_mock['results.toplist.repository'] = fn(Pimple\Container $c)
497 => $this->createMock(
498 ILIAS\Test\Results\Toplist\TestTopListRepository::class
499 );
500 $local_dic_mock['results.data.repository'] = fn(Pimple\Container $c)
501 => $this->createMock(
502 \ILIAS\Test\Results\Data\Repository::class
503 );
504 $local_dic_mock['questions.properties.repository'] = fn(Pimple\Container $c)
505 => $this->createMock(
506 ILIAS\Test\Questions\Properties\Repository::class
507 );
508 return $local_dic_mock;
509 }
510
511 protected function adaptDICServiceMock(string $service_name, callable $adapt): void
512 {
513 $reflection_function = new ReflectionFunction($adapt);
514 if ($reflection_function->getNumberOfParameters() !== 1) {
515 throw new \Exception('Callable must have exactly one parameter of type MockObject.');
516 }
517
518 if (isset($this->services[$service_name])) {
519 global $DIC;
520 if (!isset($DIC[$this->services[$service_name]])) {
521 $DIC[$this->services[$service_name]] = $this->createMock($service_name);
522 }
523
524 $adapt($DIC[$this->services[$service_name]]);
525 }
526 }
527
531 protected function expectTplContent(mixed $expected_content): void
532 {
533 $this->dic['tpl']->expects($this->once())->method('setContent')->with($expected_content);
534 }
535
536 public function mockCommand(string $command): void
537 {
538 $this->dic['ilCtrl']->method('getCmd')->willReturn($command);
539 }
540
541 public function expectRedirect(InvocationOrder $expects, string $method): void
542 {
543 $this->dic['ilCtrl']->expects($expects)->method('redirect')->with($this->anything(), $this->equalTo($method));
544 }
545
546 public function mockPostRequest(array $properties, array $query_parameters = []): void
547 {
548 $this->mockRequest('POST', $properties, $query_parameters);
549 }
550
551 public function mockGetRequest(array $query_parameters = []): void
552 {
553 $this->mockRequest('GET', [], $query_parameters);
554 }
555
556 private function mockRequest(string $request_method, array $properties, array $query_parameters): void
557 {
558 $_SERVER['REQUEST_METHOD'] = $request_method;
559 $request = $this->createConfiguredMock(ServerRequestInterface::class, [
560 'getServerParams' => ['REQUEST_METHOD' => $request_method],
561 'getParsedBody' => $properties,
562 'getQueryParams' => $query_parameters
563 ]);
564 $this->dic['http']->method('request')->willReturn($request);
565 $this->dic['http']->method('wrapper')->willReturn(new WrapperFactory($request));
566 }
567}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:38
Renderer that dispatches rendering of UI components to a Renderer found in the same namespace as the ...
Class ilDBPdoManager.
Class ilDBPdoReverse.
$c
Definition: deliver.php:25
expectRedirect(InvocationOrder $expects, string $method)
addGlobal_static_url()
addGlobal_filesystem()
mockPostRequest(array $properties, array $query_parameters=[])
addGlobal_objDefinition()
addGlobal_ilIliasIniFile()
adaptDICServiceMock(string $service_name, callable $adapt)
addGlobal_ilAppEventHandler()
mockRequest(string $request_method, array $properties, array $query_parameters)
addGlobal_ilNavigationHistory()
addGlobal_ilRbacAdmin()
addGlobal_ilComponentFactory()
addGlobal_GlobalScreenService()
setGlobalVariable(string $name, mixed $value)
addGlobal_dataFactory()
addGlobal_objectMetadata()
addGlobal_fileDelivery()
addGlobal_ilObjDataCache()
mockCommand(string $command)
mockGetRequest(array $query_parameters=[])
addGlobal_skillService()
addGlobal_resourceStorage()
addGlobal_uiUploadLimitResolver()
addGlobal_ilComponentRepository()
defineGlobalConstants()
addGlobal_ilLoggerFactory()
addGlobal_uiRenderer()
addGlobal_rbacsystem()
trait ilTestBaseTestCaseTrait
getGlobalTemplateMock()
addGlobal_rbacreview()
addGlobal_objectService()
expectTplContent(mixed $expected_content)
Expect that the template content will be set to the specified expected content.
The Filesystems interface defines the access methods which can be used to fetch the different filesys...
Definition: Filesystems.php:30
Interface GlobalHttpState.
An entity that renders components to a string output.
Definition: Renderer.php:31
Token
The string representation of these tokens must not occur in the names of metadata elements.
Definition: Token.php:28
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54