ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilWebResourceDatabaseRepositoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
22use PHPUnit\Framework\MockObject\MockObject;
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
64 protected function setGlobalDBAndRepo(
65 ilDBInterface $mock_db,
66 int $webr_id,
67 int $current_time,
68 array $datetimes
69 ): void {
70 $mock_db->method('quote')
71 ->willReturnCallback(function ($arg1, string $arg2) {
72 return (string) $arg1;
73 });
74 $mock_db->method('quoteIdentifier')
75 ->willReturnCallback(function (string $arg1) {
76 return $arg1;
77 });
78
79 $this->setGlobal('ilDB', $mock_db);
80
81 $this->web_link_repo = $this->getMockBuilder(ilWebLinkDatabaseRepository::class)
82 ->setConstructorArgs([$webr_id])
83 ->onlyMethods([
84 'getCurrentTime',
85 'getNewDateTimeImmutable',
86 'isInternalLink'
87 ])
88 ->getMock();
89
90 $this->web_link_repo->method('getCurrentTime')
91 ->willReturn($current_time);
92 $this->web_link_repo->method('getNewDateTimeImmutable')
93 ->willReturnOnConsecutiveCalls(...$datetimes);
94 }
95
96 protected function setGlobal(string $name, MockObject $obj): void
97 {
98 global $DIC;
99
100 $GLOBALS[$name] = $obj;
101 unset($DIC[$name]);
102 $DIC[$name] = static function (Container $c) use ($obj) {
103 return $obj;
104 };
105 }
106
107 protected function tearDown(): void
108 {
109 global $DIC;
111 parent::tearDown();
112 }
113
117 protected function getNewDateTimeMock(int $timestamp): MockObject
118 {
119 $datetime = $this->getMockBuilder(DateTimeImmutable::class)
120 ->disableOriginalConstructor()
121 ->onlyMethods(['getTimestamp'])
122 ->getMock();
123 $datetime->method('getTimestamp')
124 ->willReturn($timestamp);
125
126 return $datetime;
127 }
128
133 #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
134 #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
135 public function testCreateExternalItem(): void
136 {
137 $mock_db = $this->getMockBuilder(ilDBInterface::class)
138 ->disableOriginalConstructor()
139 ->getMock();
140
141 $mock_db->expects($this->exactly(3))
142 ->method('nextId')
143 ->willReturn(7, 71, 72);
144
145 /*
146 * willReturnCallback is a workaround to replace withConsecutive.
147 * The return value is irrelevant here, but if an unexpected parameter
148 * is passed, an exception will be thrown (instead of an assumption being
149 * broken as before).
150 * These tests should be rewritten to rely much less on PHPUnit for mocking.
151 */
152 $mock_db->expects($this->exactly(3))
153 ->method('insert')
154 ->willReturnCallback(fn($table, $data) => match([$table, $data]) {
155 [
157 [
158 'webr_id' => ['integer', 0],
159 'link_id' => ['integer', 7],
160 'param_id' => ['integer', 71],
161 'name' => ['text', 'name1'],
162 'value' => ['integer', ilWebLinkBaseParameter::VALUES['user_id']]
163 ]
164 ] => 1,
165 [
167 [
168 'webr_id' => ['integer', 0],
169 'link_id' => ['integer', 7],
170 'param_id' => ['integer', 72],
171 'name' => ['text', 'name2'],
172 'value' => ['integer', ilWebLinkBaseParameter::VALUES['login']]
173 ]
174 ] => 2,
175 [
177 [
178 'internal' => ['integer', 0],
179 'webr_id' => ['integer', 0],
180 'link_id' => ['integer', 7],
181 'title' => ['text', 'title'],
182 'description' => ['text', 'description'],
183 'target' => ['text', 'target'],
184 'active' => ['integer', 1],
185 'create_date' => ['integer', 12345678],
186 'last_update' => ['integer', 12345678]
187 ]
188 ] => 3
189 });
190
191 $param1 = new ilWebLinkDraftParameter(
193 'name1'
194 );
195 $param2 = new ilWebLinkDraftParameter(
197 'name2'
198 );
199 $item = new ilWebLinkDraftItem(
200 false,
201 'title',
202 'description',
203 'target',
204 true,
205 [$param1, $param2]
206 );
207
208 $datetime1 = $this->getNewDateTimeMock(12345678);
209 $datetime2 = $this->getNewDateTimeMock(12345678);
210
211 $this->setGlobalDBAndRepo(
212 $mock_db,
213 0,
214 12345678,
215 [$datetime1, $datetime2]
216 );
217
218 $this->web_link_repo->expects($this->never())
219 ->method('isInternalLink');
220
221 $expected_param1 = new ilWebLinkParameter(
222 $this->user,
223 0,
224 7,
225 71,
227 'name1'
228 );
229
230 $expected_param2 = new ilWebLinkParameter(
231 $this->user,
232 0,
233 7,
234 72,
236 'name2'
237 );
238
239 $this->assertEquals(
241 0,
242 7,
243 'title',
244 'description',
245 'target',
246 true,
247 $datetime1,
248 $datetime2,
249 [$expected_param1, $expected_param2]
250 ),
251 $this->web_link_repo->createItem($item)
252 );
253 }
254
259 #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
260 #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
262 {
263 $mock_db = $this->getMockBuilder(ilDBInterface::class)
264 ->disableOriginalConstructor()
265 ->getMock();
266 $mock_db->expects($this->exactly(3))
267 ->method('nextId')
268 ->willReturn(7, 71, 72);
269
270 /*
271 * willReturnCallback is a workaround to replace withConsecutive.
272 * The return value is irrelevant here, but if an unexpected parameter
273 * is passed, an exception will be thrown (instead of an assumption being
274 * broken as before).
275 * These tests should be rewritten to rely much less on PHPUnit for mocking.
276 */
277 $mock_db->expects($this->exactly(2))
278 ->method('insert')
279 ->willReturnCallback(fn($table, $data) => match([$table, $data]) {
280 [
282 [
283 'webr_id' => ['integer', 0],
284 'link_id' => ['integer', 7],
285 'param_id' => ['integer', 72],
286 'name' => ['text', 'name2'],
287 'value' => ['integer', ilWebLinkBaseParameter::VALUES['login']]
288 ]
289 ] => 1,
290 [
292 [
293 'internal' => ['integer', 1],
294 'webr_id' => ['integer', 0],
295 'link_id' => ['integer', 7],
296 'title' => ['text', 'title'],
297 'description' => ['text', 'description'],
298 'target' => ['text', 'trg|123'],
299 'active' => ['integer', 1],
300 'create_date' => ['integer', 12345678],
301 'last_update' => ['integer', 12345678]
302 ]
303 ] => 2
304 });
305
306 $param1 = new ilWebLinkDraftParameter(
307 23,
308 'name1'
309 );
310 $param2 = new ilWebLinkDraftParameter(
312 'name2'
313 );
314 $item = new ilWebLinkDraftItem(
315 true,
316 'title',
317 'description',
318 'trg|123',
319 true,
320 [$param1, $param2]
321 );
322
323 $datetime1 = $this->getNewDateTimeMock(12345678);
324 $datetime2 = $this->getNewDateTimeMock(12345678);
325
326 $this->setGlobalDBAndRepo(
327 $mock_db,
328 0,
329 12345678,
330 [$datetime1, $datetime2]
331 );
332
333 $this->web_link_repo->expects($this->once())
334 ->method('isInternalLink')
335 ->with('trg|123')
336 ->willReturn(true);
337
338 $expected_param2 = new ilWebLinkParameter(
339 $this->user,
340 0,
341 7,
342 72,
344 'name2'
345 );
346
347 $this->assertEquals(
349 0,
350 7,
351 'title',
352 'description',
353 'trg|123',
354 true,
355 $datetime1,
356 $datetime2,
357 [$expected_param2]
358 ),
359 $this->web_link_repo->createItem($item)
360 );
361 }
362
363 #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
364 #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
366 {
367 $mock_db = $this->getMockBuilder(ilDBInterface::class)
368 ->disableOriginalConstructor()
369 ->getMock();
370
371 $mock_db->expects($this->once())
372 ->method('nextId')
374 ->willReturn(7);
375
376 $mock_db->expects($this->never())
377 ->method('insert');
378
379 $param1 = new ilWebLinkDraftParameter(
381 'name1'
382 );
383 $param2 = new ilWebLinkDraftParameter(
385 'name2'
386 );
387 $item = new ilWebLinkDraftItem(
388 true,
389 'title',
390 'description',
391 'wrong link',
392 true,
393 [$param1, $param2]
394 );
395
396 $datetime1 = $this->getNewDateTimeMock(12345678);
397 $datetime2 = $this->getNewDateTimeMock(12345678);
398
399 $this->setGlobalDBAndRepo(
400 $mock_db,
401 0,
402 12345678,
403 [$datetime1, $datetime2]
404 );
405
406 $this->web_link_repo->expects($this->once())
407 ->method('isInternalLink')
408 ->with('wrong link')
409 ->willReturn(false);
410
411 $this->expectException(ilWebLinkDatabaseRepositoryException::class);
412 $this->web_link_repo->createItem($item);
413 }
414
415 #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
416 #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
417 public function testCreateList(): void
418 {
419 $mock_db = $this->getMockBuilder(ilDBInterface::class)
420 ->disableOriginalConstructor()
421 ->getMock();
422
423 $mock_db->expects($this->never())
424 ->method('nextId');
425
426 $mock_db->expects($this->once())
427 ->method('insert')
428 ->with(
430 [
431 'webr_id' => ['integer', 0],
432 'title' => ['text', 'title'],
433 'description' => ['text', ''],
434 'create_date' => ['integer', 12345678],
435 'last_update' => ['integer', 12345678]
436 ]
437 );
438
439 $list = new ilWebLinkDraftList(
440 'title',
441 null
442 );
443
444 $datetime1 = $this->getNewDateTimeMock(12345678);
445 $datetime2 = $this->getNewDateTimeMock(12345678);
446
447 $this->setGlobalDBAndRepo(
448 $mock_db,
449 0,
450 12345678,
451 [$datetime1, $datetime2]
452 );
453
454 $this->assertEquals(
455 new ilWebLinkList(
456 0,
457 'title',
458 '',
459 $datetime1,
460 $datetime2
461 ),
462 $this->web_link_repo->createList($list)
463 );
464 }
465
466 #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)]
467 #[\PHPUnit\Framework\Attributes\RunInSeparateProcess]
469 {
470 $mock_db = $this->getMockBuilder(ilDBInterface::class)
471 ->disableOriginalConstructor()
472 ->getMock();
473
474 $datetime1 = $this->getNewDateTimeMock(12345678);
475 $datetime2 = $this->getNewDateTimeMock(12345678);
476
477 $this->setGlobalDBAndRepo(
478 $mock_db,
479 0,
480 12345678,
481 [$datetime1, $datetime2]
482 );
483
484 $draft_param1 = new ilWebLinkDraftParameter(
486 'name1'
487 );
488 $draft_param2 = new ilWebLinkDraftParameter(
490 'name2'
491 );
492 $draft_item1 = new ilWebLinkDraftItem(
493 false,
494 'title',
495 'description',
496 'target',
497 true,
498 [$draft_param1, $draft_param2]
499 );
500
501 $draft_item2 = new ilWebLinkDraftItem(
502 true,
503 'title',
504 null,
505 'trg|123',
506 false,
507 []
508 );
509
510 $draft_container = new ilWebLinkDraftItemsContainer([
511 $draft_item1,
512 $draft_item2
513 ]);
514
515 $param1 = new ilWebLinkParameter(
516 $this->user,
517 0,
518 7,
519 71,
521 'name1'
522 );
523 $param2 = new ilWebLinkParameter(
524 $this->user,
525 0,
526 7,
527 72,
529 'name2'
530 );
531 $item1 = new ilWebLinkItemExternal(
532 0,
533 7,
534 'title',
535 'description',
536 'target',
537 true,
538 $datetime1,
539 $datetime2,
540 [$param1, $param2]
541 );
542
543 $item2 = new ilWebLinkItemInternal(
544 0,
545 8,
546 'title',
547 null,
548 'trg|123',
549 false,
550 $datetime1,
551 $datetime2,
552 []
553 );
554
555 $repo = $this->getMockBuilder(ilWebLinkDatabaseRepository::class)
556 ->setConstructorArgs([0, true])
557 ->onlyMethods(['createItem'])
558 ->getMock();
559
560 $repo->expects($this->exactly(2))
561 ->method('createItem')
562 ->willReturnCallback(fn($item) => match($item) {
563 $draft_item1 => $item1,
564 $draft_item2 => $item2
565 });
566
567 $this->assertEquals(
569 0,
570 [$item1, $item2]
571 ),
572 $repo->createAllItemsInDraftContainer($draft_container)
573 );
574 }
575
579}
$datetime
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
User class.
const array VALUES
TODO Once the GUI is updated, undefined can be dropped.
Draft class for creating and updating a Web Link item.
Container class for drafted Web Link items.
Draft class for creating or updating a Web Link list.
Draft class for creating and updating a parameter attached to Web Link items.
Immutable class for external Web Link items.
Immutable class for internal Web Link items.
Immutable container class for Web Link items.
Immutable class for parameters attached to Web Link items.
Unit tests for ilWebLinkDatabaseRepository.
setGlobalDBAndRepo(ilDBInterface $mock_db, int $webr_id, int $current_time, array $datetimes)
testCreateInternalItemWithBrokenParameter()
Test creating an item with one intact and one broken parameter, and an internal link.
testCreateExternalItem()
Test creating an item with two intact parameters, and an external link.
$c
Definition: deliver.php:25
Interface ilDBInterface.
$dic
Definition: ltiresult.php:33
global $DIC
Definition: shib_login.php:26
$GLOBALS["DIC"]
Definition: wac.php:54