ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
PresentationTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 use ILIAS\MetaData\Vocabularies\Copyright\NullBridge as NullCopyrightBridge;
34 use ILIAS\MetaData\Vocabularies\Standard\NullRepository as NullStandardRepository;
37 
38 class PresentationTest extends TestCase
39 {
40  public function getVocabulary(
41  Type $type,
42  SlotIdentifier $slot,
43  string ...$values
45  return new class ($type, $slot, $values) extends NullVocabulary {
46  public function __construct(
47  protected Type $type,
48  protected SlotIdentifier $slot,
49  protected array $values
50  ) {
51  }
52 
53  public function type(): Type
54  {
55  return $this->type;
56  }
57 
58  public function slot(): SlotIdentifier
59  {
60  return $this->slot;
61  }
62 
63  public function values(): \Generator
64  {
65  yield from $this->values;
66  }
67  };
68  }
69 
71  {
72  return new class () extends NullUtilities {
73  public function txt(string $key): string
74  {
75  return 'translated suffix';
76  }
77  };
78  }
79 
80  public function getCopyrightBridge(
81  SlotIdentifier $slot_with_vocab,
82  string ...$values_from_vocab
83  ): CopyrightBridge {
84  return new class ($slot_with_vocab, $values_from_vocab) extends NullCopyrightBridge {
85  public function __construct(
86  protected SlotIdentifier $slot_with_vocab,
87  protected array $values_from_vocab
88  ) {
89  }
90 
91  public function labelsForValues(
92  SlotIdentifier $slot,
93  string ...$values
94  ): \Generator {
95  foreach ($values as $value) {
96  if ($slot !== $this->slot_with_vocab) {
97  return;
98  }
99  if (!in_array($value, $this->values_from_vocab)) {
100  continue;
101  }
102  yield new class ($value) extends NullLabelledValue {
103  public function __construct(protected string $value)
104  {
105  }
106 
107  public function value(): string
108  {
109  return $this->value;
110  }
111 
112  public function label(): string
113  {
114  return 'copyright label for ' . $this->value;
115  }
116  };
117  }
118  }
119  };
120  }
121 
122  public function getControlledRepository(
123  SlotIdentifier $slot_with_vocab,
124  bool $only_active,
125  bool $empty_labels,
126  string ...$values_from_vocab
127  ): ControlledRepository {
128  return new class ($slot_with_vocab, $only_active, $empty_labels, $values_from_vocab) extends NullControlledRepository {
129  public function __construct(
130  protected SlotIdentifier $slot_with_vocab,
131  protected bool $only_active,
132  protected bool $empty_labels,
133  protected array $values_from_vocab
134  ) {
135  }
136 
137  public function getLabelsForValues(
138  SlotIdentifier $slot,
139  bool $only_active,
140  string ...$values
141  ): \Generator {
142  if ($slot !== $this->slot_with_vocab || $only_active !== $this->only_active) {
143  return;
144  }
145  foreach ($values as $value) {
146  if (!in_array($value, $this->values_from_vocab)) {
147  continue;
148  }
149  yield new class ($value, $this->empty_labels) extends NullLabelledValue {
150  public function __construct(
151  protected string $value,
152  protected bool $empty_label
153  ) {
154  }
155 
156  public function value(): string
157  {
158  return $this->value;
159  }
160 
161  public function label(): string
162  {
163  if ($this->empty_label) {
164  return '';
165  }
166  return 'controlled label for ' . $this->value;
167  }
168  };
169  }
170  }
171  };
172  }
173 
174  public function getStandardRepository(
175  SlotIdentifier $slot_with_vocab,
176  bool $only_active,
177  string ...$values_from_vocab
178  ): StandardRepository {
179  return new class ($slot_with_vocab, $only_active, $values_from_vocab) extends NullStandardRepository {
180  public function __construct(
181  protected SlotIdentifier $slot_with_vocab,
182  protected bool $only_active,
183  protected array $values_from_vocab
184  ) {
185  }
186 
187  public function getLabelsForValues(
188  PresentationUtilities $presentation_utilities,
189  SlotIdentifier $slot,
190  bool $only_active,
191  string ...$values
192  ): \Generator {
193  if ($slot !== $this->slot_with_vocab || $only_active !== $this->only_active) {
194  return;
195  }
196  foreach ($values as $value) {
197  if (!in_array($value, $this->values_from_vocab)) {
198  continue;
199  }
200  yield new class ($value) extends NullLabelledValue {
201  public function __construct(protected string $value)
202  {
203  }
204 
205  public function value(): string
206  {
207  return $this->value;
208  }
209 
210  public function label(): string
211  {
212  return 'standard label for ' . $this->value;
213  }
214  };
215  }
216  }
217  };
218  }
219 
220  protected function assertLabelledValueMatches(
221  LabelledValueInterface $labelled_value,
222  string $expected_value,
223  string $expected_label
224  ): void {
225  $this->assertSame(
226  $expected_value,
227  $labelled_value->value(),
228  'Value of Labelled value ' . $labelled_value->value() . ' should be ' . $expected_value
229  );
230  $this->assertSame(
231  $expected_label,
232  $labelled_value->label(),
233  'Label of Labelled value ' . $labelled_value->label() . ' should be ' . $expected_label
234  );
235  }
236 
242  \Generator $labelled_values,
243  array $expected
244  ): void {
245  $as_array = iterator_to_array($labelled_values);
246  $this->assertCount(
247  count($expected),
248  $as_array,
249  'There should be ' . count($expected) . ' labelled values, not ' . count($as_array)
250  );
251  $i = 0;
252  foreach ($as_array as $labelled_value) {
254  $labelled_value,
255  $expected[$i]['value'],
256  $expected[$i]['label']
257  );
258  $i++;
259  }
260  }
261 
262  public static function singleValueProvider(): array
263  {
264  return [
265  ['v1', true, true, true, 'copyright label for v1'],
266  ['v2', true, false, true, 'copyright label for v2'],
267  ['v3', true, true, false, 'copyright label for v3'],
268  ['v4', true, false, false, 'copyright label for v4'],
269  ['v5', false, true, true, 'controlled label for v5'],
270  ['v6', false, false, true, 'standard label for v6'],
271  ['v7', false, true, false, 'controlled label for v7'],
272  ['v8', false, false, false, 'v8']
273  ];
274  }
275 
279  public function testPresentableLabels(
280  string $value,
281  bool $is_in_copyright,
282  bool $is_in_controlled,
283  bool $is_in_standard,
284  string $expected_label
285  ): void {
286  $presentation = new Presentation(
287  $this->getCopyrightBridge(
288  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
289  ...($is_in_copyright ? [$value] : [])
290  ),
292  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
293  true,
294  false,
295  ...($is_in_controlled ? [$value] : [])
296  ),
297  $this->getStandardRepository(
298  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
299  true,
300  ...($is_in_standard ? [$value] : [])
301  )
302  );
303 
304  $labels = $presentation->presentableLabels(
305  $this->getPresentationUtilities(),
306  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
307  false,
308  $value
309  );
310 
312  $labels,
313  [['value' => $value, 'label' => $expected_label]]
314  );
315  }
316 
321  string $value,
322  bool $is_in_copyright,
323  bool $is_in_controlled,
324  bool $is_in_standard,
325  string $expected_label_without_suffix
326  ): void {
327  $presentation = new Presentation(
328  $this->getCopyrightBridge(
329  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
330  ...($is_in_copyright ? [$value] : [])
331  ),
333  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
334  true,
335  false,
336  ...($is_in_controlled ? [$value] : [])
337  ),
338  $this->getStandardRepository(
339  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
340  true,
341  ...($is_in_standard ? [$value] : [])
342  )
343  );
344 
345  $labels = $presentation->presentableLabels(
346  $this->getPresentationUtilities(),
347  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
348  true,
349  $value
350  );
351 
352  $suffix = (!$is_in_standard && !$is_in_controlled && !$is_in_copyright) ? ' translated suffix' : '';
354  $labels,
355  [['value' => $value, 'label' => $expected_label_without_suffix . $suffix]]
356  );
357  }
358 
360  {
361  $value = 'some value';
362  $presentation = new Presentation(
363  $this->getCopyrightBridge(SlotIdentifier::EDUCATIONAL_DIFFICULTY),
365  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
366  true,
367  true,
368  ),
369  $this->getStandardRepository(
370  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
371  true
372  )
373  );
374 
375  $labels = $presentation->presentableLabels(
376  $this->getPresentationUtilities(),
377  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
378  false,
379  $value
380  );
381 
383  $labels,
384  [['value' => $value, 'label' => $value]]
385  );
386  }
387 
388  public function testPresentableLabelsMultipleValues(): void
389  {
390  $presentation = new Presentation(
391  $this->getCopyrightBridge(
392  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
393  'cp 1',
394  'cp 2'
395  ),
397  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
398  true,
399  false,
400  'contr 1',
401  'contr 2',
402  'contr 3'
403  ),
404  $this->getStandardRepository(
405  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
406  true,
407  'stand 1',
408  'stand 2'
409  )
410  );
411 
412  $labels = $presentation->presentableLabels(
413  $this->getPresentationUtilities(),
414  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
415  false,
416  'contr 2',
417  'cp 1',
418  'something else',
419  'stand 2',
420  'contr 3'
421  );
422 
424  $labels,
425  [
426  ['value' => 'contr 2', 'label' => 'controlled label for contr 2'],
427  ['value' => 'cp 1', 'label' => 'copyright label for cp 1'],
428  ['value' => 'something else', 'label' => 'something else'],
429  ['value' => 'stand 2', 'label' => 'standard label for stand 2'],
430  ['value' => 'contr 3', 'label' => 'controlled label for contr 3']
431  ]
432  );
433  }
434 
435  public function testLabelsForVocabularyStandard(): void
436  {
437  $presentation = new Presentation(
438  $this->getCopyrightBridge(
439  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
440  'v1',
441  'v2',
442  'v3'
443  ),
445  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
446  false,
447  false,
448  'v1',
449  'v2',
450  'v3'
451  ),
452  $this->getStandardRepository(
453  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
454  false,
455  'v1',
456  'v2',
457  'v3'
458  )
459  );
460 
461  $labels = $presentation->labelsForVocabulary(
462  $this->getPresentationUtilities(),
463  $this->getVocabulary(
464  Type::STANDARD,
465  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
466  'v1',
467  'v2',
468  'v3'
469  )
470  );
471 
473  $labels,
474  [
475  ['value' => 'v1', 'label' => 'standard label for v1'],
476  ['value' => 'v2', 'label' => 'standard label for v2'],
477  ['value' => 'v3', 'label' => 'standard label for v3']
478  ]
479  );
480  }
481 
483  {
484  $presentation = new Presentation(
485  $this->getCopyrightBridge(
486  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
487  'v1',
488  'v2',
489  'v3'
490  ),
492  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
493  false,
494  false,
495  'v1',
496  'v2',
497  'v3'
498  ),
499  $this->getStandardRepository(
500  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
501  false,
502  'v1',
503  'v2',
504  'v3'
505  )
506  );
507 
508  $labels = $presentation->labelsForVocabulary(
509  $this->getPresentationUtilities(),
510  $this->getVocabulary(
511  Type::CONTROLLED_STRING,
512  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
513  'v1',
514  'v2',
515  'v3'
516  )
517  );
518 
520  $labels,
521  [
522  ['value' => 'v1', 'label' => 'controlled label for v1'],
523  ['value' => 'v2', 'label' => 'controlled label for v2'],
524  ['value' => 'v3', 'label' => 'controlled label for v3']
525  ]
526  );
527  }
528 
530  {
531  $presentation = new Presentation(
532  $this->getCopyrightBridge(
533  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
534  'v1',
535  'v2',
536  'v3'
537  ),
539  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
540  false,
541  false,
542  'v1',
543  'v2',
544  'v3'
545  ),
546  $this->getStandardRepository(
547  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
548  false,
549  'v1',
550  'v2',
551  'v3'
552  )
553  );
554 
555  $labels = $presentation->labelsForVocabulary(
556  $this->getPresentationUtilities(),
557  $this->getVocabulary(
558  Type::CONTROLLED_VOCAB_VALUE,
559  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
560  'v1',
561  'v2',
562  'v3'
563  )
564  );
565 
567  $labels,
568  [
569  ['value' => 'v1', 'label' => 'controlled label for v1'],
570  ['value' => 'v2', 'label' => 'controlled label for v2'],
571  ['value' => 'v3', 'label' => 'controlled label for v3']
572  ]
573  );
574  }
575 
576  public function testLabelsForVocabularyCopyright(): void
577  {
578  $presentation = new Presentation(
579  $this->getCopyrightBridge(
580  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
581  'v1',
582  'v2',
583  'v3'
584  ),
586  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
587  false,
588  false,
589  'v1',
590  'v2',
591  'v3'
592  ),
593  $this->getStandardRepository(
594  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
595  false,
596  'v1',
597  'v2',
598  'v3'
599  )
600  );
601 
602  $labels = $presentation->labelsForVocabulary(
603  $this->getPresentationUtilities(),
604  $this->getVocabulary(
606  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
607  'v1',
608  'v2',
609  'v3'
610  )
611  );
612 
614  $labels,
615  [
616  ['value' => 'v1', 'label' => 'copyright label for v1'],
617  ['value' => 'v2', 'label' => 'copyright label for v2'],
618  ['value' => 'v3', 'label' => 'copyright label for v3']
619  ]
620  );
621  }
622 }
testPresentableLabels(string $value, bool $is_in_copyright, bool $is_in_controlled, bool $is_in_standard, string $expected_label)
singleValueProvider
getControlledRepository(SlotIdentifier $slot_with_vocab, bool $only_active, bool $empty_labels, string ... $values_from_vocab)
assertLabelledValuesMatchInOrder(\Generator $labelled_values, array $expected)
getVocabulary(Type $type, SlotIdentifier $slot, string ... $values)
getStandardRepository(SlotIdentifier $slot_with_vocab, bool $only_active, string ... $values_from_vocab)
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
testPresentableLabelsWithUnknownVocab(string $value, bool $is_in_copyright, bool $is_in_controlled, bool $is_in_standard, string $expected_label_without_suffix)
singleValueProvider
assertLabelledValueMatches(LabelledValueInterface $labelled_value, string $expected_value, string $expected_label)
getCopyrightBridge(SlotIdentifier $slot_with_vocab, string ... $values_from_vocab)