Further tweaks to r96687, r90016, r97398 etc tests: actually produce a 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( $file->getWidth(), $info['width'], "$name: width check" );
36 $this->assertEquals( $file->getHeight(), $info['height'], "$name: height check" );
37 }
38
39 /**
40 *
41 * @dataProvider providerFiles
42 */
43 function testRotationRendering( $name, $type, $info ) {
44 $file = $this->localFile( $name, $type );
45 $thumb = $file->transform( array(
46 'width' => 800,
47 'height' => 600,
48 ), File::RENDER_NOW );
49
50 $this->assertEquals( $thumb->getWidth(), $info['thumbWidth'], "$name: thumb reported width check" );
51 $this->assertEquals( $thumb->getHeight(), $info['thumbHeight'], "$name: thumb reported height check" );
52
53 $gis = getimagesize( $thumb->getPath() );
54 $this->assertEquals( $gis[0], $info['thumbWidth'], "$name: thumb actual width check");
55 $this->assertEquals( $gis[0], $info['thumbWidth'], "$name: thumb actual height check");
56 }
57
58 private function localFile( $name, $type ) {
59 return new UnregisteredLocalFile( false, $this->repo, $this->filePath . $name, $type );
60 }
61
62 function providerFiles() {
63 return array(
64 array(
65 'landscape-plain.jpg',
66 'image/jpeg',
67 array(
68 'width' => 1024,
69 'height' => 768,
70 'thumbWidth' => 800,
71 'thumbHeight' => 600,
72 )
73 ),
74 array(
75 'portrait-rotated.jpg',
76 'image/jpeg',
77 array(
78 'width' => 768, // as rotated
79 'height' => 1024, // as rotated
80 'thumbWidth' => 450,
81 'thumbHeight' => 600,
82 )
83 )
84 );
85 }
86
87
88 const TEST_WIDTH = 100;
89 const TEST_HEIGHT = 200;
90
91 /**
92 * @dataProvider provideBitmapExtractPreRotationDimensions
93 */
94 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
95 $result = $this->handler->extractPreRotationDimensions( array(
96 'physicalWidth' => self::TEST_WIDTH,
97 'physicalHeight' => self::TEST_HEIGHT,
98 ), $rotation );
99 $this->assertEquals( $expected, $result );
100 }
101
102 function provideBitmapExtractPreRotationDimensions() {
103 return array(
104 array(
105 0,
106 array( self::TEST_WIDTH, self::TEST_HEIGHT )
107 ),
108 array(
109 90,
110 array( self::TEST_HEIGHT, self::TEST_WIDTH )
111 ),
112 array(
113 180,
114 array( self::TEST_WIDTH, self::TEST_HEIGHT )
115 ),
116 array(
117 270,
118 array( self::TEST_HEIGHT, self::TEST_WIDTH )
119 ),
120 );
121 }
122
123 function testWidthFlipping() {
124 # Any further test require a scaler that can rotate
125 if ( !BitmapHandler::canRotate() ) {
126 $this->markTestSkipped( 'Scaler does not support rotation' );
127 return;
128 }
129 $file = UnregisteredLocalFile::newFromPath( $this->filePath . 'portrait-rotated.jpg', 'image/jpeg' );
130 $params = array( 'width' => '50' );
131 $this->assertTrue( $this->handler->normaliseParams( $file, $params ) );
132
133 $this->assertEquals( 50, $params['height'] );
134 $this->assertEquals( round( (768/1024)*50 ), $params['width'], '', 0.1 );
135 }
136 function testWidthNotFlipping() {
137 $file = UnregisteredLocalFile::newFromPath( $this->filePath . 'landscape-plain.jpg', 'image/jpeg' );
138 $params = array( 'width' => '50' );
139 $this->assertTrue( $this->handler->normaliseParams( $file, $params ) );
140
141 $this->assertEquals( 50, $params['width'] );
142 $this->assertEquals( round( (768/1024)*50 ), $params['height'], '', 0.1 );
143 }
144 }
145