‪TYPO3CMS  11.5
ImageContentObjectTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\MockObject\MockObject;
21 use Prophecy\PhpUnit\ProphecyTrait;
26 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
29 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
32 class ‪ImageContentObjectTest extends UnitTestCase
33 {
34  use ProphecyTrait;
35 
39  protected ‪$resetSingletonInstances = true;
40 
44  protected ‪$subject;
45 
49  protected function ‪setUp(): void
50  {
51  parent::setUp();
52  $tsfe = $this->prophesize(TypoScriptFrontendController::class);
53  ‪$GLOBALS['TSFE'] = $tsfe->reveal();
54  $contentObjectRenderer = new ContentObjectRenderer($tsfe->reveal());
55  $this->subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$contentObjectRenderer]);
56  }
57 
62  {
63  return [
64  [null, null],
65  ['', null],
66  ['', []],
67  ['fooo', ['foo' => 'bar']],
68  ];
69  }
70 
79  public function ‪getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFound($key, $configuration): void
80  {
81  $defaultImgTagTemplate = '<img src="###SRC###" width="###WIDTH###" height="###HEIGHT###" ###PARAMS### ###ALTPARAMS### ###BORDER######SELFCLOSINGTAGSLASH###>';
82  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [], '', false);
83  $result = ‪$subject->_call('getImageTagTemplate', $key, $configuration);
84  self::assertEquals($result, $defaultImgTagTemplate);
85  }
86 
91  {
92  return [
93  [
94  'foo',
95  [
96  'layout.' => [
97  'foo.' => [
98  'element' => '<img src="###SRC###" srcset="###SOURCES###" ###PARAMS### ###ALTPARAMS### ###FOOBAR######SELFCLOSINGTAGSLASH###>',
99  ],
100  ],
101  ],
102  '<img src="###SRC###" srcset="###SOURCES###" ###PARAMS### ###ALTPARAMS### ###FOOBAR######SELFCLOSINGTAGSLASH###>',
103  ],
104 
105  ];
106  }
107 
117  public function ‪getImageTagTemplateReturnTemplateElementIdentifiedByKey(string $key, array $configuration, string $expectation): void
118  {
119  $result = $this->subject->_call('getImageTagTemplate', $key, $configuration);
120  self::assertEquals($result, $expectation);
121  }
122 
127  {
128  return [
129  [null, null, null],
130  ['foo', null, null],
131  ['foo', ['sourceCollection.' => 1], 'bar'],
132  ];
133  }
134 
145  ?string $layoutKey,
146  ?array $configuration,
147  ?string $file
148  ): void {
149  $result = $this->subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
150  self::assertSame($result, '');
151  }
152 
159  {
160  $cObj = $this->getMockBuilder(ContentObjectRenderer::class)
161  ->onlyMethods(['stdWrap', 'getImgResource'])
162  ->getMock();
163 
164  $cObj->start([], 'tt_content');
165 
166  $layoutKey = 'test';
167 
168  $configuration = [
169  'layoutKey' => 'test',
170  'layout.' => [
171  'test.' => [
172  'element' => '<img ###SRC### ###SRCCOLLECTION### ###SELFCLOSINGTAGSLASH###>',
173  'source' => '---###SRC###---',
174  ],
175  ],
176  'sourceCollection.' => [
177  '1.' => [
178  'width' => '200',
179  ],
180  ],
181  ];
182 
183  $file = 'testImageName';
184 
185  // Avoid calling of stdWrap
186  $cObj
187  ->method('stdWrap')
188  ->willReturnArgument(0);
189 
190  // Avoid calling of imgResource
191  $cObj
192  ->expects(self::once())
193  ->method('getImgResource')
194  ->with(self::equalTo('testImageName'))
195  ->willReturn([100, 100, null, 'bar']);
196 
197  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
198  $result = ‪$subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
199 
200  self::assertEquals('---bar---', $result);
201  }
202 
210  {
211  $sourceCollectionArray = [
212  'small.' => [
213  'width' => 200,
214  'srcsetCandidate' => '600w',
215  'mediaQuery' => '(max-device-width: 600px)',
216  'dataKey' => 'small',
217  ],
218  'smallRetina.' => [
219  'if.directReturn' => 0,
220  'width' => 200,
221  'pixelDensity' => '2',
222  'srcsetCandidate' => '600w 2x',
223  'mediaQuery' => '(max-device-width: 600px) AND (min-resolution: 192dpi)',
224  'dataKey' => 'smallRetina',
225  ],
226  ];
227  return [
228  [
229  'default',
230  [
231  'layoutKey' => 'default',
232  'layout.' => [
233  'default.' => [
234  'element' => '<img src="###SRC###" width="###WIDTH###" height="###HEIGHT###" ###PARAMS### ###ALTPARAMS### ###BORDER######SELFCLOSINGTAGSLASH###>',
235  'source' => '',
236  ],
237  ],
238  'sourceCollection.' => $sourceCollectionArray,
239  ],
240  ],
241  ];
242  }
243 
252  public function ‪getImageSourceCollectionRendersDefinedLayoutKeyDefault(string $layoutKey, array $configuration): void
253  {
254  $cObj = $this->getMockBuilder(ContentObjectRenderer::class)
255  ->onlyMethods(['stdWrap', 'getImgResource'])
256  ->getMock();
257 
258  $cObj->start([], 'tt_content');
259 
260  $file = 'testImageName';
261 
262  // Avoid calling of stdWrap
263  $cObj
264  ->method('stdWrap')
265  ->willReturnArgument(0);
266 
267  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
268  $result = ‪$subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
269 
270  self::assertEmpty($result);
271  }
272 
280  {
281  $sourceCollectionArray = [
282  'small.' => [
283  'width' => 200,
284  'srcsetCandidate' => '600w',
285  'mediaQuery' => '(max-device-width: 600px)',
286  'dataKey' => 'small',
287  ],
288  'smallRetina.' => [
289  'if.directReturn' => 1,
290  'width' => 200,
291  'pixelDensity' => '2',
292  'srcsetCandidate' => '600w 2x',
293  'mediaQuery' => '(max-device-width: 600px) AND (min-resolution: 192dpi)',
294  'dataKey' => 'smallRetina',
295  ],
296  ];
297  return [
298  [
299  'srcset',
300  [
301  'layoutKey' => 'srcset',
302  'layout.' => [
303  'srcset.' => [
304  'element' => '<img src="###SRC###" srcset="###SOURCECOLLECTION###" ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###>',
305  'source' => '|*|###SRC### ###SRCSETCANDIDATE###,|*|###SRC### ###SRCSETCANDIDATE###',
306  ],
307  ],
308  'sourceCollection.' => $sourceCollectionArray,
309  ],
310  'xhtml_strict',
311  'bar-file.jpg 600w,bar-file.jpg 600w 2x',
312  ],
313  [
314  'picture',
315  [
316  'layoutKey' => 'picture',
317  'layout.' => [
318  'picture.' => [
319  'element' => '<picture>###SOURCECOLLECTION###<img src="###SRC###" ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###></picture>',
320  'source' => '<source src="###SRC###" media="###MEDIAQUERY###"###SELFCLOSINGTAGSLASH###>',
321  ],
322  ],
323  'sourceCollection.' => $sourceCollectionArray,
324  ],
325  'xhtml_strict',
326  '<source src="bar-file.jpg" media="(max-device-width: 600px)" /><source src="bar-file.jpg" media="(max-device-width: 600px) AND (min-resolution: 192dpi)" />',
327  ],
328  [
329  'picture',
330  [
331  'layoutKey' => 'picture',
332  'layout.' => [
333  'picture.' => [
334  'element' => '<picture>###SOURCECOLLECTION###<img src="###SRC###" ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###></picture>',
335  'source' => '<source src="###SRC###" media="###MEDIAQUERY###"###SELFCLOSINGTAGSLASH###>',
336  ],
337  ],
338  'sourceCollection.' => $sourceCollectionArray,
339  ],
340  '',
341  '<source src="bar-file.jpg" media="(max-device-width: 600px)"><source src="bar-file.jpg" media="(max-device-width: 600px) AND (min-resolution: 192dpi)">',
342  ],
343  [
344  'data',
345  [
346  'layoutKey' => 'data',
347  'layout.' => [
348  'data.' => [
349  'element' => '<img src="###SRC###" ###SOURCECOLLECTION### ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###>',
350  'source' => 'data-###DATAKEY###="###SRC###"',
351  ],
352  ],
353  'sourceCollection.' => $sourceCollectionArray,
354  ],
355  'xhtml_strict',
356  'data-small="bar-file.jpg"data-smallRetina="bar-file.jpg"',
357  ],
358  ];
359  }
360 
372  string $layoutKey,
373  array $configuration,
374  string $xhtmlDoctype,
375  string $expectedHtml
376  ): void {
377  $cObj = $this->getMockBuilder(ContentObjectRenderer::class)
378  ->onlyMethods(['stdWrap', 'getImgResource'])
379  ->getMock();
380 
381  $cObj->start([], 'tt_content');
382 
383  $file = 'testImageName';
384 
385  ‪$GLOBALS['TSFE']->xhtmlDoctype = $xhtmlDoctype;
386 
387  // Avoid calling of stdWrap
388  $cObj
389  ->method('stdWrap')
390  ->willReturnArgument(0);
391 
392  // Avoid calling of imgResource
393  $cObj
394  ->expects(self::exactly(2))
395  ->method('getImgResource')
396  ->with(self::equalTo('testImageName'))
397  ->willReturn([100, 100, null, 'bar-file.jpg']);
398 
399  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
400  $result = ‪$subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
401 
402  self::assertEquals($expectedHtml, $result);
403  }
404 
410  public function ‪getImageSourceCollectionHookCalled(): void
411  {
412  $cObj = $this->getAccessibleMock(
413  ContentObjectRenderer::class,
414  ['getResourceFactory', 'stdWrap', 'getImgResource']
415  );
416  $cObj->start([], 'tt_content');
417 
418  // Avoid calling stdwrap and getImgResource
419  $cObj
420  ->method('stdWrap')
421  ->willReturnArgument(0);
422 
423  $cObj
424  ->method('getImgResource')
425  ->willReturn([100, 100, null, 'bar-file.jpg']);
426 
427  $resourceFactory = $this->createMock(ResourceFactory::class);
428  $cObj->method('getResourceFactory')->willReturn($resourceFactory);
429 
430  $className = ‪StringUtility::getUniqueId('tx_coretest_getImageSourceCollectionHookCalled');
431  $getImageSourceCollectionHookMock = $this->getMockBuilder(
432  ContentObjectOneSourceCollectionHookInterface::class
433  )
434  ->onlyMethods(['getOneSourceCollection'])
435  ->setMockClassName($className)
436  ->getMock();
437  GeneralUtility::addInstance($className, $getImageSourceCollectionHookMock);
438  ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['getImageSourceCollection'][] = $className;
439 
440  $getImageSourceCollectionHookMock
441  ->expects(self::once())
442  ->method('getOneSourceCollection')
443  ->willReturnCallback([$this, 'isGetOneSourceCollectionCalledCallback']);
444 
445  $configuration = [
446  'layoutKey' => 'data',
447  'layout.' => [
448  'data.' => [
449  'element' => '<img src="###SRC###" ###SOURCECOLLECTION### ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###>',
450  'source' => 'data-###DATAKEY###="###SRC###"',
451  ],
452  ],
453  'sourceCollection.' => [
454  'small.' => [
455  'width' => 200,
456  'srcsetCandidate' => '600w',
457  'mediaQuery' => '(max-device-width: 600px)',
458  'dataKey' => 'small',
459  ],
460  ],
461  ];
462 
463  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
464  $result = ‪$subject->_call('getImageSourceCollection', 'data', $configuration, ‪StringUtility::getUniqueId('testImage-'));
465 
466  self::assertSame($result, 'isGetOneSourceCollectionCalledCallback');
467  }
468 
478  array $sourceRenderConfiguration,
479  array $sourceConfiguration
480  ): string {
481  self::assertIsArray($sourceRenderConfiguration);
482  self::assertIsArray($sourceConfiguration);
483  return 'isGetOneSourceCollectionCalledCallback';
484  }
485 
491  public function ‪linkWrapDataProvider(): array
492  {
493  $content = ‪StringUtility::getUniqueId();
494  return [
495  'Handles a tag as wrap.' => [
496  '<tag>' . $content . '</tag>',
497  $content,
498  '<tag>|</tag>',
499  ],
500  'Handles simple text as wrap.' => [
501  'alpha' . $content . 'omega',
502  $content,
503  'alpha|omega',
504  ],
505  'Trims whitespace around tags.' => [
506  '<tag>' . $content . '</tag>',
507  $content,
508  "\t <tag>\t |\t </tag>\t ",
509  ],
510  'A wrap without pipe is placed before the content.' => [
511  '<tag>' . $content,
512  $content,
513  '<tag>',
514  ],
515  'For an empty string as wrap the content is returned as is.' => [
516  $content,
517  $content,
518  '',
519  ],
520  'For a valid rootline level the uid will be inserted.' => [
521  '<a href="?id=55">' . $content . '</a>',
522  $content,
523  '<a href="?id={3}"> | </a>',
524  ],
525  'For an invalid rootline level there is no replacement.' => [
526  '<a href="?id={4}">' . $content . '</a>',
527  $content,
528  '<a href="?id={4}"> | </a>',
529  ],
530  ];
531  }
532 
542  public function ‪linkWrap(string $expected, string $content, $wrap): void
543  {
544  ‪$GLOBALS['TSFE']->tmpl = new \stdClass();
545  ‪$GLOBALS['TSFE']->tmpl->rootLine = [3 => ['uid' => 55]];
546  $actual = $this->subject->_call('linkWrap', $content, $wrap);
547  self::assertEquals($expected, $actual);
548  }
549 }
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefined
‪getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefined(?string $layoutKey, ?array $configuration, ?string $file)
Definition: ImageContentObjectTest.php:141
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFoundDataProvider
‪array getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFoundDataProvider()
Definition: ImageContentObjectTest.php:58
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyDataDataProvider
‪array getImageSourceCollectionRendersDefinedLayoutKeyDataDataProvider()
Definition: ImageContentObjectTest.php:276
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\isGetOneSourceCollectionCalledCallback
‪string isGetOneSourceCollectionCalledCallback(array $sourceRenderConfiguration, array $sourceConfiguration)
Definition: ImageContentObjectTest.php:474
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFound
‪getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFound($key, $configuration)
Definition: ImageContentObjectTest.php:76
‪TYPO3\CMS\Frontend\ContentObject\ImageContentObject
Definition: ImageContentObject.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyDataDefaultProvider
‪array getImageSourceCollectionRendersDefinedLayoutKeyDataDefaultProvider()
Definition: ImageContentObjectTest.php:206
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionHookCalled
‪getImageSourceCollectionHookCalled()
Definition: ImageContentObjectTest.php:407
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyData
‪getImageSourceCollectionRendersDefinedLayoutKeyData(string $layoutKey, array $configuration, string $xhtmlDoctype, string $expectedHtml)
Definition: ImageContentObjectTest.php:368
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefinedDataProvider
‪array getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefinedDataProvider()
Definition: ImageContentObjectTest.php:123
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageContentObjectTest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateReturnTemplateElementIdentifiedByKey
‪getImageTagTemplateReturnTemplateElementIdentifiedByKey(string $key, array $configuration, string $expectation)
Definition: ImageContentObjectTest.php:114
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\setUp
‪setUp()
Definition: ImageContentObjectTest.php:46
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedSources
‪getImageSourceCollectionRendersDefinedSources()
Definition: ImageContentObjectTest.php:155
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\$subject
‪ImageContentObject MockObject AccessibleObjectInterface $subject
Definition: ImageContentObjectTest.php:41
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyDefault
‪getImageSourceCollectionRendersDefinedLayoutKeyDefault(string $layoutKey, array $configuration)
Definition: ImageContentObjectTest.php:249
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject
Definition: CaseContentObjectTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest
Definition: ImageContentObjectTest.php:33
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectOneSourceCollectionHookInterface
Definition: ContentObjectOneSourceCollectionHookInterface.php:22
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateReturnTemplateElementIdentifiedByKeyDataProvider
‪array getImageTagTemplateReturnTemplateElementIdentifiedByKeyDataProvider()
Definition: ImageContentObjectTest.php:87
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\linkWrapDataProvider
‪array linkWrapDataProvider()
Definition: ImageContentObjectTest.php:488
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\linkWrap
‪linkWrap(string $expected, string $content, $wrap)
Definition: ImageContentObjectTest.php:539
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22