Further tweaks to r96687, r90016, r97398, r97656 etc tests: try several thumbnail...
[lhc/web/wiklou.git] / tests / phpunit / includes / media / ExifRotationTest.php
1 <?php
2
3 /**
4 * Tests related to auto rotation
5 */
6 class ExifRotationTest extends MediaWikiTestCase {
7
8 function setUp() {
9 parent::setUp();
10 $this->filePath = dirname( __FILE__ ) . '/../../data/media/';
11 $this->handler = new BitmapHandler();
12 $this->repo = new FSRepo(array(
13 'name' => 'temp',
14 'directory' => wfTempDir() . '/exif-test-' . time(),
15 'url' => 'http://localhost/thumbtest'
16 ));
17 if ( !wfDl( 'exif' ) ) {
18 $this->markTestSkipped( "This test needs the exif extension." );
19 }
20 global $wgShowEXIF;
21 $this->show = $wgShowEXIF;
22 $wgShowEXIF = true;
23 }
24 public function tearDown() {
25 global $wgShowEXIF;
26 $wgShowEXIF = $this->show;
27 }
28
29 /**
30 *
31 * @dataProvider providerFiles
32 */
33 function testMetadata( $name, $type, $info ) {
34 $file = UnregisteredLocalFile::newFromPath( $this->filePath . $name, $type );
35 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
36 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
37 }
38
39 /**
40 *
41 * @dataProvider providerFiles
42 */
43 function testRotationRendering( $name, $type, $info, $thumbs ) {
44 foreach( $thumbs as $size => $out ) {
45 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
46 $params = array(
47 'width' => $matches[1],
48 );
49 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
50 $params = array(
51 'width' => $matches[1],
52 'height' => $matches[2]
53 );
54 } else {
55 throw new MWException('bogus test data format ' . $size);
56 }
57
58 $file = $this->localFile( $name, $type );
59 $thumb = $file->transform( $params, File::RENDER_NOW );
60
61 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
62 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
63
64 $gis = getimagesize( $thumb->getPath() );
65 if ($out[0] > $info['width']) {
66 // Physical image won't be scaled bigger than the original.
67 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
68 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
69 } else {
70 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
71 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
72 }
73 }
74 }
75
76 private function localFile( $name, $type ) {
77 return new UnregisteredLocalFile( false, $this->repo, $this->filePath . $name, $type );
78 }
79
80 function providerFiles() {
81 return array(
82 array(
83 'landscape-plain.jpg',
84 'image/jpeg',
85 array(
86 'width' => 1024,
87 'height' => 768,
88 ),
89 array(
90 '800x600px' => array( 800, 600 ),
91 '9999x800px' => array( 1067, 800 ),
92 '800px' => array( 800, 600 ),
93 '600px' => array( 600, 450 ),
94 )
95 ),
96 array(
97 'portrait-rotated.jpg',
98 'image/jpeg',
99 array(
100 'width' => 768, // as rotated
101 'height' => 1024, // as rotated
102 ),
103 array(
104 '800x600px' => array( 450, 600 ),
105 '9999x800px' => array( 600, 800 ),
106 '800px' => array( 800, 1067 ),
107 '600px' => array( 600, 800 ),
108 )
109 )
110 );
111 }
112
113
114 const TEST_WIDTH = 100;
115 const TEST_HEIGHT = 200;
116
117 /**
118 * @dataProvider provideBitmapExtractPreRotationDimensions
119 */
120 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
121 $result = $this->handler->extractPreRotationDimensions( array(
122 'physicalWidth' => self::TEST_WIDTH,
123 'physicalHeight' => self::TEST_HEIGHT,
124 ), $rotation );
125 $this->assertEquals( $expected, $result );
126 }
127
128 function provideBitmapExtractPreRotationDimensions() {
129 return array(
130 array(
131 0,
132 array( self::TEST_WIDTH, self::TEST_HEIGHT )
133 ),
134 array(
135 90,
136 array( self::TEST_HEIGHT, self::TEST_WIDTH )
137 ),
138 array(
139 180,
140 array( self::TEST_WIDTH, self::TEST_HEIGHT )
141 ),
142 array(
143 270,
144 array( self::TEST_HEIGHT, self::TEST_WIDTH )
145 ),
146 );
147 }
148
149 function testWidthFlipping() {
150 # Any further test require a scaler that can rotate
151 if ( !BitmapHandler::canRotate() ) {
152 $this->markTestSkipped( 'Scaler does not support rotation' );
153 return;
154 }
155 $file = UnregisteredLocalFile::newFromPath( $this->filePath . 'portrait-rotated.jpg', 'image/jpeg' );
156 $params = array( 'width' => '50' );
157 $this->assertTrue( $this->handler->normaliseParams( $file, $params ) );
158
159 $this->assertEquals( 50, $params['height'] );
160 $this->assertEquals( round( (768/1024)*50 ), $params['width'], '', 0.1 );
161 }
162 function testWidthNotFlipping() {
163 $file = UnregisteredLocalFile::newFromPath( $this->filePath . 'landscape-plain.jpg', 'image/jpeg' );
164 $params = array( 'width' => '50' );
165 $this->assertTrue( $this->handler->normaliseParams( $file, $params ) );
166
167 $this->assertEquals( 50, $params['width'] );
168 $this->assertEquals( round( (768/1024)*50 ), $params['height'], '', 0.1 );
169 }
170 }
171