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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/ItemEditPhotoPluginTest.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
 
 * ItemEditPhoto controller tests
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class ItemEditPhotoPluginTest extends ItemEditPluginTestCase {
29
 
 
30
 
    function ItemEditPhotoPluginTest($methodName) {
31
 
        $this->ItemEditPluginTestCase($methodName, 'core', 'ItemEditPhoto');
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        parent::setUp();
36
 
 
37
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
38
 
        if ($ret) {
39
 
            return $this->failWithStatus($ret);
40
 
        }
41
 
        $this->_markForCleanup($this->_album);
42
 
 
43
 
        /* Register a dummy toolkit */
44
 
        $path = 'modules/core/test/phpunit/ItemEditPhotoPluginTest.class';
45
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
46
 
            'GalleryToolkit', 'ItemEditPhotoPluginTestToolkit', 'ItemEditPhotoPluginTestToolkit',
47
 
            $path, 'coreTest', null);
48
 
        if ($ret) {
49
 
            print $ret->getAsHtml();
50
 
            return $this->failWithStatus($ret);
51
 
        }
52
 
        $this->_markToolkitForCleanup('ItemEditPhotoPluginTestToolkit');
53
 
 
54
 
        /* Register operations that we can perform on our mock data items */
55
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
56
 
                array('test/file'), 'scale',
57
 
                array(array('type' => 'int', 'description' => 'test'),
58
 
                      array('type' => 'int', 'description' => 'test')),
59
 
                'test-description');
60
 
        if ($ret) {
61
 
            return $this->failWithStatus($ret);
62
 
        }
63
 
 
64
 
        $ret = GalleryCoreApi::registerToolkitOperation(
65
 
            'ItemEditPhotoPluginTestToolkit', array('test/file'), 'convert-to-image/jpeg',
66
 
            array(), 'test-description', 'image/jpeg');
67
 
        if ($ret) {
68
 
            return $this->failWithStatus($ret);
69
 
        }
70
 
 
71
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
72
 
                array('test/file'), 'rotate',
73
 
                array(array('type' => 'int', 'description' => 'test')),
74
 
                'test-description');
75
 
        if ($ret) {
76
 
            return $this->failWithStatus($ret);
77
 
        }
78
 
 
79
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
80
 
                array('test/file'), 'thumbnail',
81
 
                array(array('type' => 'int', 'description' => 'test'),
82
 
                      array('type' => 'int', 'description' => 'test')),
83
 
                'test-description', '');
84
 
        if ($ret) {
85
 
            print $ret->getAsHtml();
86
 
            return $this->failWithStatus($ret);
87
 
        }
88
 
 
89
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemEditPhotoPluginTestToolkit',
90
 
                array('test/file'), 'resize',
91
 
                array(array('type' => 'int', 'description' => 'test'),
92
 
                      array('type' => 'int', 'description' => 'test')),
93
 
                'test-description', '');
94
 
        if ($ret) {
95
 
            print $ret->getAsHtml();
96
 
            return $this->failWithStatus($ret);
97
 
        }
98
 
 
99
 
        list ($ret, $this->_item) =
100
 
            $this->_createRandomDataItem($this->_album->getId(), 'test/file');
101
 
        if ($ret) {
102
 
            print $ret->getAsHtml();
103
 
            return $this->failWithStatus($ret);
104
 
        }
105
 
 
106
 
        $this->_unused = null;
107
 
    }
108
 
 
109
 
    function testEditResizes() {
110
 
        list ($ret, $derivative[0]) =
111
 
            $this->_createDerivative($this->_item, $this->_item->getId(),
112
 
                                     DERIVATIVE_TYPE_IMAGE_PREFERRED, 'rotate|90');
113
 
        if ($ret) {
114
 
            return $this->failWithStatus($ret);
115
 
        }
116
 
 
117
 
        list ($ret, $derivative[1]) =
118
 
            $this->_createDerivative($this->_item, $derivative[0]->getId(),
119
 
                                     DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'thumbnail|200');
120
 
        if ($ret) {
121
 
            return $this->failWithStatus($ret);
122
 
        }
123
 
 
124
 
        list ($ret, $derivative[2]) = $this->_createDerivative(
125
 
                $this->_item, $derivative[0]->getId(),
126
 
                DERIVATIVE_TYPE_IMAGE_RESIZE, 'scale|500,500;convert-to-image/jpeg', 'test/file',
127
 
                array('postFilterOperations' => 'test-operation|0'));
128
 
        if ($ret) {
129
 
            return $this->failWithStatus($ret);
130
 
        }
131
 
        /* Use valid inputs */
132
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
133
 
 
134
 
        for ($i = 0; $i < 3; $i++) {
135
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
136
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 250 * ($i+1));
137
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 250 * ($i+1));
138
 
        }
139
 
        GalleryUtilities::putRequestVariable('form[resizes][2][width]', '50%');
140
 
        GalleryUtilities::putRequestVariable('form[resizes][2][height]', 600);
141
 
 
142
 
        /* Perform the request and verify that we succeeded */
143
 
        $results = $this->handleRequest($this->_item, $this->_unused);
144
 
        $this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
145
 
                            $results);
146
 
 
147
 
        /* Examine the resizes and make sure they line up */
148
 
        list ($ret, $resizeTable) =
149
 
            GalleryCoreApi::fetchResizesByItemIds(array($this->_item->getId()));
150
 
        if ($ret) {
151
 
            return $this->failWithStatus($ret);
152
 
        }
153
 
 
154
 
        if (empty($resizeTable[$this->_item->getId()])) {
155
 
            $this->assert(false, 'no resizes!');
156
 
        } else {
157
 
            $resizeTable = $resizeTable[$this->_item->getId()];
158
 
            $this->assertEquals(3, count($resizeTable), 'resize count');
159
 
            for ($i = 0; $i < 3; $i++) {
160
 
                $resize = $resizeTable[$i];
161
 
                if (!isset($resize)) {
162
 
                    continue;
163
 
                }
164
 
                $this->assertEquals($derivative[0]->getId(), $resize->getDerivativeSourceId(),
165
 
                        'Derivative Source id');
166
 
                $expectedOperations = ($i < 2)
167
 
                    ? ('scale|' . ($i ? 250 : 500) . ',' . ($i ? 250 : 500)) : 'scale|50%,600';
168
 
                $expectedOperations .= ';convert-to-image/jpeg';
169
 
                $this->assertEquals($expectedOperations, $resize->getDerivativeOperations(), $i);
170
 
                /* check that postfilteroperations are maintained and added to new resizes.. */
171
 
                $this->assertEquals('test-operation|0', $resize->getPostFilterOperations(),
172
 
                        "postfilter $i");
173
 
            }
174
 
            /* Verify we reuse existing derivatives when possible */
175
 
            $this->assertEquals($derivative[2]->getId(), $resizeTable[0]->getId(), 'derivative id');
176
 
        }
177
 
    }
178
 
 
179
 
    function testEditResizesMissing() {
180
 
        /* Use valid inputs */
181
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
182
 
 
183
 
        for ($i = 0; $i < 3; $i++) {
184
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
185
 
        }
186
 
 
187
 
        /* Perform the request and verify that we failed */
188
 
        $results = $this->handleRequest($this->_item, $this->_unused);
189
 
        $this->assertEquals(array(array('form[error][resizes][0][size][missing]',
190
 
                                        'form[error][resizes][1][size][missing]',
191
 
                                        'form[error][resizes][2][size][missing]'),
192
 
                                  null),
193
 
                            $results);
194
 
    }
195
 
 
196
 
    function testEditResizesInvalid() {
197
 
        /* Use valid inputs */
198
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
199
 
 
200
 
        for ($i = 0; $i < 3; $i++) {
201
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
202
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 'x' . rand());
203
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 100);
204
 
        }
205
 
        GalleryUtilities::putRequestVariable('form[resizes][2][width]', 100);
206
 
        GalleryUtilities::putRequestVariable('form[resizes][2][height]', 'x' . rand());
207
 
 
208
 
        /* Perform the request and verify that we failed */
209
 
        $results = $this->handleRequest($this->_item, $this->_unused);
210
 
        $this->assertEquals(array(array('form[error][resizes][0][size][invalid]',
211
 
                                        'form[error][resizes][1][size][invalid]',
212
 
                                        'form[error][resizes][2][size][invalid]'),
213
 
                                  null),
214
 
                            $results);
215
 
    }
216
 
 
217
 
    function testEditResizesNoDerivative() {
218
 
        $itemId = $this->_item->getId();
219
 
        /* Use valid inputs */
220
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
221
 
 
222
 
        for ($i = 0; $i < 3; $i++) {
223
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
224
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 100 * ($i+1));
225
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 100 * ($i+1));
226
 
        }
227
 
        GalleryUtilities::putRequestVariable('form[resizes][2][height]', 600);
228
 
 
229
 
        /* Perform the request and verify that we succeeded */
230
 
        $results = $this->handleRequest($this->_item, $this->_unused);
231
 
        $this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
232
 
                            $results);
233
 
 
234
 
        /* Examine the resizes and make sure they line up */
235
 
        list ($ret, $resizeTable) =
236
 
            GalleryCoreApi::fetchResizesByItemIds(array($itemId));
237
 
        if ($ret) {
238
 
            return $this->failWithStatus($ret);
239
 
        }
240
 
        list ($ret, $preferred) =
241
 
            GalleryCoreApi::fetchPreferredSource($this->_item);
242
 
        if ($ret) {
243
 
            return $this->failWithStatus($ret);
244
 
        }
245
 
 
246
 
        if (!empty($resizeTable[$itemId])) {
247
 
            $this->assertEquals(3, sizeof($resizeTable[$this->_item->getId()]));
248
 
            for ($i = 0; $i < 3; $i++) {
249
 
                $this->assertEquals($itemId, $resizeTable[$itemId][$i]->
250
 
                    getDerivativeSourceId(), 'Derivative Source id');
251
 
                $expectedOperations =
252
 
                    ($i < 2) ? ('scale|' . 100 * ($i+1) . ',' . 100 * ($i+1)) : 'scale|300,600';
253
 
                $expectedOperations .= ';convert-to-image/jpeg';
254
 
                $this->assertEquals(
255
 
                    $expectedOperations,
256
 
                    $resizeTable[$itemId][$i]->getDerivativeOperations());
257
 
            }
258
 
        }
259
 
    }
260
 
 
261
 
    function testEditResizesRaw() {
262
 
        /*
263
 
         * Test the situation when the original file is "raw" - cannot be resized or
264
 
         * rotated; however prefered can
265
 
         */
266
 
        $itemId = $this->_item->getId();
267
 
        $ret = GalleryCoreApi::registerToolkitOperation(
268
 
            'ItemEditPhotoPluginTestToolkit', array('test/blob'), 'convert-to-image/jpeg',
269
 
            array(), 'test-description', 'image/jpeg');
270
 
        if ($ret) {
271
 
            return $this->failWithStatus($ret);
272
 
        }
273
 
 
274
 
        list ($ret, $this->_blob) =
275
 
            $this->_createRandomDataItem($this->_album->getId(), 'test/blob');
276
 
        if ($ret) {
277
 
            return $this->failWithStatus($ret);
278
 
        }
279
 
        list ($ret, $derivative[0]) =
280
 
            $this->_createDerivative($this->_item, $itemId,
281
 
                                     DERIVATIVE_TYPE_IMAGE_PREFERRED, 'convert-to-image/jpeg');
282
 
        if ($ret) {
283
 
            return $this->failWithStatus($ret);
284
 
        }
285
 
 
286
 
        /* Use valid inputs */
287
 
        GalleryUtilities::putRequestVariable('form[action][save]', 1);
288
 
 
289
 
        for ($i = 0; $i < 3; $i++) {
290
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][active]', 1);
291
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][width]', 100 * ($i+1));
292
 
            GalleryUtilities::putRequestVariable('form[resizes][' . $i . '][height]', 100 * ($i+1));
293
 
        }
294
 
 
295
 
        /* Perform the request and verify that we succeeded */
296
 
        $results = $this->handleRequest($this->_item, $this->_unused);
297
 
        $this->assertEquals(array(array(), $this->_translate('Changes saved successfully')),
298
 
                            $results);
299
 
        list ($ret, $resizeTable) = GalleryCoreApi::fetchDerivativesByItemIds(
300
 
            array($this->_item->getId()));
301
 
        if ($ret) {
302
 
            return $this->failWithStatus($ret);
303
 
        }
304
 
        $this->assert($resizeTable[$itemId], "no derivative");
305
 
        $preferredId = $resizeTable[$itemId][0]->getId();
306
 
        /* check that the derivatives are from the preferred source, not from the original */
307
 
        $this->assertEquals(4, sizeof($resizeTable[$itemId]));
308
 
        for ($i = 1; $i < 4; $i++) {
309
 
            $this->assertEquals($preferredId, $resizeTable[$itemId][$i]->
310
 
                getDerivativeSourceId(), 'Derivative Source id');
311
 
            $this->assertEquals('scale|' . 100 * $i . ',' . 100 * $i . ';convert-to-image/jpeg',
312
 
                $resizeTable[$itemId][$i]->
313
 
                getDerivativeOperations(), 'Derivative Source id');
314
 
        }
315
 
    }
316
 
}
317
 
 
318
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');
319
 
 
320
 
/**
321
 
 * Test toolkit
322
 
 */
323
 
class ItemEditPhotoPluginTestToolkit extends GalleryToolkit {
324
 
    /**
325
 
     * @see GalleryToolkit::performOperation
326
 
     * Dummy operation
327
 
     */
328
 
    function performOperation($mimeType, $operationName, $sourceFilename,
329
 
                              $destFilename, $parameters, $context=array()) {
330
 
        global $gallery;
331
 
        $platform =& $gallery->getPlatform();
332
 
 
333
 
        if ($sourceFilename != $destFilename) {
334
 
            $platform->copy($sourceFilename, $destFilename);
335
 
        }
336
 
 
337
 
        $fd = $platform->fopen($destFilename, 'a');
338
 
        $platform->fwrite($fd, $operationName);
339
 
        $platform->fclose($fd);
340
 
 
341
 
        return array(null, $mimeType, $context);
342
 
    }
343
 
 
344
 
    /**
345
 
     * @see GalleryToolkit::mergeOperations
346
 
     * Dummy operation
347
 
     */
348
 
    function mergeOperations($operation1, $args1, $operation2, $args2) {
349
 
        /* Do something stupid, but predictable */
350
 
        return array(null,
351
 
                     $operation1 . '+' . $operation2,
352
 
                     array_merge($args1, array('+'), $args2));
353
 
    }
354
 
}
355
 
?>