~ubuntu-branches/ubuntu/quantal/gallery2/quantal

« back to all changes in this revision

Viewing changes to modules/sizelimit/test/phpunit/SetSizeOptionTest.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
 
GalleryCoreApi::requireOnce('lib/tools/phpunit/ItemAddOptionTestCase.class');
22
 
 
23
 
/**
24
 
 * SizeLimitOption tests
25
 
 * @package SizeLimit
26
 
 * @subpackage PHPUnit
27
 
 * @author Felix Rabinovich <felix@rabinovich.org>
28
 
 * @version $Revision: 15513 $
29
 
 */
30
 
class SetSizeOptionTest extends ItemAddOptionTestCase {
31
 
 
32
 
    function SetSizeOptionTest($methodName) {
33
 
        $this->ItemAddOptionTestCase($methodName, 'sizelimit', 'SetSizeOption');
34
 
    }
35
 
 
36
 
    function setUp() {
37
 
        global $gallery;
38
 
 
39
 
        parent::setUp();
40
 
 
41
 
        /* create custom platform to return dummy filesize, imagesize, etc. */
42
 
        $gallery->setPlatform(new SetSizeOptionTestPlatform($gallery->getPlatform()));
43
 
 
44
 
        /* Register dummy toolkit and operations */
45
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
46
 
            'GalleryToolkit', 'SetSizeOptionTestToolkit', 'SetSizeOptionTestToolkit',
47
 
            'modules/sizelimit/test/phpunit/SetSizeOptionTest.class', 'sizelimitTest', null);
48
 
        if ($ret) {
49
 
            print $ret->getAsHtml();
50
 
            return $this->failWithStatus($ret);
51
 
        }
52
 
        $this->_markToolkitForCleanup('SetSizeOptionTestToolkit');
53
 
 
54
 
        /* applying size limits uses 'compress' */
55
 
        $ret = GalleryCoreApi::registerToolkitOperation('SetSizeOptionTestToolkit',
56
 
            array('image/known'), 'compress',
57
 
            array(array('type' => 'int', 'description' => 'size')), 'desc');
58
 
        if ($ret) {
59
 
            print $ret->getAsHtml();
60
 
            return $this->failWithStatus($ret);
61
 
        }
62
 
 
63
 
        /* applying dimension limits uses 'scale' */
64
 
        $ret = GalleryCoreApi::registerToolkitOperation('SetSizeOptionTestToolkit',
65
 
            array('image/known'), 'scale',
66
 
            array(array('type' => 'int', 'description' => 'width'),
67
 
            array('type' => 'int', 'description' => 'height')), 'desc');
68
 
        if ($ret) {
69
 
            print $ret->getAsHtml();
70
 
            return $this->failWithStatus($ret);
71
 
        }
72
 
 
73
 
        /* we will return dimensions to whoever asks */
74
 
        $ret = GalleryCoreApi::registerToolkitProperty('SetSizeOptionTestToolkit',
75
 
            array('image/known', 'image/unknown'), 'dimensions', 'int,int', 'desc');
76
 
        if ($ret) {
77
 
            print $ret->getAsHtml();
78
 
            return $this->failWithStatus($ret);
79
 
        }
80
 
 
81
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
82
 
        if ($ret) {
83
 
            print $ret->getAsHtml();
84
 
            return $this->failWithStatus($ret);
85
 
        }
86
 
        $this->_markForCleanup($this->_album);
87
 
 
88
 
        $this->_items = array();
89
 
        /* Sample photo image file that can be resized/compressed */
90
 
        list ($ret, $this->_items[0]) =
91
 
            $this->_createRandomDataItem($this->_album->getId(), 'image/known',
92
 
                                         array('summary' => 'original summary 0',
93
 
                                         'description' => 'original description 0'));
94
 
        if ($ret) {
95
 
            print $ret->getAsHtml();
96
 
            return $this->failWithStatus($ret);
97
 
        }
98
 
 
99
 
        $this->_itemsUnknown = array();
100
 
        /* Not all file types can be resized/compressed. Here is an example of such item */
101
 
        list ($ret, $this->_itemsUnknown[0]) =
102
 
            $this->_createRandomDataItem($this->_album->getId(), 'image/unknown',
103
 
                                         array('summary' => 'plagiarized summary',
104
 
                                               'description' => 'plagiarized description'));
105
 
        if ($ret) {
106
 
            print $ret->getAsHtml();
107
 
            return $this->failWithStatus($ret);
108
 
        }
109
 
 
110
 
        /* For translation */
111
 
        list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'sizelimit');
112
 
        if ($ret) {
113
 
            print $ret->getAsHtml();
114
 
            return $this->failWithStatus($ret);
115
 
        }
116
 
 
117
 
        $this->_noWarnings = array(array());
118
 
        $this->_noErrors = array();
119
 
 
120
 
        $this->_expectWarnings = array(array(
121
 
            $module->translate(array('text' => 'WARNING: Cannot resize mime type %s',
122
 
                                     'arg1' => $this->_itemsUnknown[0]->getMimeType())),
123
 
            $module->translate(array('text' => 'WARNING: Cannot compress mime type %s',
124
 
                                     'arg1' => $this->_itemsUnknown[0]->getMimeType()))));
125
 
        $this->_expectResize = array('scale');
126
 
        $this->_expectCompress = array('compress');
127
 
    }
128
 
 
129
 
    function testAppropriate() {
130
 
        list ($ret, $appropriate) = $this->_option->isAppropriate($this->_album, null);
131
 
        if ($ret) {
132
 
            return $this->failWithStatus($ret);
133
 
        }
134
 
        $this->assert($appropriate, 'appropriate');
135
 
    }
136
 
 
137
 
    /* change dimensions, don't keep original */
138
 
    function testChangeDimNoOriginal() {
139
 
        global $gallery;
140
 
        $platform =& $gallery->getPlatform();
141
 
 
142
 
        foreach (array('width' => 768, 'height' => 1024, 'keepOriginal' => 0) as $key => $value) {
143
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
144
 
                $key, $value, $this->_album->getId());
145
 
            if ($ret) {
146
 
                return $this->failWithStatus($ret);
147
 
            }
148
 
        }
149
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
150
 
        $this->assertEquals($this->_noErrors, $errors);
151
 
        $this->assertEquals($this->_noWarnings, $warnings);
152
 
 
153
 
        list ($ret, $this->_items[0]) = $this->_items[0]->refresh();
154
 
        if ($ret) {
155
 
            return $this->failWithStatus($ret);
156
 
        }
157
 
 
158
 
        $this->assertEquals(768, $this->_items[0]->getWidth(), 'Width');
159
 
        $this->assertEquals(1024, $this->_items[0]->getHeight(), 'Height');
160
 
        $operation = GalleryUtilities::getRequestVariables('operation');
161
 
        $this->assertEquals($this->_expectResize, $operation, 'operation');
162
 
        $this->assertEquals(2, $this->_items[0]->getSerialNumber(), 'Serial Number');
163
 
        $platform->clearstatcache();
164
 
        $ret = $this->_items[0]->rescan();
165
 
        if ($ret) {
166
 
            return $this->failWithStatus($ret);
167
 
        }
168
 
    }
169
 
 
170
 
    /* change dimensions on multiple items, don't keep originals */
171
 
    function testMultipleChangeDimNoOriginal() {
172
 
        global $gallery;
173
 
        $platform =& $gallery->getPlatform();
174
 
        foreach (array('width' => 768, 'height' => 1024, 'keepOriginal' => 0) as $key => $value) {
175
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit', $key, $value,
176
 
                                                      $this->_album->getId());
177
 
            if ($ret) {
178
 
                return $this->failWithStatus($ret);
179
 
            }
180
 
        }
181
 
 
182
 
        $totalTestItems = 3;
183
 
        for ($j = 1; $j < $totalTestItems; $j++) {
184
 
            list ($ret, $this->_items[$j]) =
185
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/known',
186
 
                                             array('summary' => "original summary $j",
187
 
                                             'description' => "original description $j"));
188
 
            if ($ret) {
189
 
                return $this->failWithStatus($ret);
190
 
            }
191
 
 
192
 
            $this->_noWarnings[] = $this->_noWarnings[$j-1];
193
 
            $this->_expectResize[] = $this->_expectResize[$j-1];
194
 
        }
195
 
 
196
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
197
 
        $this->assertEquals($this->_noErrors, $errors);
198
 
        $this->assertEquals($this->_noWarnings, $warnings);
199
 
 
200
 
        for ($j = 0; $j < $totalTestItems; $j++) {
201
 
            list ($ret, $this->_items[$j]) = $this->_items[$j]->refresh();
202
 
            if ($ret) {
203
 
                return $this->failWithStatus($ret);
204
 
            }
205
 
            $this->assertEquals(768, $this->_items[$j]->getWidth(), "Width $j");
206
 
            $this->assertEquals(1024, $this->_items[$j]->getHeight(), "Height $j");
207
 
            $this->assertEquals(2, $this->_items[$j]->getSerialNumber(), "Serial Number $j");
208
 
            $platform->clearstatcache();
209
 
            $ret = $this->_items[$j]->rescan();
210
 
            if ($ret) {
211
 
                return $this->failWithStatus($ret);
212
 
            }
213
 
        }
214
 
        $operation = GalleryUtilities::getRequestVariables('operation');
215
 
        $this->assertEquals($this->_expectResize, $operation, 'operation');
216
 
    }
217
 
 
218
 
    /* change dimensions, keep original */
219
 
    function testChangeDimKeepOriginal() {
220
 
        global $gallery;
221
 
 
222
 
        foreach (array('width' => 768, 'height' => 1024, 'keepOriginal' => 1) as $key => $value) {
223
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
224
 
                $key, $value, $this->_album->getId());
225
 
            if ($ret) {
226
 
                return $this->failWithStatus($ret);
227
 
            }
228
 
        }
229
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
230
 
        $this->assertEquals($this->_noErrors, $errors);
231
 
        $this->assertEquals($this->_noWarnings, $warnings);
232
 
 
233
 
        /* Check to see if we have a preferred source */
234
 
        list ($ret, $resized) =
235
 
            GalleryCoreApi::fetchPreferredSource($this->_items[0]);
236
 
        if ($ret) {
237
 
            return $this->failWithStatus($ret);
238
 
        }
239
 
        if (!GalleryUtilities::isExactlyA($resized, 'GalleryDerivativeImage')) {
240
 
            return $this->failWithStatus($ret);
241
 
        }
242
 
        $ret = $resized->rebuildCache();
243
 
        if ($ret) {
244
 
            return $this->failWithStatus($ret);
245
 
        }
246
 
        $this->assertEquals(768, $resized->getWidth(), 'Width');
247
 
        $this->assertEquals(1024, $resized->getHeight(), 'Height');
248
 
        $operation = GalleryUtilities::getRequestVariables('operation');
249
 
        $this->assertEquals($this->_expectResize, $operation, 'operation');
250
 
    }
251
 
 
252
 
    /* change dimensions on multiple items, keep originals */
253
 
    function testMultipleChangeDimKeepOriginal() {
254
 
        global $gallery;
255
 
 
256
 
        foreach (array('width' => 768, 'height' => 1024, 'keepOriginal' => 1) as $key => $value) {
257
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
258
 
                $key, $value, $this->_album->getId());
259
 
            if ($ret) {
260
 
                return $this->failWithStatus($ret);
261
 
            }
262
 
        }
263
 
 
264
 
        $totalTestItems = 3;
265
 
        for ($j = 1; $j < $totalTestItems; $j++) {
266
 
            list ($ret, $this->_items[$j]) =
267
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/known',
268
 
                                             array('summary' => "original summary $j",
269
 
                                             'description' => "original description $j"));
270
 
            if ($ret) {
271
 
                return $this->failWithStatus($ret);
272
 
            }
273
 
 
274
 
            $this->_noWarnings[] = $this->_noWarnings[$j-1];
275
 
            $this->_expectResize[] = $this->_expectResize[$j-1];
276
 
        }
277
 
 
278
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
279
 
        $this->assertEquals($this->_noErrors, $errors);
280
 
        $this->assertEquals($this->_noWarnings, $warnings);
281
 
 
282
 
 
283
 
        for ($j = 0; $j < $totalTestItems; $j++) {
284
 
            /* Check to see if we have a preferred source */
285
 
            list ($ret, $resized) =
286
 
                GalleryCoreApi::fetchPreferredSource($this->_items[$j]);
287
 
            if ($ret) {
288
 
                return $this->failWithStatus($ret);
289
 
            }
290
 
            if (!GalleryUtilities::isExactlyA($resized, 'GalleryDerivativeImage')) {
291
 
                return $this->assert(false, 'Not a derivative image');
292
 
            }
293
 
            $ret = $resized->rebuildCache();
294
 
            if ($ret) {
295
 
                return $this->failWithStatus($ret);
296
 
            }
297
 
            $this->assertEquals(768, $resized->getWidth(), 'Width');
298
 
            $this->assertEquals(1024, $resized->getHeight(), 'Height');
299
 
        }
300
 
        $operation = GalleryUtilities::getRequestVariables('operation');
301
 
        $this->assertEquals($this->_expectResize, $operation, 'operation');
302
 
    }
303
 
 
304
 
    /* change size, don't keep original */
305
 
    function testChangeSizeNoOriginal() {
306
 
        global $gallery;
307
 
        $platform =& $gallery->getPlatform();
308
 
 
309
 
        foreach (array('size' => 1000, 'keepOriginal' => 0) as $key => $value) {
310
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
311
 
                $key, $value, $this->_album->getId());
312
 
            if ($ret) {
313
 
                return $this->failWithStatus($ret);
314
 
            }
315
 
        }
316
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
317
 
        $this->assertEquals($this->_noErrors, $errors);
318
 
        $this->assertEquals($this->_noWarnings, $warnings);
319
 
        $platform->clearstatcache();
320
 
 
321
 
        list ($ret, $this->_items[0]) = $this->_items[0]->refresh();
322
 
        if ($ret) {
323
 
            return $this->failWithStatus($ret);
324
 
        }
325
 
 
326
 
        /* check item size (in KB) */
327
 
        $this->assertEquals(1000, $this->_items[0]->getSize(), 'Size');
328
 
        $operation = GalleryUtilities::getRequestVariables('operation');
329
 
        $this->assertEquals($this->_expectCompress, $operation, 'operation');
330
 
        list ($ret, $preferred) =
331
 
            GalleryCoreApi::fetchPreferredSource($this->_items[0]);
332
 
        if ($ret) {
333
 
            return $this->failWithStatus($ret);
334
 
        }
335
 
        $this->assertEquals($this->_items[0], $preferred, 'No preferred');
336
 
    }
337
 
 
338
 
    /* change size on multiple items, don't keep originals */
339
 
    function testMultipleChangeSizeNoOriginal() {
340
 
        global $gallery;
341
 
        $platform =& $gallery->getPlatform();
342
 
 
343
 
        foreach (array('size' => 1000, 'keepOriginal' => 0) as $key => $value) {
344
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
345
 
                $key, $value, $this->_album->getId());
346
 
            if ($ret) {
347
 
                return $this->failWithStatus($ret);
348
 
            }
349
 
        }
350
 
 
351
 
        $totalTestItems = 3;
352
 
        for ($j = 1; $j < $totalTestItems; $j++) {
353
 
            list ($ret, $this->_items[$j]) =
354
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/known',
355
 
                                             array('summary' => "original summary $j",
356
 
                                             'description' => "original description $j"));
357
 
            if ($ret) {
358
 
                return $this->failWithStatus($ret);
359
 
            }
360
 
 
361
 
            $this->_noWarnings[] = $this->_noWarnings[$j-1];
362
 
            $this->_expectCompress[] = $this->_expectCompress[$j-1];
363
 
        }
364
 
 
365
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
366
 
        $this->assertEquals($this->_noErrors, $errors);
367
 
        $this->assertEquals($this->_noWarnings, $warnings);
368
 
 
369
 
        for ($j = 0; $j < $totalTestItems; $j++) {
370
 
            $platform->clearstatcache();
371
 
 
372
 
            list ($ret, $this->_items[$j]) = $this->_items[$j]->refresh();
373
 
            if ($ret) {
374
 
                return $this->failWithStatus($ret);
375
 
            }
376
 
 
377
 
            /* check item size (in KB) */
378
 
            $this->assertEquals(1000, $this->_items[$j]->getSize(), 'Size');
379
 
            list ($ret, $preferred) =
380
 
                GalleryCoreApi::fetchPreferredSource($this->_items[$j]);
381
 
            if ($ret) {
382
 
                return $this->failWithStatus($ret);
383
 
            }
384
 
            $this->assertEquals($this->_items[$j], $preferred, 'No preferred');
385
 
        }
386
 
        $operation = GalleryUtilities::getRequestVariables('operation');
387
 
        $this->assertEquals($this->_expectCompress, $operation, 'operation');
388
 
    }
389
 
 
390
 
    /* change size, keep original */
391
 
    function testChangeSizeKeepOriginal() {
392
 
        foreach (array('size' => 1000, 'keepOriginal' => 1) as $key => $value) {
393
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
394
 
                $key, $value, $this->_album->getId());
395
 
            if ($ret) {
396
 
                return $this->failWithStatus($ret);
397
 
            }
398
 
        }
399
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
400
 
        $this->assertEquals($this->_noErrors, $errors);
401
 
        $this->assertEquals($this->_noWarnings, $warnings);
402
 
 
403
 
        /* Check to see if we have a preferred source */
404
 
        list ($ret, $resized) =
405
 
            GalleryCoreApi::fetchPreferredSource($this->_items[0]);
406
 
        if ($ret) {
407
 
            return $this->failWithStatus($ret);
408
 
        }
409
 
        if (!GalleryUtilities::isExactlyA($resized, 'GalleryDerivativeImage')) {
410
 
            return $this->assert(false, 'Not a derivative');
411
 
        }
412
 
        $ret = $resized->rebuildCache();
413
 
        if ($ret) {
414
 
            return $this->failWithStatus($ret);
415
 
        }
416
 
 
417
 
        /* check derivative size */
418
 
        $this->assertEquals(1000, $resized->getDerivativeSize(), 'Size');
419
 
        $operation = GalleryUtilities::getRequestVariables('operation');
420
 
        $this->assertEquals($this->_expectCompress, $operation, 'operation');
421
 
    }
422
 
 
423
 
    /* change size on multiple items, keep originals */
424
 
    function testMultipleChangeSizeKeepOriginal() {
425
 
        foreach (array('size' => 1000, 'keepOriginal' => 1) as $key => $value) {
426
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
427
 
                $key, $value, $this->_album->getId());
428
 
            if ($ret) {
429
 
                return $this->failWithStatus($ret);
430
 
            }
431
 
        }
432
 
 
433
 
        $totalTestItems = 3;
434
 
        for ($j = 1; $j < $totalTestItems; $j++) {
435
 
            list ($ret, $this->_items[$j]) =
436
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/known',
437
 
                                             array('summary' => "original summary $j",
438
 
                                             'description' => "original description $j"));
439
 
            if ($ret) {
440
 
                return $this->failWithStatus($ret);
441
 
            }
442
 
 
443
 
            $this->_noWarnings[] = $this->_noWarnings[$j-1];
444
 
            $this->_expectCompress[] = $this->_expectCompress[$j-1];
445
 
        }
446
 
 
447
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
448
 
        $this->assertEquals($this->_noErrors, $errors);
449
 
        $this->assertEquals($this->_noWarnings, $warnings);
450
 
 
451
 
        for ($j = 0; $j < $totalTestItems; $j++) {
452
 
            /* Check to see if we have a preferred source */
453
 
            list ($ret, $resized) =
454
 
                GalleryCoreApi::fetchPreferredSource($this->_items[$j]);
455
 
            if ($ret) {
456
 
                return $this->failWithStatus($ret);
457
 
            }
458
 
            if (!GalleryUtilities::isExactlyA($resized, 'GalleryDerivativeImage')) {
459
 
                return $this->assert(false, 'Not a derivative');
460
 
            }
461
 
            $ret = $resized->rebuildCache();
462
 
            if ($ret) {
463
 
                return $this->failWithStatus($ret);
464
 
            }
465
 
 
466
 
            /* check derivative size */
467
 
            $this->assertEquals(1000, $resized->getDerivativeSize(), 'Size');
468
 
        }
469
 
        $operation = GalleryUtilities::getRequestVariables('operation');
470
 
        $this->assertEquals($this->_expectCompress, $operation, 'operation');
471
 
    }
472
 
 
473
 
    /* change dimensions AND size, keep original */
474
 
    function testChangeBothKeepOriginal() {
475
 
        global $gallery;
476
 
        $platform =& $gallery->getPlatform();
477
 
        $this->_expectResizeCompress = array_merge($this->_expectResize, $this->_expectCompress);
478
 
        foreach (array('width' => 768, 'height' => 1024, 'size' => 1000, 'keepOriginal' => 1)
479
 
            as $key => $value) {
480
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
481
 
                $key, $value, $this->_album->getId());
482
 
            if ($ret) {
483
 
                return $this->failWithStatus($ret);
484
 
            }
485
 
        }
486
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
487
 
        $this->assertEquals($this->_noErrors, $errors);
488
 
        $this->assertEquals($this->_noWarnings, $warnings);
489
 
 
490
 
        /* Check to see if we have a preferred source */
491
 
        list ($ret, $resized) = GalleryCoreApi::fetchPreferredSource($this->_items[0]);
492
 
        if ($ret) {
493
 
            return $this->failWithStatus($ret);
494
 
        }
495
 
        if (!GalleryUtilities::isExactlyA($resized, 'GalleryDerivativeImage')) {
496
 
            return $this->assert(false, 'Not a derivative');
497
 
        }
498
 
        $ret = $resized->rebuildCache();
499
 
        if ($ret) {
500
 
            return $this->failWithStatus($ret);
501
 
        }
502
 
        $this->assertEquals('scale|768,1024;compress|1000',
503
 
                            $resized->getDerivativeOperations(), 'Operations');
504
 
        $this->assertEquals(768, $resized->getWidth(), 'Width');
505
 
        $this->assertEquals(1024, $resized->getHeight(), 'Height');
506
 
        $this->assertEquals(1000, $resized->getDerivativeSize(), 'Size');
507
 
        $operation = GalleryUtilities::getRequestVariables('operation');
508
 
        $this->assertEquals($this->_expectResizeCompress, $operation, 'operation');
509
 
    }
510
 
 
511
 
    /* change dimensions AND size on multiple items, keep originals */
512
 
    function testMultipleChangeBothKeepOriginal() {
513
 
        $this->_expectResizeCompress = array_merge($this->_expectResize, $this->_expectCompress);
514
 
        foreach (array('width' => 768, 'height' => 1024, 'size' => 1000, 'keepOriginal' => 1)
515
 
            as $key => $value) {
516
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
517
 
                $key, $value, $this->_album->getId());
518
 
            if ($ret) {
519
 
                return $this->failWithStatus($ret);
520
 
            }
521
 
        }
522
 
 
523
 
        $totalTestItems = 3;
524
 
        for ($j = 1; $j < $totalTestItems; $j++) {
525
 
            list ($ret, $this->_items[$j]) =
526
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/known',
527
 
                                             array('summary' => "original summary $j",
528
 
                                                   'description' => "original description $j"));
529
 
            if ($ret) {
530
 
                return $this->failWithStatus($ret);
531
 
            }
532
 
 
533
 
            $this->_noWarnings[] = $this->_noWarnings[$j-1];
534
 
            $this->_expectResizeCompress[] = $this->_expectResizeCompress[($j*2)-2];
535
 
            $this->_expectResizeCompress[] = $this->_expectResizeCompress[($j*2)-1];
536
 
        }
537
 
 
538
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
539
 
        $this->assertEquals($this->_noErrors, $errors);
540
 
        $this->assertEquals($this->_noWarnings, $warnings);
541
 
 
542
 
        for ($j = 0; $j < $totalTestItems; $j++) {
543
 
            /* Check to see if we have a preferred source */
544
 
            list ($ret, $resized) = GalleryCoreApi::fetchPreferredSource($this->_items[$j]);
545
 
            if ($ret) {
546
 
                return $this->failWithStatus($ret);
547
 
            }
548
 
            if (!GalleryUtilities::isExactlyA($resized, 'GalleryDerivativeImage')) {
549
 
                return $this->assert(false, 'Not a derivative');
550
 
            }
551
 
            $ret = $resized->rebuildCache();
552
 
            if ($ret) {
553
 
                return $this->failWithStatus($ret);
554
 
            }
555
 
            $this->assertEquals('scale|768,1024;compress|1000',
556
 
                                $resized->getDerivativeOperations(),
557
 
                                'Operations');
558
 
            $this->assertEquals(768, $resized->getWidth(), 'Width');
559
 
            $this->assertEquals(1024, $resized->getHeight(), 'Height');
560
 
            $this->assertEquals(1000, $resized->getDerivativeSize(), 'Size');
561
 
        }
562
 
        $operation = GalleryUtilities::getRequestVariables('operation');
563
 
        $this->assertEquals($this->_expectResizeCompress, $operation, 'operation');
564
 
    }
565
 
 
566
 
    /* change dimensions AND size, don't keep original */
567
 
    function testChangeBothNoOriginal() {
568
 
        global $gallery;
569
 
        $platform =& $gallery->getPlatform();
570
 
        $this->_expectResizeCompress = array_merge($this->_expectResize, $this->_expectCompress);
571
 
        foreach (array('width' => 768, 'height' => 1024, 'size' => 1000, 'keepOriginal' => 0)
572
 
                as $key => $value) {
573
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
574
 
                $key, $value, $this->_album->getId());
575
 
            if ($ret) {
576
 
                return $this->failWithStatus($ret);
577
 
            }
578
 
        }
579
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
580
 
        $this->assertEquals($this->_noErrors, $errors);
581
 
        $this->assertEquals($this->_noWarnings, $warnings);
582
 
 
583
 
        list ($ret, $this->_items[0]) = $this->_items[0]->refresh();
584
 
        if ($ret) {
585
 
            return $this->failWithStatus($ret);
586
 
        }
587
 
 
588
 
        $this->assertEquals(768, $this->_items[0]->getWidth(), 'Width');
589
 
        $this->assertEquals(1024, $this->_items[0]->getHeight(), 'Height');
590
 
        $operation = GalleryUtilities::getRequestVariables('operation');
591
 
        $this->assertEquals($this->_expectResizeCompress, $operation, 'operation');
592
 
        $platform->clearstatcache();
593
 
        $ret = $this->_items[0]->rescan();
594
 
        if ($ret) {
595
 
            return $this->failWithStatus($ret);
596
 
        }
597
 
        $this->assertEquals(1000, $this->_items[0]->getSize(), 'Size');
598
 
    }
599
 
 
600
 
    /* change dimensions AND size on multiple items, don't keep originals */
601
 
    function testMultipleChangeBothNoOriginal() {
602
 
        global $gallery;
603
 
        $platform =& $gallery->getPlatform();
604
 
        $this->_expectResizeCompress = array_merge($this->_expectResize, $this->_expectCompress);
605
 
        foreach (array('width' => 768, 'height' => 1024, 'size' => 1000, 'keepOriginal' => 0)
606
 
                as $key => $value) {
607
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
608
 
                $key, $value, $this->_album->getId());
609
 
            if ($ret) {
610
 
                return $this->failWithStatus($ret);
611
 
            }
612
 
        }
613
 
 
614
 
        $totalTestItems = 3;
615
 
        for ($j = 1; $j < $totalTestItems; $j++) {
616
 
            list ($ret, $this->_items[$j]) =
617
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/known',
618
 
                                             array('summary' => "original summary $j",
619
 
                                                   'description' => "original description $j"));
620
 
            if ($ret) {
621
 
                return $this->failWithStatus($ret);
622
 
            }
623
 
 
624
 
            $this->_noWarnings[] = $this->_noWarnings[$j-1];
625
 
            $this->_expectResizeCompress[] = $this->_expectResizeCompress[($j*2)-2];
626
 
            $this->_expectResizeCompress[] = $this->_expectResizeCompress[($j*2)-1];
627
 
        }
628
 
 
629
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
630
 
        $this->assertEquals($this->_noErrors, $errors);
631
 
        $this->assertEquals($this->_noWarnings, $warnings);
632
 
 
633
 
        for ($j = 0; $j < $totalTestItems; $j++) {
634
 
            list ($ret, $item) = $this->_items[$j]->refresh();
635
 
            if ($ret) {
636
 
                return $this->failWithStatus($ret);
637
 
            }
638
 
            $this->assertEquals(768, $item->getWidth(), 'Width');
639
 
            $this->assertEquals(1024, $item->getHeight(), 'Height');
640
 
            $platform->clearstatcache();
641
 
            $ret = $item->rescan();
642
 
            if ($ret) {
643
 
                return $this->failWithStatus($ret);
644
 
            }
645
 
            $this->assertEquals(1000, $item->getSize(), 'Size');
646
 
        }
647
 
        $operation = GalleryUtilities::getRequestVariables('operation');
648
 
        $this->assertEquals($this->_expectResizeCompress, $operation, 'operation');
649
 
    }
650
 
 
651
 
    /* try to change size and dimensions for unsupported file format */
652
 
    function testUnknownFormat() {
653
 
        foreach (array('width' => 768, 'height' => 1024, 'size' => 1000, 'keepOriginal' => 0)
654
 
            as $key => $value) {
655
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
656
 
                $key, $value, $this->_album->getId());
657
 
            if ($ret) {
658
 
                return $this->failWithStatus($ret);
659
 
            }
660
 
        }
661
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_itemsUnknown);
662
 
        $this->assertEquals($this->_noErrors, $errors);
663
 
        $this->assertEquals($this->_expectWarnings, $warnings);
664
 
    }
665
 
 
666
 
    /* try to change size and dimensions for unsupported file format on multiple files */
667
 
    function testMultipleUnknownFormat() {
668
 
        foreach (array('width' => 768, 'height' => 1024, 'size' => 1000, 'keepOriginal' => 0)
669
 
            as $key => $value) {
670
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
671
 
                $key, $value, $this->_album->getId());
672
 
            if ($ret) {
673
 
                return $this->failWithStatus($ret);
674
 
            }
675
 
        }
676
 
 
677
 
        $singleExpectedWarning = $this->_expectWarnings[0];
678
 
 
679
 
        $totalTestItems = 3;
680
 
        for ($j = 1; $j < $totalTestItems; $j++) {
681
 
            list ($ret, $this->_itemsUnknown[$j]) =
682
 
                $this->_createRandomDataItem($this->_album->getId(), 'image/unknown',
683
 
                                             array('summary' => "plagiarized summary $j",
684
 
                                                   'description' => "plagiarized description $j"));
685
 
            if ($ret) {
686
 
                return $this->failWithStatus($ret);
687
 
            }
688
 
 
689
 
            $this->_expectWarnings[] = $singleExpectedWarning;
690
 
        }
691
 
 
692
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_itemsUnknown);
693
 
        $this->assertEquals($this->_noErrors, $errors);
694
 
        $this->assertEquals($this->_expectWarnings, $warnings);
695
 
    }
696
 
 
697
 
    /* ensure no change when image is already smaller than limits (don't stretch) */
698
 
    function testBelowLimits() {
699
 
        foreach (array('width' => 1800, 'height' => 2400, 'size' => 30000, 'keepOriginal' => 0)
700
 
                as $key => $value) {
701
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
702
 
                $key, $value, $this->_album->getId());
703
 
            if ($ret) {
704
 
                return $this->failWithStatus($ret);
705
 
            }
706
 
        }
707
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), $this->_items);
708
 
        $this->assertEquals($this->_noErrors, $errors);
709
 
        $this->assertEquals($this->_noWarnings, $warnings);
710
 
 
711
 
        list ($ret, $this->_items[0]) = $this->_items[0]->refresh();
712
 
        if ($ret) {
713
 
            return $this->failWithStatus($ret);
714
 
        }
715
 
 
716
 
        $this->assertEquals(1728, $this->_items[0]->getWidth(), 'Width');
717
 
        $this->assertEquals(2304, $this->_items[0]->getHeight(), 'Height');
718
 
        $operation = GalleryUtilities::getRequestVariables('operation');
719
 
        $this->assertEquals('', $operation, 'no operation');
720
 
        $this->assertEquals(1, $this->_items[0]->getSerialNumber(), 'Serial Number');
721
 
    }
722
 
 
723
 
    /* change dimensions, delete larger resizes */
724
 
    function testChangeDimDropResizes() {
725
 
        global $gallery;
726
 
        $platform =& $gallery->getPlatform();
727
 
        $id = $this->_items[0]->getId();
728
 
        $item =& $this->_items[0];
729
 
 
730
 
        /* Add resizes and a thumbnail, that is usually done before sizelimit does its job */
731
 
        list ($ret, $thumbnail) = $this->_createDerivative($item, $id,
732
 
                                                           DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
733
 
                                                           'thumbnail|2000',
734
 
                                                           'test/file');
735
 
        if ($ret) {
736
 
            return $this->failWithStatus($ret);
737
 
        }
738
 
 
739
 
        list ($ret, $tooLargeResize) = $this->_createDerivative($item, $id,
740
 
                                                                DERIVATIVE_TYPE_IMAGE_RESIZE,
741
 
                                                                'scale|800,1400',
742
 
                                                                'test/file');
743
 
        if ($ret) {
744
 
            return $this->failWithStatus($ret);
745
 
        }
746
 
        list ($ret, $smallResize) = $this->_createDerivative($item, $id,
747
 
                                                             DERIVATIVE_TYPE_IMAGE_RESIZE,
748
 
                                                             'scale|600,800',
749
 
                                                             'test/file');
750
 
        if ($ret) {
751
 
            return $this->failWithStatus($ret);
752
 
        }
753
 
        $ret = $this->_verifyEntity($tooLargeResize);
754
 
        $this->assertEquals(null, $ret, 'verify large resize before test');
755
 
        $ret = $this->_verifyEntity($smallResize);
756
 
        $this->assertEquals(null, $ret, 'verify small resize before test');
757
 
        $ret = $this->_verifyEntity($thumbnail);
758
 
        $this->assertEquals(null, $ret, 'verify thumb before test');
759
 
 
760
 
        /* Set the sizelimit settings for the album */
761
 
        foreach (array('width' => 768, 'height' => 1024, 'keepOriginal' => 0) as $key => $value) {
762
 
            $ret = GalleryCoreApi::setPluginParameter('module', 'sizelimit',
763
 
                $key, $value, $this->_album->getId());
764
 
            if ($ret) {
765
 
                return $this->failWithStatus($ret);
766
 
            }
767
 
        }
768
 
        /* Execute the test */
769
 
        list ($errors, $warnings) = $this->_handleRequestAfterAdd(array(), array($item));
770
 
        $this->assertEquals($this->_noErrors, $errors);
771
 
        $this->assertEquals($this->_noWarnings, $warnings);
772
 
 
773
 
        list ($ret, $item) = $item->refresh();
774
 
        if ($ret) {
775
 
            return $this->failWithStatus($ret);
776
 
        }
777
 
 
778
 
        $this->assertEquals(768, $item->getWidth(), 'Width');
779
 
        $this->assertEquals(1024, $item->getHeight(), 'Height');
780
 
        $operation = GalleryUtilities::getRequestVariables('operation');
781
 
        $this->assertEquals($this->_expectResize, $operation, 'operation');
782
 
        $this->assertEquals(2, $item->getSerialNumber(), 'Serial Number');
783
 
        $platform->clearstatcache();
784
 
 
785
 
        /* We should have a new thumb and one resize should be deleted */
786
 
        $ret = $this->_verifyMissing($tooLargeResize->getId());
787
 
        $this->assertEquals(null, $ret, 'verify missing large resize');
788
 
        $ret = $this->_verifyEntity($smallResize);
789
 
        $this->assertEquals(null, $ret, 'verify small resize');
790
 
        $ret = $this->_verifyEntity($thumbnail);
791
 
        $this->assertEquals(null, $ret, 'verify that the same thumb still exists');
792
 
        list ($ret, $thumbTable) = GalleryCoreApi::fetchThumbnailsByItemIds(array($id));
793
 
        if ($ret) {
794
 
            return $this->failWithStatus($ret);
795
 
        }
796
 
        if (!empty($thumbTable[$id])) {
797
 
            $this->assertEquals($thumbnail->getId(), $thumbTable[$id]->getId(),
798
 
                                'same old thumbnail');
799
 
        } else {
800
 
            $this->assert(false, 'no thumbnail for our item id after test');
801
 
        }
802
 
    }
803
 
}
804
 
 
805
 
/**
806
 
 * Test platform
807
 
 *
808
 
 * @package SizeLimit
809
 
 * @subpackage PHPUnit
810
 
 */
811
 
class SetSizeOptionTestPlatform extends GalleryPlatform {
812
 
 
813
 
    function SetSizeOptionTestPlatform($originalPlatform) {
814
 
        $this->_savedPlatform = $originalPlatform;
815
 
        GalleryUtilities::putRequestVariable('resizePerformed', false);
816
 
        GalleryUtilities::putRequestVariable('compressPerformed', false);
817
 
    }
818
 
 
819
 
    function filesize($file) {
820
 
        $size = GalleryUtilities::getRequestVariables('size_' . basename($file));
821
 
        if ($size) {
822
 
            return (int)$size;
823
 
        }
824
 
        return 26031966;
825
 
    }
826
 
 
827
 
    function getimagesize($file) {
828
 
        $imagesize = GalleryUtilities::getRequestVariables('imagesize_' . basename($file));
829
 
        if ($imagesize) {
830
 
            /* doing it this way keeps CodeAudit happy */
831
 
            return array($imagesize[0], $imagesize[1]);
832
 
        }
833
 
        return array(1728, 2304);
834
 
    }
835
 
 
836
 
    function file_exists($filename) {
837
 
        $compressPerformed = GalleryUtilities::getRequestVariables('compressPerformed');
838
 
        $resizePerformed = GalleryUtilities::getRequestVariables('resizePerformed');
839
 
 
840
 
        /* if we created a derivative make-believe that the file exists */
841
 
        if ($resizePerformed || $compressPerformed) {
842
 
            return true;
843
 
        } else {
844
 
            return $this->_savedPlatform->file_exists($filename);
845
 
        }
846
 
    }
847
 
 
848
 
    function unlink($filename) {
849
 
        /* if it is a derivative - we didn't really create it */
850
 
        if ($this->_savedPlatform->file_exists($filename)) {
851
 
            return $this->_savedPlatform->unlink($filename);
852
 
        } else {
853
 
            return true;
854
 
        }
855
 
    }
856
 
}
857
 
 
858
 
/**
859
 
 * Test toolkit
860
 
 */
861
 
class SetSizeOptionTestToolkit extends GalleryToolkit {
862
 
    /**
863
 
     * @see GalleryToolkit::performOperation
864
 
     */
865
 
    function performOperation($mimeType, $operationName, $sourceFilename,
866
 
                              $destFilename, $parameters, $context=array()) {
867
 
        /* Here we are pretending that conversion happened by setting the flag */
868
 
        switch($operationName) {
869
 
        case 'compress':
870
 
            GalleryUtilities::putRequestVariable('compressPerformed', true);
871
 
            GalleryUtilities::putRequestVariable('size_' . basename($destFilename), 1000);
872
 
            break;
873
 
 
874
 
        case 'scale':
875
 
            GalleryUtilities::putRequestVariable('resizePerformed', true);
876
 
            GalleryUtilities::putRequestVariable(
877
 
                'imagesize_' . basename($destFilename), array(768, 1024));
878
 
            break;
879
 
 
880
 
        default:
881
 
            return
882
 
                array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED), null, null);
883
 
        }
884
 
 
885
 
        $operations = array();
886
 
        $operationRequest = GalleryUtilities::getRequestVariables('operation');
887
 
        if (!empty($operationRequest)) {
888
 
              $operations = $operationRequest;
889
 
        }
890
 
        $operations[] = $operationName;
891
 
        GalleryUtilities::putRequestVariable('operation', $operations);
892
 
 
893
 
        return array(null, $mimeType, $context);
894
 
    }
895
 
 
896
 
    /**
897
 
     * @see GalleryToolkit::getProperty
898
 
     */
899
 
    function getProperty($mimeType, $propertyName, $file) {
900
 
        global $gallery;
901
 
 
902
 
        /* Our 'mime type' has to support dimensions */
903
 
        switch($propertyName) {
904
 
        case 'dimensions':
905
 
            $platform =& $gallery->getPlatform();
906
 
            $results = $platform->getimagesize($file);
907
 
            $results = array((int)$results[0], (int)$results[1]);
908
 
            break;
909
 
 
910
 
        default:
911
 
            return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED), null);
912
 
        }
913
 
 
914
 
        return array(null, $results);
915
 
    }
916
 
}
917
 
 
918
 
?>