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