ILIAS  release_8 Revision v8.24
ilWebResourceDatabaseRepositoryTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use PHPUnit\Framework\TestCase;
22use PHPUnit\Framework\MockObject\MockObject;
24
30{
31 protected ?Container $dic = null;
32 protected ilObjUser $user;
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(['getCurrentTime', 'getNewDateTimeImmutable'])
86 ->getMock();
87
88 $this->web_link_repo->method('getCurrentTime')
89 ->willReturn($current_time);
90 $this->web_link_repo->method('getNewDateTimeImmutable')
91 ->willReturnOnConsecutiveCalls(...$datetimes);
92 }
93
94 protected function setGlobal(string $name, MockObject $obj): void
95 {
96 global $DIC;
97
98 $GLOBALS[$name] = $obj;
99 unset($DIC[$name]);
100 $DIC[$name] = static function (Container $c) use ($obj) {
101 return $obj;
102 };
103 }
104
105 protected function tearDown(): void
106 {
107 global $DIC;
109 parent::tearDown();
110 }
111
115 protected function getNewDateTimeMock(int $timestamp): MockObject
116 {
117 $datetime = $this->getMockBuilder(DateTimeImmutable::class)
118 ->disableOriginalConstructor()
119 ->onlyMethods(['getTimestamp'])
120 ->getMock();
121 $datetime->method('getTimestamp')
122 ->willReturn($timestamp);
123
124 return $datetime;
125 }
126
133 public function testCreateExternalItem(): void
134 {
135 $mock_db = $this->getMockBuilder(ilDBInterface::class)
136 ->disableOriginalConstructor()
137 ->getMock();
138
139 $mock_db->expects($this->exactly(3))
140 ->method('nextId')
141 ->withConsecutive(
145 )
146 ->willReturn(7, 71, 72);
147
148 $mock_db->expects($this->exactly(3))
149 ->method('insert')
150 ->withConsecutive(
151 [
153 [
154 'webr_id' => ['integer', 0],
155 'link_id' => ['integer', 7],
156 'param_id' => ['integer', 71],
157 'name' => ['text', 'name1'],
158 'value' => ['integer', ilWebLinkBaseParameter::VALUES['user_id']]
159 ]
160 ],
161 [
163 [
164 'webr_id' => ['integer', 0],
165 'link_id' => ['integer', 7],
166 'param_id' => ['integer', 72],
167 'name' => ['text', 'name2'],
168 'value' => ['integer', ilWebLinkBaseParameter::VALUES['login']]
169 ]
170 ],
171 [
173 [
174 'internal' => ['integer', 0],
175 'webr_id' => ['integer', 0],
176 'link_id' => ['integer', 7],
177 'title' => ['text', 'title'],
178 'description' => ['text', 'description'],
179 'target' => ['text', 'target'],
180 'active' => ['integer', 1],
181 'create_date' => ['integer', 12345678],
182 'last_update' => ['integer', 12345678]
183 ]
184 ]
185 );
186
187 $history = Mockery::mock('alias:' . ilHistory::class);
188 $history->shouldReceive('_createEntry')
189 ->once()
190 ->with(0, 'add', ['title']);
191
192 $link_input = Mockery::mock('alias:' . ilLinkInputGUI::class);
193 $link_input->shouldReceive('isInternalLink')
194 ->never();
195
196 $param1 = new ilWebLinkDraftParameter(
198 'name1'
199 );
200 $param2 = new ilWebLinkDraftParameter(
202 'name2'
203 );
204 $item = new ilWebLinkDraftItem(
205 false,
206 'title',
207 'description',
208 'target',
209 true,
210 [$param1, $param2]
211 );
212
213 $datetime1 = $this->getNewDateTimeMock(12345678);
214 $datetime2 = $this->getNewDateTimeMock(12345678);
215
216 $this->setGlobalDBAndRepo(
217 $mock_db,
218 0,
219 true,
220 12345678,
221 [$datetime1, $datetime2]
222 );
223
224 $expected_param1 = new ilWebLinkParameter(
225 $this->user,
226 0,
227 7,
228 71,
230 'name1'
231 );
232
233 $expected_param2 = new ilWebLinkParameter(
234 $this->user,
235 0,
236 7,
237 72,
239 'name2'
240 );
241
242 $this->assertEquals(
244 0,
245 7,
246 'title',
247 'description',
248 'target',
249 true,
250 $datetime1,
251 $datetime2,
252 [$expected_param1, $expected_param2]
253 ),
254 $this->web_link_repo->createItem($item)
255 );
256 }
257
265 {
266 $mock_db = $this->getMockBuilder(ilDBInterface::class)
267 ->disableOriginalConstructor()
268 ->getMock();
269
270 $mock_db->expects($this->exactly(3))
271 ->method('nextId')
272 ->withConsecutive(
276 )
277 ->willReturn(7, 71, 72);
278
279 $mock_db->expects($this->exactly(2))
280 ->method('insert')
281 ->withConsecutive(
282 [
284 [
285 'webr_id' => ['integer', 0],
286 'link_id' => ['integer', 7],
287 'param_id' => ['integer', 72],
288 'name' => ['text', 'name2'],
289 'value' => ['integer', ilWebLinkBaseParameter::VALUES['login']]
290 ]
291 ],
292 [
294 [
295 'internal' => ['integer', 1],
296 'webr_id' => ['integer', 0],
297 'link_id' => ['integer', 7],
298 'title' => ['text', 'title'],
299 'description' => ['text', 'description'],
300 'target' => ['text', 'trg|123'],
301 'active' => ['integer', 1],
302 'create_date' => ['integer', 12345678],
303 'last_update' => ['integer', 12345678]
304 ]
305 ]
306 );
307
308 $history = Mockery::mock('alias:' . ilHistory::class);
309 $history->shouldReceive('_createEntry')
310 ->once()
311 ->with(0, 'add', ['title']);
312
313 $link_input = Mockery::mock('alias:' . ilLinkInputGUI::class);
314 $link_input->shouldReceive('isInternalLink')
315 ->once()
316 ->with('trg|123')
317 ->andReturn(true);
318
319 $param1 = new ilWebLinkDraftParameter(
320 23,
321 'name1'
322 );
323 $param2 = new ilWebLinkDraftParameter(
325 'name2'
326 );
327 $item = new ilWebLinkDraftItem(
328 true,
329 'title',
330 'description',
331 'trg|123',
332 true,
333 [$param1, $param2]
334 );
335
336 $datetime1 = $this->getNewDateTimeMock(12345678);
337 $datetime2 = $this->getNewDateTimeMock(12345678);
338
339 $this->setGlobalDBAndRepo(
340 $mock_db,
341 0,
342 true,
343 12345678,
344 [$datetime1, $datetime2]
345 );
346
347 $expected_param2 = new ilWebLinkParameter(
348 $this->user,
349 0,
350 7,
351 72,
353 'name2'
354 );
355
356 $this->assertEquals(
358 0,
359 7,
360 'title',
361 'description',
362 'trg|123',
363 true,
364 $datetime1,
365 $datetime2,
366 [$expected_param2]
367 ),
368 $this->web_link_repo->createItem($item)
369 );
370 }
371
377 {
378 $mock_db = $this->getMockBuilder(ilDBInterface::class)
379 ->disableOriginalConstructor()
380 ->getMock();
381
382 $mock_db->expects($this->once())
383 ->method('nextId')
385 ->willReturn(7);
386
387 $mock_db->expects($this->never())
388 ->method('insert');
389
390 $history = Mockery::mock('alias:' . ilHistory::class);
391 $history->shouldReceive('_createEntry')
392 ->never();
393
394 $link_input = Mockery::mock('alias:' . ilLinkInputGUI::class);
395 $link_input->shouldReceive('isInternalLink')
396 ->once()
397 ->with('wrong link')
398 ->andReturn(false);
399
400 $param1 = new ilWebLinkDraftParameter(
402 'name1'
403 );
404 $param2 = new ilWebLinkDraftParameter(
406 'name2'
407 );
408 $item = new ilWebLinkDraftItem(
409 true,
410 'title',
411 'description',
412 'wrong link',
413 true,
414 [$param1, $param2]
415 );
416
417 $datetime1 = $this->getNewDateTimeMock(12345678);
418 $datetime2 = $this->getNewDateTimeMock(12345678);
419
420 $this->setGlobalDBAndRepo(
421 $mock_db,
422 0,
423 true,
424 12345678,
425 [$datetime1, $datetime2]
426 );
427
428 $this->expectException(ilWebLinkDatabaseRepositoryException::class);
429 $this->web_link_repo->createItem($item);
430 }
431
436 public function testCreateList(): void
437 {
438 $mock_db = $this->getMockBuilder(ilDBInterface::class)
439 ->disableOriginalConstructor()
440 ->getMock();
441
442 $mock_db->expects($this->never())
443 ->method('nextId');
444
445 $mock_db->expects($this->once())
446 ->method('insert')
447 ->with(
449 [
450 'webr_id' => ['integer', 0],
451 'title' => ['text', 'title'],
452 'description' => ['text', ''],
453 'create_date' => ['integer', 12345678],
454 'last_update' => ['integer', 12345678]
455 ]
456 );
457
458 $history = Mockery::mock('alias:' . ilHistory::class);
459 $history->shouldReceive('_createEntry')
460 ->once()
461 ->with(0, 'add', ['title']);
462
463 $list = new ilWebLinkDraftList(
464 'title',
465 null
466 );
467
468 $datetime1 = $this->getNewDateTimeMock(12345678);
469 $datetime2 = $this->getNewDateTimeMock(12345678);
470
471 $this->setGlobalDBAndRepo(
472 $mock_db,
473 0,
474 true,
475 12345678,
476 [$datetime1, $datetime2]
477 );
478
479 $this->assertEquals(
480 new ilWebLinkList(
481 0,
482 'title',
483 '',
484 $datetime1,
485 $datetime2
486 ),
487 $this->web_link_repo->createList($list)
488 );
489 }
490
496 {
497 $mock_db = $this->getMockBuilder(ilDBInterface::class)
498 ->disableOriginalConstructor()
499 ->getMock();
500
501 $datetime1 = $this->getNewDateTimeMock(12345678);
502 $datetime2 = $this->getNewDateTimeMock(12345678);
503
504 $this->setGlobalDBAndRepo(
505 $mock_db,
506 0,
507 false,
508 12345678,
509 [$datetime1, $datetime2]
510 );
511
512 $draft_param1 = new ilWebLinkDraftParameter(
514 'name1'
515 );
516 $draft_param2 = new ilWebLinkDraftParameter(
518 'name2'
519 );
520 $draft_item1 = new ilWebLinkDraftItem(
521 false,
522 'title',
523 'description',
524 'target',
525 true,
526 [$draft_param1, $draft_param2]
527 );
528
529 $draft_item2 = new ilWebLinkDraftItem(
530 true,
531 'title',
532 null,
533 'trg|123',
534 false,
535 []
536 );
537
538 $draft_container = new ilWebLinkDraftItemsContainer([
539 $draft_item1,
540 $draft_item2
541 ]);
542
543 $param1 = new ilWebLinkParameter(
544 $this->user,
545 0,
546 7,
547 71,
549 'name1'
550 );
551 $param2 = new ilWebLinkParameter(
552 $this->user,
553 0,
554 7,
555 72,
557 'name2'
558 );
559 $item1 = new ilWebLinkItemExternal(
560 0,
561 7,
562 'title',
563 'description',
564 'target',
565 true,
566 $datetime1,
567 $datetime2,
568 [$param1, $param2]
569 );
570
571 $item2 = new ilWebLinkItemInternal(
572 0,
573 8,
574 'title',
575 null,
576 'trg|123',
577 false,
578 $datetime1,
579 $datetime2,
580 []
581 );
582
583 $repo = $this->getMockBuilder(ilWebLinkDatabaseRepository::class)
584 ->setConstructorArgs([0, true])
585 ->onlyMethods(['createItem'])
586 ->getMock();
587
588 $repo->expects($this->exactly(2))
589 ->method('createItem')
590 ->withConsecutive([$draft_item1], [$draft_item2])
591 ->willReturn($item1, $item2);
592
593 $this->assertEquals(
595 0,
596 [$item1, $item2]
597 ),
598 $repo->createAllItemsInDraftContainer($draft_container)
599 );
600 }
601
605}
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:70
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
User class.
const VALUES
TODO Once the GUI is updated, undefined can be dropped.
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...
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...
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...
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...
Unit tests for ilWebLinkDatabaseRepository.
testCreateList()
@runInSeparateProcess @preserveGlobalState disabled
setGlobalDBAndRepo(ilDBInterface $mock_db, int $webr_id, bool $update_history, int $current_time, array $datetimes)
testCreateItemBrokenInternalLinkException()
@runInSeparateProcess @preserveGlobalState disabled
testCreateAllItemsInDraftContainer()
@runInSeparateProcess @preserveGlobalState disabled
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: cli.php:38
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if($format !==null) $name
Definition: metadata.php:247
$dic
Definition: result.php:32