ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ilWebResourceDatabaseRepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
24 
30 {
31  protected ?Container $dic = null;
32  protected ilObjUser $user;
33  protected ilWebLinkRepository&MockObject $web_link_repo;
34 
35  protected function setUp(): void
36  {
37  parent::setUp();
38  $this->initDependencies();
39  }
40 
41  protected function initDependencies(): void
42  {
43  global $DIC;
44  $this->dic = is_object($DIC) ? clone $DIC : $DIC;
45  $GLOBALS['DIC'] = new Container();
46 
47  $user = $this->getMockBuilder(ilObjUser::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $user->expects($this->never())
51  ->method($this->anything());
52 
53  $this->user = $user;
54  $this->setGlobal('ilUser', $user);
55  }
56 
65  protected function setGlobalDBAndRepo(
66  ilDBInterface $mock_db,
67  int $webr_id,
68  bool $update_history,
69  int $current_time,
70  array $datetimes
71  ): void {
72  $mock_db->method('quote')
73  ->willReturnCallback(function ($arg1, string $arg2) {
74  return (string) $arg1;
75  });
76  $mock_db->method('quoteIdentifier')
77  ->willReturnCallback(function (string $arg1) {
78  return $arg1;
79  });
80 
81  $this->setGlobal('ilDB', $mock_db);
82 
83  $this->web_link_repo = $this->getMockBuilder(ilWebLinkDatabaseRepository::class)
84  ->setConstructorArgs([$webr_id, $update_history])
85  ->onlyMethods([
86  'getCurrentTime',
87  'getNewDateTimeImmutable',
88  'createHistoryEntry',
89  'isInternalLink'
90  ])
91  ->getMock();
92 
93  $this->web_link_repo->method('getCurrentTime')
94  ->willReturn($current_time);
95  $this->web_link_repo->method('getNewDateTimeImmutable')
96  ->willReturnOnConsecutiveCalls(...$datetimes);
97  }
98 
99  protected function setGlobal(string $name, MockObject $obj): void
100  {
101  global $DIC;
102 
103  $GLOBALS[$name] = $obj;
104  unset($DIC[$name]);
105  $DIC[$name] = static function (Container $c) use ($obj) {
106  return $obj;
107  };
108  }
109 
110  protected function tearDown(): void
111  {
112  global $DIC;
113  $DIC = $this->dic;
114  parent::tearDown();
115  }
116 
120  protected function getNewDateTimeMock(int $timestamp): MockObject
121  {
122  $datetime = $this->getMockBuilder(DateTimeImmutable::class)
123  ->disableOriginalConstructor()
124  ->onlyMethods(['getTimestamp'])
125  ->getMock();
126  $datetime->method('getTimestamp')
127  ->willReturn($timestamp);
128 
129  return $datetime;
130  }
131 
138  public function testCreateExternalItem(): void
139  {
140  $mock_db = $this->getMockBuilder(ilDBInterface::class)
141  ->disableOriginalConstructor()
142  ->getMock();
143 
144  $mock_db->expects($this->exactly(3))
145  ->method('nextId')
146  ->willReturn(7, 71, 72);
147 
148  /*
149  * willReturnCallback is a workaround to replace withConsecutive.
150  * The return value is irrelevant here, but if an unexpected parameter
151  * is passed, an exception will be thrown (instead of an assumption being
152  * broken as before).
153  * These tests should be rewritten to rely much less on PHPUnit for mocking.
154  */
155  $mock_db->expects($this->exactly(3))
156  ->method('insert')
157  ->willReturnCallback(fn($table, $data) => match([$table, $data]) {
158  [
160  [
161  'webr_id' => ['integer', 0],
162  'link_id' => ['integer', 7],
163  'param_id' => ['integer', 71],
164  'name' => ['text', 'name1'],
165  'value' => ['integer', ilWebLinkBaseParameter::VALUES['user_id']]
166  ]
167  ] => 1,
168  [
170  [
171  'webr_id' => ['integer', 0],
172  'link_id' => ['integer', 7],
173  'param_id' => ['integer', 72],
174  'name' => ['text', 'name2'],
175  'value' => ['integer', ilWebLinkBaseParameter::VALUES['login']]
176  ]
177  ] => 2,
178  [
180  [
181  'internal' => ['integer', 0],
182  'webr_id' => ['integer', 0],
183  'link_id' => ['integer', 7],
184  'title' => ['text', 'title'],
185  'description' => ['text', 'description'],
186  'target' => ['text', 'target'],
187  'active' => ['integer', 1],
188  'create_date' => ['integer', 12345678],
189  'last_update' => ['integer', 12345678]
190  ]
191  ] => 3
192  });
193 
194  $param1 = new ilWebLinkDraftParameter(
196  'name1'
197  );
198  $param2 = new ilWebLinkDraftParameter(
200  'name2'
201  );
202  $item = new ilWebLinkDraftItem(
203  false,
204  'title',
205  'description',
206  'target',
207  true,
208  [$param1, $param2]
209  );
210 
211  $datetime1 = $this->getNewDateTimeMock(12345678);
212  $datetime2 = $this->getNewDateTimeMock(12345678);
213 
214  $this->setGlobalDBAndRepo(
215  $mock_db,
216  0,
217  true,
218  12345678,
219  [$datetime1, $datetime2]
220  );
221 
222  $this->web_link_repo->expects($this->once())
223  ->method('createHistoryEntry')
224  ->with(0, 'add', ['title']);
225  $this->web_link_repo->expects($this->never())
226  ->method('isInternalLink');
227 
228  $expected_param1 = new ilWebLinkParameter(
229  $this->user,
230  0,
231  7,
232  71,
234  'name1'
235  );
236 
237  $expected_param2 = new ilWebLinkParameter(
238  $this->user,
239  0,
240  7,
241  72,
243  'name2'
244  );
245 
246  $this->assertEquals(
248  0,
249  7,
250  'title',
251  'description',
252  'target',
253  true,
254  $datetime1,
255  $datetime2,
256  [$expected_param1, $expected_param2]
257  ),
258  $this->web_link_repo->createItem($item)
259  );
260  }
261 
269  {
270  $mock_db = $this->getMockBuilder(ilDBInterface::class)
271  ->disableOriginalConstructor()
272  ->getMock();
273  $mock_db->expects($this->exactly(3))
274  ->method('nextId')
275  ->willReturn(7, 71, 72);
276 
277  /*
278  * willReturnCallback is a workaround to replace withConsecutive.
279  * The return value is irrelevant here, but if an unexpected parameter
280  * is passed, an exception will be thrown (instead of an assumption being
281  * broken as before).
282  * These tests should be rewritten to rely much less on PHPUnit for mocking.
283  */
284  $mock_db->expects($this->exactly(2))
285  ->method('insert')
286  ->willReturnCallback(fn($table, $data) => match([$table, $data]) {
287  [
289  [
290  'webr_id' => ['integer', 0],
291  'link_id' => ['integer', 7],
292  'param_id' => ['integer', 72],
293  'name' => ['text', 'name2'],
294  'value' => ['integer', ilWebLinkBaseParameter::VALUES['login']]
295  ]
296  ] => 1,
297  [
299  [
300  'internal' => ['integer', 1],
301  'webr_id' => ['integer', 0],
302  'link_id' => ['integer', 7],
303  'title' => ['text', 'title'],
304  'description' => ['text', 'description'],
305  'target' => ['text', 'trg|123'],
306  'active' => ['integer', 1],
307  'create_date' => ['integer', 12345678],
308  'last_update' => ['integer', 12345678]
309  ]
310  ] => 2
311  });
312 
313  $param1 = new ilWebLinkDraftParameter(
314  23,
315  'name1'
316  );
317  $param2 = new ilWebLinkDraftParameter(
319  'name2'
320  );
321  $item = new ilWebLinkDraftItem(
322  true,
323  'title',
324  'description',
325  'trg|123',
326  true,
327  [$param1, $param2]
328  );
329 
330  $datetime1 = $this->getNewDateTimeMock(12345678);
331  $datetime2 = $this->getNewDateTimeMock(12345678);
332 
333  $this->setGlobalDBAndRepo(
334  $mock_db,
335  0,
336  true,
337  12345678,
338  [$datetime1, $datetime2]
339  );
340 
341  $this->web_link_repo->expects($this->once())
342  ->method('createHistoryEntry')
343  ->with(0, 'add', ['title']);
344  $this->web_link_repo->expects($this->once())
345  ->method('isInternalLink')
346  ->with('trg|123')
347  ->willReturn(true);
348 
349  $expected_param2 = new ilWebLinkParameter(
350  $this->user,
351  0,
352  7,
353  72,
355  'name2'
356  );
357 
358  $this->assertEquals(
360  0,
361  7,
362  'title',
363  'description',
364  'trg|123',
365  true,
366  $datetime1,
367  $datetime2,
368  [$expected_param2]
369  ),
370  $this->web_link_repo->createItem($item)
371  );
372  }
373 
379  {
380  $mock_db = $this->getMockBuilder(ilDBInterface::class)
381  ->disableOriginalConstructor()
382  ->getMock();
383 
384  $mock_db->expects($this->once())
385  ->method('nextId')
387  ->willReturn(7);
388 
389  $mock_db->expects($this->never())
390  ->method('insert');
391 
392  $param1 = new ilWebLinkDraftParameter(
394  'name1'
395  );
396  $param2 = new ilWebLinkDraftParameter(
398  'name2'
399  );
400  $item = new ilWebLinkDraftItem(
401  true,
402  'title',
403  'description',
404  'wrong link',
405  true,
406  [$param1, $param2]
407  );
408 
409  $datetime1 = $this->getNewDateTimeMock(12345678);
410  $datetime2 = $this->getNewDateTimeMock(12345678);
411 
412  $this->setGlobalDBAndRepo(
413  $mock_db,
414  0,
415  true,
416  12345678,
417  [$datetime1, $datetime2]
418  );
419 
420  $this->web_link_repo->expects($this->never())
421  ->method('createHistoryEntry');
422  $this->web_link_repo->expects($this->once())
423  ->method('isInternalLink')
424  ->with('wrong link')
425  ->willReturn(false);
426 
427  $this->expectException(ilWebLinkDatabaseRepositoryException::class);
428  $this->web_link_repo->createItem($item);
429  }
430 
435  public function testCreateList(): void
436  {
437  $mock_db = $this->getMockBuilder(ilDBInterface::class)
438  ->disableOriginalConstructor()
439  ->getMock();
440 
441  $mock_db->expects($this->never())
442  ->method('nextId');
443 
444  $mock_db->expects($this->once())
445  ->method('insert')
446  ->with(
448  [
449  'webr_id' => ['integer', 0],
450  'title' => ['text', 'title'],
451  'description' => ['text', ''],
452  'create_date' => ['integer', 12345678],
453  'last_update' => ['integer', 12345678]
454  ]
455  );
456 
457  $list = new ilWebLinkDraftList(
458  'title',
459  null
460  );
461 
462  $datetime1 = $this->getNewDateTimeMock(12345678);
463  $datetime2 = $this->getNewDateTimeMock(12345678);
464 
465  $this->setGlobalDBAndRepo(
466  $mock_db,
467  0,
468  true,
469  12345678,
470  [$datetime1, $datetime2]
471  );
472 
473  $this->web_link_repo->expects($this->once())
474  ->method('createHistoryEntry')
475  ->with(0, 'add', ['title']);
476 
477  $this->assertEquals(
478  new ilWebLinkList(
479  0,
480  'title',
481  '',
482  $datetime1,
483  $datetime2
484  ),
485  $this->web_link_repo->createList($list)
486  );
487  }
488 
493  public function testCreateAllItemsInDraftContainer(): void
494  {
495  $mock_db = $this->getMockBuilder(ilDBInterface::class)
496  ->disableOriginalConstructor()
497  ->getMock();
498 
499  $datetime1 = $this->getNewDateTimeMock(12345678);
500  $datetime2 = $this->getNewDateTimeMock(12345678);
501 
502  $this->setGlobalDBAndRepo(
503  $mock_db,
504  0,
505  false,
506  12345678,
507  [$datetime1, $datetime2]
508  );
509 
510  $draft_param1 = new ilWebLinkDraftParameter(
512  'name1'
513  );
514  $draft_param2 = new ilWebLinkDraftParameter(
516  'name2'
517  );
518  $draft_item1 = new ilWebLinkDraftItem(
519  false,
520  'title',
521  'description',
522  'target',
523  true,
524  [$draft_param1, $draft_param2]
525  );
526 
527  $draft_item2 = new ilWebLinkDraftItem(
528  true,
529  'title',
530  null,
531  'trg|123',
532  false,
533  []
534  );
535 
536  $draft_container = new ilWebLinkDraftItemsContainer([
537  $draft_item1,
538  $draft_item2
539  ]);
540 
541  $param1 = new ilWebLinkParameter(
542  $this->user,
543  0,
544  7,
545  71,
547  'name1'
548  );
549  $param2 = new ilWebLinkParameter(
550  $this->user,
551  0,
552  7,
553  72,
555  'name2'
556  );
557  $item1 = new ilWebLinkItemExternal(
558  0,
559  7,
560  'title',
561  'description',
562  'target',
563  true,
564  $datetime1,
565  $datetime2,
566  [$param1, $param2]
567  );
568 
569  $item2 = new ilWebLinkItemInternal(
570  0,
571  8,
572  'title',
573  null,
574  'trg|123',
575  false,
576  $datetime1,
577  $datetime2,
578  []
579  );
580 
581  $repo = $this->getMockBuilder(ilWebLinkDatabaseRepository::class)
582  ->setConstructorArgs([0, true])
583  ->onlyMethods(['createItem'])
584  ->getMock();
585 
586  $repo->expects($this->exactly(2))
587  ->method('createItem')
588  ->willReturnCallback(fn($item) => match($item) {
589  $draft_item1 => $item1,
590  $draft_item2 => $item2
591  });
592 
593  $this->assertEquals(
595  0,
596  [$item1, $item2]
597  ),
598  $repo->createAllItemsInDraftContainer($draft_container)
599  );
600  }
601 
605 }
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...
setGlobalDBAndRepo(ilDBInterface $mock_db, int $webr_id, bool $update_history, int $current_time, array $datetimes)
$datetime
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$c
Definition: deliver.php:9
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
$GLOBALS["DIC"]
Definition: wac.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testCreateInternalItemWithBrokenParameter()
Test creating an item with one intact and one broken parameter, and an internal link.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Immutable class for internal Web Link items.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const VALUES
TODO Once the GUI is updated, undefined can be dropped.
testCreateExternalItem()
Test creating an item with two intact parameters, and an external link.
Unit tests for ilWebLinkDatabaseRepository.