~ubuntu-branches/ubuntu/trusty/mediawiki/trusty

« back to all changes in this revision

Viewing changes to tests/phpunit/includes/media/BitmapScalingTest.php

  • Committer: Package Import Robot
  • Author(s): Thorsten Glaser
  • Date: 2014-03-28 09:56:29 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20140328095629-1526y9tchdd507id
Tags: 1:1.19.14+dfsg-1
* New upstream security fix release (Closes: #742857):
  - (bug 62497) SECURITY: Add CSRF token on Special:ChangePassword
  - (bug 62467) Set a title for the context during import on the cli
* Use upstream-provided signing key bundle

Show diffs side-by-side

added added

removed removed

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