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