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