Merge "Remove (edit) from Special:Tags for non-editinterface users"
[lhc/web/wiklou.git] / tests / phpunit / includes / media / BitmapScalingTest.php
1 <?php
2
3 class BitmapScalingTest extends MediaWikiTestCase {
4
5 protected function setUp() {
6 parent::setUp();
7
8 $this->setMwGlobals( array(
9 'wgMaxImageArea' => 1.25e7, // 3500x3500
10 'wgCustomConvertCommand' => 'dummy', // Set so that we don't get client side rendering
11 ) );
12 }
13
14 /**
15 * @dataProvider provideNormaliseParams
16 */
17 function testNormaliseParams( $fileDimensions, $expectedParams, $params, $msg ) {
18 $file = new FakeDimensionFile( $fileDimensions );
19 $handler = new BitmapHandler;
20 $valid = $handler->normaliseParams( $file, $params );
21 $this->assertTrue( $valid );
22 $this->assertEquals( $expectedParams, $params, $msg );
23 }
24
25 public static function provideNormaliseParams() {
26 return array(
27 /* Regular resize operations */
28 array(
29 array( 1024, 768 ),
30 array(
31 'width' => 512, 'height' => 384,
32 'physicalWidth' => 512, 'physicalHeight' => 384,
33 'page' => 1,
34 ),
35 array( 'width' => 512 ),
36 'Resizing with width set',
37 ),
38 array(
39 array( 1024, 768 ),
40 array(
41 'width' => 512, 'height' => 384,
42 'physicalWidth' => 512, 'physicalHeight' => 384,
43 'page' => 1,
44 ),
45 array( 'width' => 512, 'height' => 768 ),
46 'Resizing with height set too high',
47 ),
48 array(
49 array( 1024, 768 ),
50 array(
51 'width' => 512, 'height' => 384,
52 'physicalWidth' => 512, 'physicalHeight' => 384,
53 'page' => 1,
54 ),
55 array( 'width' => 1024, 'height' => 384 ),
56 'Resizing with height set',
57 ),
58
59 /* Very tall images */
60 array(
61 array( 1000, 100 ),
62 array(
63 'width' => 5, 'height' => 1,
64 'physicalWidth' => 5, 'physicalHeight' => 1,
65 'page' => 1,
66 ),
67 array( 'width' => 5 ),
68 'Very wide image',
69 ),
70
71 array(
72 array( 100, 1000 ),
73 array(
74 'width' => 1, 'height' => 10,
75 'physicalWidth' => 1, 'physicalHeight' => 10,
76 'page' => 1,
77 ),
78 array( 'width' => 1 ),
79 'Very high image',
80 ),
81 array(
82 array( 100, 1000 ),
83 array(
84 'width' => 1, 'height' => 5,
85 'physicalWidth' => 1, 'physicalHeight' => 10,
86 'page' => 1,
87 ),
88 array( 'width' => 10, 'height' => 5 ),
89 'Very high image with height set',
90 ),
91 /* Max image area */
92 array(
93 array( 4000, 4000 ),
94 array(
95 'width' => 5000, 'height' => 5000,
96 'physicalWidth' => 4000, 'physicalHeight' => 4000,
97 'page' => 1,
98 ),
99 array( 'width' => 5000 ),
100 'Bigger than max image size but doesn\'t need scaling',
101 ),
102 );
103 }
104
105 function testTooBigImage() {
106 $file = new FakeDimensionFile( array( 4000, 4000 ) );
107 $handler = new BitmapHandler;
108 $params = array( 'width' => '3700' ); // Still bigger than max size.
109 $this->assertEquals( 'TransformParameterError',
110 get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) );
111 }
112
113 function testTooBigMustRenderImage() {
114 $file = new FakeDimensionFile( array( 4000, 4000 ) );
115 $file->mustRender = true;
116 $handler = new BitmapHandler;
117 $params = array( 'width' => '5000' ); // Still bigger than max size.
118 $this->assertEquals( 'TransformParameterError',
119 get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) );
120 }
121
122 function testImageArea() {
123 $file = new FakeDimensionFile( array( 7, 9 ) );
124 $handler = new BitmapHandler;
125 $this->assertEquals( 63, $handler->getImageArea( $file ) );
126 }
127 }
128
129 class FakeDimensionFile extends File {
130 public $mustRender = false;
131
132 public function __construct( $dimensions ) {
133 parent::__construct( Title::makeTitle( NS_FILE, 'Test' ),
134 new NullRepo( null ) );
135
136 $this->dimensions = $dimensions;
137 }
138
139 public function getWidth( $page = 1 ) {
140 return $this->dimensions[0];
141 }
142
143 public function getHeight( $page = 1 ) {
144 return $this->dimensions[1];
145 }
146
147 public function mustRender() {
148 return $this->mustRender;
149 }
150
151 public function getPath() {
152 return '';
153 }
154 }