ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
MapTest.php
Go to the documentation of this file.
1 <?php
2 
20 
31 
32 // require_once('./vendor/composer/vendor/autoload.php');
33 
39 class MapTest extends TestCase
40 {
44 
45  private function getMap(): Map
46  {
47  return new Map($this->factory);
48  }
49 
53  protected function setUp(): void
54  {
55  parent::setUp();
56 
57  $this->provider = $this->getDummyProvider();
58  $this->identification = new IdentificationFactory(new NullProviderFactory());
59  $this->factory = new MainMenuItemFactory();
60  }
61 
62  private function getId(string $id): IdentificationInterface
63  {
64  return $this->identification->core($this->provider)->identifier($id);
65  }
66 
67  public function testAddItem(): void
68  {
69  $map = $this->getMap();
70 
71  $p1 = $this->getId('parent_1');
72  $p2 = $this->getId('parent_2');
73  $p3 = $this->getId('parent_3');
74  $map->addMultiple(
75  $this->factory->topParentItem($p1),
76  $this->factory->topParentItem($p2),
77  $this->factory->topParentItem($p3)
78  );
79 
80  $p4 = $this->getId('parent_4');
81  $map->add($this->factory->topParentItem($p4));
82 
83  $this->assertTrue($map->has());
84  $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
85  $this->assertTrue($map->existsInFilter($p1));
86  $this->assertTrue($map->existsInFilter($p2));
87  $this->assertTrue($map->existsInFilter($p3));
88  $this->assertTrue($map->existsInFilter($p4));
89  }
90 
91  public function testFilterItems(): void
92  {
93  $map = $this->getMap();
94 
95  $p1 = $this->getId('parent_1');
96  $p2 = $this->getId('parent_2');
97  $p3 = $this->getId('parent_3');
98  $p4 = $this->getId('parent_4');
99  $map->addMultiple(
100  $this->factory->topParentItem($p1),
101  $this->factory->topParentItem($p2),
102  $this->factory->topParentItem($p3),
103  $this->factory->topParentItem($p4)
104  );
105 
106  $this->assertTrue($map->has());
107  $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
108 
109  $map->filter(static fn(): bool => true);
110 
111  $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
112 
113  $map->filter(static fn(isItem $i): bool => $i->getProviderIdentification()->getInternalIdentifier() !== 'parent_1');
114 
115  $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 3);
116  $this->assertFalse($map->existsInFilter($p1));
117  $this->assertTrue($map->existsInFilter($p2));
118  $this->assertTrue($map->existsInFilter($p3));
119  $this->assertTrue($map->existsInFilter($p4));
120 
121  $map->filter(static fn(): bool => false);
122  $this->assertFalse($map->existsInFilter($p1));
123  $this->assertFalse($map->existsInFilter($p2));
124  $this->assertFalse($map->existsInFilter($p3));
125  $this->assertFalse($map->existsInFilter($p4));
126  }
127 
128  public function testSortingTopItems(): void
129  {
130  $map = $this->getMap();
131 
132  for ($x = 1; $x <= 10; $x++) {
133  $map->add($this->factory->topParentItem($this->getId((string) $x))->withPosition(11 - $x));
134  }
135 
136  $x = 10;
137  foreach ($map->getAllFromFilter() as $i) {
138  $this->assertSame($i->getPosition(), $x);
139  $x--;
140  }
141 
142  $map->sort();
143 
144  $generator = $map->getAllFromFilter();
145 
146  $one = static function () use ($generator): isItem {
147  $i = $generator->current();
148  $generator->next();
149  return $i;
150  };
151 
152  $i = $one();
153  $this->assertSame('10', $i->getProviderIdentification()->getInternalIdentifier());
154  $this->assertSame(1, $i->getPosition());
155 
156  $i = $one();
157  $this->assertSame('9', $i->getProviderIdentification()->getInternalIdentifier());
158  $this->assertSame(2, $i->getPosition());
159 
160  $i = $one();
161  $this->assertSame('8', $i->getProviderIdentification()->getInternalIdentifier());
162  $this->assertSame(3, $i->getPosition());
163 
164  $i = $one();
165  $this->assertSame('7', $i->getProviderIdentification()->getInternalIdentifier());
166  $this->assertSame(4, $i->getPosition());
167 
168  $i = $one();
169  $this->assertSame('6', $i->getProviderIdentification()->getInternalIdentifier());
170  $this->assertSame(5, $i->getPosition());
171 
172  $i = $one();
173  $this->assertSame('5', $i->getProviderIdentification()->getInternalIdentifier());
174  $this->assertSame(6, $i->getPosition());
175 
176  $i = $one();
177  $this->assertSame('4', $i->getProviderIdentification()->getInternalIdentifier());
178  $this->assertSame(7, $i->getPosition());
179 
180  $i = $one();
181  $this->assertSame('3', $i->getProviderIdentification()->getInternalIdentifier());
182  $this->assertSame(8, $i->getPosition());
183 
184  $i = $one();
185  $this->assertSame('2', $i->getProviderIdentification()->getInternalIdentifier());
186  $this->assertSame(9, $i->getPosition());
187 
188  $i = $one();
189  $this->assertSame('1', $i->getProviderIdentification()->getInternalIdentifier());
190  $this->assertSame(10, $i->getPosition());
191  }
192 
193  public function testSortingNestedItems(): void
194  {
195  $map = $this->getMap();
197  $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
198  ->withPosition(1);
199  $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
200  ->withParent($tp_1->getProviderIdentification())
201  ->withPosition(1);
202  $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
203  ->withParent($tp_1->getProviderIdentification())
204  ->withPosition(2);
205  $tp_1 = $tp_1->withChildren([
206  $tp_1_2,
207  $tp_1_1,
208  ]);
209  $map->add($tp_1);
210  $map->add($tp_1_2);
211  $map->add($tp_1_1);
212 
213  $tp_2 = $this->factory->topParentItem($this->getId('tp_2'))
214  ->withPosition(2);
215  $tp_2_1 = $this->factory->link($this->getId('tp_2_1'))
216  ->withParent($tp_2->getProviderIdentification())
217  ->withPosition(1);
218  $tp_2_2 = $this->factory->link($this->getId('tp_2_2'))
219  ->withParent($tp_2->getProviderIdentification())
220  ->withPosition(2);
221  $tp_2 = $tp_2->withChildren([
222  $tp_2_2,
223  $tp_2_1,
224  ]);
225  $map->add($tp_2);
226  $map->add($tp_2_1);
227  $map->add($tp_2_2);
228 
229  $map->sort();
230 
231  $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_1'))->getPosition());
232  $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_1_1'))->getPosition());
233  $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_1_2'))->getPosition());
234  $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_2'))->getPosition());
235  $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_2_1'))->getPosition());
236  $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_2_2'))->getPosition());
237 
238  // check position in parent
239  $get_tp_1 = $map->getSingleItemFromFilter($this->getId('tp_1'));
240  $children_of_tp_1 = $get_tp_1->getChildren();
241  $first = reset($children_of_tp_1);
242  $this->assertEquals($tp_1_1, $first);
243  $this->assertEquals(1, $first->getPosition());
244 
245  $get_tp_2 = $map->getSingleItemFromFilter($this->getId('tp_2'));
246  $children_of_tp_2 = $get_tp_2->getChildren();
247  $first = reset($children_of_tp_2);
248  $this->assertEquals($tp_2_1, $first);
249  $this->assertEquals(1, $first->getPosition());
250  }
251 
252  public function testSamePositionResolution(): void
253  {
254  $map = $this->getMap();
256  $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
257  ->withPosition(1);
258  $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
259  ->withParent($tp_1->getProviderIdentification())
260  ->withPosition(1);
261  $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
262  ->withParent($tp_1->getProviderIdentification())
263  ->withPosition(1);
264  $tp_1 = $tp_1->withChildren([
265  $tp_1_2,
266  $tp_1_1,
267  ]);
268  $map->add($tp_1);
269  $map->add($tp_1_2);
270  $map->add($tp_1_1);
271  $map->sort();
272  $item = $map->getSingleItemFromFilter($this->getId('tp_1'));
273  $this->assertCount(2, $item->getChildren());
274  }
275 
277  {
278  return new class () implements StaticMainMenuProvider {
279  public function getAllIdentifications(): array
280  {
281  return [];
282  }
283 
284  public function getFullyQualifiedClassName(): string
285  {
286  return 'Provider';
287  }
288 
289  public function getProviderNameForPresentation(): string
290  {
291  return 'Provider';
292  }
293 
294  public function getStaticTopItems(): array
295  {
296  return [];
297  }
298 
299  public function getStaticSubItems(): array
300  {
301  return [];
302  }
303 
304  public function provideTypeInformation(): TypeInformationCollection
305  {
306  return new TypeInformationCollection();
307  }
308  };
309  }
310 }
IdentificationFactory $identification
Definition: MapTest.php:41
StaticMainMenuProvider $provider
Definition: MapTest.php:43
factory()
Class FactoryImplTest.
Definition: MapTest.php:39
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class IdentificationFactory All elements in the GlobalScreen service must be identifiable for the sup...
MainMenuItemFactory $factory
Definition: MapTest.php:42