‪TYPO3CMS  ‪main
GalleryProcessorTest.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\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use PHPUnit\Framework\MockObject\MockObject;
23 use Psr\EventDispatcher\EventDispatcherInterface;
24 use Symfony\Component\DependencyInjection\Container;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32 
33 final class ‪GalleryProcessorTest extends UnitTestCase
34 {
36 
37  protected function ‪setUp(): void
38  {
39  parent::setUp();
40  $this->contentObjectRenderer = $this->getMockBuilder(ContentObjectRenderer::class)
41  ->onlyMethods([])
42  ->getMock();
43  }
44 
45  #[Test]
47  {
48  $this->expectException(ContentRenderingException::class);
49  $this->expectExceptionCode(1436809789);
50  $processor = new ‪GalleryProcessor();
51  $processor->process(
52  $this->contentObjectRenderer,
53  [],
54  [],
55  []
56  );
57  }
58 
62  public static function ‪galleryPositionDataProvider(): array
63  {
64  return [
65  'Default: horizontal above' => [
66  [],
67  [
68  'horizontal' => 'center',
69  'vertical' => 'above',
70  'noWrap' => false,
71  ],
72  ],
73  'right above' => [
74  ['mediaOrientation' => 1],
75  [
76  'horizontal' => 'right',
77  'vertical' => 'above',
78  'noWrap' => false,
79  ],
80  ],
81  'left above' => [
82  ['mediaOrientation' => 2],
83  [
84  'horizontal' => 'left',
85  'vertical' => 'above',
86  'noWrap' => false,
87  ],
88  ],
89  'center below' => [
90  ['mediaOrientation' => 8],
91  [
92  'horizontal' => 'center',
93  'vertical' => 'below',
94  'noWrap' => false,
95  ],
96  ],
97  'right below' => [
98  ['mediaOrientation' => 9],
99  [
100  'horizontal' => 'right',
101  'vertical' => 'below',
102  'noWrap' => false,
103  ],
104  ],
105  'left below' => [
106  ['mediaOrientation' => 10],
107  [
108  'horizontal' => 'left',
109  'vertical' => 'below',
110  'noWrap' => false,
111  ],
112  ],
113  'right intext' => [
114  ['mediaOrientation' => 17],
115  [
116  'horizontal' => 'right',
117  'vertical' => 'intext',
118  'noWrap' => false,
119  ],
120  ],
121  'left intext' => [
122  ['mediaOrientation' => 18],
123  [
124  'horizontal' => 'left',
125  'vertical' => 'intext',
126  'noWrap' => false,
127  ],
128  ],
129  'right intext no wrap' => [
130  ['mediaOrientation' => 25],
131  [
132  'horizontal' => 'right',
133  'vertical' => 'intext',
134  'noWrap' => true,
135  ],
136  ],
137  'left intext no wrap' => [
138  ['mediaOrientation' => 26],
139  [
140  'horizontal' => 'left',
141  'vertical' => 'intext',
142  'noWrap' => true,
143  ],
144  ],
145 
146  ];
147  }
148 
149  #[DataProvider('galleryPositionDataProvider')]
150  #[Test]
151  public function ‪galleryPositionTest(array $processorConfiguration, array $expected): void
152  {
153  $processor = new ‪GalleryProcessor();
154  $processedData = $processor->process(
155  $this->contentObjectRenderer,
156  [],
157  $processorConfiguration,
158  ['files' => []]
159  );
160 
161  self::assertEquals($expected, $processedData['gallery']['position']);
162  }
163 
164  #[Test]
165  public function ‪maxGalleryWidthTest(): void
166  {
167  $processor = new ‪GalleryProcessor();
168  $processedData = $processor->process(
169  $this->contentObjectRenderer,
170  [],
171  ['maxGalleryWidth' => 200, 'maxGalleryWidthInText' => 100],
172  ['files' => []]
173  );
174 
175  self::assertEquals(200, $processedData['gallery']['width']);
176  }
177 
178  #[Test]
179  public function ‪maxGalleryWidthWhenInTextTest(): void
180  {
181  $processor = new ‪GalleryProcessor();
182  $processedData = $processor->process(
183  $this->contentObjectRenderer,
184  [],
185  ['maxGalleryWidth' => 200, 'maxGalleryWidthInText' => 100, 'mediaOrientation' => 26],
186  ['files' => []]
187  );
188 
189  self::assertEquals(100, $processedData['gallery']['width']);
190  }
191 
195  public static function ‪countDataProvider(): array
196  {
197  return [
198  'Default settings with 3 files' => [
199  3,
200  [],
201  [],
202  [
203  'files' => 3,
204  'columns' => 1,
205  'rows' => 3,
206  ],
207  ],
208  'NumberOfColumns set by value' => [
209  3,
210  [],
211  ['numberOfColumns' => 2],
212  [
213  'files' => 3,
214  'columns' => 2,
215  'rows' => 2,
216  ],
217  ],
218  'NumberOfColumns set in data' => [
219  3,
220  ['imagecols' => 3],
221  [],
222  [
223  'files' => 3,
224  'columns' => 3,
225  'rows' => 1,
226  ],
227  ],
228  'NumberOfColumns set in custom data field' => [
229  6,
230  ['my_imagecols' => 4],
231  ['numberOfColumns.' => [
232  'field' => 'my_imagecols',
233  ]],
234  [
235  'files' => 6,
236  'columns' => 4,
237  'rows' => 2,
238  ],
239  ],
240  ];
241  }
242 
243  #[DataProvider('countDataProvider')]
244  #[Test]
245  public function ‪countResultTest(int $numberOfFiles, array $data, array $processorConfiguration, array $expected): void
246  {
247  $files = [];
248  for ($i = 0; $i < $numberOfFiles; $i++) {
249  $files[] = $this->createMock(FileReference::class);
250  }
251  $this->contentObjectRenderer->data = $data;
252  $container = new Container();
253  $container->set(EventDispatcherInterface::class, new ‪NoopEventDispatcher());
254  GeneralUtility::setContainer($container);
255  $processor = new ‪GalleryProcessor();
256  $processedData = $processor->process(
257  $this->contentObjectRenderer,
258  [],
259  $processorConfiguration,
260  ['files' => $files]
261  );
262 
263  self::assertEquals($expected, $processedData['gallery']['count']);
264  }
265 
269  public static function ‪calculateMediaWidthsAndHeightsDataProvider(): array
270  {
271  return [
272  'Default settings' => [
273  [
274  [200, 100],
275  [200, 100],
276  [200, 100],
277  ],
278  [],
279  [
280  1 => [
281  1 => ['width' => 200, 'height' => 100],
282  ],
283  2 => [
284  1 => ['width' => 200, 'height' => 100],
285  ],
286  3 => [
287  1 => ['width' => 200, 'height' => 100],
288  ],
289  ],
290  ],
291  'Max width set + number of columns set' => [
292  [
293  [200, 100],
294  [200, 100],
295  [200, 100],
296  ],
297  ['maxGalleryWidth' => 200, 'numberOfColumns' => 2],
298  [
299  1 => [
300  1 => ['width' => 100, 'height' => 50],
301  2 => ['width' => 100, 'height' => 50],
302  ],
303  2 => [
304  1 => ['width' => 100, 'height' => 50],
305  2 => ['width' => null, 'height' => null],
306  ],
307  ],
308  ],
309  'Max width set, number of columns + border (padding) set' => [
310  [
311  [200, 100],
312  [200, 100],
313  [200, 100],
314  ],
315  [
316  'maxGalleryWidth' => 200,
317  'numberOfColumns' => 2,
318  'borderEnabled' => true,
319  'borderPadding' => 4,
320  'borderWidth' => 0,
321  ],
322  [
323  1 => [
324  1 => ['width' => 92, 'height' => 46],
325  2 => ['width' => 92, 'height' => 46],
326  ],
327  2 => [
328  1 => ['width' => 92, 'height' => 46],
329  2 => ['width' => null, 'height' => null],
330  ],
331  ],
332  ],
333  'Max width set, number of columns + border (width) set' => [
334  [
335  [200, 100],
336  [200, 100],
337  [200, 100],
338  ],
339  [
340  'maxGalleryWidth' => 200,
341  'numberOfColumns' => 2,
342  'borderEnabled' => true,
343  'borderPadding' => 0,
344  'borderWidth' => 4,
345  ],
346  [
347  1 => [
348  1 => ['width' => 92, 'height' => 46],
349  2 => ['width' => 92, 'height' => 46],
350  ],
351  2 => [
352  1 => ['width' => 92, 'height' => 46],
353  2 => ['width' => null, 'height' => null],
354  ],
355  ],
356  ],
357  'Max width set, number of columns + border (padding + width) set' => [
358  [
359  [200, 100],
360  [200, 100],
361  [200, 100],
362  ],
363  [
364  'maxGalleryWidth' => 200,
365  'numberOfColumns' => 2,
366  'borderEnabled' => true,
367  'borderPadding' => 1,
368  'borderWidth' => 4,
369  ],
370  [
371  1 => [
372  1 => ['width' => 90, 'height' => 45],
373  2 => ['width' => 90, 'height' => 45],
374  ],
375  2 => [
376  1 => ['width' => 90, 'height' => 45],
377  2 => ['width' => null, 'height' => null],
378  ],
379  ],
380  ],
381  'Equal height set' => [
382  [
383  [200, 100],
384  [200, 300],
385  [100, 50],
386  [2020, 1000],
387  [1000, 1000],
388  ],
389  [
390  'maxGalleryWidth' => 500,
391  'numberOfColumns' => 3,
392  'equalMediaHeight' => 75,
393  ],
394  [
395  1 => [
396  1 => ['width' => 150, 'height' => 75],
397  2 => ['width' => 50, 'height' => 75],
398  3 => ['width' => 150, 'height' => 75],
399  ],
400  2 => [
401  1 => ['width' => 151, 'height' => 75],
402  2 => ['width' => 75, 'height' => 75],
403  3 => ['width' => null, 'height' => null],
404  ],
405  ],
406  ],
407  'Equal width set' => [
408  [
409  [200, 100],
410  [200, 300],
411  [100, 50],
412  ],
413  [
414  'maxGalleryWidth' => 200,
415  'numberOfColumns' => 3,
416  'equalMediaWidth' => 75,
417  ],
418  [
419  1 => [
420  1 => ['width' => 66, 'height' => 33],
421  2 => ['width' => 66, 'height' => 99],
422  3 => ['width' => 66, 'height' => 33],
423  ],
424  ],
425  ],
426  ];
427  }
428 
429  #[DataProvider('calculateMediaWidthsAndHeightsDataProvider')]
430  #[Test]
431  public function ‪calculateMediaWidthsAndHeightsTest(array $testFiles, array $processorConfiguration, array $expected): void
432  {
433  $files = [];
434  foreach ($testFiles as $fileConfig) {
435  $fileReference = $this->createMock(FileReference::class);
436  $fileReference
437  ->method('getProperty')
438  ->willReturnMap([
439  ['width', $fileConfig[0]],
440  ['height', $fileConfig[1]],
441  ]);
442  $files[] = $fileReference;
443  }
444 
445  $processor = new ‪GalleryProcessor();
446  $processedData = $processor->process(
447  $this->contentObjectRenderer,
448  [],
449  $processorConfiguration,
450  ['files' => $files]
451  );
452 
453  foreach ($expected as $row => $columns) {
454  self::assertArrayHasKey($row, $processedData['gallery']['rows'], 'Row exists');
455  foreach ($columns as $column => $dimensions) {
456  self::assertArrayHasKey($column, $processedData['gallery']['rows'][$row]['columns'], 'Column exists');
457  self::assertEquals($dimensions, $processedData['gallery']['rows'][$row]['columns'][$column]['dimensions'], 'Dimensions match');
458  }
459  }
460  }
461 }
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:37
‪TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
Definition: ContentRenderingException.php:24
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Frontend\Tests\Unit\Processor
Definition: GalleryProcessorTest.php:18
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52