ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ilArtifactComponentRepositoryTest.php
Go to the documentation of this file.
1<?php
2
19use PHPUnit\Framework\TestCase;
20use ILIAS\Data;
21
23{
24 public static array $component_data = [
25 "mod1" => ["components/ILIAS", "Module1", [
26 ["slt1", "Slot1"],
27 ["slt2", "Slot2"],
28 ]],
29 "mod2" => ["components/ILIAS", "Module2", [
30 ]],
31 "ser1" => ["components/ILIAS", "Service1", [
32 ["slt3", "Slot3"]
33 ]],
34 "ser2" => ["components/ILIAS", "Service2", [
35 ["slt4", "Slot4"]
36 ]]
37 ];
38
39 public static array $plugin_data = [
40 "plg1" => [
41 "Type1",
42 "Module1",
43 "Slot1",
44 "Plugin1",
45 "1.9.1",
46 "8.0",
47 "8.999",
48 "Richard Klees",
49 "richard.klees@concepts-and-training.de",
50 true,
51 false,
52 null
53 ],
54 "plg2" => [
55 "Type2",
56 "Service2",
57 "Slot4",
58 "Plugin2",
59 "2.9.1",
60 "8.1",
61 "8.999",
62 "Richard Klees",
63 "richard.klees@concepts-and-training.de",
64 null,
65 true,
66 false
67 ],
68 "plg3" => [
69 "Type3",
70 "Service2",
71 "Slot4",
72 "Plugin3",
73 "2.9.2",
74 "8.1",
75 "8.999",
76 "Richard Klees",
77 "richard.klees@concepts-and-training.de",
78 null,
79 true,
80 false
81 ]
82 ];
83
84 protected Data\Factory $data_factory;
88 protected \PHPUnit\Framework\MockObject\Stub $ilias_version;
102
103 protected function setUp(): void
104 {
105 $this->data_factory = new Data\Factory();
106 $this->ilias_version = $this->createStub(Data\Version::class);
107 $this->plugin_state_db = new class () implements ilPluginStateDB {
108 public function isPluginActivated(string $id): bool
109 {
110 if ($id == 'plg3') {
111 return true;
112 }
113
114 return false;
115 }
116 public function setActivation(string $id, bool $activated): void
117 {
118 }
119 public function getCurrentPluginVersion(string $id): ?Data\Version
120 {
121 return (new Data\Factory())->version("0.9.1");
122 }
123 public function getCurrentPluginDBVersion(string $id): ?int
124 {
125 return 13;
126 }
127 public function setCurrentPluginVersion(string $id, Data\Version $version, int $db_version): void
128 {
129 }
130 public function remove(string $id): void
131 {
132 }
133 };
134
136 protected function readComponentData(): array
137 {
139 }
140 protected function readPluginData(): array
141 {
143 }
144 public function _buildDatabase(): void
145 {
146 $this->buildDatabase();
147 }
148 };
149
150 $slots1 = [];
151 $this->mod1 = new ilComponentInfo(
152 "mod1",
153 "components/ILIAS",
154 "Module1",
155 $slots1
156 );
157 $plugins1 = [];
158 $this->slt1 = new ilPluginSlotInfo(
159 $this->mod1,
160 "slt1",
161 "Slot1",
162 $plugins1
163 );
164 $this->plg1 = new ilPluginInfo(
165 $this->ilias_version,
166 $this->slt1,
167 "plg1",
168 "Plugin1",
169 "Type1",
170 false,
171 $this->data_factory->version("0.9.1"),
172 13,
173 $this->data_factory->version("1.9.1"),
174 $this->data_factory->version("8.0"),
175 $this->data_factory->version("8.999"),
176 "Richard Klees",
177 "richard.klees@concepts-and-training.de",
178 true,
179 false,
180 true
181 );
182 $plugins1["plg1"] = $this->plg1;
183 $plugins2 = [];
184 $this->slt2 = new ilPluginSlotInfo(
185 $this->mod1,
186 "slt2",
187 "Slot2",
188 $plugins2
189 );
190 $slots1 = ["slt1" => $this->slt1, "slt2" => $this->slt2];
191
192 $slots2 = [];
193 $this->mod2 = new ilComponentInfo(
194 "mod2",
195 "components/ILIAS",
196 "Module2",
197 $slots2
198 );
199
200 $slots3 = [];
201 $this->ser1 = new ilComponentInfo(
202 "ser1",
203 "components/ILIAS",
204 "Service1",
205 $slots3
206 );
207
208 $plugins3 = [];
209 $this->slt3 = new ilPluginSlotInfo(
210 $this->ser1,
211 "slt3",
212 "Slot3",
213 $plugins3
214 );
215 $slots3 = ["slt3" => $this->slt3];
216
217 $slots4 = [];
218 $this->ser2 = new ilComponentInfo(
219 "ser2",
220 "components/ILIAS",
221 "Service2",
222 $slots4
223 );
224 $plugins4 = [];
225 $this->slt4 = new ilPluginSlotInfo(
226 $this->ser2,
227 "slt4",
228 "Slot4",
229 $plugins4
230 );
231 $slots4 = ["slt4" => $this->slt4];
232
233 $this->plg2 = new ilPluginInfo(
234 $this->ilias_version,
235 $this->slt4,
236 "plg2",
237 "Plugin2",
238 "Type2",
239 false,
240 $this->data_factory->version("0.9.1"),
241 13,
242 $this->data_factory->version("2.9.1"),
243 $this->data_factory->version("8.1"),
244 $this->data_factory->version("8.999"),
245 "Richard Klees",
246 "richard.klees@concepts-and-training.de",
247 false,
248 true,
249 false
250 );
251 $plugins4["plg2"] = $this->plg2;
252
253 $this->plg3 = new ilPluginInfo(
254 $this->ilias_version,
255 $this->slt4,
256 "plg3",
257 "Plugin3",
258 "Type3",
259 true,
260 $this->data_factory->version("0.9.1"),
261 13,
262 $this->data_factory->version("2.9.2"),
263 $this->data_factory->version("8.1"),
264 $this->data_factory->version("8.999"),
265 "Richard Klees",
266 "richard.klees@concepts-and-training.de",
267 false,
268 true,
269 false
270 );
271 $plugins4["plg3"] = $this->plg3;
272 }
273
274 public function testHasComponent(): void
275 {
276 $this->assertTrue($this->db->hasComponent("components/ILIAS", "Module1"));
277 $this->assertTrue($this->db->hasComponent("components/ILIAS", "Module2"));
278 $this->assertTrue($this->db->hasComponent("components/ILIAS", "Service1"));
279 $this->assertTrue($this->db->hasComponent("components/ILIAS", "Service2"));
280 $this->assertFalse($this->db->hasComponent("components/ILIAS", "Module3"));
281 $this->assertFalse($this->db->hasComponent("components/ILIAS", "Module4"));
282 $this->assertFalse($this->db->hasComponent("components/ILIAS", "Service3"));
283 $this->assertFalse($this->db->hasComponent("components/ILIAS", "Service4"));
284 }
285
287 {
288 $this->expectException(\InvalidArgumentException::class);
289 $this->db->hasComponent("OtherComponent", "Module1");
290 }
291
292 public function testHasComponentId(): void
293 {
294 $this->assertTrue($this->db->hasComponentId("mod1"));
295 $this->assertTrue($this->db->hasComponentId("mod2"));
296 $this->assertTrue($this->db->hasComponentId("ser1"));
297 $this->assertTrue($this->db->hasComponentId("ser2"));
298 $this->assertFalse($this->db->hasComponentId("mod3"));
299 $this->assertFalse($this->db->hasComponentId("mod4"));
300 $this->assertFalse($this->db->hasComponentId("ser3"));
301 $this->assertFalse($this->db->hasComponentId("ser4"));
302 }
303
304 public function testGetComponents(): void
305 {
306 $result = iterator_to_array($this->db->getComponents());
307
308 $ids = array_keys($result);
309 $expected_ids = ["mod1", "mod2", "ser1", "ser2"];
310 sort($ids);
311 sort($expected_ids);
312
313 $this->assertEquals($expected_ids, $ids);
314
315 $this->assertEquals($this->mod1, $result["mod1"]);
316 $this->assertEquals($this->mod2, $result["mod2"]);
317 $this->assertEquals($this->ser1, $result["ser1"]);
318 $this->assertEquals($this->ser2, $result["ser2"]);
319 }
320
321 public function testGetComponentById(): void
322 {
323 $this->assertEquals($this->mod1, $this->db->getComponentById("mod1"));
324 $this->assertEquals($this->mod2, $this->db->getComponentById("mod2"));
325 $this->assertEquals($this->ser1, $this->db->getComponentById("ser1"));
326 $this->assertEquals($this->ser2, $this->db->getComponentById("ser2"));
327 }
328
329 public function testGetComponentByTypeAndName(): void
330 {
331 $this->assertEquals($this->mod1, $this->db->getComponentByTypeAndName("components/ILIAS", "Module1"));
332 $this->assertEquals($this->mod2, $this->db->getComponentByTypeAndName("components/ILIAS", "Module2"));
333 $this->assertEquals($this->ser1, $this->db->getComponentByTypeAndName("components/ILIAS", "Service1"));
334 $this->assertEquals($this->ser2, $this->db->getComponentByTypeAndName("components/ILIAS", "Service2"));
335 }
336
338 {
339 $this->expectException(\InvalidArgumentException::class);
340 $this->db->getComponentByTypeAndName("components/ILIAS", "Module3");
341 }
342
344 {
345 $this->expectException(\InvalidArgumentException::class);
346 $this->db->getComponentByTypeAndName("OtherComponent", "Service1");
347 }
348
350 {
351 $this->expectException(\InvalidArgumentException::class);
352 $this->db->getComponentById("some_id");
353 }
354
355 public function testGetPluginSlots(): void
356 {
357 $slots = iterator_to_array($this->db->getPluginSlots());
358
359 $ids = array_keys($slots);
360 $expected_ids = ["slt1", "slt2", "slt3", "slt4"];
361 sort($ids);
362 sort($expected_ids);
363
364 $this->assertEquals($expected_ids, $ids);
365
366 $this->assertEquals($this->slt1, $slots["slt1"]);
367 $this->assertEquals($this->slt2, $slots["slt2"]);
368 $this->assertEquals($this->slt3, $slots["slt3"]);
369 $this->assertEquals($this->slt4, $slots["slt4"]);
370 }
371
372 public function testGetPluginslotById(): void
373 {
374 $this->assertEquals($this->slt1, $this->db->getPluginSlotById("slt1"));
375 $this->assertEquals($this->slt2, $this->db->getPluginSlotById("slt2"));
376 $this->assertEquals($this->slt3, $this->db->getPluginSlotById("slt3"));
377 $this->assertEquals($this->slt4, $this->db->getPluginSlotById("slt4"));
378 }
379
380 public function testNoPluginSlot(): void
381 {
383 protected function readComponentData(): array
384 {
385 return ["mod2" => ["components/ILIAS", "Module2", []]];
386 }
387 protected function readPluginData(): array
388 {
389 return [];
390 }
391 };
392
393 $slots = iterator_to_array($db->getPluginSlots());
394 $this->assertEquals([], $slots);
395 }
396
397 public function testGetPlugins(): void
398 {
399 $plugins = iterator_to_array($this->db->getPlugins());
400
401 $ids = array_keys($plugins);
402 $expected_ids = ["plg1", "plg2", "plg3"];
403 sort($ids);
404 sort($expected_ids);
405
406 $this->assertEquals($expected_ids, $ids);
407
408 $this->assertEquals($this->plg1, $plugins["plg1"]);
409 $this->assertEquals($this->plg2, $plugins["plg2"]);
410 }
411
412 public function testGetPluginById(): void
413 {
414 $this->assertEquals($this->plg1, $this->db->getPluginById("plg1"));
415 $this->assertEquals($this->plg2, $this->db->getPluginById("plg2"));
416 }
417
418 public function testGetPluginByName(): void
419 {
420 $this->assertEquals($this->plg1, $this->db->getPluginByName("Plugin1"));
421 $this->assertEquals($this->plg2, $this->db->getPluginByName("Plugin2"));
422 }
423
424 public function testUnknownPlugin(): void
425 {
426 $this->expectException(\InvalidArgumentException::class);
427 $this->db->getPluginById("some_id");
428 }
429
430 public function testUsesPluginStateDB(): void
431 {
432 $plugin_state_db = $this->createMock(ilPluginStateDB::class);
433 $plugin_state_db->expects($this->once())
434 ->method("isPluginActivated")
435 ->with("plg1")
436 ->willReturn(true);
437 $plugin_state_db->expects($this->once())
438 ->method("getCurrentPluginVersion")
439 ->with("plg1")
440 ->willReturn($this->data_factory->version("1.8.0"));
441 $plugin_state_db->expects($this->once())
442 ->method("getCurrentPluginDBVersion")
443 ->with("plg1")
444 ->willReturn(42);
445
447 protected function readComponentData(): array
448 {
449 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
450 }
451 protected function readPluginData(): array
452 {
453 return [
454 "plg1" => [
455 "components/ILIAS",
456 "Module1",
457 "Slot1",
458 "Plugin1",
459 "1.9.1",
460 "8.0",
461 "8.999",
462 "Richard Klees",
463 "richard.klees@concepts-and-training.de",
464 true,
465 false,
466 null
467 ]
468 ];
469 }
470 };
471
472 $plugin = $db->getPluginById("plg1");
473 $this->assertTrue($plugin->isActivated());
474 $this->assertEquals(
475 $this->data_factory->version("1.8.0"),
476 $plugin->getCurrentVersion()
477 );
478 $this->assertEquals(42, $plugin->getCurrentDBVersion());
479 }
480
482 {
483 $plg1 = $this->db
484 ->getComponentByTypeAndName("components/ILIAS", "Module1")
485 ->getPluginSlotById("slt1")
486 ->getPluginById("plg1");
487
488 $this->assertEquals($this->plg1, $plg1);
489 }
490
492 {
493 $VERSION = $this->data_factory->version("1000.0.0");
494 $DB_VERSION = 1000;
495
496 $plugin_state_db = $this->createMock(ilPluginStateDB::class);
497 $plugin_state_db->expects($this->atLeastOnce())
498 ->method("isPluginActivated")
499 ->with("plg1")
500 ->willReturn(true);
501 $plugin_state_db->expects($this->atLeastOnce())
502 ->method("getCurrentPluginVersion")
503 ->with("plg1")
504 ->willReturn($this->data_factory->version("1.8.0"));
505 $plugin_state_db->expects($this->atLeastOnce())
506 ->method("getCurrentPluginDBVersion")
507 ->with("plg1")
508 ->willReturn(42);
509 $plugin_state_db->expects($this->once())
510 ->method("setCurrentPluginVersion")
511 ->with("plg1", $VERSION, $DB_VERSION);
512
514 protected function readComponentData(): array
515 {
516 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
517 }
518 protected function readPluginData(): array
519 {
520 return [
521 "plg1" => [
522 "components/ILIAS",
523 "Module1",
524 "Slot1",
525 "Plugin1",
526 "1.9.1",
527 "8.0",
528 "8.999",
529 "Richard Klees",
530 "richard.klees@concepts-and-training.de",
531 true,
532 false,
533 null
534 ]
535 ];
536 }
537 };
538
539 $db->setCurrentPluginVersion("plg1", $VERSION, $DB_VERSION);
540 }
541
543 {
544 $VERSION = $this->data_factory->version("1000.0.0");
545 $DB_VERSION = 1000;
546
547 $plugin_state_db = $this->createStub(ilPluginStateDB::class);
549 public int $build_called = 0;
550 protected function buildDatabase(): void
551 {
552 $this->build_called++;
553 parent::buildDatabase();
554 }
555 protected function readComponentData(): array
556 {
557 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
558 }
559 protected function readPluginData(): array
560 {
561 return [
562 "plg1" => [
563 "components/ILIAS",
564 "Module1",
565 "Slot1",
566 "Plugin1",
567 "1.9.1",
568 "8.0",
569 "8.999",
570 "Richard Klees",
571 "richard.klees@concepts-and-training.de",
572 true,
573 false,
574 null
575 ]
576 ];
577 }
578 };
579
580 $this->assertEquals(1, $db->build_called);
581
582 $db->setCurrentPluginVersion("plg1", $VERSION, $DB_VERSION);
583
584 $this->assertEquals(2, $db->build_called);
585 }
586
587 public function testCallBuildDatabaseTwice(): void
588 {
589 $this->db->_buildDatabase();
590
591 $this->assertEquals($this->mod1, $this->db->getComponentById("mod1"));
592 $this->assertEquals($this->mod1, $this->db->getComponentByTypeAndName("components/ILIAS", "Module1"));
593 $this->assertEquals($this->slt1, $this->db->getPluginSlotById("slt1"));
594 $this->assertEquals($this->plg1, $this->db->getPluginById("plg1"));
595 $this->assertEquals($this->plg2, $this->db->getPluginByName("Plugin2"));
596 }
597
598 public function testSetActivationCallsStateDB(): void
599 {
600 $plugin_state_db = $this->createMock(ilPluginStateDB::class);
601
603 protected function readComponentData(): array
604 {
605 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
606 }
607 protected function readPluginData(): array
608 {
609 return [
610 "plg1" => [
611 "components/ILIAS",
612 "Module1",
613 "Slot1",
614 "Plugin1",
615 "1.9.1",
616 "8.0",
617 "8.999",
618 "Richard Klees",
619 "richard.klees@concepts-and-training.de",
620 true,
621 false,
622 null
623 ]
624 ];
625 }
626 };
627
628 $plugin_state_db->expects($this->once())
629 ->method("setActivation")
630 ->with("plg1", true);
631
632 $db->setActivation("plg1", true);
633 }
634
635 public function testSetActivationTriggersRebuild(): void
636 {
637 $plugin_state_db = $this->createStub(ilPluginStateDB::class);
639 public int $build_called = 0;
640 protected function buildDatabase(): void
641 {
642 $this->build_called++;
643 parent::buildDatabase();
644 }
645 protected function readComponentData(): array
646 {
647 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
648 }
649 protected function readPluginData(): array
650 {
651 return [
652 "plg1" => [
653 "components/ILIAS",
654 "Module1",
655 "Slot1",
656 "Plugin1",
657 "1.9.1",
658 "8.0",
659 "8.999",
660 "Richard Klees",
661 "richard.klees@concepts-and-training.de",
662 true,
663 false,
664 null
665 ]
666 ];
667 }
668 };
669
670 $this->assertEquals(1, $db->build_called);
671
672 $db->setActivation("plg1", false);
673
674 $this->assertEquals(2, $db->build_called);
675 }
676
678 {
679 $plugin_state_db = $this->createMock(ilPluginStateDB::class);
680
682 protected function readComponentData(): array
683 {
684 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
685 }
686 protected function readPluginData(): array
687 {
688 return [
689 "plg1" => [
690 "components/ILIAS",
691 "Module1",
692 "Slot1",
693 "Plugin1",
694 "1.9.1",
695 "8.0",
696 "8.999",
697 "Richard Klees",
698 "richard.klees@concepts-and-training.de",
699 true,
700 false,
701 null
702 ]
703 ];
704 }
705 };
706
707 $plugin_state_db->expects($this->once())
708 ->method("remove")
709 ->with("plg1");
710
712 }
713
715 {
716 $plugin_state_db = $this->createStub(ilPluginStateDB::class);
718 public int $build_called = 0;
719 protected function buildDatabase(): void
720 {
721 $this->build_called++;
722 parent::buildDatabase();
723 }
724 protected function readComponentData(): array
725 {
726 return ["mod1" => ["components/ILIAS", "Module1", [["slt1", "Slot1"]]]];
727 }
728 protected function readPluginData(): array
729 {
730 return [
731 "plg1" => [
732 "components/ILIAS",
733 "Module1",
734 "Slot1",
735 "Plugin1",
736 "1.9.1",
737 "8.0",
738 "8.999",
739 "Richard Klees",
740 "richard.klees@concepts-and-training.de",
741 true,
742 false,
743 null
744 ]
745 ];
746 }
747 };
748
749 $this->assertEquals(1, $db->build_called);
750
752
753 $this->assertEquals(2, $db->build_called);
754 }
755
756 public function testHasPluginId(): void
757 {
758 $this->assertTrue($this->db->hasPluginId("plg1"));
759 $this->assertFalse($this->db->hasPluginId("plg666"));
760 }
761
762 public function testHasActivatedPlugin(): void
763 {
764 $this->assertFalse($this->db->hasActivatedPlugin("plg1")); // exists, but is not activated
765 $this->assertFalse($this->db->hasActivatedPlugin("plg666")); // does not exist
766 $this->assertTrue($this->db->hasActivatedPlugin("plg3")); // exists and is activated
767 }
768}
$version
Definition: plugin.php:24
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:36
A version number that consists of three numbers (major, minor, patch).
Definition: Version.php:27
PHPUnit Framework MockObject Stub $ilias_version
Repository for component data implemented over artifacts.
setCurrentPluginVersion(string $plugin_id, Data\Version $version, int $db_version)
setActivation(string $plugin_id, bool $activated)
Simple value class for basic information about a component.
Simple value class for information about a plugin.
Simple value class for basic information about a pluginslot.
Repository interface for plugin state data.