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