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