3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22
* SizeLimitOption tests
25
* @author Felix Rabinovich <felix@rabinovich.org>
26
* @version $Revision: 15513 $
28
class SizeLimitOptionTest extends ItemEditOptionTestCase {
30
function SizeLimitOptionTest($methodName) {
31
$this->ItemEditOptionTestCase(
32
$methodName, 'sizelimit', 'SizeLimitOption');
39
/* create custom platform to return dummy filesize, imagesize, etc. */
40
$gallery->setPlatform(new SizeLimitOptionTestPlatform($gallery->getPlatform()));
42
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
44
print $ret->getAsHtml();
45
return $this->failWithStatus($ret);
47
$this->_markForCleanup($this->_album);
50
function testAppropriate() {
51
list ($ret, $appropriate) = $this->_option->isAppropriate($this->_album, null);
53
return $this->failWithStatus($ret);
55
$this->assert($appropriate, 'appropriate');
59
$form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
60
$form['SizeLimitOption']['sizeChoice'] = 'unlimited';
61
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
62
$this->assertEquals(array(), $error);
63
$this->assertEquals(array(), $warning);
64
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
65
'module', 'sizelimit', $this->_album->getId());
67
return $this->failWithStatus($ret);
69
$this->assert(empty($params), 'Params not empty:');
72
function testDimWidth() {
73
$form['SizeLimitOption']['dimensionChoice'] = 'explicit';
74
$form['SizeLimitOption']['dimensions']['width'] = 1024;
75
$form['SizeLimitOption']['dimensions']['height'] = '';
76
$form['SizeLimitOption']['sizeChoice'] = 'unlimited';
77
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
78
$this->assertEquals(array(), $error);
79
$this->assertEquals(array(), $warning);
80
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
81
'module', 'sizelimit', $this->_album->getId());
83
return $this->failWithStatus($ret);
85
$this->assertEquals(array('height' => 1024, 'width' => 1024, 'keepOriginal' => 0), $params);
88
function testDimHeight() {
89
$form['SizeLimitOption']['dimensionChoice'] = 'explicit';
90
$form['SizeLimitOption']['dimensions']['width'] = '';
91
$form['SizeLimitOption']['dimensions']['height'] = 768;
92
$form['SizeLimitOption']['sizeChoice'] = 'unlimited';
93
$form['SizeLimitOption']['keepOriginal'] = 'checked';
94
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
95
$this->assertEquals(array(), $error);
96
$this->assertEquals(array(), $warning);
97
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
98
'module', 'sizelimit', $this->_album->getId());
100
return $this->failWithStatus($ret);
102
$this->assertEquals(array('height' => 768, 'width' => 768, 'keepOriginal' => 1), $params);
105
function testDimXY() {
106
$form['SizeLimitOption']['dimensionChoice'] = 'explicit';
107
$form['SizeLimitOption']['dimensions']['width'] = 1024;
108
$form['SizeLimitOption']['dimensions']['height'] = 768;
109
$form['SizeLimitOption']['sizeChoice'] = 'unlimited';
110
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
111
$this->assertEquals(array(), $error);
112
$this->assertEquals(array(), $warning);
113
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
114
'module', 'sizelimit', $this->_album->getId());
116
return $this->failWithStatus($ret);
118
$this->assertEquals(array('keepOriginal' => '0',
119
'width' => '1024', 'height' => '768'), $params);
122
function testSize() {
123
$form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
124
$form['SizeLimitOption']['sizeChoice'] = 'explicit';
125
$form['SizeLimitOption']['filesize'] = 2000;
126
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
127
$this->assertEquals(array(), $error);
128
$this->assertEquals(array(), $warning);
129
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
130
'module', 'sizelimit', $this->_album->getId());
132
return $this->failWithStatus($ret);
134
$this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
137
function testBadSize() {
138
$form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
139
$form['SizeLimitOption']['sizeChoice'] = 'explicit';
140
$form['SizeLimitOption']['filesize'] = 'bad';
141
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
142
$this->assertEquals(array('form[error][SizeLimitOption][filesize][invalid]'), $error);
143
$this->assertEquals(array(), $warning);
144
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
145
'module', 'sizelimit', $this->_album->getId());
147
return $this->failWithStatus($ret);
149
$this->assert(empty($params));
152
function test0Size() {
153
$form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
154
$form['SizeLimitOption']['sizeChoice'] = 'explicit';
155
$form['SizeLimitOption']['filesize'] = '0';
156
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
157
$this->assertEquals(array('form[error][SizeLimitOption][filesize][invalid]'),
158
$error, 'Invalid file size');
159
$this->assertEquals(array(), $warning);
160
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
161
'module', 'sizelimit', $this->_album->getId());
163
return $this->failWithStatus($ret);
165
$this->assert(empty($params));
168
function testApplyToDescendents() {
170
$mockAdapter = new MockTemplateAdapter();
171
$gallery->_templateAdapter =& $mockAdapter;
173
$ret = $this->_registerToolkit();
175
return $this->failWithStatus($ret);
177
$platform =& $gallery->getPlatform();
178
$form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
179
$form['SizeLimitOption']['sizeChoice'] = 'explicit';
180
$form['SizeLimitOption']['filesize'] = 2000;
181
$form['SizeLimitOption']['applyToDescendents'] = 'on';
182
$results = $this->_handleRequestAfterEdit($form, $this->_album);
183
$this->assertEquals(array(array(), array()), $results, 'Results');
184
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
185
'module', 'sizelimit', $this->_album->getId());
187
return $this->failWithStatus($ret);
189
$this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
191
$platform->clearstatcache();
192
list ($ret, $this->_item) = $this->_item->refresh();
194
return $this->failWithStatus($ret);
196
$this->assertEquals(1000, $this->_item->getSize(), 'Size');
199
function testChangeInDescendents() {
200
$form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
201
$form['SizeLimitOption']['sizeChoice'] = 'explicit';
202
$form['SizeLimitOption']['filesize'] = 2000;
203
$form['changeInDescendents']['sizelimit'] = 1;
205
list ($ret, $this->_childAlbum) = $this->_createRandomAlbum($this->_album->getId());
207
return $this->failWithStatus($ret);
210
list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
211
$this->assertEquals(array(), $error);
212
$this->assertEquals(array(), $warning);
214
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
215
'module', 'sizelimit', $this->_album->getId());
217
return $this->failWithStatus($ret);
219
$this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
221
list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
222
'module', 'sizelimit', $this->_childAlbum->getId());
224
return $this->failWithStatus($ret);
226
$this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
230
* Register dummy toolkit and operations
232
function _registerToolkit() {
233
$ret = GalleryCoreApi::registerFactoryImplementation(
234
'GalleryToolkit', 'SizeLimitOptionTestToolkit', 'SizeLimitOptionTestToolkit',
235
'modules/sizelimit/test/phpunit/SizeLimitOptionTest.class', 'sizelimitTest', null);
237
return $this->failWithStatus($ret);
242
$this->_markToolkitForCleanup('SizeLimitOptionTestToolkit');
244
/* applying size limits uses 'compress' */
245
$ret = GalleryCoreApi::registerToolkitOperation('SizeLimitOptionTestToolkit',
246
array('image/known'), 'compress',
247
array(array('type' => 'int', 'description' => 'size')), 'desc');
252
/* applying dimension limits uses 'scale' */
253
$ret = GalleryCoreApi::registerToolkitOperation('SizeLimitOptionTestToolkit',
254
array('image/known'), 'scale',
255
array(array('type' => 'int', 'description' => 'width'),
256
array('type' => 'int', 'description' => 'height')), 'desc');
261
/* we will return dimensions to whoever asks */
262
$ret = GalleryCoreApi::registerToolkitProperty('SizeLimitOptionTestToolkit',
263
array('image/known'), 'dimensions', 'int,int', 'desc');
268
list ($ret, $this->_item) =
269
$this->_createRandomDataItem($this->_album->getId(), 'image/known',
270
array('summary' => 'original summary 0',
271
'description' => 'original description 0'));
283
* @subpackage PHPUnit
285
class SizeLimitOptionTestPlatform extends GalleryPlatform {
287
function SizeLimitOptionTestPlatform($originalPlatform) {
288
$this->_savedPlatform = $originalPlatform;
289
GalleryUtilities::putRequestVariable('resizePerformed', false);
290
GalleryUtilities::putRequestVariable('compressPerformed', false);
293
function filesize($file) {
294
$size = GalleryUtilities::getRequestVariables('size_' . basename($file));
301
function getimagesize($file) {
302
$imagesize = GalleryUtilities::getRequestVariables('imagesize_' . basename($file));
304
/* doing it this way keeps CodeAudit happy */
305
return array($imagesize[0], $imagesize[1]);
307
return array(1728, 2304);
310
function file_exists($filename) {
311
$compressPerformed = GalleryUtilities::getRequestVariables('compressPerformed');
312
$resizePerformed = GalleryUtilities::getRequestVariables('resizePerformed');
314
/* if we created a derivative make-believe that the file exists */
315
if ($resizePerformed || $compressPerformed) {
318
return $this->_savedPlatform->file_exists($filename);
322
function unlink($filename) {
323
/* if it is a derivative - we didn't really create it */
324
if ($this->_savedPlatform->file_exists($filename)) {
325
return $this->_savedPlatform->unlink($filename);
335
class SizeLimitOptionTestToolkit extends GalleryToolkit {
337
* @see GalleryToolkit::performOperation
339
function performOperation($mimeType, $operationName, $sourceFilename,
340
$destFilename, $parameters, $context=array()) {
341
/* Here we are pretending that conversion happened by setting the flag */
342
switch($operationName) {
344
GalleryUtilities::putRequestVariable('compressPerformed', true);
345
GalleryUtilities::putRequestVariable('size_' . basename($destFilename), 1000);
349
GalleryUtilities::putRequestVariable('resizePerformed', true);
350
GalleryUtilities::putRequestVariable(
351
'imagesize_' . basename($destFilename), array(768, 1024));
355
return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED),
359
$operations = array();
360
$operationRequest = GalleryUtilities::getRequestVariables('operation');
361
if (!empty($operationRequest)) {
362
$operations = $operationRequest;
364
$operations[] = $operationName;
365
GalleryUtilities::putRequestVariable('operation', $operations);
367
return array(null, $mimeType, $context);
371
* @see GalleryToolkit::getProperty
373
function getProperty($mimeType, $propertyName, $file) {
376
/* Our 'mime type' has to support dimensions */
377
switch($propertyName) {
379
$platform =& $gallery->getPlatform();
380
$results = $platform->getimagesize($file);
384
return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED), null);
387
return array(null, $results);