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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/ItemMoveSingleControllerTest.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
 
 * ItemMoveSingle controller tests
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class ItemMoveSingleControllerTest extends GalleryControllerTestCase {
29
 
 
30
 
    function ItemMoveSingleControllerTest($methodName) {
31
 
        $this->GalleryControllerTestCase($methodName, 'core.ItemMoveSingle');
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        parent::setUp();
36
 
 
37
 
        /* Register a dummy toolkit */
38
 
        $path = 'modules/core/test/phpunit/ItemMoveSingleControllerTest.class';
39
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
40
 
            'GalleryToolkit', 'ItemMoveSingleTestToolkit', 'ItemMoveSingleTestToolkit', $path,
41
 
            'coreTest', null);
42
 
        if ($ret) {
43
 
            print $ret->getAsHtml();
44
 
            return $this->failWithStatus($ret);
45
 
        }
46
 
        $this->_markToolkitForCleanup('ItemMoveSingleTestToolkit');
47
 
 
48
 
        /* Register operations that we can perform on our mock data items */
49
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemMoveSingleTestToolkit',
50
 
                array('image/test'), 'thumbnail',
51
 
                array(array('type' => 'int', 'description' => 'foo'),
52
 
                      array('type' => 'int', 'description' => 'foo')),
53
 
                'test-description');
54
 
        if ($ret) {
55
 
            print $ret->getAsHtml();
56
 
            return $this->failWithStatus($ret);
57
 
        }
58
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemMoveSingleTestToolkit',
59
 
                array('image/test'), 'scale',
60
 
                array(array('type' => 'int', 'description' => 'foo'),
61
 
                      array('type' => 'int', 'description' => 'foo')),
62
 
                'test-description');
63
 
        if ($ret) {
64
 
            print $ret->getAsHtml();
65
 
            return $this->failWithStatus($ret);
66
 
        }
67
 
        $ret = GalleryCoreApi::registerToolkitOperation('ItemMoveSingleTestToolkit',
68
 
                array('image/test'), 'crop',
69
 
                array(array('type' => 'float', 'description' => 'foo'),
70
 
                      array('type' => 'float', 'description' => 'foo'),
71
 
                      array('type' => 'float', 'description' => 'foo'),
72
 
                      array('type' => 'float', 'description' => 'foo')), 'test-description');
73
 
        if ($ret) {
74
 
            print $ret->getAsHtml();
75
 
            return $this->failWithStatus($ret);
76
 
        }
77
 
 
78
 
        list ($ret, $this->_rootAlbum) = $this->_createRandomAlbum($this->_getRootId());
79
 
        if ($ret) {
80
 
            print $ret->getAsHtml();
81
 
            return $this->failWithStatus($ret);
82
 
        }
83
 
 
84
 
        $this->_markForCleanup($this->_rootAlbum);
85
 
        $this->_setDerivativePreferences($this->_rootAlbum->getId(), 150, 300, 400);
86
 
 
87
 
        list ($ret, $this->_destinationAlbum) = $this->_createRandomAlbum($this->_getRootId());
88
 
        if ($ret) {
89
 
            print $ret->getAsHtml();
90
 
            return $this->failWithStatus($ret);
91
 
        }
92
 
 
93
 
        $this->_markForCleanup($this->_destinationAlbum);
94
 
        $this->_setDerivativePreferences($this->_destinationAlbum->getId(), 200, 400, 600, 800);
95
 
 
96
 
        /* Create a data item */
97
 
        list ($ret, $this->_item) = $this->_createRandomDataItem(
98
 
                $this->_rootAlbum->getId(), 'image/test', array('width' => 1000));
99
 
        if ($ret) {
100
 
            print $ret->getAsHtml();
101
 
            return $this->failWithStatus($ret);
102
 
        }
103
 
    }
104
 
 
105
 
    function _setDerivativePreferences() {
106
 
        $args = func_get_args();
107
 
        $itemId = array_shift($args);
108
 
        $thumbSize = array_shift($args);
109
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($itemId);
110
 
        if ($ret) {
111
 
            print $ret->getAsHtml();
112
 
            return $this->failWithStatus($ret);
113
 
        }
114
 
        $ret = GalleryCoreApi::addDerivativePreference(0, $itemId,
115
 
                   DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'thumbnail|' . $thumbSize);
116
 
        if ($ret) {
117
 
            print $ret->getAsHtml();
118
 
            return $this->failWithStatus($ret);
119
 
        }
120
 
        foreach ($args as $i => $resizeSize) {
121
 
            $ret = GalleryCoreApi::addDerivativePreference($i+1, $itemId,
122
 
                        DERIVATIVE_TYPE_IMAGE_RESIZE, 'scale|' . $resizeSize);
123
 
            if ($ret) {
124
 
                print $ret->getAsHtml();
125
 
                return $this->failWithStatus($ret);
126
 
            }
127
 
        }
128
 
    }
129
 
 
130
 
    function _getDerivativeSizes($itemIds) {
131
 
        if (!is_array($itemIds)) {
132
 
            $itemIds = array($itemIds);
133
 
        }
134
 
        $data = array();
135
 
        list ($ret, $thumbnails) = GalleryCoreApi::fetchThumbnailsByItemIds($itemIds);
136
 
        if ($ret) {
137
 
            print $ret->getAsHtml();
138
 
            return $this->failWithStatus($ret);
139
 
        }
140
 
        foreach ($thumbnails as $id => $deriv) {
141
 
            $data[$id][] = array('type' => $deriv->getDerivativeType(),
142
 
                                 'op' => $deriv->getDerivativeOperations());
143
 
        }
144
 
        list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds($itemIds);
145
 
        if ($ret) {
146
 
            print $ret->getAsHtml();
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
        foreach ($resizes as $id => $derivs) {
150
 
            foreach ($derivs as $deriv) {
151
 
                $data[$id][] = array('type' => $deriv->getDerivativeType(),
152
 
                                     'op' => $deriv->getDerivativeOperations());
153
 
            }
154
 
        }
155
 
        return count($data)==1 ? array_shift($data) : $data;
156
 
    }
157
 
 
158
 
    /**
159
 
     * Test moving an item
160
 
     */
161
 
    function testMoveItem() {
162
 
        /* Create derivatives according to prefs of original album */
163
 
        $ret = GalleryCoreApi::addExistingItemToAlbum(
164
 
                                $this->_item, $this->_rootAlbum->getId(), true);
165
 
        if ($ret) {
166
 
            return $this->failWithStatus($ret);
167
 
        }
168
 
 
169
 
        /* Verify derivatives were created ok */
170
 
        $sizes = $this->_getDerivativeSizes($this->_item->getId());
171
 
        $this->assertEquals(array(
172
 
            array('type' => DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'op' => 'thumbnail|150'),
173
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|300'),
174
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|400')),
175
 
            $sizes, 'Initial derivative sizes');
176
 
 
177
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
178
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
179
 
        GalleryUtilities::putRequestVariable('form[destination]',
180
 
                                             $this->_destinationAlbum->getId());
181
 
 
182
 
        $results = $this->handleRequest();
183
 
        $this->assertEquals(array('redirect' => array('view' => 'core.ItemAdmin',
184
 
                                                      'subView' => 'core.ItemMoveSingle',
185
 
                                                      'itemId' => $this->_item->getId()),
186
 
                                  'status' => array('moved' => 1),
187
 
                                  'error' => array()),
188
 
                            $results);
189
 
 
190
 
        /* Verify parent sequence */
191
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($this->_item->getId());
192
 
        if ($ret) {
193
 
            return $this->failWithStatus($ret);
194
 
        }
195
 
        $this->assertEquals(array($this->_getRootId(),
196
 
                                  $this->_destinationAlbum->getId()),
197
 
                            $parentSequence, 'Parent sequence');
198
 
 
199
 
        /* Verify derivatives were recreated with preferences of new parent album */
200
 
        $sizes = $this->_getDerivativeSizes($this->_item->getId());
201
 
        $this->assertEquals(array(
202
 
            array('type' => DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'op' => 'thumbnail|200'),
203
 
            /* Order differs because old 400 kept as-is, and old 300 is reused for 600. */
204
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|600'),
205
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|400'),
206
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|800')),
207
 
            $sizes, 'Derivative sizes after move');
208
 
 
209
 
        /* Move back */
210
 
        clearstatcache();
211
 
        GalleryUtilities::putRequestVariable('form[destination]', $this->_rootAlbum->getId());
212
 
        $results = $this->handleRequest();
213
 
        $this->assertEquals(array('redirect' => array('view' => 'core.ItemAdmin',
214
 
                                                      'subView' => 'core.ItemMoveSingle',
215
 
                                                      'itemId' => $this->_item->getId()),
216
 
                                  'status' => array('moved' => 1),
217
 
                                  'error' => array()),
218
 
                            $results);
219
 
 
220
 
        /* Verify parent sequence */
221
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($this->_item->getId());
222
 
        if ($ret) {
223
 
            return $this->failWithStatus($ret);
224
 
        }
225
 
        $this->assertEquals(array($this->_getRootId(),
226
 
                                  $this->_rootAlbum->getId()),
227
 
                            $parentSequence, 'Parent sequence 2');
228
 
 
229
 
        /* Verify derivatives were recreated with preferences of new parent album */
230
 
        $sizes = $this->_getDerivativeSizes($this->_item->getId());
231
 
        $this->assertEquals(array(
232
 
            array('type' => DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'op' => 'thumbnail|150'),
233
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|300'),
234
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|400')),
235
 
            $sizes, 'Derivative sizes after move 2');
236
 
    }
237
 
 
238
 
    /**
239
 
     * Test moving an item to a bogus destination
240
 
     */
241
 
    function testMoveItemIntoEmptyDestination() {
242
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
243
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
244
 
 
245
 
        $results = $this->handleRequest();
246
 
        $this->assertEquals(array('delegate' => array('view' => 'core.ItemAdmin',
247
 
                                                      'subView' => 'core.ItemMoveSingle'),
248
 
                                  'status' => array(),
249
 
                                  'error' => array('form[error][destination][empty]')),
250
 
                            $results);
251
 
    }
252
 
 
253
 
    /**
254
 
     * Test moving an item into itself
255
 
     */
256
 
    function testMoveItemIntoSelf() {
257
 
        GalleryUtilities::putRequestVariable('itemId', $this->_rootAlbum->getId());
258
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
259
 
        GalleryUtilities::putRequestVariable('form[destination]', $this->_rootAlbum->getId());
260
 
 
261
 
        $results = $this->handleRequest();
262
 
        $this->assertEquals(array('delegate' => array('view' => 'core.ItemAdmin',
263
 
                                                      'subView' => 'core.ItemMoveSingle'),
264
 
                                  'status' => array(),
265
 
                                  'error' => array('form[error][destination][selfMove]')),
266
 
                            $results);
267
 
    }
268
 
 
269
 
    /**
270
 
     * Test moving an item into its own child
271
 
     */
272
 
    function testMoveItemIntoChild() {
273
 
        list ($ret, $this->_childAlbum) = $this->_createRandomAlbum($this->_rootAlbum->getId());
274
 
        if ($ret) {
275
 
            print $ret->getAsHtml();
276
 
            return $this->failWithStatus($ret);
277
 
        }
278
 
 
279
 
        GalleryUtilities::putRequestVariable('itemId', $this->_rootAlbum->getId());
280
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
281
 
        GalleryUtilities::putRequestVariable('form[destination]', $this->_childAlbum->getId());
282
 
 
283
 
        $results = $this->handleRequest();
284
 
        $this->assertEquals(array('delegate' => array('view' => 'core.ItemAdmin',
285
 
                                                      'subView' => 'core.ItemMoveSingle'),
286
 
                                  'status' => array(),
287
 
                                  'error' => array('form[error][destination][selfMove]')),
288
 
                            $results);
289
 
    }
290
 
 
291
 
    /**
292
 
     * Test moving an item into a container where we have no add permissions
293
 
     */
294
 
    function testMoveItemIntoAlbumWithNoPermissions() {
295
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
296
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
297
 
        GalleryUtilities::putRequestVariable('form[destination]',
298
 
                                             $this->_destinationAlbum->getId());
299
 
 
300
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_destinationAlbum->getId());
301
 
        if ($ret) {
302
 
            return $this->failWithStatus($ret);
303
 
        }
304
 
 
305
 
        // The UI never displays targets that we can't add to
306
 
        $results = $this->handleRequest(ERROR_PERMISSION_DENIED);
307
 
    }
308
 
 
309
 
    /**
310
 
     * Test moving an item that we can't delete
311
 
     */
312
 
    function testMoveItemWithNoDeletePermissions() {
313
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
314
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
315
 
        GalleryUtilities::putRequestVariable('form[destination]',
316
 
                                             $this->_destinationAlbum->getId());
317
 
 
318
 
        $ret = GalleryCoreApi::removeItemPermissions($this->_item->getId());
319
 
        if ($ret) {
320
 
            return $this->failWithStatus($ret);
321
 
        }
322
 
 
323
 
        // The UI shouldn't allow us to try to move an item that we can't delete
324
 
        $results = $this->handleRequest(ERROR_PERMISSION_DENIED);
325
 
    }
326
 
 
327
 
    /**
328
 
     * Test moving an item with a cropped thumbnail; ensure crop operation is maintained
329
 
     */
330
 
    function testMoveItemWithCroppedThumbnail() {
331
 
        /* Create derivatives according to prefs of original album */
332
 
        $ret = GalleryCoreApi::addExistingItemToAlbum(
333
 
                                $this->_item, $this->_rootAlbum->getId(), true);
334
 
        if ($ret) {
335
 
            return $this->failWithStatus($ret);
336
 
        }
337
 
 
338
 
        /* Add crop operation */
339
 
        list ($ret, $thumbnails) =
340
 
            GalleryCoreApi::fetchThumbnailsByItemIds(array($this->_item->getId()));
341
 
        if ($ret) {
342
 
            return $this->failWithStatus($ret);
343
 
        }
344
 
        $thumb = $thumbnails[$this->_item->getId()];
345
 
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($thumb->getId());
346
 
        if ($ret) {
347
 
            return $this->failWithStatus($ret);
348
 
        }
349
 
        $thumb->setDerivativeOperations('crop|0,0,50,50;' . $thumb->getDerivativeOperations());
350
 
        $ret = $thumb->save();
351
 
        if ($ret) {
352
 
            return $this->failWithStatus($ret);
353
 
        }
354
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
355
 
        if ($ret) {
356
 
            return $this->failWithStatus($ret);
357
 
        }
358
 
 
359
 
        /* Verify derivatives were created ok */
360
 
        $sizes = $this->_getDerivativeSizes($this->_item->getId());
361
 
        $this->assertEquals(array(
362
 
            array('type' => DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
363
 
                  'op' => 'crop|0,0,50,50;thumbnail|150'),
364
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|300'),
365
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|400')),
366
 
            $sizes, 'Initial derivative sizes');
367
 
 
368
 
        GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
369
 
        GalleryUtilities::putRequestVariable('form[action][move]', 1);
370
 
        GalleryUtilities::putRequestVariable('form[destination]',
371
 
                                             $this->_destinationAlbum->getId());
372
 
 
373
 
        $results = $this->handleRequest();
374
 
        $this->assertEquals(array('redirect' => array('view' => 'core.ItemAdmin',
375
 
                                                      'subView' => 'core.ItemMoveSingle',
376
 
                                                      'itemId' => $this->_item->getId()),
377
 
                                  'status' => array('moved' => 1),
378
 
                                  'error' => array()),
379
 
                            $results);
380
 
 
381
 
        /* Verify derivatives were recreated with preferences of new parent album */
382
 
        $sizes = $this->_getDerivativeSizes($this->_item->getId());
383
 
        $this->assertEquals(array(
384
 
            array('type' => DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
385
 
                  'op' => 'crop|0,0,50,50;thumbnail|200'),
386
 
            /* Order differs because old 400 kept as-is, and old 300 is reused for 600. */
387
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|600'),
388
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|400'),
389
 
            array('type' => DERIVATIVE_TYPE_IMAGE_RESIZE, 'op' => 'scale|800')),
390
 
            $sizes, 'Derivative sizes after move');
391
 
    }
392
 
}
393
 
 
394
 
/**
395
 
 * Dummy test toolkit
396
 
 * @package GalleryCore
397
 
 * @subpackage PHPUnit
398
 
 */
399
 
class ItemMoveSingleTestToolkit extends GalleryToolkit { }
400
 
?>