~ubuntu-branches/ubuntu/maverick/gallery2/maverick

« back to all changes in this revision

Viewing changes to modules/sizelimit/test/phpunit/SizeLimitOptionTest.class

  • Committer: Bazaar Package Importer
  • Author(s): Michael C. Schultheiss
  • Date: 2007-09-10 20:22:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070910202219-0jsuntvqge4ade6b
Tags: 2.2.3-2
Add Slovak translation of Debconf templates.  (Thanks to 
Ivan Masá.  Closes: #441671)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Gallery - a web based photo album viewer and editor
4
 
 * Copyright (C) 2000-2007 Bharat Mediratta
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 */
20
 
 
21
 
/**
22
 
 * SizeLimitOption tests
23
 
 * @package SizeLimit
24
 
 * @subpackage PHPUnit
25
 
 * @author Felix Rabinovich <felix@rabinovich.org>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class SizeLimitOptionTest extends ItemEditOptionTestCase {
29
 
 
30
 
    function SizeLimitOptionTest($methodName) {
31
 
        $this->ItemEditOptionTestCase(
32
 
            $methodName, 'sizelimit', 'SizeLimitOption');
33
 
    }
34
 
 
35
 
    function setUp() {
36
 
        global $gallery;
37
 
        parent::setUp();
38
 
 
39
 
        /* create custom platform to return dummy filesize, imagesize, etc. */
40
 
        $gallery->setPlatform(new SizeLimitOptionTestPlatform($gallery->getPlatform()));
41
 
 
42
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
43
 
        if ($ret) {
44
 
            print $ret->getAsHtml();
45
 
            return $this->failWithStatus($ret);
46
 
        }
47
 
        $this->_markForCleanup($this->_album);
48
 
    }
49
 
 
50
 
    function testAppropriate() {
51
 
        list ($ret, $appropriate) = $this->_option->isAppropriate($this->_album, null);
52
 
        if ($ret) {
53
 
            return $this->failWithStatus($ret);
54
 
        }
55
 
        $this->assert($appropriate, 'appropriate');
56
 
    }
57
 
 
58
 
    function testNone() {
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());
66
 
        if ($ret) {
67
 
            return $this->failWithStatus($ret);
68
 
        }
69
 
        $this->assert(empty($params), 'Params not empty:');
70
 
    }
71
 
 
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());
82
 
        if ($ret) {
83
 
            return $this->failWithStatus($ret);
84
 
        }
85
 
        $this->assertEquals(array('height' => 1024, 'width' => 1024, 'keepOriginal' => 0), $params);
86
 
    }
87
 
 
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());
99
 
        if ($ret) {
100
 
            return $this->failWithStatus($ret);
101
 
        }
102
 
        $this->assertEquals(array('height' => 768, 'width' => 768, 'keepOriginal' => 1), $params);
103
 
    }
104
 
 
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());
115
 
        if ($ret) {
116
 
            return $this->failWithStatus($ret);
117
 
        }
118
 
        $this->assertEquals(array('keepOriginal' => '0',
119
 
            'width' => '1024', 'height' => '768'), $params);
120
 
    }
121
 
 
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());
131
 
        if ($ret) {
132
 
            return $this->failWithStatus($ret);
133
 
        }
134
 
        $this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
135
 
    }
136
 
 
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());
146
 
        if ($ret) {
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
        $this->assert(empty($params));
150
 
    }
151
 
 
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());
162
 
        if ($ret) {
163
 
            return $this->failWithStatus($ret);
164
 
        }
165
 
        $this->assert(empty($params));
166
 
    }
167
 
 
168
 
    function testApplyToDescendents() {
169
 
        global $gallery;
170
 
        $mockAdapter = new MockTemplateAdapter();
171
 
        $gallery->_templateAdapter =& $mockAdapter;
172
 
 
173
 
        $ret = $this->_registerToolkit();
174
 
        if ($ret) {
175
 
            return $this->failWithStatus($ret);
176
 
        }
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());
186
 
        if ($ret) {
187
 
            return $this->failWithStatus($ret);
188
 
        }
189
 
        $this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
190
 
 
191
 
        $platform->clearstatcache();
192
 
        list ($ret, $this->_item) = $this->_item->refresh();
193
 
        if ($ret) {
194
 
            return $this->failWithStatus($ret);
195
 
        }
196
 
        $this->assertEquals(1000, $this->_item->getSize(), 'Size');
197
 
    }
198
 
 
199
 
    function testChangeInDescendents() {
200
 
        $form['SizeLimitOption']['dimensionChoice'] = 'unlimited';
201
 
        $form['SizeLimitOption']['sizeChoice'] = 'explicit';
202
 
        $form['SizeLimitOption']['filesize'] = 2000;
203
 
        $form['changeInDescendents']['sizelimit'] = 1;
204
 
 
205
 
        list ($ret, $this->_childAlbum) = $this->_createRandomAlbum($this->_album->getId());
206
 
        if ($ret) {
207
 
            return $this->failWithStatus($ret);
208
 
        }
209
 
 
210
 
        list ($error, $warning) = $this->_handleRequestAfterEdit($form, $this->_album);
211
 
        $this->assertEquals(array(), $error);
212
 
        $this->assertEquals(array(), $warning);
213
 
 
214
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
215
 
            'module', 'sizelimit', $this->_album->getId());
216
 
        if ($ret) {
217
 
            return $this->failWithStatus($ret);
218
 
        }
219
 
        $this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
220
 
 
221
 
        list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters(
222
 
            'module', 'sizelimit', $this->_childAlbum->getId());
223
 
        if ($ret) {
224
 
            return $this->failWithStatus($ret);
225
 
        }
226
 
        $this->assertEquals(array('keepOriginal' => 0, 'size' => 2000), $params);
227
 
    }
228
 
 
229
 
    /**
230
 
     * Register dummy toolkit and operations
231
 
     */
232
 
    function _registerToolkit() {
233
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
234
 
            'GalleryToolkit', 'SizeLimitOptionTestToolkit', 'SizeLimitOptionTestToolkit',
235
 
            'modules/sizelimit/test/phpunit/SizeLimitOptionTest.class', 'sizelimitTest', null);
236
 
        if ($ret) {
237
 
            return $this->failWithStatus($ret);
238
 
        }
239
 
        if ($ret) {
240
 
            return $ret;
241
 
        }
242
 
        $this->_markToolkitForCleanup('SizeLimitOptionTestToolkit');
243
 
 
244
 
        /* applying size limits uses 'compress' */
245
 
        $ret = GalleryCoreApi::registerToolkitOperation('SizeLimitOptionTestToolkit',
246
 
            array('image/known'), 'compress',
247
 
            array(array('type' => 'int', 'description' => 'size')), 'desc');
248
 
        if ($ret) {
249
 
            return $ret;
250
 
        }
251
 
 
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');
257
 
        if ($ret) {
258
 
            return $ret;
259
 
        }
260
 
 
261
 
        /* we will return dimensions to whoever asks */
262
 
        $ret = GalleryCoreApi::registerToolkitProperty('SizeLimitOptionTestToolkit',
263
 
            array('image/known'), 'dimensions', 'int,int', 'desc');
264
 
        if ($ret) {
265
 
            return $ret;
266
 
        }
267
 
 
268
 
        list ($ret, $this->_item) =
269
 
            $this->_createRandomDataItem($this->_album->getId(), 'image/known',
270
 
                                         array('summary' => 'original summary 0',
271
 
                                         'description' => 'original description 0'));
272
 
        if ($ret) {
273
 
            return $ret;
274
 
        }
275
 
        return (null);
276
 
    }
277
 
}
278
 
 
279
 
/**
280
 
 * Test platform
281
 
 *
282
 
 * @package SizeLimit
283
 
 * @subpackage PHPUnit
284
 
 */
285
 
class SizeLimitOptionTestPlatform extends GalleryPlatform {
286
 
 
287
 
    function SizeLimitOptionTestPlatform($originalPlatform) {
288
 
        $this->_savedPlatform = $originalPlatform;
289
 
        GalleryUtilities::putRequestVariable('resizePerformed', false);
290
 
        GalleryUtilities::putRequestVariable('compressPerformed', false);
291
 
    }
292
 
 
293
 
    function filesize($file) {
294
 
        $size = GalleryUtilities::getRequestVariables('size_' . basename($file));
295
 
        if ($size) {
296
 
            return $size;
297
 
        }
298
 
        return 26031966;
299
 
    }
300
 
 
301
 
    function getimagesize($file) {
302
 
        $imagesize = GalleryUtilities::getRequestVariables('imagesize_' . basename($file));
303
 
        if ($imagesize) {
304
 
            /* doing it this way keeps CodeAudit happy */
305
 
            return array($imagesize[0], $imagesize[1]);
306
 
        }
307
 
        return array(1728, 2304);
308
 
    }
309
 
 
310
 
    function file_exists($filename) {
311
 
        $compressPerformed = GalleryUtilities::getRequestVariables('compressPerformed');
312
 
        $resizePerformed = GalleryUtilities::getRequestVariables('resizePerformed');
313
 
 
314
 
        /* if we created a derivative make-believe that the file exists */
315
 
        if ($resizePerformed || $compressPerformed) {
316
 
            return true;
317
 
        } else {
318
 
            return $this->_savedPlatform->file_exists($filename);
319
 
        }
320
 
    }
321
 
 
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);
326
 
        } else {
327
 
            return true;
328
 
        }
329
 
    }
330
 
}
331
 
 
332
 
/**
333
 
 * Test toolkit
334
 
 */
335
 
class SizeLimitOptionTestToolkit extends GalleryToolkit {
336
 
    /**
337
 
     * @see GalleryToolkit::performOperation
338
 
     */
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) {
343
 
        case 'compress':
344
 
            GalleryUtilities::putRequestVariable('compressPerformed', true);
345
 
            GalleryUtilities::putRequestVariable('size_' . basename($destFilename), 1000);
346
 
            break;
347
 
 
348
 
        case 'scale':
349
 
            GalleryUtilities::putRequestVariable('resizePerformed', true);
350
 
            GalleryUtilities::putRequestVariable(
351
 
                'imagesize_' . basename($destFilename), array(768, 1024));
352
 
            break;
353
 
 
354
 
        default:
355
 
            return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED),
356
 
                         null, null);
357
 
        }
358
 
 
359
 
        $operations = array();
360
 
        $operationRequest = GalleryUtilities::getRequestVariables('operation');
361
 
        if (!empty($operationRequest)) {
362
 
              $operations = $operationRequest;
363
 
        }
364
 
        $operations[] = $operationName;
365
 
        GalleryUtilities::putRequestVariable('operation', $operations);
366
 
 
367
 
        return array(null, $mimeType, $context);
368
 
    }
369
 
 
370
 
    /**
371
 
     * @see GalleryToolkit::getProperty
372
 
     */
373
 
    function getProperty($mimeType, $propertyName, $file) {
374
 
        global $gallery;
375
 
 
376
 
        /* Our 'mime type' has to support dimensions */
377
 
        switch($propertyName) {
378
 
        case 'dimensions':
379
 
            $platform =& $gallery->getPlatform();
380
 
            $results = $platform->getimagesize($file);
381
 
            break;
382
 
 
383
 
        default:
384
 
            return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED), null);
385
 
        }
386
 
 
387
 
        return array(null, $results);
388
 
    }
389
 
}
390
 
?>