ILIAS  release_8 Revision v8.24
MapTest.php
Go to the documentation of this file.
1<?php
2
20
29use PHPUnit\Framework\TestCase;
31
32// require_once('./libs/composer/vendor/autoload.php');
33
39class 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 function (): bool {
110 return true;
111 });
112
113 $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 4);
114
115 $map->filter(static function (isItem $i): bool {
116 return $i->getProviderIdentification()->getInternalIdentifier() !== 'parent_1';
117 });
118
119 $this->assertSame(count(iterator_to_array($map->getAllFromFilter())), 3);
120 $this->assertFalse($map->existsInFilter($p1));
121 $this->assertTrue($map->existsInFilter($p2));
122 $this->assertTrue($map->existsInFilter($p3));
123 $this->assertTrue($map->existsInFilter($p4));
124
125 $map->filter(static function (): bool {
126 return false;
127 });
128 $this->assertFalse($map->existsInFilter($p1));
129 $this->assertFalse($map->existsInFilter($p2));
130 $this->assertFalse($map->existsInFilter($p3));
131 $this->assertFalse($map->existsInFilter($p4));
132 }
133
134 public function testSortingTopItems(): void
135 {
136 $map = $this->getMap();
137
138 for ($x = 1; $x <= 10; $x++) {
139 $map->add($this->factory->topParentItem($this->getId((string) $x))->withPosition(11 - $x));
140 }
141
142 $x = 10;
143 foreach ($map->getAllFromFilter() as $i) {
144 $this->assertSame($i->getPosition(), $x);
145 $x--;
146 }
147
148 $map->sort();
149
150 $generator = $map->getAllFromFilter();
151
152 $one = static function () use ($generator): isItem {
153 $i = $generator->current();
154 $generator->next();
155 return $i;
156 };
157
158 $i = $one();
159 $this->assertSame('10', $i->getProviderIdentification()->getInternalIdentifier());
160 $this->assertSame(1, $i->getPosition());
161
162 $i = $one();
163 $this->assertSame('9', $i->getProviderIdentification()->getInternalIdentifier());
164 $this->assertSame(2, $i->getPosition());
165
166 $i = $one();
167 $this->assertSame('8', $i->getProviderIdentification()->getInternalIdentifier());
168 $this->assertSame(3, $i->getPosition());
169
170 $i = $one();
171 $this->assertSame('7', $i->getProviderIdentification()->getInternalIdentifier());
172 $this->assertSame(4, $i->getPosition());
173
174 $i = $one();
175 $this->assertSame('6', $i->getProviderIdentification()->getInternalIdentifier());
176 $this->assertSame(5, $i->getPosition());
177
178 $i = $one();
179 $this->assertSame('5', $i->getProviderIdentification()->getInternalIdentifier());
180 $this->assertSame(6, $i->getPosition());
181
182 $i = $one();
183 $this->assertSame('4', $i->getProviderIdentification()->getInternalIdentifier());
184 $this->assertSame(7, $i->getPosition());
185
186 $i = $one();
187 $this->assertSame('3', $i->getProviderIdentification()->getInternalIdentifier());
188 $this->assertSame(8, $i->getPosition());
189
190 $i = $one();
191 $this->assertSame('2', $i->getProviderIdentification()->getInternalIdentifier());
192 $this->assertSame(9, $i->getPosition());
193
194 $i = $one();
195 $this->assertSame('1', $i->getProviderIdentification()->getInternalIdentifier());
196 $this->assertSame(10, $i->getPosition());
197 }
198
199 public function testSortingNestedItems(): void
200 {
201 $map = $this->getMap();
203 $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
204 ->withPosition(1);
205 $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
206 ->withParent($tp_1->getProviderIdentification())
207 ->withPosition(1);
208 $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
209 ->withParent($tp_1->getProviderIdentification())
210 ->withPosition(2);
211 $tp_1 = $tp_1->withChildren([
212 $tp_1_2,
213 $tp_1_1,
214 ]);
215 $map->add($tp_1);
216 $map->add($tp_1_2);
217 $map->add($tp_1_1);
218
219 $tp_2 = $this->factory->topParentItem($this->getId('tp_2'))
220 ->withPosition(2);
221 $tp_2_1 = $this->factory->link($this->getId('tp_2_1'))
222 ->withParent($tp_2->getProviderIdentification())
223 ->withPosition(1);
224 $tp_2_2 = $this->factory->link($this->getId('tp_2_2'))
225 ->withParent($tp_2->getProviderIdentification())
226 ->withPosition(2);
227 $tp_2 = $tp_2->withChildren([
228 $tp_2_2,
229 $tp_2_1,
230 ]);
231 $map->add($tp_2);
232 $map->add($tp_2_1);
233 $map->add($tp_2_2);
234
235 $map->sort();
236
237 $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_1'))->getPosition());
238 $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_1_1'))->getPosition());
239 $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_1_2'))->getPosition());
240 $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_2'))->getPosition());
241 $this->assertEquals(1, $map->getSingleItemFromRaw($this->getId('tp_2_1'))->getPosition());
242 $this->assertEquals(2, $map->getSingleItemFromRaw($this->getId('tp_2_2'))->getPosition());
243
244 // check position in parent
245 $get_tp_1 = $map->getSingleItemFromFilter($this->getId('tp_1'));
246 $children_of_tp_1 = $get_tp_1->getChildren();
247 $first = reset($children_of_tp_1);
248 $this->assertEquals($tp_1_1, $first);
249 $this->assertEquals(1, $first->getPosition());
250
251 $get_tp_2 = $map->getSingleItemFromFilter($this->getId('tp_2'));
252 $children_of_tp_2 = $get_tp_2->getChildren();
253 $first = reset($children_of_tp_2);
254 $this->assertEquals($tp_2_1, $first);
255 $this->assertEquals(1, $first->getPosition());
256 }
257
258 public function testSamePositionResolution(): void
259 {
260 $map = $this->getMap();
262 $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
263 ->withPosition(1);
264 $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
265 ->withParent($tp_1->getProviderIdentification())
266 ->withPosition(1);
267 $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
268 ->withParent($tp_1->getProviderIdentification())
269 ->withPosition(1);
270 $tp_1 = $tp_1->withChildren([
271 $tp_1_2,
272 $tp_1_1,
273 ]);
274 $map->add($tp_1);
275 $map->add($tp_1_2);
276 $map->add($tp_1_1);
277 $map->sort();
278 $item = $map->getSingleItemFromFilter($this->getId('tp_1'));
279 $this->assertCount(2, $item->getChildren());
280 }
281
283 {
284 return new class () implements StaticMainMenuProvider {
285 public function getAllIdentifications(): array
286 {
287 return [];
288 }
289
290 public function getFullyQualifiedClassName(): string
291 {
292 return 'Provider';
293 }
294
295 public function getProviderNameForPresentation(): string
296 {
297 return 'Provider';
298 }
299
300 public function getStaticTopItems(): array
301 {
302 return [];
303 }
304
305 public function getStaticSubItems(): array
306 {
307 return [];
308 }
309
310 public function provideTypeInformation(): TypeInformationCollection
311 {
312 return new TypeInformationCollection();
313 }
314 };
315 }
316}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Class IdentificationFactory All elements in the GlobalScreen service must be identifiable for the sup...
Class FactoryImplTest.
Definition: MapTest.php:40
StaticMainMenuProvider $provider
Definition: MapTest.php:43
IdentificationFactory $identification
Definition: MapTest.php:41
MainMenuItemFactory $factory
Definition: MapTest.php:42
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
$i
Definition: metadata.php:41
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...