xref: /dokuwiki/_test/tests/File/MediaFileTest.php (revision 1c00c02121477c81b95fe750b94acdf109cba20a)
1<?php
2
3namespace dokuwiki\test\File;
4
5use dokuwiki\File\MediaFile;
6
7/**
8 * Tests for dokuwiki\File\MediaFile display-dimension helpers.
9 *
10 * The fixture wiki:exif-orient-6.jpg is a 20x30 raw JPEG carrying EXIF
11 * orientation 6, so its display orientation is 30x20 (portrait -> landscape swap).
12 */
13class MediaFileTest extends \DokuWikiTest
14{
15    /** @var string */
16    private $rotated = 'wiki:exif-orient-6.jpg';
17
18    /** @var string */
19    private $unrotated = 'wiki:dokuwiki-128.png';
20
21    public function testDisplayDimsForNonImageReturnsZero()
22    {
23        $mf = new MediaFile('nonexistent:file.jpg');
24        $this->assertSame([0, 0], $mf->getDisplayDimensions(500, 500));
25    }
26
27    public function testGetDisplayDimensionsBboxFitRotated()
28    {
29        $mf = new MediaFile($this->rotated);
30        // raw 20x30 -> rotated 30x20 -> already fits inside 500x500, so the
31        // no-upscale bounding-box fit (fit=1 URLs) keeps it at its native size
32        $this->assertSame([30, 20], $mf->getDisplayDimensions(500, 500, true));
33    }
34
35    public function testGetDisplayDimensionsBboxFitScaleDown()
36    {
37        $mf = new MediaFile($this->rotated);
38        // rotated 30x20 fitted into a smaller 15x15 box preserves aspect,
39        // scale = min(15/30, 15/20) = 0.5 => 15 x 10
40        $this->assertSame([15, 10], $mf->getDisplayDimensions(15, 15, true));
41    }
42
43    public function testGetDisplayDimensionsResizeWithoutFitUpscales()
44    {
45        $mf = new MediaFile($this->rotated);
46        // no fit flag + single dimension = plain resize, which enlarges small
47        // images just as fetch.php does: rotated 30x20 to width 500 => 500 x round(500 * 20/30)
48        $this->assertSame([500, 333], $mf->getDisplayDimensions(500, 0, false));
49    }
50
51    public function testGetDisplayDimensionsCropPassthrough()
52    {
53        // both dimensions without fit = center-crop, producing exactly the box
54        $mf = new MediaFile($this->rotated);
55        $this->assertSame([100, 100], $mf->getDisplayDimensions(100, 100, false));
56    }
57
58    public function testGetDisplayDimensionsNativeWhenNoRequest()
59    {
60        $mf = new MediaFile($this->rotated);
61        $this->assertSame([30, 20], $mf->getDisplayDimensions(0, 0, false));
62    }
63
64    public function testGetDisplayDimensionsUnrotatedImage()
65    {
66        $mf = new MediaFile($this->unrotated);
67        // non-JPEG: display dims equal raw dims
68        $this->assertSame([$mf->getWidth(), $mf->getHeight()], $mf->getDisplayDimensions(0, 0, false));
69    }
70}
71