‪TYPO3CMS  10.4
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 
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
29 class ‪ImageContentObjectTest extends UnitTestCase
30 {
34  protected ‪$resetSingletonInstances = true;
35 
39  protected ‪$subject;
40 
44  protected function ‪setUp(): void
45  {
46  parent::setUp();
47  $tsfe = $this->prophesize(TypoScriptFrontendController::class);
48  ‪$GLOBALS['TSFE'] = $tsfe->reveal();
49  $contentObjectRenderer = new ‪ContentObjectRenderer($tsfe->reveal());
50  $this->subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$contentObjectRenderer]);
51  }
52 
57  {
58  return [
59  [null, null],
60  ['', null],
61  ['', []],
62  ['fooo', ['foo' => 'bar']]
63  ];
64  }
65 
74  public function ‪getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFound($key, $configuration): void
75  {
76  $defaultImgTagTemplate = '<img src="###SRC###" width="###WIDTH###" height="###HEIGHT###" ###PARAMS### ###ALTPARAMS### ###BORDER######SELFCLOSINGTAGSLASH###>';
77  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [], '', false);
78  $result = ‪$subject->_call('getImageTagTemplate', $key, $configuration);
79  self::assertEquals($result, $defaultImgTagTemplate);
80  }
81 
86  {
87  return [
88  [
89  'foo',
90  [
91  'layout.' => [
92  'foo.' => [
93  'element' => '<img src="###SRC###" srcset="###SOURCES###" ###PARAMS### ###ALTPARAMS### ###FOOBAR######SELFCLOSINGTAGSLASH###>'
94  ]
95  ]
96  ],
97  '<img src="###SRC###" srcset="###SOURCES###" ###PARAMS### ###ALTPARAMS### ###FOOBAR######SELFCLOSINGTAGSLASH###>'
98  ]
99 
100  ];
101  }
102 
112  public function ‪getImageTagTemplateReturnTemplateElementIdentifiedByKey($key, $configuration, $expectation): void
113  {
114  $result = $this->subject->_call('getImageTagTemplate', $key, $configuration);
115  self::assertEquals($result, $expectation);
116  }
117 
122  {
123  return [
124  [null, null, null],
125  ['foo', null, null],
126  ['foo', ['sourceCollection.' => 1], 'bar']
127  ];
128  }
129 
140  $layoutKey,
141  $configuration,
142  $file
143  ): void {
144  $result = $this->subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
145  self::assertSame($result, '');
146  }
147 
154  {
156  $cObj = $this->getMockBuilder(ContentObjectRenderer::class)
157  ->setMethods(['stdWrap', 'getImgResource'])
158  ->getMock();
159 
160  $cObj->start([], 'tt_content');
161 
162  $layoutKey = 'test';
163 
164  $configuration = [
165  'layoutKey' => 'test',
166  'layout.' => [
167  'test.' => [
168  'element' => '<img ###SRC### ###SRCCOLLECTION### ###SELFCLOSINGTAGSLASH###>',
169  'source' => '---###SRC###---'
170  ]
171  ],
172  'sourceCollection.' => [
173  '1.' => [
174  'width' => '200'
175  ]
176  ]
177  ];
178 
179  $file = 'testImageName';
180 
181  // Avoid calling of stdWrap
182  $cObj
183  ->expects(self::any())
184  ->method('stdWrap')
185  ->willReturnArgument(0);
186 
187  // Avoid calling of imgResource
188  $cObj
189  ->expects(self::exactly(1))
190  ->method('getImgResource')
191  ->with(self::equalTo('testImageName'))
192  ->willReturn([100, 100, null, 'bar']);
193 
194  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
195  $result = ‪$subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
196 
197  self::assertEquals('---bar---', $result);
198  }
199 
207  {
208  $sourceCollectionArray = [
209  'small.' => [
210  'width' => 200,
211  'srcsetCandidate' => '600w',
212  'mediaQuery' => '(max-device-width: 600px)',
213  'dataKey' => 'small',
214  ],
215  'smallRetina.' => [
216  'if.directReturn' => 0,
217  'width' => 200,
218  'pixelDensity' => '2',
219  'srcsetCandidate' => '600w 2x',
220  'mediaQuery' => '(max-device-width: 600px) AND (min-resolution: 192dpi)',
221  'dataKey' => 'smallRetina',
222  ]
223  ];
224  return [
225  [
226  'default',
227  [
228  'layoutKey' => 'default',
229  'layout.' => [
230  'default.' => [
231  'element' => '<img src="###SRC###" width="###WIDTH###" height="###HEIGHT###" ###PARAMS### ###ALTPARAMS### ###BORDER######SELFCLOSINGTAGSLASH###>',
232  'source' => ''
233  ]
234  ],
235  'sourceCollection.' => $sourceCollectionArray
236  ]
237  ],
238  ];
239  }
240 
249  public function ‪getImageSourceCollectionRendersDefinedLayoutKeyDefault($layoutKey, $configuration): void
250  {
252  $cObj = $this->getMockBuilder(ContentObjectRenderer::class)
253  ->setMethods(['stdWrap', 'getImgResource'])
254  ->getMock();
255 
256  $cObj->start([], 'tt_content');
257 
258  $file = 'testImageName';
259 
260  // Avoid calling of stdWrap
261  $cObj
262  ->expects(self::any())
263  ->method('stdWrap')
264  ->willReturnArgument(0);
265 
266  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
267  $result = ‪$subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
268 
269  self::assertEmpty($result);
270  }
271 
279  {
280  $sourceCollectionArray = [
281  'small.' => [
282  'width' => 200,
283  'srcsetCandidate' => '600w',
284  'mediaQuery' => '(max-device-width: 600px)',
285  'dataKey' => 'small',
286  ],
287  'smallRetina.' => [
288  'if.directReturn' => 1,
289  'width' => 200,
290  'pixelDensity' => '2',
291  'srcsetCandidate' => '600w 2x',
292  'mediaQuery' => '(max-device-width: 600px) AND (min-resolution: 192dpi)',
293  'dataKey' => 'smallRetina',
294  ]
295  ];
296  return [
297  [
298  'srcset',
299  [
300  'layoutKey' => 'srcset',
301  'layout.' => [
302  'srcset.' => [
303  'element' => '<img src="###SRC###" srcset="###SOURCECOLLECTION###" ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###>',
304  'source' => '|*|###SRC### ###SRCSETCANDIDATE###,|*|###SRC### ###SRCSETCANDIDATE###'
305  ]
306  ],
307  'sourceCollection.' => $sourceCollectionArray
308  ],
309  'xhtml_strict',
310  'bar-file.jpg 600w,bar-file.jpg 600w 2x',
311  ],
312  [
313  'picture',
314  [
315  'layoutKey' => 'picture',
316  'layout.' => [
317  'picture.' => [
318  'element' => '<picture>###SOURCECOLLECTION###<img src="###SRC###" ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###></picture>',
319  'source' => '<source src="###SRC###" media="###MEDIAQUERY###"###SELFCLOSINGTAGSLASH###>'
320  ]
321  ],
322  'sourceCollection.' => $sourceCollectionArray,
323  ],
324  'xhtml_strict',
325  '<source src="bar-file.jpg" media="(max-device-width: 600px)" /><source src="bar-file.jpg" media="(max-device-width: 600px) AND (min-resolution: 192dpi)" />',
326  ],
327  [
328  'picture',
329  [
330  'layoutKey' => 'picture',
331  'layout.' => [
332  'picture.' => [
333  'element' => '<picture>###SOURCECOLLECTION###<img src="###SRC###" ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###></picture>',
334  'source' => '<source src="###SRC###" media="###MEDIAQUERY###"###SELFCLOSINGTAGSLASH###>'
335  ]
336  ],
337  'sourceCollection.' => $sourceCollectionArray,
338  ],
339  '',
340  '<source src="bar-file.jpg" media="(max-device-width: 600px)"><source src="bar-file.jpg" media="(max-device-width: 600px) AND (min-resolution: 192dpi)">',
341  ],
342  [
343  'data',
344  [
345  'layoutKey' => 'data',
346  'layout.' => [
347  'data.' => [
348  'element' => '<img src="###SRC###" ###SOURCECOLLECTION### ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###>',
349  'source' => 'data-###DATAKEY###="###SRC###"'
350  ]
351  ],
352  'sourceCollection.' => $sourceCollectionArray
353  ],
354  'xhtml_strict',
355  'data-small="bar-file.jpg"data-smallRetina="bar-file.jpg"',
356  ],
357  ];
358  }
359 
371  $layoutKey,
372  $configuration,
373  $xhtmlDoctype,
374  $expectedHtml
375  ): void {
377  $cObj = $this->getMockBuilder(ContentObjectRenderer::class)
378  ->setMethods(['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  ->expects(self::any())
390  ->method('stdWrap')
391  ->willReturnArgument(0);
392 
393  // Avoid calling of imgResource
394  $cObj
395  ->expects(self::exactly(2))
396  ->method('getImgResource')
397  ->with(self::equalTo('testImageName'))
398  ->willReturn([100, 100, null, 'bar-file.jpg']);
399 
400  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
401  $result = ‪$subject->_call('getImageSourceCollection', $layoutKey, $configuration, $file);
402 
403  self::assertEquals($expectedHtml, $result);
404  }
405 
411  public function ‪getImageSourceCollectionHookCalled(): void
412  {
413  $cObj = $this->getAccessibleMock(
414  ContentObjectRenderer::class,
415  ['getResourceFactory', 'stdWrap', 'getImgResource']
416  );
417  $cObj->start([], 'tt_content');
418 
419  // Avoid calling stdwrap and getImgResource
420  $cObj->expects(self::any())
421  ->method('stdWrap')
422  ->willReturnArgument(0);
423 
424  $cObj->expects(self::any())
425  ->method('getImgResource')
426  ->willReturn([100, 100, null, 'bar-file.jpg']);
427 
428  $resourceFactory = $this->createMock(ResourceFactory::class);
429  $cObj->expects(self::any())->method('getResourceFactory')->willReturn($resourceFactory);
430 
431  $className = ‪StringUtility::getUniqueId('tx_coretest_getImageSourceCollectionHookCalled');
432  $getImageSourceCollectionHookMock = $this->getMockBuilder(
433  ContentObjectOneSourceCollectionHookInterface::class
434  )
435  ->setMethods(['getOneSourceCollection'])
436  ->setMockClassName($className)
437  ->getMock();
438  GeneralUtility::addInstance($className, $getImageSourceCollectionHookMock);
439  ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_content.php']['getImageSourceCollection'][] = $className;
440 
441  $getImageSourceCollectionHookMock
442  ->expects(self::exactly(1))
443  ->method('getOneSourceCollection')
444  ->willReturnCallback([$this, 'isGetOneSourceCollectionCalledCallback']);
445 
446  $configuration = [
447  'layoutKey' => 'data',
448  'layout.' => [
449  'data.' => [
450  'element' => '<img src="###SRC###" ###SOURCECOLLECTION### ###PARAMS### ###ALTPARAMS######SELFCLOSINGTAGSLASH###>',
451  'source' => 'data-###DATAKEY###="###SRC###"'
452  ]
453  ],
454  'sourceCollection.' => [
455  'small.' => [
456  'width' => 200,
457  'srcsetCandidate' => '600w',
458  'mediaQuery' => '(max-device-width: 600px)',
459  'dataKey' => 'small',
460  ],
461  ],
462  ];
463 
464  ‪$subject = $this->getAccessibleMock(ImageContentObject::class, ['dummy'], [$cObj]);
465  $result = ‪$subject->_call('getImageSourceCollection', 'data', $configuration, ‪StringUtility::getUniqueId('testImage-'));
466 
467  self::assertSame($result, 'isGetOneSourceCollectionCalledCallback');
468  }
469 
481  array $sourceRenderConfiguration,
482  array $sourceConfiguration,
483  $oneSourceCollection,
484  $parent
485  ): string {
486  self::assertTrue(is_array($sourceRenderConfiguration));
487  self::assertTrue(is_array($sourceConfiguration));
488  return 'isGetOneSourceCollectionCalledCallback';
489  }
490 
496  public function ‪linkWrapDataProvider(): array
497  {
498  $content = ‪StringUtility::getUniqueId();
499  return [
500  'Handles a tag as wrap.' => [
501  '<tag>' . $content . '</tag>',
502  $content,
503  '<tag>|</tag>'
504  ],
505  'Handles simple text as wrap.' => [
506  'alpha' . $content . 'omega',
507  $content,
508  'alpha|omega'
509  ],
510  'Trims whitespace around tags.' => [
511  '<tag>' . $content . '</tag>',
512  $content,
513  "\t <tag>\t |\t </tag>\t "
514  ],
515  'A wrap without pipe is placed before the content.' => [
516  '<tag>' . $content,
517  $content,
518  '<tag>'
519  ],
520  'For an empty string as wrap the content is returned as is.' => [
521  $content,
522  $content,
523  ''
524  ],
525  'For null as wrap the content is returned as is.' => [
526  $content,
527  $content,
528  null
529  ],
530  'For a valid rootline level the uid will be inserted.' => [
531  '<a href="?id=55">' . $content . '</a>',
532  $content,
533  '<a href="?id={3}"> | </a>'
534  ],
535  'For an invalid rootline level there is no replacement.' => [
536  '<a href="?id={4}">' . $content . '</a>',
537  $content,
538  '<a href="?id={4}"> | </a>'
539  ],
540  ];
541  }
542 
552  public function ‪linkWrap(string $expected, string $content, $wrap): void
553  {
554  ‪$GLOBALS['TSFE']->tmpl = new \stdClass();
555  ‪$GLOBALS['TSFE']->tmpl->rootLine = [3 => ['uid' => 55]];
556  $actual = $this->subject->_call('linkWrap', $content, $wrap);
557  self::assertEquals($expected, $actual);
558  }
559 }
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFoundDataProvider
‪array getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFoundDataProvider()
Definition: ImageContentObjectTest.php:54
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyDataDataProvider
‪array getImageSourceCollectionRendersDefinedLayoutKeyDataDataProvider()
Definition: ImageContentObjectTest.php:276
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFound
‪getImageTagTemplateFallsBackToDefaultTemplateIfNoTemplateIsFound($key, $configuration)
Definition: ImageContentObjectTest.php:72
‪TYPO3\CMS\Frontend\ContentObject\ImageContentObject
Definition: ImageContentObject.php:29
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyDataDefaultProvider
‪array getImageSourceCollectionRendersDefinedLayoutKeyDataDefaultProvider()
Definition: ImageContentObjectTest.php:204
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionHookCalled
‪getImageSourceCollectionHookCalled()
Definition: ImageContentObjectTest.php:409
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\$subject
‪ImageContentObject $subject
Definition: ImageContentObjectTest.php:37
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefinedDataProvider
‪array getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefinedDataProvider()
Definition: ImageContentObjectTest.php:119
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ImageContentObjectTest.php:33
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyData
‪getImageSourceCollectionRendersDefinedLayoutKeyData( $layoutKey, $configuration, $xhtmlDoctype, $expectedHtml)
Definition: ImageContentObjectTest.php:368
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\setUp
‪setUp()
Definition: ImageContentObjectTest.php:42
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedSources
‪getImageSourceCollectionRendersDefinedSources()
Definition: ImageContentObjectTest.php:151
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\isGetOneSourceCollectionCalledCallback
‪string isGetOneSourceCollectionCalledCallback(array $sourceRenderConfiguration, array $sourceConfiguration, $oneSourceCollection, $parent)
Definition: ImageContentObjectTest.php:478
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject
Definition: CaseContentObjectTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionRendersDefinedLayoutKeyDefault
‪getImageSourceCollectionRendersDefinedLayoutKeyDefault($layoutKey, $configuration)
Definition: ImageContentObjectTest.php:247
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest
Definition: ImageContentObjectTest.php:30
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateReturnTemplateElementIdentifiedByKey
‪getImageTagTemplateReturnTemplateElementIdentifiedByKey($key, $configuration, $expectation)
Definition: ImageContentObjectTest.php:110
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectOneSourceCollectionHookInterface
Definition: ContentObjectOneSourceCollectionHookInterface.php:22
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageTagTemplateReturnTemplateElementIdentifiedByKeyDataProvider
‪array getImageTagTemplateReturnTemplateElementIdentifiedByKeyDataProvider()
Definition: ImageContentObjectTest.php:83
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefined
‪getImageSourceCollectionReturnsEmptyStringIfNoSourcesAreDefined( $layoutKey, $configuration, $file)
Definition: ImageContentObjectTest.php:137
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\linkWrapDataProvider
‪array linkWrapDataProvider()
Definition: ImageContentObjectTest.php:494
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\Tests\Unit\ContentObject\ImageContentObjectTest\linkWrap
‪linkWrap(string $expected, string $content, $wrap)
Definition: ImageContentObjectTest.php:550
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22