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