ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
RendererTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Copyright;
22 
34 use ILIAS\Data\URI;
36 
37 class 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 ?string $legacy_text = null;
51 
52  public function __construct(
53  protected Icon $icon,
54  protected Link $link,
55  protected Content $legacy,
56  protected string $src_from_irss
57  ) {
58  }
59 
60  protected function getFallBackSrc(): string
61  {
62  return 'fallback src';
63  }
64 
65  protected function customIcon(string $src, string $alt): Icon
66  {
67  $this->icon_src = $src;
68  $this->icon_alt = $alt;
69  return $this->icon;
70  }
71 
72  protected function standardLink(string $label, string $action): Link
73  {
74  $this->link_label = $label;
75  $this->link_action = $action;
76  return $this->link;
77  }
78 
79  protected function textInLegacy(string $text): Content
80  {
81  $this->legacy_text = $text;
82  return $this->legacy;
83  }
84 
85  protected function getSourceFromIRSS(string $string_id): string
86  {
87  return $this->src_from_irss;
88  }
89 
90  public function exposeData(): array
91  {
92  return [
93  'icon_src' => $this->icon_src,
94  'icon_alt' => $this->icon_alt,
95  'link_label' => $this->link_label,
96  'link_action' => $this->link_action,
97  'legacy_text' => $this->legacy_text
98  ];
99  }
100  };
101  }
102 
103  protected function getMockIcon(): MockObject|Icon
104  {
105  return $this->getMockBuilder(IIcon::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  }
109 
110  protected function getMockLink(): MockObject|Link
111  {
112  return $this->getMockBuilder(ILink::class)
113  ->disableOriginalConstructor()
114  ->onlyMethods(['withAdditionalRelationshipToReferencedResource'])
115  ->getMock();
116  }
117 
118  protected function getMockLegacy(): MockObject|Content
119  {
120  return $this->getMockBuilder(ILegacy::class)
121  ->disableOriginalConstructor()
122  ->getMock();
123  }
124 
125  protected function getMockURI(string $link): URI
126  {
127  $uri = $this->getMockBuilder(URI::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130  $uri->method('__toString')->willReturn($link);
131  return $uri;
132  }
133 
134  public function testToUIComponentsWithLinkAndImage(): void
135  {
136  $link = $this->getMockLink();
137  $link->expects($this->once())
138  ->method('withAdditionalRelationshipToReferencedResource')
139  ->with(Relationship::LICENSE);
140  $uri = $this->getMockURI('link');
141  $img_uri = $this->getMockURI('image link');
142 
143  $renderer = $this->getMockRenderer(
144  $this->getMockIcon(),
145  $link,
146  $this->getMockLegacy(),
147  ''
148  );
149  $data = new class ($uri, $img_uri) extends NullCopyrightData {
150  public function __construct(
151  protected URI $uri,
152  protected URI $img_uri
153  ) {
154  }
155 
156  public function fullName(): string
157  {
158  return 'full name';
159  }
160 
161  public function link(): ?URI
162  {
163  return $this->uri;
164  }
165 
166  public function hasImage(): bool
167  {
168  return true;
169  }
170 
171  public function isImageLink(): bool
172  {
173  return true;
174  }
175 
176  public function imageLink(): ?URI
177  {
178  return $this->img_uri;
179  }
180 
181  public function altText(): string
182  {
183  return 'alt text';
184  }
185  };
186 
187  $result = $renderer->toUIComponents($data);
188  $this->assertSame(2, count($result));
189  $this->assertInstanceOf(Icon::class, $result[0]);
190  $this->assertInstanceOf(Link::class, $result[1]);
191  $this->assertSame(
192  [
193  'icon_src' => 'image link',
194  'icon_alt' => 'alt text',
195  'link_label' => 'full name',
196  'link_action' => 'link',
197  'legacy_text' => null
198  ],
199  $renderer->exposeData()
200  );
201  }
202 
203  public function testToUIComponentsEmpty(): void
204  {
205  $legacy = $this->getMockLegacy();
206 
207  $renderer = $this->getMockRenderer(
208  $this->getMockIcon(),
209  $this->getMockLink(),
210  $legacy,
211  ''
212  );
213 
214  $result = $renderer->toUIComponents(new NullCopyrightData());
215  $this->assertSame(0, count($result));
216  }
217 
218  public function testToUIComponentsWithoutLink(): void
219  {
220  $uri = $this->getMockURI('image link');
221 
222  $renderer = $this->getMockRenderer(
223  $this->getMockIcon(),
224  $this->getMockLink(),
225  $this->getMockLegacy(),
226  ''
227  );
228  $data = new class ($uri) extends NullCopyrightData {
229  public function __construct(protected URI $uri)
230  {
231  }
232 
233  public function fullName(): string
234  {
235  return 'full name';
236  }
237 
238  public function hasImage(): bool
239  {
240  return true;
241  }
242 
243  public function isImageLink(): bool
244  {
245  return true;
246  }
247 
248  public function imageLink(): ?URI
249  {
250  return $this->uri;
251  }
252 
253  public function altText(): string
254  {
255  return 'alt text';
256  }
257  };
258 
259  $result = $renderer->toUIComponents($data);
260  $this->assertSame(2, count($result));
261  $this->assertInstanceOf(Icon::class, $result[0]);
262  $this->assertInstanceOf(Content::class, $result[1]);
263  $this->assertSame(
264  [
265  'icon_src' => 'image link',
266  'icon_alt' => 'alt text',
267  'link_label' => null,
268  'link_action' => null,
269  'legacy_text' => 'full name'
270  ],
271  $renderer->exposeData()
272  );
273  }
274 
275  public function testToUIComponentsWithLinkNoImage(): void
276  {
277  $link = $this->getMockLink();
278  $link->expects($this->once())
279  ->method('withAdditionalRelationshipToReferencedResource')
280  ->with(Relationship::LICENSE);
281  $uri = $this->getMockURI('link');
282 
283  $renderer = $this->getMockRenderer(
284  $this->getMockIcon(),
285  $link,
286  $this->getMockLegacy(),
287  ''
288  );
289  $data = new class ($uri) extends NullCopyrightData {
290  public function __construct(protected URI $uri)
291  {
292  }
293 
294  public function fullName(): string
295  {
296  return 'full name';
297  }
298 
299  public function link(): ?URI
300  {
301  return $this->uri;
302  }
303  };
304 
305  $result = $renderer->toUIComponents($data);
306  $this->assertSame(1, count($result));
307  $this->assertInstanceOf(Link::class, $result[0]);
308  $this->assertSame(
309  [
310  'icon_src' => null,
311  'icon_alt' => null,
312  'link_label' => 'full name',
313  'link_action' => 'link',
314  'legacy_text' => null
315  ],
316  $renderer->exposeData()
317  );
318  }
319 
321  {
322  $link = $this->getMockLink();
323  $link->expects($this->once())
324  ->method('withAdditionalRelationshipToReferencedResource')
325  ->with(Relationship::LICENSE);
326  $uri = $this->getMockURI('link');
327 
328  $renderer = $this->getMockRenderer(
329  $this->getMockIcon(),
330  $link,
331  $this->getMockLegacy(),
332  ''
333  );
334  $data = new class ($uri) extends NullCopyrightData {
335  public function __construct(protected URI $uri)
336  {
337  }
338 
339  public function link(): ?URI
340  {
341  return $this->uri;
342  }
343  };
344 
345  $result = $renderer->toUIComponents($data);
346  $this->assertSame(1, count($result));
347  $this->assertInstanceOf(Link::class, $result[0]);
348  $this->assertSame(
349  [
350  'icon_src' => null,
351  'icon_alt' => null,
352  'link_label' => 'link',
353  'link_action' => 'link',
354  'legacy_text' => null
355  ],
356  $renderer->exposeData()
357  );
358  }
359 
360  public function testToUIComponentsWithImageFromLink(): void
361  {
362  $uri = $this->getMockURI('image link');
363 
364  $renderer = $this->getMockRenderer(
365  $this->getMockIcon(),
366  $this->getMockLink(),
367  $this->getMockLegacy(),
368  ''
369  );
370  $data = new class ($uri) extends NullCopyrightData {
371  public function __construct(protected URI $uri)
372  {
373  }
374 
375  public function hasImage(): bool
376  {
377  return true;
378  }
379 
380  public function isImageLink(): bool
381  {
382  return true;
383  }
384 
385  public function imageLink(): ?URI
386  {
387  return $this->uri;
388  }
389 
390  public function altText(): string
391  {
392  return 'alt text';
393  }
394  };
395 
396  $result = $renderer->toUIComponents($data);
397  $this->assertSame(1, count($result));
398  $this->assertInstanceOf(Icon::class, $result[0]);
399  $this->assertSame(
400  [
401  'icon_src' => 'image link',
402  'icon_alt' => 'alt text',
403  'link_label' => null,
404  'link_action' => null,
405  'legacy_text' => null
406  ],
407  $renderer->exposeData()
408  );
409  }
410 
411  public function testToUIComponentsWithImageFromIRSS(): void
412  {
413  $uri = $this->getMockURI('image link');
414 
415  $renderer = $this->getMockRenderer(
416  $this->getMockIcon(),
417  $this->getMockLink(),
418  $this->getMockLegacy(),
419  'image link'
420  );
421  $data = new class ($uri) extends NullCopyrightData {
422  public function __construct(protected URI $uri)
423  {
424  }
425 
426  public function hasImage(): bool
427  {
428  return true;
429  }
430 
431  public function imageFile(): string
432  {
433  return 'some string';
434  }
435 
436  public function altText(): string
437  {
438  return 'alt text';
439  }
440  };
441 
442  $result = $renderer->toUIComponents($data);
443  $this->assertSame(1, count($result));
444  $this->assertInstanceOf(Icon::class, $result[0]);
445  $this->assertSame(
446  [
447  'icon_src' => 'image link',
448  'icon_alt' => 'alt text',
449  'link_label' => null,
450  'link_action' => null,
451  'legacy_text' => null
452  ],
453  $renderer->exposeData()
454  );
455  }
456 
457  public function testToUIComponentsWithFallbackImage(): void
458  {
459  $renderer = $this->getMockRenderer(
460  $this->getMockIcon(),
461  $this->getMockLink(),
462  $this->getMockLegacy(),
463  ''
464  );
465  $data = new class () extends NullCopyrightData {
466  public function fallBackToDefaultImage(): bool
467  {
468  return true;
469  }
470  };
471 
472  $result = $renderer->toUIComponents($data);
473  $this->assertSame(1, count($result));
474  $this->assertInstanceOf(Icon::class, $result[0]);
475  $this->assertSame(
476  [
477  'icon_src' => 'fallback src',
478  'icon_alt' => '',
479  'link_label' => null,
480  'link_action' => null,
481  'legacy_text' => null
482  ],
483  $renderer->exposeData()
484  );
485  }
486 
487  public function testCopyrightAsStringHasFullName(): void
488  {
489  $renderer = $this->getMockRenderer(
490  $this->getMockIcon(),
491  $this->getMockLink(),
492  $this->getMockLegacy(),
493  ''
494  );
495  $data = new class () extends NullCopyrightData {
496  public function fullName(): string
497  {
498  return 'full name of copyright';
499  }
500 
501  public function link(): ?URI
502  {
503  return null;
504  }
505  };
506 
507  $this->assertSame(
508  'full name of copyright',
509  $renderer->toString($data)
510  );
511  }
512 
513  public function testCopyrightAsStringHasLink(): void
514  {
515  $renderer = $this->getMockRenderer(
516  $this->getMockIcon(),
517  $this->getMockLink(),
518  $this->getMockLegacy(),
519  ''
520  );
521  $uri = $this->getMockURI('http://www.example2.com');
522  $data = new class ($uri) extends NullCopyrightData {
523  public function __construct(protected URI $uri)
524  {
525  }
526 
527  public function fullName(): string
528  {
529  return '';
530  }
531 
532  public function link(): ?URI
533  {
534  return $this->uri;
535  }
536  };
537 
538  $this->assertSame(
539  'http://www.example2.com',
540  $renderer->toString($data)
541  );
542  }
543 
545  {
546  $renderer = $this->getMockRenderer(
547  $this->getMockIcon(),
548  $this->getMockLink(),
549  $this->getMockLegacy(),
550  ''
551  );
552  $uri = $this->getMockURI('http://www.example2.com');
553  $data = new class ($uri) extends NullCopyrightData {
554  public function __construct(protected URI $uri)
555  {
556  }
557 
558  public function fullName(): string
559  {
560  return 'full name of copyright';
561  }
562 
563  public function link(): ?URI
564  {
565  return $this->uri;
566  }
567  };
568 
569  $this->assertSame(
570  'full name of copyright http://www.example2.com',
571  $renderer->toString($data)
572  );
573  }
574 
576  {
577  $renderer = $this->getMockRenderer(
578  $this->getMockIcon(),
579  $this->getMockLink(),
580  $this->getMockLegacy(),
581  ''
582  );
583  $data = new class () extends NullCopyrightData {
584  public function fullName(): string
585  {
586  return '';
587  }
588 
589  public function link(): ?URI
590  {
591  return null;
592  }
593  };
594 
595  $this->assertSame(
596  '',
597  $renderer->toString($data)
598  );
599  }
600 }
getMockRenderer(Icon $icon, Link $link, Content $legacy, string $src_from_irss)
$renderer
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
link(string $caption, string $href, bool $new_viewport=false)