ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ElementTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
37
38class ElementTest extends TestCase
39{
40 protected function getDefinition(string $name): DefinitionInterface
41 {
42 return new class ($name) extends NullDefinition {
43 public function __construct(protected string $name)
44 {
45 }
46
47 public function name(): string
48 {
49 return $this->name;
50 }
51 };
52 }
53
55 {
56 return new class () extends NullMarkerFactory {
57 public function marker(Action $action, string $data_value = ''): MarkerInterface
58 {
59 return new class ($action) extends NullMarker {
60 protected Action $action;
61
62 public function __construct(Action $action)
63 {
64 $this->action = $action;
65 }
66
67 public function action(): Action
68 {
69 return $this->action;
70 }
71
72 public function dataValue(): string
73 {
74 return '';
75 }
76 };
77 }
78 };
79 }
80
81 protected function getScaffoldProvider(bool $broken = false): ScaffoldProviderInterface
82 {
83 return new class ($broken) extends NullScaffoldProvider {
84 public function __construct(protected bool $broken)
85 {
86 }
87
88 protected function getScaffold(string $name, ElementInterface ...$elements): ElementInterface
89 {
90 $definition = new class ($name) implements DefinitionInterface {
91 protected string $name;
92
93 public function __construct(string $name)
94 {
95 $this->name = $name;
96 }
97
98 public function name(): string
99 {
100 return $this->name;
101 }
102
103 public function unique(): bool
104 {
105 return false;
106 }
107
108 public function dataType(): Type
109 {
110 return Type::NULL;
111 }
112 };
113
114 $data = new class () extends NullData {
115 public function type(): Type
116 {
117 return Type::STRING;
118 }
119
120 public function value(): string
121 {
122 return 'value';
123 }
124 };
125
126 return new Element(
127 NoID::SCAFFOLD,
128 $definition,
129 $data,
130 ...$elements
131 );
132 }
133
134 public function getScaffoldsForElement(ElementInterface $element): \Generator
135 {
136 if ($this->broken) {
137 $sub = $this->getScaffold('name');
138 $with_sub = $this->getScaffold('with sub', $sub);
139
140 yield '' => $with_sub;
141 return;
142 }
143
144 $first = $this->getScaffold('first');
145 $second = $this->getScaffold('second');
146 $third = $this->getScaffold('third');
147 $fourth = $this->getScaffold('fourth');
148
149 yield $first;
150 yield $second;
151 yield $third;
152 yield $fourth;
153 }
154
155 public function getPossibleSubElementNamesForElementInOrder(ElementInterface $element): \Generator
156 {
157 yield 'first';
158 yield 'second';
159 yield 'third';
160 yield 'fourth';
161 }
162 };
163 }
164
165 protected function getElement(
166 int|NoID $id,
167 Element ...$elements
168 ): Element {
169 return new Element(
170 $id,
171 $this->getDefinition('name'),
172 new NullData(),
173 ...$elements
174 );
175 }
176
177 protected function getElementWithName(
178 int|NoID $id,
179 string $name,
180 Element ...$elements
181 ): Element {
182 return new Element(
183 $id,
184 $this->getDefinition($name),
185 new NullData(),
186 ...$elements
187 );
188 }
189
190 public function testSubAndSuperElements(): void
191 {
192 $el11 = $this->getElement(11);
193 $el1 = $this->getElement(1, $el11);
194 $el2 = $this->getElement(2);
195 $root = $this->getElement(NoID::ROOT, $el1, $el2);
196
197 $subs = $root->getSubElements();
198 $this->assertSame($el1, $subs->current());
199 $subs->next();
200 $this->assertSame($el2, $subs->current());
201 $subs->next();
202 $this->assertNull($subs->current());
203
204 $this->assertSame($root, $el1->getSuperElement());
205 $this->assertSame($el11, $el1->getSubElements()->current());
206 }
207
208 public function testGetData(): void
209 {
210 $data = new NullData();
211 $el = new Element(
212 7,
213 $this->getDefinition('name'),
214 $data
215 );
216
217 $this->assertSame($data, $el->getData());
218 }
219
220 public function testIsScaffold(): void
221 {
222 $scaffold = $this->getElement(NoID::SCAFFOLD);
223 $not_scaffold = $this->getElement(5);
224
225 $this->assertTrue($scaffold->isScaffold());
226 $this->assertFalse($not_scaffold->isScaffold());
227 }
228
229 public function testGetMarkerAndIsMarked(): void
230 {
231 $mark_me = $this->getElement(13);
232 $stay_away = $this->getElement(7);
233 $mark_me->mark($this->getMarkerFactory(), Action::NEUTRAL);
234
235 $this->assertTrue($mark_me->isMarked());
236 $this->assertInstanceOf(MarkerInterface::class, $mark_me->getMarker());
237 $this->assertFalse($stay_away->isMarked());
238 $this->assertNull($stay_away->getMarker());
239 }
240
241 public function testMarkerTrail(): void
242 {
243 $el111 = $this->getElement(111);
244 $el11 = $this->getElement(11, $el111);
245 $el12 = $this->getElement(12);
246 $el1 = $this->getElement(1, $el11, $el12);
247 $el2 = $this->getElement(2);
248 $root = $this->getElement(NoID::ROOT, $el1, $el2);
249
250 $el11->mark($this->getMarkerFactory(), Action::CREATE_OR_UPDATE);
251
252 $this->assertTrue($el11->isMarked());
253 $this->assertSame(Action::CREATE_OR_UPDATE, $el11->getMarker()->action());
254 $this->assertTrue($el1->isMarked());
255 $this->assertSame(Action::NEUTRAL, $el1->getMarker()->action());
256 $this->assertTrue($root->isMarked());
257 $this->assertSame(Action::NEUTRAL, $root->getMarker()->action());
258
259 $this->assertFalse($el111->isMarked());
260 $this->assertFalse($el12->isMarked());
261 $this->assertFalse($el2->isMarked());
262 }
263
264 public function testMarkTwice(): void
265 {
266 $marker_factory = $this->getMarkerFactory();
267 $sub = $this->getElement(11);
268 $el = $this->getElement(1, $sub);
269
270 $el->mark($marker_factory, Action::CREATE_OR_UPDATE);
271 $this->assertSame(Action::CREATE_OR_UPDATE, $el->getMarker()->action());
272
273 $sub->mark($marker_factory, Action::DELETE);
274 $this->assertSame(Action::DELETE, $sub->getMarker()->action());
275 $this->assertSame(Action::CREATE_OR_UPDATE, $el->getMarker()->action());
276
277 $el->mark($marker_factory, Action::DELETE);
278 $this->assertSame(Action::DELETE, $el->getMarker()->action());
279 }
280
281 public function testMarkWithScaffolds(): void
282 {
283 $marker_factory = $this->getMarkerFactory();
284 $sub = $this->getElement(NoID::SCAFFOLD);
285 $el = $this->getElement(NoID::SCAFFOLD, $sub);
286
287 $sub->mark($marker_factory, Action::CREATE_OR_UPDATE);
288 $this->assertSame(Action::CREATE_OR_UPDATE, $sub->getMarker()->action());
289 $this->assertSame(Action::CREATE_OR_UPDATE, $el->getMarker()->action());
290
291 $sub = $this->getElement(NoID::SCAFFOLD);
292 $el = $this->getElement(NoID::SCAFFOLD, $sub);
293
294 $sub->mark($marker_factory, Action::DELETE);
295 $this->assertSame(Action::DELETE, $sub->getMarker()->action());
296 $this->assertSame(Action::NEUTRAL, $el->getMarker()->action());
297 }
298
299 public function testUnmark(): void
300 {
301 $el111 = $this->getElement(111);
302 $el11 = $this->getElement(11, $el111);
303 $el1 = $this->getElement(1, $el11);
304 $root = $this->getElement(NoID::ROOT, $el1);
305
306 $el111->mark($this->getMarkerFactory(), Action::CREATE_OR_UPDATE);
307 $el11->unmark();
308
309 $this->assertTrue($root->isMarked());
310 $this->assertTrue($el1->isMarked());
311 $this->assertFalse($el11->isMarked());
312 $this->assertFalse($el111->isMarked());
313 }
314
315 public function testAddScaffolds(): void
316 {
317 $second = $this->getElementWithName(6, 'second');
318 $fourth = $this->getElementWithName(6, 'fourth');
319 $el = $this->getElement(13, $second, $fourth);
320
321 $el->addScaffoldsToSubElements($this->getScaffoldProvider());
322
323 $subs = $el->getSubElements();
324 $this->assertTrue($subs->current()->isScaffold());
325 $this->assertSame('first', $subs->current()->getDefinition()->name());
326 $subs->next();
327 $this->assertSame($second, $subs->current());
328 $subs->next();
329 $this->assertTrue($subs->current()->isScaffold());
330 $this->assertSame('second', $subs->current()->getDefinition()->name());
331 $subs->next();
332 $this->assertTrue($subs->current()->isScaffold());
333 $this->assertSame('third', $subs->current()->getDefinition()->name());
334 $subs->next();
335 $this->assertSame($fourth, $subs->current());
336 $subs->next();
337 $this->assertTrue($subs->current()->isScaffold());
338 $this->assertSame('fourth', $subs->current()->getDefinition()->name());
339 $subs->next();
340 $this->assertNull($subs->current());
341 }
342
343 public function testAddScaffoldByName(): void
344 {
345 $second = $this->getElementWithName(6, 'second');
346 $third = $this->getElementWithName(17, 'third');
347 $el = $this->getElement(13, $second, $third);
348
349 $el->addScaffoldToSubElements($this->getScaffoldProvider(), 'second');
350
351 $subs = $el->getSubElements();
352 $this->assertSame($second, $subs->current());
353 $subs->next();
354 $this->assertTrue($subs->current()->isScaffold());
355 $this->assertSame('second', $subs->current()->getDefinition()->name());
356 $subs->next();
357 $this->assertSame($third, $subs->current());
358 $subs->next();
359 $this->assertNull($subs->current());
360 }
361
362
363
364 public function testAddScaffoldByNameWithGap(): void
365 {
366 $second = $this->getElementWithName(6, 'second');
367 $fourth = $this->getElementWithName(17, 'fourth');
368 $el = $this->getElement(13, $second, $fourth);
369
370 $el->addScaffoldToSubElements($this->getScaffoldProvider(), 'second');
371
372 $subs = $el->getSubElements();
373 $this->assertSame($second, $subs->current());
374 $subs->next();
375 $this->assertTrue($subs->current()->isScaffold());
376 $this->assertSame('second', $subs->current()->getDefinition()->name());
377 $subs->next();
378 $this->assertSame($fourth, $subs->current());
379 $subs->next();
380 $this->assertNull($subs->current());
381 }
382
384 {
385 $el = $this->getElement(37);
386
387 $this->expectException(\ilMDElementsException::class);
388 $el->addScaffoldsToSubElements($this->getScaffoldProvider(true));
389 }
390
392 {
393 $el = $this->getElement(37);
394
395 $this->expectException(\ilMDElementsException::class);
396 $el->addScaffoldToSubElements($this->getScaffoldProvider(true), 'with sub');
397 }
398}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getElementWithName(int|NoID $id, string $name, Element ... $elements)
getElement(int|NoID $id, Element ... $elements)
getScaffoldProvider(bool $broken=false)
Definition: ElementTest.php:81
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...