Merge "Display "Printable version" links in toolbox on special pages"
[lhc/web/wiklou.git] / tests / phpunit / mocks / media / MockBitmapHandler.php
1 <?php
2 /**
3 * Fake handler for images.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 class MockBitmapHandler extends BitmapHandler {
25 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
26 return MockImageHandler::doFakeTransform( $this, $image, $dstPath, $dstUrl, $params, $flags );
27 }
28 function doClientImage( $image, $scalerParams ) {
29 return $this->getClientScalingThumbnailImage( $image, $scalerParams );
30 }
31 }
32 class MockSvgHandler extends SvgHandler {
33 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
34 return MockImageHandler::doFakeTransform( $this, $image, $dstPath, $dstUrl, $params, $flags );
35 }
36 }
37 /**
38 * Mock handler for images.
39 *
40 * This is really intended for unit testing.
41 *
42 * @ingroup Media
43 */
44 class MockImageHandler {
45
46 /**
47 * Override BitmapHandler::doTransform() making sure we do not generate
48 * a thumbnail at all. That is merely returning a ThumbnailImage that
49 * will be consumed by the unit test. There is no need to create a real
50 * thumbnail on the filesystem.
51 */
52 static function doFakeTransform( $that, $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
53 # Example of what we receive:
54 # $image: LocalFile
55 # $dstPath: /tmp/transform_7d0a7a2f1a09-1.jpg
56 # $dstUrl : http://example.com/images/thumb/0/09/Bad.jpg/320px-Bad.jpg
57 # $params: width: 320, descriptionUrl http://trunk.dev/wiki/File:Bad.jpg
58
59 $that->normaliseParams( $image, $params );
60
61 $scalerParams = array(
62 # The size to which the image will be resized
63 'physicalWidth' => $params['physicalWidth'],
64 'physicalHeight' => $params['physicalHeight'],
65 'physicalDimensions' => "{$params['physicalWidth']}x{$params['physicalHeight']}",
66 # The size of the image on the page
67 'clientWidth' => $params['width'],
68 'clientHeight' => $params['height'],
69 # Comment as will be added to the EXIF of the thumbnail
70 'comment' => isset( $params['descriptionUrl'] ) ?
71 "File source: {$params['descriptionUrl']}" : '',
72 # Properties of the original image
73 'srcWidth' => $image->getWidth(),
74 'srcHeight' => $image->getHeight(),
75 'mimeType' => $image->getMimeType(),
76 'dstPath' => $dstPath,
77 'dstUrl' => $dstUrl,
78 );
79
80 # In some cases, we do not bother generating a thumbnail.
81 if ( !$image->mustRender() &&
82 $scalerParams['physicalWidth'] == $scalerParams['srcWidth']
83 && $scalerParams['physicalHeight'] == $scalerParams['srcHeight']
84 ) {
85 wfDebug( __METHOD__ . ": returning unscaled image\n" );
86 // getClientScalingThumbnailImage is protected
87 return $that->doClientImage( $image, $scalerParams );
88 }
89
90 return new ThumbnailImage( $image, $dstUrl, false, $params );
91 }
92 }