ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
RendererTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
30use ILIAS\Filesystem\Filesystem as WebFiles;
33use PHPUnit\Framework\MockObject\MockObject;
36
37class RendererTest extends TestCase
38{
39 protected function getMockRenderer(
40 Icon $icon,
41 Link $link,
42 Content $legacy,
43 string $src_from_irss
44 ): Renderer {
45 return new class ($icon, $link, $legacy, $src_from_irss) extends Renderer {
46 protected ?string $icon_src = null;
47 protected ?string $icon_alt = null;
48 protected ?string $link_label = null;
49 protected ?string $link_action = null;
50 protected ?Relationship $link_relationship = null;
51 protected ?bool $link_disabled = null;
52 protected ?string $legacy_text = null;
53
54 public function __construct(
55 protected Icon $icon,
56 protected Link $link,
57 protected Content $legacy,
58 protected string $src_from_irss
59 ) {
60 }
61
62 protected function getFallBackSrc(): string
63 {
64 return 'fallback src';
65 }
66
67 protected function customIcon(string $src, string $alt): Icon
68 {
69 $this->icon_src = $src;
70 $this->icon_alt = $alt;
71 return $this->icon;
72 }
73
74 protected function standardLink(
75 string $label,
76 string $action,
77 ?Relationship $relationship,
78 bool $disabled
79 ): Link {
80 $this->link_label = $label;
81 $this->link_action = $action;
82 $this->link_relationship = $relationship;
83 $this->link_disabled = $disabled;
84 return $this->link;
85 }
86
87 protected function textInLegacy(string $text): Content
88 {
89 $this->legacy_text = $text;
90 return $this->legacy;
91 }
92
93 protected function getSourceFromIRSS(string $string_id): string
94 {
95 return $this->src_from_irss;
96 }
97
98 public function exposeImageData(): array
99 {
100 return [
101 'icon_src' => $this->icon_src,
102 'icon_alt' => $this->icon_alt
103 ];
104 }
105
106 public function exposeLinkData(): array
107 {
108 return [
109 'link_label' => $this->link_label,
110 'link_action' => $this->link_action,
111 'link_relationship' => $this->link_relationship,
112 'link_disabled' => $this->link_disabled
113 ];
114 }
115
116 public function exposeLegacyData(): array
117 {
118 return [
119 'legacy_text' => $this->legacy_text
120 ];
121 }
122 };
123 }
124
125 protected function getMockIcon(): MockObject|Icon
126 {
127 return $this->getMockBuilder(IIcon::class)
128 ->disableOriginalConstructor()
129 ->getMock();
130 }
131
132 protected function getMockLink(): MockObject|Link
133 {
134 return $this->getMockBuilder(ILink::class)
135 ->disableOriginalConstructor()
136 ->onlyMethods(['withAdditionalRelationshipToReferencedResource'])
137 ->getMock();
138 }
139
140 protected function getMockLegacy(): MockObject|Content
141 {
142 return $this->getMockBuilder(ILegacy::class)
143 ->disableOriginalConstructor()
144 ->getMock();
145 }
146
147 protected function getMockURI(string $link): URI
148 {
149 $uri = $this->getMockBuilder(URI::class)
150 ->disableOriginalConstructor()
151 ->getMock();
152 $uri->method('__toString')->willReturn($link);
153 return $uri;
154 }
155
157 {
158 $link = $this->getMockLink();
159 $uri = $this->getMockURI('link');
160 $img_uri = $this->getMockURI('image link');
161
162 $renderer = $this->getMockRenderer(
163 $this->getMockIcon(),
164 $link,
165 $this->getMockLegacy(),
166 ''
167 );
168 $data = new class ($uri, $img_uri) extends NullCopyrightData {
169 public function __construct(
170 protected URI $uri,
171 protected URI $img_uri
172 ) {
173 }
174
175 public function fullName(): string
176 {
177 return 'full name';
178 }
179
180 public function link(): ?URI
181 {
182 return $this->uri;
183 }
184
185 public function hasImage(): bool
186 {
187 return true;
188 }
189
190 public function isImageLink(): bool
191 {
192 return true;
193 }
194
195 public function imageLink(): ?URI
196 {
197 return $this->img_uri;
198 }
199
200 public function altText(): string
201 {
202 return 'alt text';
203 }
204 };
205
206 $result = $renderer->toUIComponents($data);
207 $this->assertSame(2, count($result));
208 $this->assertInstanceOf(Icon::class, $result[0]);
209 $this->assertInstanceOf(Link::class, $result[1]);
210 $this->assertSame(
211 [
212 'icon_src' => 'image link',
213 'icon_alt' => 'alt text'
214 ],
215 $renderer->exposeImageData()
216 );
217 $this->assertSame(
218 [
219 'link_label' => 'full name',
220 'link_action' => 'link',
221 'link_relationship' => Relationship::LICENSE,
222 'link_disabled' => false
223 ],
224 $renderer->exposeLinkData()
225 );
226 }
227
228 public function testToUIComponentsEmpty(): void
229 {
230 $legacy = $this->getMockLegacy();
231
232 $renderer = $this->getMockRenderer(
233 $this->getMockIcon(),
234 $this->getMockLink(),
235 $legacy,
236 ''
237 );
238
239 $result = $renderer->toUIComponents(new NullCopyrightData());
240 $this->assertSame(0, count($result));
241 }
242
243 public function testToUIComponentsWithoutLink(): void
244 {
245 $uri = $this->getMockURI('image link');
246
247 $renderer = $this->getMockRenderer(
248 $this->getMockIcon(),
249 $this->getMockLink(),
250 $this->getMockLegacy(),
251 ''
252 );
253 $data = new class ($uri) extends NullCopyrightData {
254 public function __construct(protected URI $uri)
255 {
256 }
257
258 public function fullName(): string
259 {
260 return 'full name';
261 }
262
263 public function hasImage(): bool
264 {
265 return true;
266 }
267
268 public function isImageLink(): bool
269 {
270 return true;
271 }
272
273 public function imageLink(): ?URI
274 {
275 return $this->uri;
276 }
277
278 public function altText(): string
279 {
280 return 'alt text';
281 }
282 };
283
284 $result = $renderer->toUIComponents($data);
285 $this->assertSame(2, count($result));
286 $this->assertInstanceOf(Icon::class, $result[0]);
287 $this->assertInstanceOf(Content::class, $result[1]);
288 $this->assertSame(
289 [
290 'icon_src' => 'image link',
291 'icon_alt' => 'alt text'
292 ],
293 $renderer->exposeImageData()
294 );
295 $this->assertSame(
296 ['legacy_text' => 'full name'],
297 $renderer->exposeLegacyData()
298 );
299 }
300
301 public function testToUIComponentsWithLinkNoImage(): void
302 {
303 $link = $this->getMockLink();
304 $uri = $this->getMockURI('link');
305
306 $renderer = $this->getMockRenderer(
307 $this->getMockIcon(),
308 $link,
309 $this->getMockLegacy(),
310 ''
311 );
312 $data = new class ($uri) extends NullCopyrightData {
313 public function __construct(protected URI $uri)
314 {
315 }
316
317 public function fullName(): string
318 {
319 return 'full name';
320 }
321
322 public function link(): ?URI
323 {
324 return $this->uri;
325 }
326 };
327
328 $result = $renderer->toUIComponents($data);
329 $this->assertSame(1, count($result));
330 $this->assertInstanceOf(Link::class, $result[0]);
331 $this->assertSame(
332 [
333 'link_label' => 'full name',
334 'link_action' => 'link',
335 'link_relationship' => Relationship::LICENSE,
336 'link_disabled' => false
337 ],
338 $renderer->exposeLinkData()
339 );
340 }
341
343 {
344 $link = $this->getMockLink();
345 $uri = $this->getMockURI('link');
346
347 $renderer = $this->getMockRenderer(
348 $this->getMockIcon(),
349 $link,
350 $this->getMockLegacy(),
351 ''
352 );
353 $data = new class ($uri) extends NullCopyrightData {
354 public function __construct(protected URI $uri)
355 {
356 }
357
358 public function link(): ?URI
359 {
360 return $this->uri;
361 }
362 };
363
364 $result = $renderer->toUIComponents($data);
365 $this->assertSame(1, count($result));
366 $this->assertInstanceOf(Link::class, $result[0]);
367 $this->assertSame(
368 [
369 'link_label' => 'link',
370 'link_action' => 'link',
371 'link_relationship' => Relationship::LICENSE,
372 'link_disabled' => false
373 ],
374 $renderer->exposeLinkData()
375 );
376 }
377
379 {
380 $uri = $this->getMockURI('image link');
381
382 $renderer = $this->getMockRenderer(
383 $this->getMockIcon(),
384 $this->getMockLink(),
385 $this->getMockLegacy(),
386 ''
387 );
388 $data = new class ($uri) extends NullCopyrightData {
389 public function __construct(protected URI $uri)
390 {
391 }
392
393 public function hasImage(): bool
394 {
395 return true;
396 }
397
398 public function isImageLink(): bool
399 {
400 return true;
401 }
402
403 public function imageLink(): ?URI
404 {
405 return $this->uri;
406 }
407
408 public function altText(): string
409 {
410 return 'alt text';
411 }
412 };
413
414 $result = $renderer->toUIComponents($data);
415 $this->assertSame(1, count($result));
416 $this->assertInstanceOf(Icon::class, $result[0]);
417 $this->assertSame(
418 [
419 'icon_src' => 'image link',
420 'icon_alt' => 'alt text'
421 ],
422 $renderer->exposeImageData()
423 );
424 }
425
427 {
428 $uri = $this->getMockURI('image link');
429
430 $renderer = $this->getMockRenderer(
431 $this->getMockIcon(),
432 $this->getMockLink(),
433 $this->getMockLegacy(),
434 'image link'
435 );
436 $data = new class ($uri) extends NullCopyrightData {
437 public function __construct(protected URI $uri)
438 {
439 }
440
441 public function hasImage(): bool
442 {
443 return true;
444 }
445
446 public function imageFile(): string
447 {
448 return 'some string';
449 }
450
451 public function altText(): string
452 {
453 return 'alt text';
454 }
455 };
456
457 $result = $renderer->toUIComponents($data);
458 $this->assertSame(1, count($result));
459 $this->assertInstanceOf(Icon::class, $result[0]);
460 $this->assertSame(
461 [
462 'icon_src' => 'image link',
463 'icon_alt' => 'alt text'
464 ],
465 $renderer->exposeImageData()
466 );
467 }
468
470 {
471 $renderer = $this->getMockRenderer(
472 $this->getMockIcon(),
473 $this->getMockLink(),
474 $this->getMockLegacy(),
475 ''
476 );
477 $data = new class () extends NullCopyrightData {
478 public function fallBackToDefaultImage(): bool
479 {
480 return true;
481 }
482 };
483
484 $result = $renderer->toUIComponents($data);
485 $this->assertSame(1, count($result));
486 $this->assertInstanceOf(Icon::class, $result[0]);
487 $this->assertSame(
488 [
489 'icon_src' => 'fallback src',
490 'icon_alt' => ''
491 ],
492 $renderer->exposeImageData()
493 );
494 }
495
496 public function testToImageOnlyWithImageFromLink(): void
497 {
498 $uri = $this->getMockURI('image link');
499
500 $renderer = $this->getMockRenderer(
501 $this->getMockIcon(),
502 $this->getMockLink(),
503 $this->getMockLegacy(),
504 ''
505 );
506 $data = new class ($uri) extends NullCopyrightData {
507 public function __construct(protected URI $uri)
508 {
509 }
510
511 public function hasImage(): bool
512 {
513 return true;
514 }
515
516 public function isImageLink(): bool
517 {
518 return true;
519 }
520
521 public function imageLink(): ?URI
522 {
523 return $this->uri;
524 }
525
526 public function altText(): string
527 {
528 return 'alt text';
529 }
530 };
531
532 $result = $renderer->toImageOnly($data);
533 $this->assertNotNull($result);
534 $this->assertSame(
535 [
536 'icon_src' => 'image link',
537 'icon_alt' => 'alt text'
538 ],
539 $renderer->exposeImageData()
540 );
541 }
542
543 public function testToImageOnlyWithImageFromIRSS(): void
544 {
545 $uri = $this->getMockURI('image link');
546
547 $renderer = $this->getMockRenderer(
548 $this->getMockIcon(),
549 $this->getMockLink(),
550 $this->getMockLegacy(),
551 'image link'
552 );
553 $data = new class ($uri) extends NullCopyrightData {
554 public function __construct(protected URI $uri)
555 {
556 }
557
558 public function hasImage(): bool
559 {
560 return true;
561 }
562
563 public function imageFile(): string
564 {
565 return 'some string';
566 }
567
568 public function altText(): string
569 {
570 return 'alt text';
571 }
572 };
573
574 $result = $renderer->toImageOnly($data);
575 $this->assertNotNull($result);
576 $this->assertSame(
577 [
578 'icon_src' => 'image link',
579 'icon_alt' => 'alt text'
580 ],
581 $renderer->exposeImageData()
582 );
583 }
584
585 public function testToImageOnlyWithFallbackImage(): void
586 {
587 $renderer = $this->getMockRenderer(
588 $this->getMockIcon(),
589 $this->getMockLink(),
590 $this->getMockLegacy(),
591 ''
592 );
593 $data = new class () extends NullCopyrightData {
594 public function fallBackToDefaultImage(): bool
595 {
596 return true;
597 }
598 };
599
600 $result = $renderer->toImageOnly($data);
601 $this->assertNotNull($result);
602 $this->assertSame(
603 [
604 'icon_src' => 'fallback src',
605 'icon_alt' => ''
606 ],
607 $renderer->exposeImageData()
608 );
609 }
610
611 public function testToImageOnlyWithoutImage(): void
612 {
613 $renderer = $this->getMockRenderer(
614 $this->getMockIcon(),
615 $this->getMockLink(),
616 $this->getMockLegacy(),
617 ''
618 );
619 $data = new NullCopyrightData();
620
621 $result = $renderer->toImageOnly($data);
622 $this->assertNull($result);
623 }
624
625 public function testToLinkOnly(): void
626 {
627 $link = $this->getMockLink();
628 $uri = $this->getMockURI('link');
629
630 $renderer = $this->getMockRenderer(
631 $this->getMockIcon(),
632 $link,
633 $this->getMockLegacy(),
634 ''
635 );
636 $data = new class ($uri) extends NullCopyrightData {
637 public function __construct(
638 protected URI $uri
639 ) {
640 }
641
642 public function fullName(): string
643 {
644 return 'full name';
645 }
646
647 public function link(): ?URI
648 {
649 return $this->uri;
650 }
651 };
652
653 $result = $renderer->toLinkOnly($data);
654 $this->assertNotNull($result);
655 $this->assertSame(
656 [
657 'link_label' => 'full name',
658 'link_action' => 'link',
659 'link_relationship' => Relationship::LICENSE,
660 'link_disabled' => false
661 ],
662 $renderer->exposeLinkData()
663 );
664 }
665
667 {
668 $renderer = $this->getMockRenderer(
669 $this->getMockIcon(),
670 $this->getMockLink(),
671 $this->getMockLegacy(),
672 ''
673 );
674
675 $result = $renderer->toLinkOnly(new NullCopyrightData());
676 $this->assertNull($result);
677 }
678
679 public function testToLinkOnlyWithoutLink(): void
680 {
681 $link = $this->getMockLink();
682 $renderer = $this->getMockRenderer(
683 $this->getMockIcon(),
684 $link,
685 $this->getMockLegacy(),
686 ''
687 );
688 $data = new class () extends NullCopyrightData {
689 public function fullName(): string
690 {
691 return 'full name';
692 }
693 };
694
695 $result = $renderer->toLinkOnly($data);
696 $this->assertNotNull($result);
697 $this->assertSame(
698 [
699 'link_label' => 'full name',
700 'link_action' => '',
701 'link_relationship' => null,
702 'link_disabled' => true
703 ],
704 $renderer->exposeLinkData()
705 );
706 }
707
708 public function testToLinkOnlyWithoutFullName(): void
709 {
710 $link = $this->getMockLink();
711 $uri = $this->getMockURI('link');
712
713 $renderer = $this->getMockRenderer(
714 $this->getMockIcon(),
715 $link,
716 $this->getMockLegacy(),
717 ''
718 );
719 $data = new class ($uri) extends NullCopyrightData {
720 public function __construct(protected URI $uri)
721 {
722 }
723
724 public function link(): ?URI
725 {
726 return $this->uri;
727 }
728 };
729
730 $result = $renderer->toLinkOnly($data);
731 $this->assertNotNull($result);
732 $this->assertSame(
733 [
734 'link_label' => 'link',
735 'link_action' => 'link',
736 'link_relationship' => Relationship::LICENSE,
737 'link_disabled' => false
738 ],
739 $renderer->exposeLinkData()
740 );
741 }
742
743 public function testCopyrightAsStringHasFullName(): void
744 {
745 $renderer = $this->getMockRenderer(
746 $this->getMockIcon(),
747 $this->getMockLink(),
748 $this->getMockLegacy(),
749 ''
750 );
751 $data = new class () extends NullCopyrightData {
752 public function fullName(): string
753 {
754 return 'full name of copyright';
755 }
756
757 public function link(): ?URI
758 {
759 return null;
760 }
761 };
762
763 $this->assertSame(
764 'full name of copyright',
765 $renderer->toString($data)
766 );
767 }
768
769 public function testCopyrightAsStringHasLink(): void
770 {
771 $renderer = $this->getMockRenderer(
772 $this->getMockIcon(),
773 $this->getMockLink(),
774 $this->getMockLegacy(),
775 ''
776 );
777 $uri = $this->getMockURI('http://www.example2.com');
778 $data = new class ($uri) extends NullCopyrightData {
779 public function __construct(protected URI $uri)
780 {
781 }
782
783 public function fullName(): string
784 {
785 return '';
786 }
787
788 public function link(): ?URI
789 {
790 return $this->uri;
791 }
792 };
793
794 $this->assertSame(
795 'http://www.example2.com',
796 $renderer->toString($data)
797 );
798 }
799
801 {
802 $renderer = $this->getMockRenderer(
803 $this->getMockIcon(),
804 $this->getMockLink(),
805 $this->getMockLegacy(),
806 ''
807 );
808 $uri = $this->getMockURI('http://www.example2.com');
809 $data = new class ($uri) extends NullCopyrightData {
810 public function __construct(protected URI $uri)
811 {
812 }
813
814 public function fullName(): string
815 {
816 return 'full name of copyright';
817 }
818
819 public function link(): ?URI
820 {
821 return $this->uri;
822 }
823 };
824
825 $this->assertSame(
826 'full name of copyright http://www.example2.com',
827 $renderer->toString($data)
828 );
829 }
830
832 {
833 $renderer = $this->getMockRenderer(
834 $this->getMockIcon(),
835 $this->getMockLink(),
836 $this->getMockLegacy(),
837 ''
838 );
839 $data = new class () extends NullCopyrightData {
840 public function fullName(): string
841 {
842 return '';
843 }
844
845 public function link(): ?URI
846 {
847 return null;
848 }
849 };
850
851 $this->assertSame(
852 '',
853 $renderer->toString($data)
854 );
855 }
856}
$renderer
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
getMockRenderer(Icon $icon, Link $link, Content $legacy, string $src_from_irss)
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
link(string $caption, string $href, bool $new_viewport=false)
$text
Definition: xapiexit.php:21