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