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