ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
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 
276  #[\PHPUnit\Framework\Attributes\DataProvider('singleValueProvider')]
277  public function testPresentableLabels(
278  string $value,
279  bool $is_in_copyright,
280  bool $is_in_controlled,
281  bool $is_in_standard,
282  string $expected_label
283  ): void {
284  $presentation = new Presentation(
285  $this->getCopyrightBridge(
286  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
287  ...($is_in_copyright ? [$value] : [])
288  ),
290  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
291  true,
292  false,
293  ...($is_in_controlled ? [$value] : [])
294  ),
295  $this->getStandardRepository(
296  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
297  true,
298  ...($is_in_standard ? [$value] : [])
299  )
300  );
301 
302  $labels = $presentation->presentableLabels(
303  $this->getPresentationUtilities(),
304  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
305  false,
306  $value
307  );
308 
310  $labels,
311  [['value' => $value, 'label' => $expected_label]]
312  );
313  }
314 
315  #[\PHPUnit\Framework\Attributes\DataProvider('singleValueProvider')]
317  string $value,
318  bool $is_in_copyright,
319  bool $is_in_controlled,
320  bool $is_in_standard,
321  string $expected_label_without_suffix
322  ): void {
323  $presentation = new Presentation(
324  $this->getCopyrightBridge(
325  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
326  ...($is_in_copyright ? [$value] : [])
327  ),
329  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
330  true,
331  false,
332  ...($is_in_controlled ? [$value] : [])
333  ),
334  $this->getStandardRepository(
335  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
336  true,
337  ...($is_in_standard ? [$value] : [])
338  )
339  );
340 
341  $labels = $presentation->presentableLabels(
342  $this->getPresentationUtilities(),
343  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
344  true,
345  $value
346  );
347 
348  $suffix = (!$is_in_standard && !$is_in_controlled && !$is_in_copyright) ? ' translated suffix' : '';
350  $labels,
351  [['value' => $value, 'label' => $expected_label_without_suffix . $suffix]]
352  );
353  }
354 
356  {
357  $value = 'some value';
358  $presentation = new Presentation(
359  $this->getCopyrightBridge(SlotIdentifier::EDUCATIONAL_DIFFICULTY),
361  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
362  true,
363  true,
364  ),
365  $this->getStandardRepository(
366  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
367  true
368  )
369  );
370 
371  $labels = $presentation->presentableLabels(
372  $this->getPresentationUtilities(),
373  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
374  false,
375  $value
376  );
377 
379  $labels,
380  [['value' => $value, 'label' => $value]]
381  );
382  }
383 
384  public function testPresentableLabelsMultipleValues(): void
385  {
386  $presentation = new Presentation(
387  $this->getCopyrightBridge(
388  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
389  'cp 1',
390  'cp 2'
391  ),
393  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
394  true,
395  false,
396  'contr 1',
397  'contr 2',
398  'contr 3'
399  ),
400  $this->getStandardRepository(
401  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
402  true,
403  'stand 1',
404  'stand 2'
405  )
406  );
407 
408  $labels = $presentation->presentableLabels(
409  $this->getPresentationUtilities(),
410  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
411  false,
412  'contr 2',
413  'cp 1',
414  'something else',
415  'stand 2',
416  'contr 3'
417  );
418 
420  $labels,
421  [
422  ['value' => 'contr 2', 'label' => 'controlled label for contr 2'],
423  ['value' => 'cp 1', 'label' => 'copyright label for cp 1'],
424  ['value' => 'something else', 'label' => 'something else'],
425  ['value' => 'stand 2', 'label' => 'standard label for stand 2'],
426  ['value' => 'contr 3', 'label' => 'controlled label for contr 3']
427  ]
428  );
429  }
430 
431  public function testLabelsForVocabularyStandard(): void
432  {
433  $presentation = new Presentation(
434  $this->getCopyrightBridge(
435  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
436  'v1',
437  'v2',
438  'v3'
439  ),
441  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
442  false,
443  false,
444  'v1',
445  'v2',
446  'v3'
447  ),
448  $this->getStandardRepository(
449  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
450  false,
451  'v1',
452  'v2',
453  'v3'
454  )
455  );
456 
457  $labels = $presentation->labelsForVocabulary(
458  $this->getPresentationUtilities(),
459  $this->getVocabulary(
460  Type::STANDARD,
461  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
462  'v1',
463  'v2',
464  'v3'
465  )
466  );
467 
469  $labels,
470  [
471  ['value' => 'v1', 'label' => 'standard label for v1'],
472  ['value' => 'v2', 'label' => 'standard label for v2'],
473  ['value' => 'v3', 'label' => 'standard label for v3']
474  ]
475  );
476  }
477 
479  {
480  $presentation = new Presentation(
481  $this->getCopyrightBridge(
482  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
483  'v1',
484  'v2',
485  'v3'
486  ),
488  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
489  false,
490  false,
491  'v1',
492  'v2',
493  'v3'
494  ),
495  $this->getStandardRepository(
496  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
497  false,
498  'v1',
499  'v2',
500  'v3'
501  )
502  );
503 
504  $labels = $presentation->labelsForVocabulary(
505  $this->getPresentationUtilities(),
506  $this->getVocabulary(
507  Type::CONTROLLED_STRING,
508  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
509  'v1',
510  'v2',
511  'v3'
512  )
513  );
514 
516  $labels,
517  [
518  ['value' => 'v1', 'label' => 'controlled label for v1'],
519  ['value' => 'v2', 'label' => 'controlled label for v2'],
520  ['value' => 'v3', 'label' => 'controlled label for v3']
521  ]
522  );
523  }
524 
526  {
527  $presentation = new Presentation(
528  $this->getCopyrightBridge(
529  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
530  'v1',
531  'v2',
532  'v3'
533  ),
535  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
536  false,
537  false,
538  'v1',
539  'v2',
540  'v3'
541  ),
542  $this->getStandardRepository(
543  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
544  false,
545  'v1',
546  'v2',
547  'v3'
548  )
549  );
550 
551  $labels = $presentation->labelsForVocabulary(
552  $this->getPresentationUtilities(),
553  $this->getVocabulary(
554  Type::CONTROLLED_VOCAB_VALUE,
555  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
556  'v1',
557  'v2',
558  'v3'
559  )
560  );
561 
563  $labels,
564  [
565  ['value' => 'v1', 'label' => 'controlled label for v1'],
566  ['value' => 'v2', 'label' => 'controlled label for v2'],
567  ['value' => 'v3', 'label' => 'controlled label for v3']
568  ]
569  );
570  }
571 
572  public function testLabelsForVocabularyCopyright(): void
573  {
574  $presentation = new Presentation(
575  $this->getCopyrightBridge(
576  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
577  'v1',
578  'v2',
579  'v3'
580  ),
582  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
583  false,
584  false,
585  'v1',
586  'v2',
587  'v3'
588  ),
589  $this->getStandardRepository(
590  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
591  false,
592  'v1',
593  'v2',
594  'v3'
595  )
596  );
597 
598  $labels = $presentation->labelsForVocabulary(
599  $this->getPresentationUtilities(),
600  $this->getVocabulary(
602  SlotIdentifier::EDUCATIONAL_DIFFICULTY,
603  'v1',
604  'v2',
605  'v3'
606  )
607  );
608 
610  $labels,
611  [
612  ['value' => 'v1', 'label' => 'copyright label for v1'],
613  ['value' => 'v2', 'label' => 'copyright label for v2'],
614  ['value' => 'v3', 'label' => 'copyright label for v3']
615  ]
616  );
617  }
618 }
testPresentableLabels(string $value, bool $is_in_copyright, bool $is_in_controlled, bool $is_in_standard, string $expected_label)
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)
assertLabelledValueMatches(LabelledValueInterface $labelled_value, string $expected_value, string $expected_label)
getCopyrightBridge(SlotIdentifier $slot_with_vocab, string ... $values_from_vocab)