ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
RepositoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
33use ILIAS\MetaData\Presentation\UtilitiesInterface as PresentationUtilities;
36
37class RepositoryTest extends TestCase
38{
39 protected function getGateway(SlotIdentifier ...$deactivation_entries): GatewayInterface
40 {
41 return new class ($deactivation_entries) extends NullGateway {
42 public function __construct(
43 public array $exposed_deactivation_entries
44 ) {
45 }
46
47 public function createDeactivationEntry(SlotIdentifier $slot): void
48 {
49 $this->exposed_deactivation_entries[] = $slot;
50 }
51
52 public function deleteDeactivationEntry(SlotIdentifier $slot): void
53 {
54 if (!in_array($slot, $this->exposed_deactivation_entries)) {
55 return;
56 }
57 unset($this->exposed_deactivation_entries[array_search($slot, $this->exposed_deactivation_entries)]);
58 }
59
60 public function doesDeactivationEntryExistForSlot(SlotIdentifier $slot): bool
61 {
62 return in_array($slot, $this->exposed_deactivation_entries);
63 }
64 };
65 }
66
67 protected function getVocabFactory(): FactoryInterface
68 {
69 return new class () extends NullFactory {
70 public function null(): VocabularyInterface
71 {
72 return new NullVocabulary();
73 }
74
75 public function standard(SlotIdentifier $slot, string ...$values): BuilderInterface
76 {
77 return new class ($slot, $values) extends NullBuilder {
78 protected bool $active = true;
79
80 public function __construct(
81 protected SlotIdentifier $slot,
82 protected array $values
83 ) {
84 }
85
86 public function withIsDeactivated(bool $deactivated): BuilderInterface
87 {
88 $clone = clone $this;
89 $clone->active = !$deactivated;
90 return $clone;
91 }
92
93 public function get(): VocabularyInterface
94 {
95 return new class ($this->slot, $this->active, $this->values) extends NullVocabulary {
96 public function __construct(
97 protected SlotIdentifier $slot,
98 protected bool $active,
99 protected array $values
100 ) {
101 }
102
103 public function slot(): SlotIdentifier
104 {
105 return $this->slot;
106 }
107
108 public function isActive(): bool
109 {
110 return $this->active;
111 }
112
113
114 public function values(): \Generator
115 {
116 yield from $this->values;
117 }
118 };
119 }
120 };
121 }
122 };
123 }
124
125 protected function getAssignments(array $assignments = []): AssignmentsInterface
126 {
127 return new class ($assignments) extends NullAssignments {
128 public function __construct(protected array $assignments)
129 {
130 }
131
132 public function doesSlotHaveValues(SlotIdentifier $slot): bool
133 {
134 return array_key_exists($slot->value, $this->assignments);
135 }
136
137 public function valuesForSlot(SlotIdentifier $slot): \Generator
138 {
139 yield from $this->assignments[$slot->value] ?? [];
140 }
141 };
142 }
143
144 protected function getPresentationUtilities(): PresentationUtilities
145 {
146 return new class () extends NullUtilities {
147 public function txt(string $key): string
148 {
149 return 'translated ' . $key;
150 }
151 };
152 }
153
154 protected function assertVocabularyMatches(
155 VocabularyInterface $vocabulary,
156 SlotIdentifier $expected_slot,
157 bool $expected_active,
158 string ...$expected_values
159 ): void {
160 $this->assertSame($expected_slot, $vocabulary->slot());
161 $this->assertSame(
162 $expected_active,
163 $vocabulary->isActive(),
164 'Vocab for slot ' . $expected_slot->value . ' activation should be ' . $expected_active
165 );
166 $this->assertSame(
167 $expected_values,
168 iterator_to_array($vocabulary->values()),
169 'Vocab for slot ' . $expected_slot->value . ' values not correct.'
170 );
171 }
172
173 protected function assertVocabulariesMatch(
174 array $expected_content,
175 array $deactivated_slots,
176 VocabularyInterface ...$vocabularies,
177 ): void {
178 $this->assertCount(count($expected_content), $vocabularies);
179
180 $i = 0;
181 foreach ($expected_content as $expected_slot => $expected_values) {
182 $slot = SlotIdentifier::from($expected_slot);
183 $this->assertVocabularyMatches(
184 $vocabularies[$i],
185 $slot,
186 !in_array($slot, $deactivated_slots),
187 ...$expected_values
188 );
189 $i++;
190 }
191 }
192
194 {
195 $already_deactivated = [
196 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
197 SlotIdentifier::CLASSIFICATION_PURPOSE
198 ];
199 $repo = new Repository(
200 $gateway = $this->getGateway(...$already_deactivated),
201 $this->getVocabFactory(),
202 $this->getAssignments()
203 );
204
205 $repo->deactivateVocabulary(SlotIdentifier::NULL);
206
207 $this->assertSame(
208 $already_deactivated,
209 $gateway->exposed_deactivation_entries
210 );
211 }
212
214 {
215 $already_deactivated = [
216 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
217 SlotIdentifier::CLASSIFICATION_PURPOSE
218 ];
219 $repo = new Repository(
220 $gateway = $this->getGateway(...$already_deactivated),
221 $this->getVocabFactory(),
222 $this->getAssignments()
223 );
224
225 $repo->deactivateVocabulary(SlotIdentifier::EDUCATIONAL_DIFFICULTY);
226
227 $this->assertSame(
228 $already_deactivated,
229 $gateway->exposed_deactivation_entries
230 );
231 }
232
233 public function testDeactivateVocabulary(): void
234 {
235 $already_deactivated = [
236 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
237 SlotIdentifier::CLASSIFICATION_PURPOSE
238 ];
239 $repo = new Repository(
240 $gateway = $this->getGateway(...$already_deactivated),
241 $this->getVocabFactory(),
242 $this->getAssignments()
243 );
244
245 $repo->deactivateVocabulary(SlotIdentifier::GENERAL_STRUCTURE);
246
247 $this->assertSame(
248 array_merge($already_deactivated, [SlotIdentifier::GENERAL_STRUCTURE]),
249 $gateway->exposed_deactivation_entries
250 );
251 }
252
253 public function testActivateVocabularyInvalidSlot(): void
254 {
255 $already_deactivated = [
256 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
257 SlotIdentifier::CLASSIFICATION_PURPOSE
258 ];
259 $repo = new Repository(
260 $gateway = $this->getGateway(...$already_deactivated),
261 $this->getVocabFactory(),
262 $this->getAssignments()
263 );
264
265 $repo->activateVocabulary(SlotIdentifier::NULL);
266
267 $this->assertSame(
268 $already_deactivated,
269 $gateway->exposed_deactivation_entries
270 );
271 }
272
273 public function testActivateVocabulary(): void
274 {
275 $already_deactivated = [
276 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
277 SlotIdentifier::CLASSIFICATION_PURPOSE
278 ];
279 $repo = new Repository(
280 $gateway = $this->getGateway(...$already_deactivated),
281 $this->getVocabFactory(),
282 $this->getAssignments()
283 );
284
285 $repo->activateVocabulary(SlotIdentifier::CLASSIFICATION_PURPOSE);
286
287 $this->assertSame(
288 [SlotIdentifier::EDUCATIONAL_DIFFICULTY],
289 $gateway->exposed_deactivation_entries
290 );
291 }
292
294 {
295 $already_deactivated = [
296 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
297 SlotIdentifier::CLASSIFICATION_PURPOSE
298 ];
299 $repo = new Repository(
300 $gateway = $this->getGateway(...$already_deactivated),
301 $this->getVocabFactory(),
302 $this->getAssignments()
303 );
304
305 $repo->activateVocabulary(SlotIdentifier::GENERAL_STRUCTURE);
306
307 $this->assertSame(
308 $already_deactivated,
309 $gateway->exposed_deactivation_entries
310 );
311 }
312
313 public function testIsVocabularyActiveTrue(): void
314 {
315 $already_deactivated = [
316 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
317 SlotIdentifier::CLASSIFICATION_PURPOSE
318 ];
319 $repo = new Repository(
320 $gateway = $this->getGateway(...$already_deactivated),
321 $this->getVocabFactory(),
322 $this->getAssignments()
323 );
324
325 $this->assertTrue($repo->isVocabularyActive(SlotIdentifier::GENERAL_STRUCTURE));
326 }
327
328 public function testIsVocabularyActiveFalse(): void
329 {
330 $already_deactivated = [
331 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
332 SlotIdentifier::CLASSIFICATION_PURPOSE
333 ];
334 $repo = new Repository(
335 $gateway = $this->getGateway(...$already_deactivated),
336 $this->getVocabFactory(),
337 $this->getAssignments()
338 );
339
340 $this->assertFalse($repo->isVocabularyActive(SlotIdentifier::EDUCATIONAL_DIFFICULTY));
341 }
342
343 public function testGetVocabularyInvalidSlot(): void
344 {
345 $repo = new Repository(
346 $gateway = $this->getGateway(),
347 $this->getVocabFactory(),
348 $this->getAssignments([
349 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
350 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4']
351 ])
352 );
353
354 $vocab = $repo->getVocabulary(SlotIdentifier::LIFECYCLE_STATUS);
355 $this->assertSame(SlotIdentifier::NULL, $vocab->slot());
356 $this->assertEmpty(iterator_to_array($vocab->values()));
357 }
358
359 public function testGetVocabulary(): void
360 {
361 $repo = new Repository(
362 $gateway = $this->getGateway(),
363 $this->getVocabFactory(),
364 $this->getAssignments([
365 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
366 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4']
367 ])
368 );
369
370 $vocab = $repo->getVocabulary(SlotIdentifier::EDUCATIONAL_DIFFICULTY);
371 $this->assertSame(SlotIdentifier::EDUCATIONAL_DIFFICULTY, $vocab->slot());
372 $this->assertSame(['value 1', 'value 2'], iterator_to_array($vocab->values()));
373 $this->assertTrue($vocab->isActive());
374 }
375
376 public function testGetVocabularyInactive(): void
377 {
378 $repo = new Repository(
379 $gateway = $this->getGateway(SlotIdentifier::EDUCATIONAL_DIFFICULTY),
380 $this->getVocabFactory(),
381 $this->getAssignments([
382 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
383 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4']
384 ])
385 );
386
387 $vocab = $repo->getVocabulary(SlotIdentifier::EDUCATIONAL_DIFFICULTY);
388 $this->assertSame(SlotIdentifier::EDUCATIONAL_DIFFICULTY, $vocab->slot());
389 $this->assertSame(['value 1', 'value 2'], iterator_to_array($vocab->values()));
390 $this->assertFalse($vocab->isActive());
391 }
392
393 public function testGetVocabulariesEmpty(): void
394 {
395 $repo = new Repository(
396 $gateway = $this->getGateway(
397 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
398 SlotIdentifier::LIFECYCLE_STATUS
399 ),
400 $this->getVocabFactory(),
401 $this->getAssignments([
402 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
403 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4'],
404 SlotIdentifier::LIFECYCLE_STATUS->value => ['value 5', 'value 6']
405 ])
406 );
407
408 $vocabs = $repo->getVocabularies(SlotIdentifier::GENERAL_STRUCTURE);
409
410 $this->assertNull($vocabs->current());
411 }
412
413 public function testGetVocabularies(): void
414 {
415 $expected_content = [
416 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
417 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4'],
418 SlotIdentifier::LIFECYCLE_STATUS->value => ['value 5', 'value 6']
419 ];
420 $deactivated_slots = [
421 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
422 SlotIdentifier::LIFECYCLE_STATUS
423 ];
424
425 $repo = new Repository(
426 $gateway = $this->getGateway(...$deactivated_slots),
427 $this->getVocabFactory(),
428 $this->getAssignments($expected_content)
429 );
430
431 $vocabs = $repo->getVocabularies(
432 SlotIdentifier::GENERAL_STRUCTURE,
433 SlotIdentifier::LIFECYCLE_STATUS,
434 SlotIdentifier::CLASSIFICATION_PURPOSE
435 );
436
437 $this->assertVocabulariesMatch(
438 [
439 SlotIdentifier::LIFECYCLE_STATUS->value => ['value 5', 'value 6'],
440 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4']
441 ],
442 $deactivated_slots,
443 ...$vocabs
444 );
445 }
446
447 public function testGetActiveVocabulariesEmpty(): void
448 {
449 $repo = new Repository(
450 $gateway = $this->getGateway(
451 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
452 SlotIdentifier::LIFECYCLE_STATUS
453 ),
454 $this->getVocabFactory(),
455 $this->getAssignments([
456 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
457 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4'],
458 SlotIdentifier::LIFECYCLE_STATUS->value => ['value 5', 'value 6']
459 ])
460 );
461
462 $vocabs = $repo->getActiveVocabularies(SlotIdentifier::GENERAL_STRUCTURE);
463
464 $this->assertNull($vocabs->current());
465 }
466
467 public function testGetActiveVocabularies(): void
468 {
469 $expected_content = [
470 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2'],
471 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4'],
472 SlotIdentifier::LIFECYCLE_STATUS->value => ['value 5', 'value 6'],
473 SlotIdentifier::TECHNICAL_REQUIREMENT_BROWSER->value => ['value 7', 'value 8']
474 ];
475 $deactivated_slots = [
476 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
477 SlotIdentifier::LIFECYCLE_STATUS
478 ];
479
480 $repo = new Repository(
481 $gateway = $this->getGateway(...$deactivated_slots),
482 $this->getVocabFactory(),
483 $this->getAssignments($expected_content)
484 );
485
486 $vocabs = $repo->getActiveVocabularies(
487 SlotIdentifier::TECHNICAL_REQUIREMENT_BROWSER,
488 SlotIdentifier::GENERAL_STRUCTURE,
489 SlotIdentifier::LIFECYCLE_STATUS,
490 SlotIdentifier::CLASSIFICATION_PURPOSE
491 );
492
493 $this->assertVocabulariesMatch(
494 [
495 SlotIdentifier::TECHNICAL_REQUIREMENT_BROWSER->value => ['value 7', 'value 8'],
496 SlotIdentifier::CLASSIFICATION_PURPOSE->value => ['value 3', 'value 4']
497 ],
498 $deactivated_slots,
499 ...$vocabs
500 );
501 }
502
503 public function testGetLabelsForValues(): void
504 {
505 $vocab_values = [
506 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2', 'ProblemStatement', 'ispartof'],
507 ];
508
509 $repo = new Repository(
510 $gateway = $this->getGateway(),
511 $this->getVocabFactory(),
512 $this->getAssignments($vocab_values)
513 );
514
515 $labelled_values = $repo->getLabelsForValues(
516 $this->getPresentationUtilities(),
517 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
518 false,
519 'ispartof',
520 'value 2',
521 'ProblemStatement',
522 'something else'
523 );
524
525 $l1 = $labelled_values->current();
526 $this->assertSame('ispartof', $l1->value());
527 $this->assertSame('translated meta_is_part_of', $l1->label());
528 $labelled_values->next();
529
530 $l2 = $labelled_values->current();
531 $this->assertSame('value 2', $l2->value());
532 $this->assertSame('translated meta_value_2', $l2->label());
533 $labelled_values->next();
534
535 $l3 = $labelled_values->current();
536 $this->assertSame('ProblemStatement', $l3->value());
537 $this->assertSame('translated meta_problem_statement', $l3->label());
538 $labelled_values->next();
539
540 $this->assertNull($labelled_values->current());
541 }
542
544 {
545 $vocab_values = [
546 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2', 'ProblemStatement', 'ispartof'],
547 ];
548
549 $repo = new Repository(
550 $gateway = $this->getGateway(),
551 $this->getVocabFactory(),
552 $this->getAssignments($vocab_values)
553 );
554
555 $labelled_values = $repo->getLabelsForValues(
556 $this->getPresentationUtilities(),
557 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
558 true,
559 'ispartof',
560 'value 2',
561 'ProblemStatement',
562 'something else'
563 );
564
565 $l1 = $labelled_values->current();
566 $this->assertSame('ispartof', $l1->value());
567 $this->assertSame('translated meta_is_part_of', $l1->label());
568 $labelled_values->next();
569
570 $l2 = $labelled_values->current();
571 $this->assertSame('value 2', $l2->value());
572 $this->assertSame('translated meta_value_2', $l2->label());
573 $labelled_values->next();
574
575 $l3 = $labelled_values->current();
576 $this->assertSame('ProblemStatement', $l3->value());
577 $this->assertSame('translated meta_problem_statement', $l3->label());
578 $labelled_values->next();
579
580 $this->assertNull($labelled_values->current());
581 }
582
584 {
585 $vocab_values = [
586 SlotIdentifier::EDUCATIONAL_DIFFICULTY->value => ['value 1', 'value 2', 'ProblemStatement', 'ispartof'],
587 ];
588
589 $repo = new Repository(
590 $gateway = $this->getGateway(SlotIdentifier::EDUCATIONAL_DIFFICULTY),
591 $this->getVocabFactory(),
592 $this->getAssignments($vocab_values)
593 );
594
595 $labelled_values = $repo->getLabelsForValues(
596 $this->getPresentationUtilities(),
597 SlotIdentifier::EDUCATIONAL_DIFFICULTY,
598 true,
599 'ispartof',
600 'value 2',
601 'ProblemStatement',
602 'something else'
603 );
604
605 $this->assertNull($labelled_values->current());
606 }
607}
$vocab
assertVocabulariesMatch(array $expected_content, array $deactivated_slots, VocabularyInterface ... $vocabularies,)
getGateway(SlotIdentifier ... $deactivation_entries)
assertVocabularyMatches(VocabularyInterface $vocabulary, SlotIdentifier $expected_slot, bool $expected_active, string ... $expected_values)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76