ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ElementTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Elements;
22 
34 
35 class ElementTest extends TestCase
36 {
37  protected function getDefinition(string $name): DefinitionInterface
38  {
39  return new class ($name) extends NullDefinition {
40  public function __construct(protected string $name)
41  {
42  }
43 
44  public function name(): string
45  {
46  return $this->name;
47  }
48  };
49  }
50 
51  protected function getElement(
52  int|NoID $id,
53  Element ...$elements
54  ): Element {
55  return new Element(
56  $id,
57  $this->getDefinition('name'),
58  new NullData(),
59  ...$elements
60  );
61  }
62 
63  protected function getElementWithName(
64  int|NoID $id,
65  string $name,
66  Element ...$elements
67  ): Element {
68  return new Element(
69  $id,
70  $this->getDefinition($name),
71  new NullData(),
72  ...$elements
73  );
74  }
75 
76  public function testSubAndSuperElements(): void
77  {
78  $el11 = $this->getElement(11);
79  $el1 = $this->getElement(1, $el11);
80  $el2 = $this->getElement(2);
81  $root = $this->getElement(NoID::ROOT, $el1, $el2);
82 
83  $subs = $root->getSubElements();
84  $this->assertSame($el1, $subs->current());
85  $subs->next();
86  $this->assertSame($el2, $subs->current());
87  $subs->next();
88  $this->assertNull($subs->current());
89 
90  $this->assertSame($root, $el1->getSuperElement());
91  $this->assertSame($el11, $el1->getSubElements()->current());
92  }
93 
94  public function testGetData(): void
95  {
96  $data = new NullData();
97  $el = new Element(
98  7,
99  $this->getDefinition('name'),
100  $data
101  );
102 
103  $this->assertSame($data, $el->getData());
104  }
105 
106  public function testIsScaffold(): void
107  {
108  $scaffold = $this->getElement(NoID::SCAFFOLD);
109  $not_scaffold = $this->getElement(5);
110 
111  $this->assertTrue($scaffold->isScaffold());
112  $this->assertFalse($not_scaffold->isScaffold());
113  }
114 
115  public function testGetMarkerAndIsMarked(): void
116  {
117  $mark_me = $this->getElement(13);
118  $stay_away = $this->getElement(7);
119  $mark_me->mark(new MockMarkerFactory(), Action::NEUTRAL);
120 
121  $this->assertTrue($mark_me->isMarked());
122  $this->assertInstanceOf(MarkerInterface::class, $mark_me->getMarker());
123  $this->assertFalse($stay_away->isMarked());
124  $this->assertNull($stay_away->getMarker());
125  }
126 
127  public function testMarkerTrail(): void
128  {
129  $el111 = $this->getElement(111);
130  $el11 = $this->getElement(11, $el111);
131  $el12 = $this->getElement(12);
132  $el1 = $this->getElement(1, $el11, $el12);
133  $el2 = $this->getElement(2);
134  $root = $this->getElement(NoID::ROOT, $el1, $el2);
135 
136  $el11->mark(new MockMarkerFactory(), Action::CREATE_OR_UPDATE);
137 
138  $this->assertTrue($el11->isMarked());
139  $this->assertSame(Action::CREATE_OR_UPDATE, $el11->getMarker()->action());
140  $this->assertTrue($el1->isMarked());
141  $this->assertSame(Action::NEUTRAL, $el1->getMarker()->action());
142  $this->assertTrue($root->isMarked());
143  $this->assertSame(Action::NEUTRAL, $root->getMarker()->action());
144 
145  $this->assertFalse($el111->isMarked());
146  $this->assertFalse($el12->isMarked());
147  $this->assertFalse($el2->isMarked());
148  }
149 
150  public function testMarkTwice(): void
151  {
152  $marker_factory = new MockMarkerFactory();
153  $sub = $this->getElement(11);
154  $el = $this->getElement(1, $sub);
155 
156  $el->mark($marker_factory, Action::CREATE_OR_UPDATE);
157  $this->assertSame(Action::CREATE_OR_UPDATE, $el->getMarker()->action());
158 
159  $sub->mark($marker_factory, Action::DELETE);
160  $this->assertSame(Action::DELETE, $sub->getMarker()->action());
161  $this->assertSame(Action::CREATE_OR_UPDATE, $el->getMarker()->action());
162 
163  $el->mark($marker_factory, Action::DELETE);
164  $this->assertSame(Action::DELETE, $el->getMarker()->action());
165  }
166 
167  public function testMarkWithScaffolds(): void
168  {
169  $marker_factory = new MockMarkerFactory();
170  $sub = $this->getElement(NoID::SCAFFOLD);
171  $el = $this->getElement(NoID::SCAFFOLD, $sub);
172 
173  $sub->mark($marker_factory, Action::CREATE_OR_UPDATE);
174  $this->assertSame(Action::CREATE_OR_UPDATE, $sub->getMarker()->action());
175  $this->assertSame(Action::CREATE_OR_UPDATE, $el->getMarker()->action());
176 
177  $sub = $this->getElement(NoID::SCAFFOLD);
178  $el = $this->getElement(NoID::SCAFFOLD, $sub);
179 
180  $sub->mark($marker_factory, Action::DELETE);
181  $this->assertSame(Action::DELETE, $sub->getMarker()->action());
182  $this->assertSame(Action::NEUTRAL, $el->getMarker()->action());
183  }
184 
185  public function testAddScaffolds(): void
186  {
187  $second = $this->getElementWithName(6, 'second');
188  $el = $this->getElement(13, $second);
189 
190  $el->addScaffoldsToSubElements(new MockScaffoldProvider());
191 
192  $subs = $el->getSubElements();
193  $this->assertTrue($subs->current()->isScaffold());
194  $this->assertSame('first', $subs->current()->getDefinition()->name());
195  $subs->next();
196  $this->assertSame($second, $subs->current());
197  $subs->next();
198  $this->assertTrue($subs->current()->isScaffold());
199  $this->assertSame('second', $subs->current()->getDefinition()->name());
200  $subs->next();
201  $this->assertTrue($subs->current()->isScaffold());
202  $this->assertSame('third', $subs->current()->getDefinition()->name());
203  $subs->next();
204  $this->assertNull($subs->current());
205  }
206 
207  public function testAddScaffoldByName(): void
208  {
209  $second = $this->getElementWithName(6, 'second');
210  $third = $this->getElementWithName(17, 'third');
211  $el = $this->getElement(13, $second, $third);
212 
213  $el->addScaffoldToSubElements(new MockScaffoldProvider(), 'second');
214 
215  $subs = $el->getSubElements();
216  $this->assertSame($second, $subs->current());
217  $subs->next();
218  $this->assertTrue($subs->current()->isScaffold());
219  $this->assertSame('second', $subs->current()->getDefinition()->name());
220  $subs->next();
221  $this->assertSame($third, $subs->current());
222  $subs->next();
223  $this->assertNull($subs->current());
224  }
225 
227  {
228  $el = $this->getElement(37);
229 
230  $this->expectException(\ilMDElementsException::class);
231  $el->addScaffoldsToSubElements(new MockBrokenScaffoldProvider());
232  }
233 
235  {
236  $el = $this->getElement(37);
237 
238  $this->expectException(\ilMDElementsException::class);
239  $el->addScaffoldToSubElements(new MockBrokenScaffoldProvider(), 'with sub');
240  }
241 }
242 
244 {
245  public function marker(Action $action, string $data_value = ''): MarkerInterface
246  {
247  return new MockMarker($action);
248  }
249 }
250 
251 class MockMarker implements MarkerInterface
252 {
253  protected Action $action;
254 
255  public function __construct(Action $action)
256  {
257  $this->action = $action;
258  }
259 
260  public function action(): Action
261  {
262  return $this->action;
263  }
264 
265  public function dataValue(): string
266  {
267  return '';
268  }
269 }
270 
272 {
273  protected function getScaffold(string $name, ElementInterface ...$elements): ElementInterface
274  {
275  $definition = new class ($name) implements DefinitionInterface {
276  protected string $name;
277 
278  public function __construct(string $name)
279  {
280  $this->name = $name;
281  }
282 
283  public function name(): string
284  {
285  return $this->name;
286  }
287 
288  public function unique(): bool
289  {
290  return false;
291  }
292 
293  public function dataType(): Type
294  {
295  return Type::NULL;
296  }
297  };
298 
299  $data = new class () implements DataInterface {
300  public function type(): Type
301  {
302  return Type::STRING;
303  }
304 
305  public function value(): string
306  {
307  return 'value';
308  }
309  };
310 
311  return new Element(
312  NoID::SCAFFOLD,
313  $definition,
314  $data,
315  ...$elements
316  );
317  }
318 
320  {
321  $first = $this->getScaffold('first');
322  $second = $this->getScaffold('second');
323  $third = $this->getScaffold('third');
324 
325  yield 'second' => $first;
326  yield 'third' => $second;
327  yield '' => $third;
328  }
329 }
330 
332 {
334  {
335  $sub = $this->getScaffold('name');
336  $with_sub = $this->getScaffold('with sub', $sub);
337 
338  yield '' => $with_sub;
339  }
340 }
marker(Action $action, string $data_value='')
getScaffoldsForElement(ElementInterface $element)
Returns all elements that could be added as sub-elements to the given element as scaffolds.
getElementWithName(int|NoID $id, string $name, Element ... $elements)
Definition: ElementTest.php:63
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:62
getElement(int|NoID $id, Element ... $elements)
Definition: ElementTest.php:51
getScaffoldsForElement(ElementInterface $element)
Returns all elements that could be added as sub-elements to the given element as scaffolds.
getScaffold(string $name, ElementInterface ... $elements)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23