ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
AdapterTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
33
34class AdapterTest extends TestCase
35{
36 protected static function getMockVocabulary(
37 bool $allows_custom_inputs = false,
38 string $source = '',
39 string ...$values
41 return new class ($allows_custom_inputs, $source, $values) extends NullVocabulary {
42 public function __construct(
43 protected bool $allows_custom_inputs,
44 protected string $source,
45 protected array $values
46 ) {
47 }
48
49 public function allowsCustomInputs(): bool
50 {
51 return $this->allows_custom_inputs;
52 }
53
54 public function source(): string
55 {
56 return $this->source;
57 }
58
59 public function values(): \Generator
60 {
61 yield from $this->values;
62 }
63 };
64 }
65
66 protected function getMockReader(
67 Identifier $expected_slot = Identifier::NULL,
68 VocabularyInterface ...$returned_vocabularies
70 return new class ($expected_slot, $returned_vocabularies) extends NullReader {
71 public function __construct(
72 protected Identifier $expected_slot,
73 protected array $returned_vocabularies
74 ) {
75 }
76
77 public function activeVocabulariesForSlots(Identifier ...$slots): \Generator
78 {
79 if (count($slots) !== 1 && $slots[0] !== $this->expected_slot) {
80 throw new \Exception('invalid slot!');
81 }
82 yield from $this->returned_vocabularies;
83 }
84 };
85 }
86
88 {
89 return new class () extends NullElementHelper {
90 public array $find_element_of_condition_args = [];
91 public array $slot_for_element_args = [];
92 public array $slots_for_element_without_condition_args = [];
93 public array $potential_slot_for_element_by_condition_args = [];
94
95 public function findElementOfCondition(
96 Identifier $slot,
97 ElementInterface $element,
98 ElementInterface ...$all_elements
100 $this->find_element_of_condition_args[] = [
101 'slot' => $slot,
102 'element' => $element,
103 'all_elements' => $all_elements
104 ];
105 return null;
106 }
107
108 public function slotForElement(ElementInterface $element): Identifier
109 {
110 $this->slot_for_element_args[] = [
111 'element' => $element
112 ];
113 return Identifier::NULL;
114 }
115
116 public function slotsForElementWithoutCondition(ElementInterface $element): \Generator
117 {
118 $this->slots_for_element_without_condition_args[] = [
119 'element' => $element
120 ];
121 yield from [];
122 }
123
124 public function potentialSlotForElementByCondition(
125 ElementInterface $element,
126 ElementInterface $element_in_condition,
127 string $value
128 ): Identifier {
129 $this->potential_slot_for_element_by_condition_args[] = [
130 'element' => $element,
131 'element_in_condition' => $element_in_condition,
132 'value' => $value
133 ];
134 return Identifier::NULL;
135 }
136 };
137 }
138
139 public function testFindElementOfCondition(): void
140 {
141 $adapter = new Adapter(
142 $this->getMockReader(),
143 $helper = $this->getMockElementHelper()
144 );
145 $element = new NullElement();
146 $all_elements = [new NullElement(), new NullElement(), new NullElement()];
147
148 $adapter->findElementOfCondition(
149 Identifier::EDCUCATIONAL_INTENDED_END_USER_ROLE,
150 $element,
151 ...$all_elements
152 );
153
154 $this->assertSame(
155 [[
156 'slot' => Identifier::EDCUCATIONAL_INTENDED_END_USER_ROLE,
157 'element' => $element,
158 'all_elements' => $all_elements
159 ]],
160 $helper->find_element_of_condition_args
161 );
162 }
163
164 public function testSlotForElement(): void
165 {
166 $adapter = new Adapter(
167 $this->getMockReader(),
168 $helper = $this->getMockElementHelper()
169 );
170 $element = new NullElement();
171
172 $adapter->slotForElement($element);
173
174 $this->assertSame(
175 [['element' => $element]],
176 $helper->slot_for_element_args
177 );
178 }
179
181 {
182 $adapter = new Adapter(
183 $this->getMockReader(),
184 $helper = $this->getMockElementHelper()
185 );
186 $element = new NullElement();
187
188 iterator_to_array($adapter->slotsForElementWithoutCondition($element));
189
190 $this->assertSame(
191 [['element' => $element]],
192 $helper->slots_for_element_without_condition_args
193 );
194 }
195
197 {
198 $adapter = new Adapter(
199 $this->getMockReader(),
200 $helper = $this->getMockElementHelper()
201 );
202 $element = new NullElement();
203 $element_in_condition = new NullElement();
204
205 $adapter->potentialSlotForElementByCondition(
206 $element,
207 $element_in_condition,
208 'some value'
209 );
210
211 $this->assertSame(
212 [[
213 'element' => $element,
214 'element_in_condition' => $element_in_condition,
215 'value' => 'some value'
216 ]],
217 $helper->potential_slot_for_element_by_condition_args
218 );
219 }
220
221 public static function doesSlotHaveVocabulariesProvider(): array
222 {
223 return [
224 [
225 Identifier::EDUCATIONAL_CONTEXT,
226 [],
227 false
228 ],
229 [
230 Identifier::EDUCATIONAL_CONTEXT,
231 [self::getMockVocabulary()],
232 true
233 ],
234 [
235 Identifier::EDUCATIONAL_CONTEXT,
236 [
237 self::getMockVocabulary(),
238 self::getMockVocabulary(),
239 self::getMockVocabulary(),
240 self::getMockVocabulary()
241 ],
242 true
243 ]
244 ];
245 }
246
247 #[\PHPUnit\Framework\Attributes\DataProvider('doesSlotHaveVocabulariesProvider')]
249 Identifier $slot,
250 array $vocabularies,
251 bool $has_vocabs_expected
252 ): void {
253 $adapter = new Adapter(
254 $this->getMockReader($slot, ...$vocabularies),
255 $this->getMockElementHelper()
256 );
257
258 $has_vocabs_actual = $adapter->doesSlotHaveVocabularies($slot);
259
260 $this->assertSame($has_vocabs_expected, $has_vocabs_actual);
261 }
262
263 public static function doesSlotAllowCustomInputProvider(): array
264 {
265 return [
266 [
267 Identifier::EDUCATIONAL_CONTEXT,
268 [],
269 true
270 ],
271 [
272 Identifier::EDUCATIONAL_CONTEXT,
273 [self::getMockVocabulary(true)],
274 true
275 ],
276 [
277 Identifier::EDUCATIONAL_CONTEXT,
278 [self::getMockVocabulary(false)],
279 false
280 ],
281 [
282 Identifier::EDUCATIONAL_CONTEXT,
283 [
284 self::getMockVocabulary(true),
285 self::getMockVocabulary(true),
286 self::getMockVocabulary(false),
287 self::getMockVocabulary(true)
288 ],
289 false
290 ]
291 ];
292 }
293
294 #[\PHPUnit\Framework\Attributes\DataProvider('doesSlotAllowCustomInputProvider')]
296 Identifier $slot,
297 array $vocabularies,
298 bool $allows_custom_input_expected
299 ): void {
300 $adapter = new Adapter(
301 $this->getMockReader($slot, ...$vocabularies),
302 $this->getMockElementHelper()
303 );
304
305 $allows_custom_input_actual = $adapter->doesSlotAllowCustomInput($slot);
306
307 $this->assertSame($allows_custom_input_expected, $allows_custom_input_actual);
308 }
309
310 public static function isValueInVocabulariesForSlotProvider(): array
311 {
312 return [
313 [
314 Identifier::EDUCATIONAL_CONTEXT,
315 [],
316 'some value',
317 false
318 ],
319 [
320 Identifier::EDUCATIONAL_CONTEXT,
321 [self::getMockVocabulary(false, '')],
322 'some value',
323 false
324 ],
325 [
326 Identifier::EDUCATIONAL_CONTEXT,
327 [self::getMockVocabulary(false, '', 'val1', 'val2')],
328 'some value',
329 false
330 ],
331 [
332 Identifier::EDUCATIONAL_CONTEXT,
333 [self::getMockVocabulary(false, '', 'val1', 'some value', 'val2')],
334 'some value',
335 true
336 ],
337 [
338 Identifier::EDUCATIONAL_CONTEXT,
339 [
340 self::getMockVocabulary(false, '', 'val1', 'val2'),
341 self::getMockVocabulary(false, '', 'val3', 'val4')
342 ],
343 'some value',
344 false
345 ],
346 [
347 Identifier::EDUCATIONAL_CONTEXT,
348 [
349 self::getMockVocabulary(false, '', 'val1', 'val2'),
350 self::getMockVocabulary(false, '', 'val3', 'some value', 'val4')
351 ],
352 'some value',
353 true
354 ],
355 [
356 Identifier::EDUCATIONAL_CONTEXT,
357 [
358 self::getMockVocabulary(false, '', 'some value', 'some value'),
359 self::getMockVocabulary(false, '', 'some value', 'some value', 'some value')
360 ],
361 'some value',
362 true
363 ]
364 ];
365 }
366
367 #[\PHPUnit\Framework\Attributes\DataProvider('isValueInVocabulariesForSlotProvider')]
369 Identifier $slot,
370 array $vocabularies,
371 string $value,
372 bool $is_in_vocabs_expected
373 ): void {
374 $adapter = new Adapter(
375 $this->getMockReader($slot, ...$vocabularies),
376 $this->getMockElementHelper()
377 );
378
379 $is_in_vocabs_actual = $adapter->isValueInVocabulariesForSlot($slot, $value);
380
381 $this->assertSame($is_in_vocabs_expected, $is_in_vocabs_actual);
382 }
383
384 public static function valuesInVocabulariesForSlotProvider(): array
385 {
386 return [
387 [
388 Identifier::EDUCATIONAL_CONTEXT,
389 null,
390 [],
391 []
392 ],
393 [
394 Identifier::EDUCATIONAL_CONTEXT,
395 'additional value',
396 [],
397 ['additional value']
398 ],
399 [
400 Identifier::EDUCATIONAL_CONTEXT,
401 null,
402 [self::getMockVocabulary(false, '')],
403 []
404 ],
405 [
406 Identifier::EDUCATIONAL_CONTEXT,
407 null,
408 [self::getMockVocabulary(false, '', 'val1')],
409 ['val1']
410 ],
411 [
412 Identifier::EDUCATIONAL_CONTEXT,
413 'val1',
414 [self::getMockVocabulary(false, '', 'val1')],
415 ['val1']
416 ],
417 [
418 Identifier::EDUCATIONAL_CONTEXT,
419 'additional value',
420 [self::getMockVocabulary(false, '', 'val1')],
421 ['additional value', 'val1']
422 ],
423 [
424 Identifier::EDUCATIONAL_CONTEXT,
425 null,
426 [self::getMockVocabulary(false, '', 'val1', 'val2')],
427 ['val1', 'val2']
428 ],
429 [
430 Identifier::EDUCATIONAL_CONTEXT,
431 null,
432 [
433 self::getMockVocabulary(false, '', 'val1', 'val2'),
434 self::getMockVocabulary(false, '', 'val3', 'val4')
435 ],
436 ['val1', 'val2', 'val3', 'val4']
437 ],
438 [
439 Identifier::EDUCATIONAL_CONTEXT,
440 'val3',
441 [
442 self::getMockVocabulary(false, '', 'val1', 'val2'),
443 self::getMockVocabulary(false, '', 'val3'),
444 self::getMockVocabulary(false, '', 'val4', 'val5')
445 ],
446 ['val1', 'val2', 'val3', 'val4', 'val5']
447 ],
448 [
449 Identifier::EDUCATIONAL_CONTEXT,
450 'additional value',
451 [
452 self::getMockVocabulary(false, '', 'val1', 'val2'),
453 self::getMockVocabulary(false, '', 'val3'),
454 self::getMockVocabulary(false, '', 'val4', 'val5')
455 ],
456 ['additional value', 'val1', 'val2', 'val3', 'val4', 'val5']
457 ]
458 ];
459 }
460
461 #[\PHPUnit\Framework\Attributes\DataProvider('valuesInVocabulariesForSlotProvider')]
463 Identifier $slot,
464 ?string $add_if_not_included,
465 array $vocabularies,
466 array $expected_values
467 ): void {
468 $adapter = new Adapter(
469 $this->getMockReader($slot, ...$vocabularies),
470 $this->getMockElementHelper()
471 );
472
473 $actual_values = iterator_to_array($adapter->valuesInVocabulariesForSlot($slot, $add_if_not_included));
474
475 $this->assertSame($expected_values, $actual_values);
476 }
477
478 public static function sourceMapForSlotProvider(): array
479 {
480 return [
481 [
482 Identifier::EDUCATIONAL_CONTEXT,
483 [],
484 'some value',
485 null
486 ],
487 [
488 Identifier::EDUCATIONAL_CONTEXT,
489 [self::getMockVocabulary(false, '')],
490 'some value',
491 null
492 ],
493 [
494 Identifier::EDUCATIONAL_CONTEXT,
495 [self::getMockVocabulary(false, 'some source', 'some value')],
496 'some value',
497 'some source'
498 ],
499 [
500 Identifier::EDUCATIONAL_CONTEXT,
501 [self::getMockVocabulary(false, 'some source', 'some value')],
502 'other value',
503 null
504 ],
505 [
506 Identifier::EDUCATIONAL_CONTEXT,
507 [
508 self::getMockVocabulary(false, 'source 1', 'val1.1', 'val1.2'),
509 self::getMockVocabulary(false, 'source 2', 'val2.1', 'val2.2')
510 ],
511 'val1.2',
512 'source 1'
513 ],
514 [
515 Identifier::EDUCATIONAL_CONTEXT,
516 [
517 self::getMockVocabulary(false, 'source 1', 'val1.1', 'val1.2'),
518 self::getMockVocabulary(false, 'source 2', 'val2.1', 'val2.2')
519 ],
520 'val2.1',
521 'source 2'
522 ],
523 [
524 Identifier::EDUCATIONAL_CONTEXT,
525 [
526 self::getMockVocabulary(false, 'source 1', 'val1.1', 'val1.2'),
527 self::getMockVocabulary(false, 'source 2', 'val2.1', 'val2.2')
528 ],
529 'other value',
530 null
531 ]
532 ];
533 }
534
535 #[\PHPUnit\Framework\Attributes\DataProvider('sourceMapForSlotProvider')]
536 public function testSourceMapForSlot(
537 Identifier $slot,
538 array $vocabularies,
539 string $value,
540 ?string $expected_source
541 ): void {
542 $adapter = new Adapter(
543 $this->getMockReader($slot, ...$vocabularies),
544 $this->getMockElementHelper()
545 );
546
547 $closure = $adapter->sourceMapForSlot($slot);
548 $actual_source = $closure($value);
549
550 $this->assertSame($expected_source, $actual_source);
551 }
552}
testIsValueInVocabulariesForSlot(Identifier $slot, array $vocabularies, string $value, bool $is_in_vocabs_expected)
testSourceMapForSlot(Identifier $slot, array $vocabularies, string $value, ?string $expected_source)
testValuesInVocabulariesForSlot(Identifier $slot, ?string $add_if_not_included, array $vocabularies, array $expected_values)
static getMockVocabulary(bool $allows_custom_inputs=false, string $source='', string ... $values)
Definition: AdapterTest.php:36
testDoesSlotAllowCustomInput(Identifier $slot, array $vocabularies, bool $allows_custom_input_expected)
getMockReader(Identifier $expected_slot=Identifier::NULL, VocabularyInterface ... $returned_vocabularies)
Definition: AdapterTest.php:66
testDoesSlotHaveVocabularies(Identifier $slot, array $vocabularies, bool $has_vocabs_expected)
isValueInVocabulariesForSlot(SlotIdentifier $slot, string $value)
Definition: Adapter.php:107
doesSlotHaveVocabularies(SlotIdentifier $slot)
Definition: Adapter.php:92
doesSlotAllowCustomInput(SlotIdentifier $slot)
Definition: Adapter.php:97
sourceMapForSlot(SlotIdentifier $slot)
Returned closure takes a string value as argument, and returns the source of the vocabulary it's from...
Definition: Adapter.php:138
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
return true
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc