summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/media/BitmapScalingTest.php
blob: 3de60b73064c461f3bda05c6ca84991a05993723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

class BitmapScalingTest extends MediaWikiTestCase {

	protected function setUp() {
		parent::setUp();

		$this->setMwGlobals( array(
			'wgMaxImageArea' => 1.25e7, // 3500x3500
			'wgCustomConvertCommand' => 'dummy', // Set so that we don't get client side rendering
		) );
	}

	/**
	 * @dataProvider provideNormaliseParams
	 */
	function testNormaliseParams( $fileDimensions, $expectedParams, $params, $msg ) {
		$file = new FakeDimensionFile( $fileDimensions );
		$handler = new BitmapHandler;
		$valid = $handler->normaliseParams( $file, $params );
		$this->assertTrue( $valid );
		$this->assertEquals( $expectedParams, $params, $msg );
	}

	function provideNormaliseParams() {
		return array(
			/* Regular resize operations */
			array(
				array( 1024, 768 ),
				array(
					'width' => 512, 'height' => 384,
					'physicalWidth' => 512, 'physicalHeight' => 384,
					'page' => 1,
				),
				array( 'width' => 512 ),
				'Resizing with width set',
			),
			array(
				array( 1024, 768 ),
				array(
					'width' => 512, 'height' => 384,
					'physicalWidth' => 512, 'physicalHeight' => 384,
					'page' => 1,
				),
				array( 'width' => 512, 'height' => 768 ),
				'Resizing with height set too high',
			),
			array(
				array( 1024, 768 ),
				array(
					'width' => 512, 'height' => 384,
					'physicalWidth' => 512, 'physicalHeight' => 384,
					'page' => 1,
				),
				array( 'width' => 1024, 'height' => 384 ),
				'Resizing with height set',
			),

			/* Very tall images */
			array(
				array( 1000, 100 ),
				array(
					'width' => 5, 'height' => 1,
					'physicalWidth' => 5, 'physicalHeight' => 1,
					'page' => 1,
				),
				array( 'width' => 5 ),
				'Very wide image',
			),

			array(
				array( 100, 1000 ),
				array(
					'width' => 1, 'height' => 10,
					'physicalWidth' => 1, 'physicalHeight' => 10,
					'page' => 1,
				),
				array( 'width' => 1 ),
				'Very high image',
			),
			array(
				array( 100, 1000 ),
				array(
					'width' => 1, 'height' => 5,
					'physicalWidth' => 1, 'physicalHeight' => 10,
					'page' => 1,
				),
				array( 'width' => 10, 'height' => 5 ),
				'Very high image with height set',
			),
			/* Max image area */
			array(
				array( 4000, 4000 ),
				array(
					'width' => 5000, 'height' => 5000,
					'physicalWidth' => 4000, 'physicalHeight' => 4000,
					'page' => 1,
				),
				array( 'width' => 5000 ),
				'Bigger than max image size but doesn\'t need scaling',
			),
		);
	}

	function testTooBigImage() {
		$file = new FakeDimensionFile( array( 4000, 4000 ) );
		$handler = new BitmapHandler;
		$params = array( 'width' => '3700' ); // Still bigger than max size.
		$this->assertEquals( 'TransformParameterError',
			get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) );
	}

	function testTooBigMustRenderImage() {
		$file = new FakeDimensionFile( array( 4000, 4000 ) );
		$file->mustRender = true;
		$handler = new BitmapHandler;
		$params = array( 'width' => '5000' ); // Still bigger than max size.
		$this->assertEquals( 'TransformParameterError',
			get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) );
	}

	function testImageArea() {
		$file = new FakeDimensionFile( array( 7, 9 ) );
		$handler = new BitmapHandler;
		$this->assertEquals( 63, $handler->getImageArea( $file ) );
	}
}

class FakeDimensionFile extends File {
	public $mustRender = false;

	public function __construct( $dimensions ) {
		parent::__construct( Title::makeTitle( NS_FILE, 'Test' ),
			new NullRepo( null ) );

		$this->dimensions = $dimensions;
	}

	public function getWidth( $page = 1 ) {
		return $this->dimensions[0];
	}

	public function getHeight( $page = 1 ) {
		return $this->dimensions[1];
	}

	public function mustRender() {
		return $this->mustRender;
	}

	public function getPath() {
		return '';
	}
}