3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22
* ItemMoveSingle controller tests
23
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
26
* @version $Revision: 15513 $
28
class ItemMoveSingleControllerTest extends GalleryControllerTestCase {
30
function ItemMoveSingleControllerTest($methodName) {
31
$this->GalleryControllerTestCase($methodName, 'core.ItemMoveSingle');
37
/* Register a dummy toolkit */
38
$path = 'modules/core/test/phpunit/ItemMoveSingleControllerTest.class';
39
$ret = GalleryCoreApi::registerFactoryImplementation(
40
'GalleryToolkit', 'ItemMoveSingleTestToolkit', 'ItemMoveSingleTestToolkit', $path,
43
print $ret->getAsHtml();
44
return $this->failWithStatus($ret);
46
$this->_markToolkitForCleanup('ItemMoveSingleTestToolkit');
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')),
55
print $ret->getAsHtml();
56
return $this->failWithStatus($ret);
58
$ret = GalleryCoreApi::registerToolkitOperation('ItemMoveSingleTestToolkit',
59
array('image/test'), 'scale',
60
array(array('type' => 'int', 'description' => 'foo'),
61
array('type' => 'int', 'description' => 'foo')),
64
print $ret->getAsHtml();
65
return $this->failWithStatus($ret);
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');
74
print $ret->getAsHtml();
75
return $this->failWithStatus($ret);
78
list ($ret, $this->_rootAlbum) = $this->_createRandomAlbum($this->_getRootId());
80
print $ret->getAsHtml();
81
return $this->failWithStatus($ret);
84
$this->_markForCleanup($this->_rootAlbum);
85
$this->_setDerivativePreferences($this->_rootAlbum->getId(), 150, 300, 400);
87
list ($ret, $this->_destinationAlbum) = $this->_createRandomAlbum($this->_getRootId());
89
print $ret->getAsHtml();
90
return $this->failWithStatus($ret);
93
$this->_markForCleanup($this->_destinationAlbum);
94
$this->_setDerivativePreferences($this->_destinationAlbum->getId(), 200, 400, 600, 800);
96
/* Create a data item */
97
list ($ret, $this->_item) = $this->_createRandomDataItem(
98
$this->_rootAlbum->getId(), 'image/test', array('width' => 1000));
100
print $ret->getAsHtml();
101
return $this->failWithStatus($ret);
105
function _setDerivativePreferences() {
106
$args = func_get_args();
107
$itemId = array_shift($args);
108
$thumbSize = array_shift($args);
109
$ret = GalleryCoreApi::removeDerivativePreferencesForItem($itemId);
111
print $ret->getAsHtml();
112
return $this->failWithStatus($ret);
114
$ret = GalleryCoreApi::addDerivativePreference(0, $itemId,
115
DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'thumbnail|' . $thumbSize);
117
print $ret->getAsHtml();
118
return $this->failWithStatus($ret);
120
foreach ($args as $i => $resizeSize) {
121
$ret = GalleryCoreApi::addDerivativePreference($i+1, $itemId,
122
DERIVATIVE_TYPE_IMAGE_RESIZE, 'scale|' . $resizeSize);
124
print $ret->getAsHtml();
125
return $this->failWithStatus($ret);
130
function _getDerivativeSizes($itemIds) {
131
if (!is_array($itemIds)) {
132
$itemIds = array($itemIds);
135
list ($ret, $thumbnails) = GalleryCoreApi::fetchThumbnailsByItemIds($itemIds);
137
print $ret->getAsHtml();
138
return $this->failWithStatus($ret);
140
foreach ($thumbnails as $id => $deriv) {
141
$data[$id][] = array('type' => $deriv->getDerivativeType(),
142
'op' => $deriv->getDerivativeOperations());
144
list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds($itemIds);
146
print $ret->getAsHtml();
147
return $this->failWithStatus($ret);
149
foreach ($resizes as $id => $derivs) {
150
foreach ($derivs as $deriv) {
151
$data[$id][] = array('type' => $deriv->getDerivativeType(),
152
'op' => $deriv->getDerivativeOperations());
155
return count($data)==1 ? array_shift($data) : $data;
159
* Test moving an item
161
function testMoveItem() {
162
/* Create derivatives according to prefs of original album */
163
$ret = GalleryCoreApi::addExistingItemToAlbum(
164
$this->_item, $this->_rootAlbum->getId(), true);
166
return $this->failWithStatus($ret);
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');
177
GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
178
GalleryUtilities::putRequestVariable('form[action][move]', 1);
179
GalleryUtilities::putRequestVariable('form[destination]',
180
$this->_destinationAlbum->getId());
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),
190
/* Verify parent sequence */
191
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($this->_item->getId());
193
return $this->failWithStatus($ret);
195
$this->assertEquals(array($this->_getRootId(),
196
$this->_destinationAlbum->getId()),
197
$parentSequence, 'Parent sequence');
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');
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),
220
/* Verify parent sequence */
221
list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($this->_item->getId());
223
return $this->failWithStatus($ret);
225
$this->assertEquals(array($this->_getRootId(),
226
$this->_rootAlbum->getId()),
227
$parentSequence, 'Parent sequence 2');
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');
239
* Test moving an item to a bogus destination
241
function testMoveItemIntoEmptyDestination() {
242
GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
243
GalleryUtilities::putRequestVariable('form[action][move]', 1);
245
$results = $this->handleRequest();
246
$this->assertEquals(array('delegate' => array('view' => 'core.ItemAdmin',
247
'subView' => 'core.ItemMoveSingle'),
249
'error' => array('form[error][destination][empty]')),
254
* Test moving an item into itself
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());
261
$results = $this->handleRequest();
262
$this->assertEquals(array('delegate' => array('view' => 'core.ItemAdmin',
263
'subView' => 'core.ItemMoveSingle'),
265
'error' => array('form[error][destination][selfMove]')),
270
* Test moving an item into its own child
272
function testMoveItemIntoChild() {
273
list ($ret, $this->_childAlbum) = $this->_createRandomAlbum($this->_rootAlbum->getId());
275
print $ret->getAsHtml();
276
return $this->failWithStatus($ret);
279
GalleryUtilities::putRequestVariable('itemId', $this->_rootAlbum->getId());
280
GalleryUtilities::putRequestVariable('form[action][move]', 1);
281
GalleryUtilities::putRequestVariable('form[destination]', $this->_childAlbum->getId());
283
$results = $this->handleRequest();
284
$this->assertEquals(array('delegate' => array('view' => 'core.ItemAdmin',
285
'subView' => 'core.ItemMoveSingle'),
287
'error' => array('form[error][destination][selfMove]')),
292
* Test moving an item into a container where we have no add permissions
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());
300
$ret = GalleryCoreApi::removeItemPermissions($this->_destinationAlbum->getId());
302
return $this->failWithStatus($ret);
305
// The UI never displays targets that we can't add to
306
$results = $this->handleRequest(ERROR_PERMISSION_DENIED);
310
* Test moving an item that we can't delete
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());
318
$ret = GalleryCoreApi::removeItemPermissions($this->_item->getId());
320
return $this->failWithStatus($ret);
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);
328
* Test moving an item with a cropped thumbnail; ensure crop operation is maintained
330
function testMoveItemWithCroppedThumbnail() {
331
/* Create derivatives according to prefs of original album */
332
$ret = GalleryCoreApi::addExistingItemToAlbum(
333
$this->_item, $this->_rootAlbum->getId(), true);
335
return $this->failWithStatus($ret);
338
/* Add crop operation */
339
list ($ret, $thumbnails) =
340
GalleryCoreApi::fetchThumbnailsByItemIds(array($this->_item->getId()));
342
return $this->failWithStatus($ret);
344
$thumb = $thumbnails[$this->_item->getId()];
345
list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($thumb->getId());
347
return $this->failWithStatus($ret);
349
$thumb->setDerivativeOperations('crop|0,0,50,50;' . $thumb->getDerivativeOperations());
350
$ret = $thumb->save();
352
return $this->failWithStatus($ret);
354
$ret = GalleryCoreApi::releaseLocks($lockId);
356
return $this->failWithStatus($ret);
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');
368
GalleryUtilities::putRequestVariable('itemId', $this->_item->getId());
369
GalleryUtilities::putRequestVariable('form[action][move]', 1);
370
GalleryUtilities::putRequestVariable('form[destination]',
371
$this->_destinationAlbum->getId());
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),
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');
396
* @package GalleryCore
397
* @subpackage PHPUnit
399
class ItemMoveSingleTestToolkit extends GalleryToolkit { }