ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
MapTest.php
Go to the documentation of this file.
1<?php
2
20
29use PHPUnit\Framework\TestCase;
31
32// require_once('./vendor/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->assertCount(4, iterator_to_array($map->getAllFromFilter()));
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->assertCount(4, iterator_to_array($map->getAllFromFilter()));
108
109 $map->filter(static fn(): bool => true);
110
111 $this->assertCount(4, iterator_to_array($map->getAllFromFilter()));
112
113 $map->filter(static fn(isItem $i): bool => $i->getProviderIdentification()->getInternalIdentifier() !== 'parent_1');
114
115 $this->assertCount(3, iterator_to_array($map->getAllFromFilter()));
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 $this->assertInstanceOf(\ILIAS\GlobalScreen\isGlobalScreenItem::class, $get_tp_1);
241 $children_of_tp_1 = $get_tp_1->getChildren();
242 $first = reset($children_of_tp_1);
243 $this->assertEquals($tp_1_1, $first);
244 $this->assertEquals(1, $first->getPosition());
245
246 $get_tp_2 = $map->getSingleItemFromFilter($this->getId('tp_2'));
247 $this->assertInstanceOf(\ILIAS\GlobalScreen\isGlobalScreenItem::class, $get_tp_2);
248 $children_of_tp_2 = $get_tp_2->getChildren();
249 $first = reset($children_of_tp_2);
250 $this->assertEquals($tp_2_1, $first);
251 $this->assertEquals(1, $first->getPosition());
252 }
253
254 public function testSamePositionResolution(): void
255 {
256 $map = $this->getMap();
258 $tp_1 = $this->factory->topParentItem($this->getId('tp_1'))
259 ->withPosition(1);
260 $tp_1_1 = $this->factory->link($this->getId('tp_1_1'))
261 ->withParent($tp_1->getProviderIdentification())
262 ->withPosition(1);
263 $tp_1_2 = $this->factory->link($this->getId('tp_1_2'))
264 ->withParent($tp_1->getProviderIdentification())
265 ->withPosition(1);
266 $tp_1 = $tp_1->withChildren([
267 $tp_1_2,
268 $tp_1_1,
269 ]);
270 $map->add($tp_1);
271 $map->add($tp_1_2);
272 $map->add($tp_1_1);
273 $map->sort();
274 $item = $map->getSingleItemFromFilter($this->getId('tp_1'));
275 $this->assertInstanceOf(\ILIAS\GlobalScreen\isGlobalScreenItem::class, $item);
276 $this->assertCount(2, $item->getChildren());
277 }
278
280 {
281 return new class () implements StaticMainMenuProvider {
282 public function getAllIdentifications(): array
283 {
284 return [];
285 }
286
287 public function getFullyQualifiedClassName(): string
288 {
289 return 'Provider';
290 }
291
292 public function getProviderNameForPresentation(): string
293 {
294 return 'Provider';
295 }
296
297 public function getStaticTopItems(): array
298 {
299 return [];
300 }
301
302 public function getStaticSubItems(): array
303 {
304 return [];
305 }
306
307 public function provideTypeInformation(): TypeInformationCollection
308 {
309 return new TypeInformationCollection();
310 }
311 };
312 }
313}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
factory()
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.