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

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/ItemTest.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
 
 * Test Item functionality
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15628 $
27
 
 */
28
 
class ItemTest extends GalleryTestCase {
29
 
 
30
 
    function ItemTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        global $gallery;
36
 
 
37
 
        parent::setUp();
38
 
 
39
 
        $this->_userId = $gallery->getActiveUserId();
40
 
        list ($ret, $groupIds) = GalleryCoreApi::fetchGroupsForUser($this->_userId);
41
 
        if ($ret) {
42
 
            return $this->failWithStatus($ret);
43
 
        }
44
 
 
45
 
        $groupIds = array_keys($groupIds);
46
 
        $this->_groupId = $groupIds[0];
47
 
 
48
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
49
 
        if ($ret) {
50
 
            return $this->failWithStatus($ret);
51
 
        }
52
 
 
53
 
        $this->_markForCleanup($this->_album);
54
 
 
55
 
        /* Register a dummy toolkit */
56
 
        $path = 'modules/core/test/phpunit/ItemTest.class';
57
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
58
 
            'GalleryToolkit', 'ItemTestToolkit', 'ItemTestToolkit',
59
 
            $path, 'coreTest', null);
60
 
        if ($ret) {
61
 
            print $ret->getAsHtml();
62
 
            return $this->failWithStatus($ret);
63
 
        }
64
 
        $this->_markToolkitForCleanup('ItemTestToolkit');
65
 
 
66
 
        /* Register jpeg thumbnail/gif scale in case there are no other toolkits active */
67
 
        $ret = GalleryCoreApi::registerToolkitOperation(
68
 
            'ItemTestToolkit', array('test/image', 'image/jpeg', 'image/gif'), 'thumbnail',
69
 
            array(array('type' => 'int', 'description' => 'foo'),
70
 
                  array('type' => 'int', 'description' => 'foo')),
71
 
            'itemtest-description', '', 7);
72
 
        if ($ret) {
73
 
            print $ret->getAsHtml();
74
 
            return $this->failWithStatus($ret);
75
 
        }
76
 
        $ret = GalleryCoreApi::registerToolkitOperation(
77
 
            'ItemTestToolkit', array('image/gif'), 'scale',
78
 
            array(array('type' => 'int', 'description' => 'foo'),
79
 
                  array('type' => 'int', 'description' => 'foo')),
80
 
            'itemtest-description', '', 7);
81
 
        if ($ret) {
82
 
            print $ret->getAsHtml();
83
 
            return $this->failWithStatus($ret);
84
 
        }
85
 
        $ret = GalleryCoreApi::registerToolkitProperty(
86
 
                'ItemTestToolkit', array('image/gif', 'image/jpeg'),
87
 
                'dimensions', 'int,int', 'Get the dimensions');
88
 
        if ($ret) {
89
 
            print $ret->getAsHtml();
90
 
            return $this->failWithStatus($ret);
91
 
        }
92
 
 
93
 
        /* Register a conversion operation that we can perform on our mock data items */
94
 
        $ret = GalleryCoreApi::registerToolkitOperation(
95
 
            'ItemTestToolkit', array('test/movie', 'image/x-portable-pixmap'),
96
 
            'convert-to-image/jpeg', array(), 'itemtest-description', 'image/jpeg', 7);
97
 
        if ($ret) {
98
 
            print $ret->getAsHtml();
99
 
            return $this->failWithStatus($ret);
100
 
        }
101
 
 
102
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
103
 
            'GalleryItem', 'ItemTestMovieItem', 'ItemTestMovieItem',
104
 
            $path, 'coreTest', array('test/movie'));
105
 
        $this->_markFactoryModuleForCleanup('coreTest');
106
 
        if ($ret) {
107
 
            print $ret->getAsHtml();
108
 
            return $this->failWithStatus($ret);
109
 
        }
110
 
 
111
 
        $ret = GalleryCoreApi::registerFactoryImplementation(
112
 
            'GalleryRenderer', 'ItemTestRenderer', 'ItemTestRenderer', $path, 'coreTest', null);
113
 
        if ($ret) {
114
 
            print $ret->getAsHtml();
115
 
            return $this->failWithStatus($ret);
116
 
        }
117
 
    }
118
 
 
119
 
    function testNewItemByMimeType() {
120
 
        list ($ret, $instance) = GalleryCoreApi::newItemByMimeType('image/jpeg');
121
 
        if ($ret) {
122
 
            return $this->failWithStatus($ret);
123
 
        }
124
 
        $this->assert(GalleryUtilities::isExactlyA($instance, 'GalleryPhotoItem'));
125
 
 
126
 
        list ($ret, $instance) = GalleryCoreApi::newItemByMimeType('image/randomimagetype');
127
 
        if ($ret) {
128
 
            return $this->failWithStatus($ret);
129
 
        }
130
 
        $this->assert(GalleryUtilities::isExactlyA($instance, 'GalleryPhotoItem'));
131
 
 
132
 
        list ($ret, $instance) = GalleryCoreApi::newItemByMimeType('video/x-msvideo');
133
 
        if ($ret) {
134
 
            return $this->failWithStatus($ret);
135
 
        }
136
 
        $this->assert(GalleryUtilities::isExactlyA($instance, 'GalleryMovieItem'));
137
 
 
138
 
        list ($ret, $instance) = GalleryCoreApi::newItemByMimeType('randomtype/randomtype');
139
 
        if ($ret) {
140
 
            return $this->failWithStatus($ret);
141
 
        }
142
 
        $this->assert(GalleryUtilities::isExactlyA($instance, 'GalleryUnknownItem'));
143
 
    }
144
 
 
145
 
    function testCanBeViewedInlineImages() {
146
 
        /* This list should be mime types accepted by browsers in the <img> tag */
147
 
        $mimeTypesInline = array(
148
 
            'image/jpeg' => true,
149
 
            'image/pjpeg' => true,
150
 
            'image/png' => true,
151
 
            'image/gif' => true,
152
 
            'image/vnd.wap.wbmp' => true,
153
 
            'image/tiff' => false,
154
 
            'image/bmp' => false,  /* only IE accepts this one */
155
 
            'image/otherUnknown' => false,
156
 
            'application/photoshop' => false,
157
 
        );
158
 
        foreach ($mimeTypesInline as $mimeType => $canBeViewedInline) {
159
 
            list ($ret, $instance) = GalleryCoreApi::newItemByMimeType($mimeType);
160
 
            if ($ret) {
161
 
                return $this->failWithStatus($ret);
162
 
            }
163
 
            $this->assert(GalleryUtilities::isExactlyA($instance, 'GalleryPhotoItem'));
164
 
            $instance->setMimeType($mimeType);
165
 
            $this->assertEquals($canBeViewedInline, $instance->canBeViewedInline(), $mimeType);
166
 
        }
167
 
 
168
 
    }
169
 
 
170
 
    function testCanBeViewedInlineOthers() {
171
 
        global $gallery;
172
 
        $query = '
173
 
            SELECT [GalleryMimeTypeMap::mimeType]
174
 
            FROM [GalleryMimeTypeMap]
175
 
            WHERE [GalleryMimeTypeMap::mimeType] NOT LIKE \'image/%\'
176
 
        ';
177
 
        list ($ret, $searchResults) = $gallery->search($query);
178
 
        if ($ret) {
179
 
            return $this->failWithStatus($ret);
180
 
        }
181
 
        while ($results = $searchResults->nextResult()) {
182
 
            $mimeType = $results[0];
183
 
            if ($mimeType == 'application/photoshop') {
184
 
                continue;
185
 
            }
186
 
            list ($ret, $instance) = GalleryCoreApi::newItemByMimeType($mimeType);
187
 
            if ($ret) {
188
 
                return $this->failWithStatus($ret);
189
 
            }
190
 
            $instance->setMimeType($mimeType);
191
 
            if (GalleryUtilities::isExactlyA($instance, 'GalleryMovieItem')
192
 
                    || GalleryUtilities::isExactlyA($instance, 'GalleryAnimationItem')) {
193
 
                $instance->setWidth(100);
194
 
                $instance->setHeight(100);
195
 
            }
196
 
            /* We need to be able to render what we can view inline */
197
 
            $html = $instance->render('HTML', array('fallback' => 'ERROR', 'class' => 'test'));
198
 
            $inlineOk = ($html != 'ERROR' && $html != null);
199
 
            $this->assertEquals($inlineOk, $instance->canBeViewedInline(), $mimeType);
200
 
        }
201
 
    }
202
 
 
203
 
    function testCanBeViewedInlineRenderer() {
204
 
        list ($ret, $instance) = GalleryCoreApi::newItemByMimeType('image/jpeg');
205
 
        if ($ret) {
206
 
            return $this->failWithStatus($ret);
207
 
        }
208
 
        $instance->setMimeType('image/jpeg');
209
 
        $this->assert($instance->canBeViewedInline(), 'jpeg');
210
 
        $instance->setRenderer('ItemTestRenderer');
211
 
        $this->assert(!$instance->canBeViewedInline(), 'jpeg renderer');
212
 
 
213
 
        list ($ret, $instance) = GalleryCoreApi::newItemByMimeType('test/item');
214
 
        if ($ret) {
215
 
            return $this->failWithStatus($ret);
216
 
        }
217
 
        $instance->setMimeType('test/item');
218
 
        $this->assert(!$instance->canBeViewedInline(), 'test item');
219
 
        $instance->setRenderer('ItemTestRenderer');
220
 
        $this->assert($instance->canBeViewedInline(), 'test item renderer');
221
 
    }
222
 
 
223
 
    function testGetChildCounts() {
224
 
        for ($i = 0; $i < 3; $i++) {
225
 
            list ($ret, $child[$i]) = $this->_createRandomAlbum($this->_album->getId());
226
 
            if ($ret) {
227
 
                return $this->failWithStatus($ret);
228
 
            }
229
 
 
230
 
            /* Remove all permissions */
231
 
            $ret = GalleryCoreApi::removeItemPermissions($child[$i]->getId());
232
 
            if ($ret) {
233
 
                return $this->failWithStatus($ret);
234
 
            }
235
 
        }
236
 
 
237
 
        /* Set permissions */
238
 
        $ret = GalleryCoreApi::addUserPermission(
239
 
            $child[0]->getId(), $this->_userId, 'core.view', false);
240
 
        if ($ret) {
241
 
            return $this->failWithStatus($ret);
242
 
        }
243
 
 
244
 
        $ret = GalleryCoreApi::addGroupPermission(
245
 
            $child[1]->getId(), $this->_groupId, 'core.view', false);
246
 
        if ($ret) {
247
 
            return $this->failWithStatus($ret);
248
 
        }
249
 
 
250
 
        list ($ret, $counts) = GalleryCoreApi::fetchChildCounts(array($this->_album->getId()));
251
 
        if ($ret) {
252
 
            return $this->failWithStatus($ret);
253
 
        }
254
 
 
255
 
        /* We can view child[0] and child[1] */
256
 
        $this->assertEquals(array($this->_album->getId() => 2), $counts);
257
 
    }
258
 
 
259
 
    function testGetChildCountsDifferentUser() {
260
 
        global $gallery;
261
 
 
262
 
        /* Create a new user and give him limited view permissions on our tree */
263
 
        list ($ret, $user) = $this->_createRandomUser();
264
 
        if ($ret) {
265
 
            return $this->failWithStatus($ret);
266
 
        }
267
 
        $this->_markForCleanup($user);
268
 
 
269
 
        for ($i = 0; $i < 3; $i++) {
270
 
            list ($ret, $child[$i]) = $this->_createRandomAlbum($this->_album->getId());
271
 
            if ($ret) {
272
 
                return $this->failWithStatus($ret);
273
 
            }
274
 
 
275
 
            /* Remove all permissions */
276
 
            $ret = GalleryCoreApi::removeItemPermissions($child[$i]->getId());
277
 
            if ($ret) {
278
 
                return $this->failWithStatus($ret);
279
 
            }
280
 
 
281
 
            /* Add back admin permission */
282
 
            $ret = GalleryCoreApi::addUserPermission(
283
 
                $child[$i]->getId(), $gallery->getActiveUserId(), 'core.view', false);
284
 
            if ($ret) {
285
 
                return $this->failWithStatus($ret);
286
 
            }
287
 
        }
288
 
 
289
 
        /* Set permissions */
290
 
        $ret = GalleryCoreApi::addUserPermission(
291
 
            $child[0]->getId(), $user->getId(), 'core.view', false);
292
 
        if ($ret) {
293
 
            return $this->failWithStatus($ret);
294
 
        }
295
 
        $ret = GalleryCoreApi::addUserPermission(
296
 
            $child[2]->getId(), $user->getId(), 'core.view', false);
297
 
        if ($ret) {
298
 
            return $this->failWithStatus($ret);
299
 
        }
300
 
 
301
 
        /* Used to have a bug where count was cached by itemId only, not itemId+userId */
302
 
        list ($ret, $counts) = GalleryCoreApi::fetchChildCounts(array($this->_album->getId()));
303
 
        if ($ret) {
304
 
            return $this->failWithStatus($ret);
305
 
        }
306
 
        $this->assertEquals(array($this->_album->getId() => 3), $counts);
307
 
 
308
 
        list ($ret, $counts) =
309
 
            GalleryCoreApi::fetchChildCounts(array($this->_album->getId()), $user->getId());
310
 
        if ($ret) {
311
 
            return $this->failWithStatus($ret);
312
 
        }
313
 
 
314
 
        /* We can view child[0] and child[2] */
315
 
        $this->assertEquals(array($this->_album->getId() => 2), $counts);
316
 
    }
317
 
 
318
 
    function testGetPermissions() {
319
 
 
320
 
        for ($i = 0; $i < 3; $i++) {
321
 
            list ($ret, $child[$i]) = $this->_createRandomAlbum($this->_album->getId());
322
 
            if ($ret) {
323
 
                return $this->failWithStatus($ret);
324
 
            }
325
 
 
326
 
            /* Remove all permissions */
327
 
            $ret = GalleryCoreApi::removeItemPermissions($child[$i]->getId());
328
 
            if ($ret) {
329
 
                return $this->failWithStatus($ret);
330
 
            }
331
 
 
332
 
            $ret = array();
333
 
            switch ($i) {
334
 
            case 0:
335
 
                $ret[0] = GalleryCoreApi::addUserPermission(
336
 
                    $child[$i]->getId(), $this->_userId, 'core.delete', false);
337
 
                break;
338
 
 
339
 
            case 1:
340
 
                $ret[0] = GalleryCoreApi::addUserPermission(
341
 
                    $child[$i]->getId(), $this->_userId, 'core.view', false);
342
 
                $ret[1] = GalleryCoreApi::addUserPermission(
343
 
                    $child[$i]->getId(), $this->_userId, 'core.addDataItem', false);
344
 
                $ret[2] = GalleryCoreApi::addUserPermission(
345
 
                    $child[$i]->getId(), $this->_userId, 'core.edit', false);
346
 
                break;
347
 
 
348
 
            case 2:
349
 
                $ret[0] = GalleryCoreApi::addGroupPermission(
350
 
                    $child[$i]->getId(), $this->_groupId, 'core.view', false);
351
 
                $ret[1] = GalleryCoreApi::addGroupPermission(
352
 
                    $child[$i]->getId(), $this->_groupId, 'core.addDataItem', false);
353
 
                break;
354
 
 
355
 
            }
356
 
            for ($j = 0; $j < 3; $j++) {
357
 
                if (isset($ret[$j])) {
358
 
                    return $this->failWithStatus($ret[$j]);
359
 
                }
360
 
            }
361
 
        }
362
 
 
363
 
        list ($ret, $perms) = GalleryCoreApi::getPermissions($child[0]->getId());
364
 
        if ($ret) {
365
 
            return $this->failWithStatus($ret);
366
 
        }
367
 
        $this->assertEquals(array('core.delete' => 1), $perms);
368
 
 
369
 
        list ($ret, $perms) = GalleryCoreApi::getPermissions($child[1]->getId());
370
 
        if ($ret) {
371
 
            return $this->failWithStatus($ret);
372
 
        }
373
 
        $this->assertEquals(array('core.view' => 1,
374
 
                                  'core.addDataItem' => 1,
375
 
                                  'core.edit' => 1), $perms);
376
 
 
377
 
        list ($ret, $perms) = GalleryCoreApi::getPermissions($child[2]->getId());
378
 
        if ($ret) {
379
 
            return $this->failWithStatus($ret);
380
 
        }
381
 
        $this->assertEquals(array('core.view' => 1,
382
 
                                  'core.addDataItem' => 1), $perms);
383
 
    }
384
 
 
385
 
    function testFetchAllItemIds() {
386
 
        for ($i = 0; $i < 3; $i++) {
387
 
            list ($ret, $child[$i]) = $this->_createRandomAlbum($this->_album->getId());
388
 
            if ($ret) {
389
 
                return $this->failWithStatus($ret);
390
 
            }
391
 
 
392
 
            if ($i == 1) {
393
 
                $ret = GalleryCoreApi::removeItemPermissions($child[$i]->getId());
394
 
                if ($ret) {
395
 
                    return $this->failWithStatus($ret);
396
 
                }
397
 
 
398
 
                $ret = GalleryCoreApi::addUserPermission(
399
 
                    $child[$i]->getId(), $this->_userId, 'core.addDataItem', false);
400
 
                if ($ret) {
401
 
                    return $this->failWithStatus($ret);
402
 
                }
403
 
            }
404
 
        }
405
 
 
406
 
        list ($ret, $ids) = GalleryCoreApi::fetchAllItemIds('GalleryAlbumItem');
407
 
        if ($ret) {
408
 
            return $this->failWithStatus($ret);
409
 
        }
410
 
 
411
 
        /* We should see children 0 & 2 and the parent */
412
 
        $this->assert(in_array($this->_album->getId(), $ids), 'parent');
413
 
        $this->assert(in_array($child[0]->getId(), $ids), 'child 0');
414
 
        $this->assert(!in_array($child[1]->getId(), $ids), 'child 1');
415
 
        $this->assert(in_array($child[2]->getId(), $ids), 'child 2');
416
 
 
417
 
        /* Make sure there's no duplication */
418
 
        $this->assertEquals(sizeof($ids), sizeof(array_flip(array_flip($ids))));
419
 
 
420
 
        list ($ret, $ids) = GalleryCoreApi::fetchAllItemIds('GalleryAlbumItem', 'core.addDataItem');
421
 
        if ($ret) {
422
 
            return $this->failWithStatus($ret);
423
 
        }
424
 
 
425
 
        /* We should see them all */
426
 
        $this->assert(in_array($child[0]->getId(), $ids), 'child 0');
427
 
        $this->assert(in_array($child[1]->getId(), $ids), 'child 1');
428
 
        $this->assert(in_array($child[2]->getId(), $ids), 'child 2');
429
 
 
430
 
        /* Make sure there's no duplication */
431
 
        $this->assertEquals(sizeof($ids), sizeof(array_flip(array_flip($ids))));
432
 
    }
433
 
 
434
 
    /*
435
 
     * Create a user, add some items, test the function
436
 
     * fetchAllItemIdsByOwnerId() and verify
437
 
     */
438
 
    function testFetchAllItemIdsByOwnerId() {
439
 
        global $gallery;
440
 
 
441
 
        /* Create a user that we can delete */
442
 
        list ($ret, $user) = $this->_createRandomUser();
443
 
        if ($ret) {
444
 
            return $this->failWithStatus($ret);
445
 
        }
446
 
        $this->_markForCleanup($user);
447
 
 
448
 
        /* Without permissions, the user can't add items */
449
 
        $ret = GalleryCoreApi::addUserPermission(
450
 
            $this->_album->getId(), $user->getId(),
451
 
            array('core.addDataItem','core.addAlbumItem'), false);
452
 
        if ($ret) {
453
 
            return $this->failWithStatus($ret);
454
 
        }
455
 
 
456
 
        $this->_activeUserBackup = $gallery->getActiveUser();
457
 
        $gallery->setActiveUser($user);
458
 
 
459
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_album->getId());
460
 
        if ($ret) {
461
 
            return $this->failWithStatus($ret);
462
 
        }
463
 
 
464
 
        /* Add some items */
465
 
        $items = array();
466
 
        for ($i = 0; $i < 3; $i++) {
467
 
            list ($ret, $items[]) = $this->_createRandomAlbum($album->getId());
468
 
            if ($ret) {
469
 
                return $this->failWithStatus($ret);
470
 
            }
471
 
            list ($ret, $items[]) = $this->_createRandomDataItem($album->getId());
472
 
            if ($ret) {
473
 
                return $this->failWithStatus($ret);
474
 
            }
475
 
        }
476
 
 
477
 
        /* Restore the activeUser, should be a Site Admin */
478
 
        $gallery->setActiveUser($this->_activeUserBackup);
479
 
 
480
 
        list ($ret, $ids) = GalleryCoreApi::fetchAllItemIdsByOwnerId($user->getId());
481
 
        if ($ret) {
482
 
            return $this->failWithStatus($ret);
483
 
        }
484
 
        GalleryDataCache::reset();
485
 
 
486
 
        /* Verification */
487
 
        $this->assert(in_array($album->getId(), $ids), 'parent album not returned');
488
 
        $itemCount = 1;
489
 
        foreach ($items as $item) {
490
 
            $this->assert(in_array($item->getId(), $ids), 'child '.$item->getId() .' not returned');
491
 
            $itemCount++;
492
 
        }
493
 
        $this->assert(count($ids) == $itemCount,
494
 
                      'returned '. count($ids) .
495
 
                      ' instead of ' . $itemCount.' items');
496
 
 
497
 
        /* The teardown cleanup code deletes all item that are marked for deletion */
498
 
    }
499
 
 
500
 
    /*
501
 
     * Create test user1, test group1, add user1 to group1, create an item as user1,
502
 
     * create test user2 and group2, assign user2 to group2, grant to user1, group1,
503
 
     * user2 and group2 some permissions on the item, call remapOwnerId($user1Id, $user2Id)
504
 
     * and verify the new owner and (new) permissions
505
 
     */
506
 
    function testRemapOwnerId() {
507
 
        global $gallery;
508
 
 
509
 
        /* Create test user 1, the oldOwner */
510
 
        list ($ret, $oldUser) = $this->_createRandomUser();
511
 
        if ($ret) {
512
 
            return $this->failWithStatus($ret);
513
 
        }
514
 
        $this->_markForCleanup($oldUser);
515
 
        /* Create test user 2, the newOwner */
516
 
        list ($ret, $newUser) = $this->_createRandomUser();
517
 
        if ($ret) {
518
 
            return $this->failWithStatus($ret);
519
 
        }
520
 
        $this->_markForCleanup($newUser);
521
 
        /* Create Group 1 for user 1 for indirect permissions */
522
 
        list ($ret, $group1) = $this->_createRandomGroup();
523
 
        if ($ret) {
524
 
            return $this->failWithStatus($ret);
525
 
        }
526
 
        $this->_markForCleanup($group1);
527
 
        $ret = GalleryCoreApi::addUserToGroup($oldUser->getId(), $group1->getId());
528
 
        if ($ret) {
529
 
            return $this->failWithStatus($ret);
530
 
        }
531
 
        /* Create Group 2 for user2 for indirect permissions */
532
 
        list ($ret, $group2) = $this->_createRandomGroup();
533
 
        if ($ret) {
534
 
            return $this->failWithStatus($ret);
535
 
        }
536
 
        $this->_markForCleanup($group2);
537
 
        $ret = GalleryCoreApi::addUserToGroup($newUser->getId(), $group2->getId());
538
 
        if ($ret) {
539
 
            return $this->failWithStatus($ret);
540
 
        }
541
 
 
542
 
        /* Without permissions, the user can't add items */
543
 
        $ret = GalleryCoreApi::addUserPermission($this->_album->getId(), $oldUser->getId(),
544
 
                                                array('core.addDataItem'), false);
545
 
        if ($ret) {
546
 
            return $this->failWithStatus($ret);
547
 
        }
548
 
 
549
 
        $this->_activeUserBackup = $gallery->getActiveUser();
550
 
        $gallery->setActiveUser($oldUser);
551
 
 
552
 
        /* Add a test item with the old user as the owner */
553
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_album->getId());
554
 
        if ($ret) {
555
 
            return $this->failWithStatus($ret);
556
 
        }
557
 
 
558
 
        /* Restore the activeUser, should be a Site Admin */
559
 
        $gallery->setActiveUser($this->_activeUserBackup);
560
 
 
561
 
        /* Reset all permissions for this item */
562
 
        $ret = GalleryCoreApi::removeItemPermissions($album->getId());
563
 
        if ($ret) {
564
 
            return $this->failWithStatus($ret);
565
 
        }
566
 
        list ($ret, $checkPermissions) =
567
 
            GalleryCoreApi::fetchAllPermissionsForItem($album->getId(), false);
568
 
        if ($ret) {
569
 
            return $this->failWithStatus($ret);
570
 
        }
571
 
        $this->assertEquals(array(), $checkPermissions, 'could not reset item permissions');
572
 
 
573
 
        /* Add indirect permissions to oldOwner by a group membership */
574
 
        $ret = GalleryCoreApi::addGroupPermission(
575
 
            $album->getId(), $group1->getId(), array('core.changePermissions', 'core.view'), false);
576
 
        if ($ret) {
577
 
            return $this->failWithStatus($ret);
578
 
        }
579
 
        /* Add direct permissions to oldOwner */
580
 
        $ret = GalleryCoreApi::addUserPermission($album->getId(), $oldUser->getId(),
581
 
                                                array('core.addDataItem', 'core.delete'), false);
582
 
        if ($ret) {
583
 
            return $this->failWithStatus($ret);
584
 
        }
585
 
        /* Add indirect permissions to newOwner by a group membership */
586
 
        $ret = GalleryCoreApi::addGroupPermission($album->getId(),
587
 
                                        $group2->getId(), array('core.edit', 'core.view'), false);
588
 
        if ($ret) {
589
 
            return $this->failWithStatus($ret);
590
 
        }
591
 
        /* Add direct permissions to newOwner */
592
 
        $ret = GalleryCoreApi::addUserPermission(
593
 
            $album->getId(), $newUser->getId(),
594
 
            array('core.addDataItem', 'core.addAlbumItem'), false);
595
 
        if ($ret) {
596
 
            return $this->failWithStatus($ret);
597
 
        }
598
 
 
599
 
        /* Execute the call */
600
 
        $ret = GalleryCoreApi::remapOwnerId($oldUser->getId(), $newUser->getId());
601
 
        if ($ret) {
602
 
            return $this->failWithStatus($ret);
603
 
        }
604
 
        GalleryDataCache::reset();
605
 
 
606
 
        /* Verify the owner Id has changed */
607
 
        list ($ret, $album) = $album->refresh();
608
 
        if ($ret) {
609
 
            return $this->failWithStatus($ret);
610
 
        }
611
 
        $this->assert($album->getOwnerId() == $newUser->getId(), 'ownerId was not changed');
612
 
        /* Verify the old owner owns zero items */
613
 
        list ($ret, $itemIds) = GalleryCoreApi::fetchAllItemIdsByOwnerId($oldUser->getId());
614
 
        if ($ret) {
615
 
            return $this->failWithStatus($ret);
616
 
        }
617
 
        $this->assert(count($itemIds) == 0, 'old owner still owns some items');
618
 
        /* Verify the new owner owns exactly 1 item */
619
 
        list ($ret, $itemIds) = GalleryCoreApi::fetchAllItemIdsByOwnerId($newUser->getId());
620
 
        if ($ret) {
621
 
            return $this->failWithStatus($ret);
622
 
        }
623
 
        $this->assert(count($itemIds) == 1, 'new owner owns not exactly 1 item');
624
 
        /* Verify the permissions are direct / indirect */
625
 
        list ($ret, $permissions) =
626
 
            GalleryCoreApi::fetchAllPermissionsForItem($album->getId(), false);
627
 
        if ($ret) {
628
 
            return $this->failWithStatus($ret);
629
 
        }
630
 
        $groupPermissions = $userPermissions = array();
631
 
        foreach ($permissions as $perm) {
632
 
            if (isset($perm['userId'])) {
633
 
                $userPermissions[$perm['userId']][$perm['permission']] = true;
634
 
            } else if (isset($perm['groupId'])) {
635
 
                $groupPermissions[$perm['groupId']][$perm['permission']] = true;
636
 
            }
637
 
        }
638
 
        /* Verify the new user has the right permissions */
639
 
        /* Verify direct permissions */
640
 
        foreach (array('core.changePermissions',  'core.addDataItem',
641
 
                       'core.delete', 'core.addAlbumItem') as $permission) {
642
 
            $this->assert(isset($userPermissions[$newUser->getId()][$permission]),
643
 
                          "direct permission '$permission' not granted");
644
 
        }
645
 
        $this->assert(count($userPermissions[$newUser->getId()]) == 4,
646
 
                      'too few/many direct permissions');
647
 
        /* Verify indirect permissions */
648
 
        foreach (array('core.edit', 'core.view') as $permission) {
649
 
            $this->assert(isset($groupPermissions[$group2->getId()][$permission]),
650
 
                          'indirect permissions changed');
651
 
        }
652
 
        $this->assert(count($groupPermissions[$group2->getId()]) == 2,
653
 
                      'too few/many indirect group permissions');
654
 
        /* Verify the old user's permissions have not been changed */
655
 
        /* Verify direct permissions */
656
 
        foreach (array('core.addDataItem', 'core.delete') as $permission) {
657
 
            $this->assert(isset($userPermissions[$oldUser->getId()][$permission]),
658
 
                          "direct permission '$permission' of old owner changed");
659
 
        }
660
 
        $this->assert(count($userPermissions[$oldUser->getId()]) == 2,
661
 
                      'changed direct permissions of old owner');
662
 
        /* Verify indirect permissions */
663
 
        foreach (array('core.changePermissions', 'core.view') as $permission) {
664
 
            $this->assert(isset($groupPermissions[$group1->getId()][$permission]),
665
 
                          "indirect permission '$permission' of old owner changed");
666
 
        }
667
 
        $this->assert(count($groupPermissions[$group1->getId()]) == 2,
668
 
                      'changed indirect permissions of old owner');
669
 
 
670
 
        /* All test items/users/groups will be deleted in the teardown function */
671
 
    }
672
 
 
673
 
    function testSetThumbnailFromItem() {
674
 
        /* Create album tree */
675
 
        $parentId = $this->_album->getId();
676
 
        for ($i = 0; $i < 3; $i++) {
677
 
            list ($ret, $album[$i]) = $this->_createRandomAlbum($parentId);
678
 
            if ($ret) {
679
 
                return $this->failWithStatus($ret);
680
 
            }
681
 
            $parentId = $album[$i]->getId();
682
 
        }
683
 
 
684
 
        /* Set the preference for one album to something unusual */
685
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($album[0]->getId());
686
 
        if ($ret) {
687
 
            return $this->failWithStatus($ret);
688
 
        }
689
 
 
690
 
        /* Add a thumbnail preference */
691
 
        $ret = GalleryCoreApi::addDerivativePreference(0, $album[0]->getId(),
692
 
                                                       DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
693
 
                                                       'thumbnail|123');
694
 
        if ($ret) {
695
 
            return $this->failWithStatus($ret);
696
 
        }
697
 
 
698
 
        /* Then attach a derivative to one item and associate it with another */
699
 
        list ($ret, $derivative) =
700
 
            $this->_createDerivative($album[2], $album[2]->getId(),
701
 
                                     DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
702
 
                                     'itemtest-operation',
703
 
                                     'test/image');
704
 
        if ($ret) {
705
 
            return $this->failWithStatus($ret);
706
 
        }
707
 
 
708
 
        list ($ret, $success) =
709
 
            GalleryCoreApi::setThumbnailFromItem($album[1]->getId(), $album[2]->getId());
710
 
        if ($ret) {
711
 
            return $this->failWithStatus($ret);
712
 
        }
713
 
        $this->assert($success, 'should be able to set thumbnail');
714
 
 
715
 
        /*
716
 
         * At this point, $album[1]'s thumbnail should match $album[0]'s preference,
717
 
         * and $album[0]'s thumbnail should match $this->_album's preference
718
 
         */
719
 
        list ($ret, $thumbTable) =
720
 
            GalleryCoreApi::fetchThumbnailsByItemIds(array($album[0]->getId(),
721
 
                                                           $album[1]->getId()));
722
 
        if ($ret) {
723
 
            return $this->failWithStatus($ret);
724
 
        }
725
 
 
726
 
        /* Make sure that child(1) has a thumbnail matching child(0)'s preference */
727
 
        list ($ret, $preferences) =
728
 
            GalleryCoreApi::fetchDerivativePreferencesForItem($album[0]->getId());
729
 
        if ($ret) {
730
 
            return $this->failWithStatus($ret);
731
 
        }
732
 
        $thumbnailPreference = null;
733
 
        for ($i = 0; $i < sizeof($preferences); $i++) {
734
 
            if (preg_match('/thumbnail/', $preferences[$i]['derivativeOperations'])) {
735
 
                $thumbnailPreference = $preferences[$i];
736
 
                break;
737
 
            }
738
 
        }
739
 
        $this->assert(isset($thumbnailPreference), 'missing thumbnail preference');
740
 
        $this->assert(isset($thumbTable[$album[1]->getId()]), 'child(1) should have a thumbnail');
741
 
        $this->assertEquals($thumbTable[$album[1]->getId()]->getDerivativeOperations(),
742
 
                            $thumbnailPreference['derivativeOperations']);
743
 
 
744
 
        /* Make sure that child(0) has a thumbnail matching parentItem's preference */
745
 
        list ($ret, $preferences) =
746
 
            GalleryCoreApi::fetchDerivativePreferencesForItem($this->_album->getId());
747
 
        if ($ret) {
748
 
            return $this->failWithStatus($ret);
749
 
        }
750
 
        $thumbnailPreference = null;
751
 
        for ($i = 0; $i < sizeof($preferences); $i++) {
752
 
            if (preg_match('/thumbnail/', $preferences[$i]['derivativeOperations'])) {
753
 
                $thumbnailPreference = $preferences[$i];
754
 
                break;
755
 
            }
756
 
        }
757
 
        $this->assert(isset($thumbnailPreference), 'missing thumbnail preference');
758
 
        $this->assert(isset($thumbTable[$album[0]->getId()]), 'child(0) should have a thumbnail');
759
 
        $this->assertEquals($thumbTable[$album[0]->getId()]->getDerivativeOperations(),
760
 
                            $thumbnailPreference['derivativeOperations']);
761
 
 
762
 
        /* Make sure that the derivatives are linked together properly */
763
 
        $thumb0 = $thumbTable[$album[0]->getId()];
764
 
        $thumb1 = $thumbTable[$album[1]->getId()];
765
 
        $this->assertEquals($thumb0->getDerivativeSourceId(), $thumb1->getId());
766
 
    }
767
 
 
768
 
    /*
769
 
     * Exercise a bug: if you add an unknown item to a new album, it tries to make
770
 
     * that item the thumbnail, but we can't thumbnail-ify it so we wind up throwing
771
 
     * an ERROR_UNSUPPORTED_OPERATION.  Don't let that happen!
772
 
     */
773
 
    function testSetThumbnailFromUnknownItem() {
774
 
        list ($ret, $unknownItem) = $this->_createRandomDataItem($this->_album->getId(),
775
 
                                                                 'application/unknown',
776
 
                                                                 array(),
777
 
                                                                 __FILE__,
778
 
                                                                 'GalleryUnknownItem');
779
 
        if ($ret) {
780
 
            return $this->failWithStatus($ret);
781
 
        }
782
 
 
783
 
        list ($ret, $success) =
784
 
            GalleryCoreApi::setThumbnailFromItem($this->_album->getId(), $unknownItem->getId());
785
 
        if ($ret) {
786
 
            return $this->failWithStatus($ret);
787
 
        }
788
 
 
789
 
        $this->assert(!$success, 'should not be able to set thumbnail');
790
 
 
791
 
        list ($ret, $thumbTable) =
792
 
            GalleryCoreApi::fetchThumbnailsByItemIds(array($this->_album->getId()));
793
 
        if ($ret) {
794
 
            return $this->failWithStatus($ret);
795
 
        }
796
 
 
797
 
        $this->assert(!isset($thumbTable[$this->_album->getId()]));
798
 
    }
799
 
 
800
 
    /*
801
 
     * Exercise a bug: if you add an unknown item and a photo to an album (album highlight
802
 
     * automatically becomes the photo) and then move the photo to another album then the
803
 
     * highlight source is changed to the unknown item --> ERROR_UNSUPPORTED_OPERATION
804
 
     */
805
 
    function testSetThumbnailDontChangeToUnknownItem() {
806
 
        list ($ret, $unknownItem) = $this->_createRandomDataItem($this->_album->getId(),
807
 
                                                                 'application/unknown',
808
 
                                                                 array(),
809
 
                                                                 __FILE__,
810
 
                                                                 'GalleryUnknownItem');
811
 
        if ($ret) {
812
 
            return $this->failWithStatus($ret);
813
 
        }
814
 
 
815
 
        list ($ret, $photoItem) =
816
 
            $this->_createRandomDataItem($this->_album->getId(), 'test/image');
817
 
        if ($ret) {
818
 
            return $this->failWithStatus($ret);
819
 
        }
820
 
 
821
 
        list ($ret, $thumb) = $this->_createDerivative($photoItem, $photoItem->getId(),
822
 
                                                       DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
823
 
                                                       'itemtest-operation', 'test/image');
824
 
        if ($ret) {
825
 
            return $this->failWithStatus($ret);
826
 
        }
827
 
 
828
 
        list ($ret, $highlight) = $this->_createDerivative($this->_album, $thumb->getId(),
829
 
                                                       DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
830
 
                                                       'itemtest-operation', 'test/image');
831
 
        if ($ret) {
832
 
            return $this->failWithStatus($ret);
833
 
        }
834
 
 
835
 
        list ($ret, $success) =
836
 
            GalleryCoreApi::setThumbnailFromItem($this->_album->getId(), $unknownItem->getId());
837
 
        if ($ret) {
838
 
            return $this->failWithStatus($ret);
839
 
        }
840
 
 
841
 
        $this->assert(!$success, 'should not be able to set thumbnail');
842
 
    }
843
 
 
844
 
    function testAddItemToAlbum() {
845
 
        global $gallery;
846
 
        $gallery->_shutdownActions = null;
847
 
 
848
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
849
 
        if ($ret) {
850
 
            return $this->failWithStatus($ret);
851
 
        }
852
 
 
853
 
        list ($ret, $maxWeight) =
854
 
            GalleryCoreApi::fetchExtremeChildWeight($this->_album->getId(), HIGHER_WEIGHT);
855
 
        if ($ret) {
856
 
            return $this->failWithStatus($ret);
857
 
        }
858
 
 
859
 
        list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum(
860
 
            __FILE__, 'itemName', 'title', 'caption', 'description',
861
 
            'application/unknown', $this->_album->getId());
862
 
        if ($ret) {
863
 
            return $this->failWithStatus($ret);
864
 
        }
865
 
 
866
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
867
 
        if ($ret) {
868
 
            return $this->failWithStatus($ret);
869
 
        }
870
 
 
871
 
        $this->assert(GalleryUtilities::isA($newItem, 'GalleryDataItem'),
872
 
                      'newItem is not a GalleryDataItem');
873
 
        $this->assertEquals('itemName', $newItem->getPathComponent());
874
 
        $this->assertEquals('title', $newItem->getTitle());
875
 
        $this->assertEquals('caption', $newItem->getSummary());
876
 
        $this->assertEquals('description', $newItem->getDescription());
877
 
        $this->assertEquals('application/unknown', $newItem->getMimeType());
878
 
        $this->assertEquals($this->_album->getId(), $newItem->getParentId(), 'parent id');
879
 
 
880
 
        /* Test the order weight */
881
 
        list ($ret, $weights) = GalleryCoreApi::fetchItemOrderWeights(array($newItem->getId()));
882
 
        if ($ret) {
883
 
            return $this->failWithStatus($ret);
884
 
        }
885
 
 
886
 
        $this->assert(isset($weights[$newItem->getId()]));
887
 
        $this->assert($weights[$newItem->getId()] > $maxWeight);
888
 
 
889
 
        /* Test that shutdown actions will update parent album's modification timestamp */
890
 
        sleep(1); /* Make sure new timestamp won't equal old one even after update */
891
 
        $gallery->performShutdownActions();
892
 
        list ($ret, $album) = $this->_album->refresh();
893
 
        if ($ret) {
894
 
            return $this->failWithStatus($ret);
895
 
        }
896
 
        $this->assert(
897
 
            $album->getModificationTimestamp() > $this->_album->getModificationTimestamp(),
898
 
            'parent modification timestamp');
899
 
    }
900
 
 
901
 
    /*
902
 
     * Check that when adding a file to an album, the originationTimestamp
903
 
     * provided by some toolkit gets added
904
 
     */
905
 
    function testAddItemToAlbumWithOriginationTimestamp() {
906
 
        /* Register the originationTimestamp property that we can get from our mock data items */
907
 
        $ret = GalleryCoreApi::registerToolkitProperty('ItemTestToolkit',
908
 
                                                       array('test/imageWithTimestamp'),
909
 
                                                       'originationTimestamp',
910
 
                                                       'int',
911
 
                                                       'Get the origination timestamp');
912
 
        if ($ret) {
913
 
            return $this->failWithStatus($ret);
914
 
        }
915
 
 
916
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
917
 
        if ($ret) {
918
 
            return $this->failWithStatus($ret);
919
 
        }
920
 
        list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum(__FILE__,
921
 
                                                               'itemNameWts',
922
 
                                                               'title',
923
 
                                                               'caption',
924
 
                                                               'description',
925
 
                                                               'test/imageWithTimestamp',
926
 
                                                               $this->_album->getId());
927
 
        if ($ret) {
928
 
            return $this->failWithStatus($ret);
929
 
        }
930
 
 
931
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
932
 
        if ($ret) {
933
 
            return $this->failWithStatus($ret);
934
 
        }
935
 
 
936
 
        $this->assertEquals((int)12345, $newItem->getOriginationTimestamp());
937
 
    }
938
 
 
939
 
    function testAddMovieToAlbum() {
940
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
941
 
        if ($ret) {
942
 
            return $this->failWithStatus($ret);
943
 
        }
944
 
 
945
 
        /* Delete existing derivative preferences */
946
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($this->_album->getId());
947
 
        if ($ret) {
948
 
            return $this->failWithStatus($ret);
949
 
        }
950
 
 
951
 
        /* Add a thumbnail preference */
952
 
        $ret = GalleryCoreApi::addDerivativePreference(
953
 
            0, $this->_album->getId(), DERIVATIVE_TYPE_IMAGE_THUMBNAIL, 'thumbnail|101');
954
 
        if ($ret) {
955
 
            return $this->failWithStatus($ret);
956
 
        }
957
 
 
958
 
        /* Add our item to the album */
959
 
        list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum(
960
 
            __FILE__, 'itemName', 'title', 'caption', 'description',
961
 
            'test/movie', $this->_album->getId());
962
 
        if ($ret) {
963
 
            return $this->failWithStatus($ret);
964
 
        }
965
 
 
966
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
967
 
        if ($ret) {
968
 
            return $this->failWithStatus($ret);
969
 
        }
970
 
 
971
 
        /*
972
 
         * Verify that the new item's derivatives.. note we don't expect a preferred derivative
973
 
         * with convert-to-image/jpeg.. we only do this for image/* mime types.
974
 
         */
975
 
        list ($ret, $derivativeTable) =
976
 
            GalleryCoreApi::fetchDerivativesByItemIds(array($newItem->getId()));
977
 
        if ($ret) {
978
 
            return $this->failWithStatus($ret);
979
 
        }
980
 
 
981
 
        if (!isset($derivativeTable[$newItem->getId()])) {
982
 
            return $this->assert(false, 'Missing item derivative');
983
 
        }
984
 
        $derivs = $derivativeTable[$newItem->getId()];
985
 
        if (count($derivs) != 1) {
986
 
            return $this->assertEquals(1, count($derivs), 'Wrong number of derivatives');
987
 
        }
988
 
 
989
 
        $this->assertEquals(DERIVATIVE_TYPE_IMAGE_THUMBNAIL, $derivs[0]->getDerivativeType());
990
 
        $this->assertEquals('convert-to-image/jpeg;thumbnail|101',
991
 
                            $derivs[0]->getDerivativeOperations());
992
 
    }
993
 
 
994
 
    function testFetchItemIdCount() {
995
 
        global $gallery;
996
 
 
997
 
        /* Switch to the anonymous user id so that this returns false */
998
 
        $this->_becomeGuestUser();
999
 
 
1000
 
        list ($ret, $count) =
1001
 
            GalleryCoreApi::fetchItemIdCount('GalleryAlbumItem', 'core.changePermissions');
1002
 
        if ($ret) {
1003
 
            return $this->failWithStatus($ret);
1004
 
        }
1005
 
 
1006
 
        $this->assertEquals(0, $count, 'Should not have permission');
1007
 
    }
1008
 
 
1009
 
    function testFetchItemIdCount2() {
1010
 
        /* We're the admin user so this should return a positive value */
1011
 
        list ($ret, $count) =
1012
 
            GalleryCoreApi::fetchItemIdCount('GalleryAlbumItem', 'core.changePermissions');
1013
 
        if ($ret) {
1014
 
            return $this->failWithStatus($ret);
1015
 
        }
1016
 
 
1017
 
        $this->assert($count > 0, 'Should have permission');
1018
 
    }
1019
 
 
1020
 
    function testDeletingLinks() {
1021
 
        global $gallery;
1022
 
        $platform =& $gallery->getPlatform();
1023
 
 
1024
 
        /* Create a target item */
1025
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
1026
 
        if ($ret) {
1027
 
            return $this->failWithStatus($ret);
1028
 
        }
1029
 
 
1030
 
        /* Create a couple of linked items */
1031
 
        for ($i = 0; $i < 2; $i++) {
1032
 
            list ($ret, $linkedItem[$i]) = $this->_createLinkedItem($this->_album->getId(), $item);
1033
 
            if ($ret) {
1034
 
                return $this->failWithStatus($ret);
1035
 
            }
1036
 
        }
1037
 
 
1038
 
        /* Create a thumbnail */
1039
 
        list ($ret, $thumb) = $this->_createDerivative($linkedItem[0], $item->getId(),
1040
 
                                                       DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
1041
 
                                                       'itemtest-operation', 'test/image');
1042
 
        if ($ret) {
1043
 
            return $this->failWithStatus($ret);
1044
 
        }
1045
 
 
1046
 
        /* All links should point to the original */
1047
 
        list ($ret, $truePath) = $item->fetchPath();
1048
 
        if ($ret) {
1049
 
            return $this->failWithStatus($ret);
1050
 
        }
1051
 
        $this->assert($platform->file_exists($truePath), "Path [$truePath] should exist");
1052
 
 
1053
 
        for ($i = 0; $i < 2; $i++) {
1054
 
            list ($ret, $linkPath) = $linkedItem[$i]->fetchPath();
1055
 
            if ($ret) {
1056
 
                return $this->failWithStatus($ret);
1057
 
            }
1058
 
 
1059
 
            $this->assertEquals($truePath, $linkPath, "Link $i doesn't point to the original");
1060
 
        }
1061
 
 
1062
 
        /* Delete the original, now all links should point to the first link */
1063
 
        $ret = GalleryCoreApi::deleteEntityById($item->getId());
1064
 
        if ($ret) {
1065
 
            return $this->failWithStatus($ret);
1066
 
        }
1067
 
 
1068
 
        /* Deleting changed our items out from under us -- adapt */
1069
 
        list ($ret, $linkedItem[0]) = $linkedItem[0]->refresh();
1070
 
        if ($ret) {
1071
 
            return $this->failWithStatus($ret);
1072
 
        }
1073
 
        list ($ret, $linkedItem[1]) = $linkedItem[1]->refresh();
1074
 
        if ($ret) {
1075
 
            return $this->failWithStatus($ret);
1076
 
        }
1077
 
        list ($ret, $thumb) = $thumb->refresh();
1078
 
        if ($ret) {
1079
 
            return $this->failWithStatus($ret);
1080
 
        }
1081
 
 
1082
 
        list ($ret, $truePath) = $linkedItem[0]->fetchPath();
1083
 
        if ($ret) {
1084
 
            return $this->failWithStatus($ret);
1085
 
        }
1086
 
        $this->assert($platform->file_exists($truePath), "Path [$truePath] should exist");
1087
 
 
1088
 
        list ($ret, $linkPath) = $linkedItem[1]->fetchPath();
1089
 
        if ($ret) {
1090
 
            return $this->failWithStatus($ret);
1091
 
        }
1092
 
 
1093
 
        $this->assertEquals($truePath, $linkPath, "Link doesn't point to the original (link)");
1094
 
 
1095
 
        $this->assertEquals($linkedItem[0]->getId(), $thumb->getDerivativeSourceId(),
1096
 
                            'Bad thumbnail source (deleted item was ' . $item->getId() . ')');
1097
 
    }
1098
 
 
1099
 
    function testDelete() {
1100
 
        global $gallery;
1101
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_album->getId());
1102
 
        if ($ret) {
1103
 
            return $this->failWithStatus($ret);
1104
 
        }
1105
 
        list ($ret, $item) = $this->_createRandomDataItem($album->getId());
1106
 
        if ($ret) {
1107
 
            return $this->failWithStatus($ret);
1108
 
        }
1109
 
        $gallery->_shutdownActions = null;
1110
 
 
1111
 
        $ret = GalleryCoreApi::deleteEntityById($album->getId());
1112
 
        if ($ret) {
1113
 
            return $this->failWithStatus($ret);
1114
 
        }
1115
 
 
1116
 
        list ($ret, $ids) = GalleryCoreApi::fetchChildItemIds($this->_album);
1117
 
        if ($ret) {
1118
 
            return $this->failWithStatus($ret);
1119
 
        }
1120
 
        $this->assertEquals(array(), $ids, 'child item ids');
1121
 
 
1122
 
        /*
1123
 
         * Test that shutdown actions will update parent album's modification timestamp
1124
 
         * Also ensure invalid id in shutdown action is ignored (delete of $item will
1125
 
         * trigger shutdown action for $album which was deleted too...)
1126
 
         */
1127
 
        sleep(1); /* Make sure new timestamp won't equal old one even after update */
1128
 
        $gallery->performShutdownActions();
1129
 
        list ($ret, $album) = $this->_album->refresh();
1130
 
        if ($ret) {
1131
 
            return $this->failWithStatus($ret);
1132
 
        }
1133
 
        $this->assert(
1134
 
            $album->getModificationTimestamp() > $this->_album->getModificationTimestamp(),
1135
 
            'parent modification timestamp');
1136
 
    }
1137
 
 
1138
 
    function testMove() {
1139
 
        global $gallery;
1140
 
 
1141
 
        /* Create a destination album */
1142
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_album->getId());
1143
 
        if ($ret) {
1144
 
            return $this->failWithStatus($ret);
1145
 
        }
1146
 
 
1147
 
        /* Create a target item */
1148
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId());
1149
 
        if ($ret) {
1150
 
            return $this->failWithStatus($ret);
1151
 
        }
1152
 
        $gallery->_shutdownActions = null;
1153
 
 
1154
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
1155
 
        if ($ret) {
1156
 
            return $this->failWithStatus($ret);
1157
 
        }
1158
 
 
1159
 
        $this->assertEquals($this->_album->getId(), $item->getParentId(), 'Wrong original parent');
1160
 
        $this->assertEquals(array($this->_getRootId(),
1161
 
                                  $this->_album->getId()),
1162
 
                            $parentSequence,
1163
 
                            'Wrong original parent sequence');
1164
 
 
1165
 
        list ($ret, $locks[]) =
1166
 
            GalleryCoreApi::acquireReadLock(array($this->_album->getId(),
1167
 
                                                  $album->getId()));
1168
 
        if ($ret) {
1169
 
            return $this->failWithStatus($ret);
1170
 
        }
1171
 
 
1172
 
        list ($ret, $locks[]) = GalleryCoreApi::acquireWriteLock($item->getId());
1173
 
        if ($ret) {
1174
 
            return $this->failWithStatus($ret);
1175
 
        }
1176
 
 
1177
 
        list ($ret, $item) = $item->refresh();
1178
 
        if ($ret) {
1179
 
            return $this->failWithStatus($ret);
1180
 
        }
1181
 
 
1182
 
        $ret = $item->move($album->getId());
1183
 
        if ($ret) {
1184
 
            return $this->failWithStatus($ret);
1185
 
        }
1186
 
 
1187
 
        $ret = $item->save();
1188
 
        if ($ret) {
1189
 
            return $this->failWithStatus($ret);
1190
 
        }
1191
 
 
1192
 
        GalleryDataCache::reset();
1193
 
 
1194
 
        list ($ret, $parentSequence) = GalleryCoreApi::fetchParentSequence($item->getId());
1195
 
        if ($ret) {
1196
 
            return $this->failWithStatus($ret);
1197
 
        }
1198
 
 
1199
 
        $this->assertEquals($album->getId(), $item->getParentId(), 'Wrong parent');
1200
 
        $this->assertEquals(array($this->_getRootId(),
1201
 
                                  $this->_album->getId(),
1202
 
                                  $album->getId()),
1203
 
                            $parentSequence,
1204
 
                            'Wrong parent sequence');
1205
 
 
1206
 
        $ret = GalleryCoreApi::releaseLocks($locks);
1207
 
        if ($ret) {
1208
 
            return $this->failWithStatus($ret);
1209
 
        }
1210
 
 
1211
 
        /* Test that shutdown actions will update parent album's modification timestamp */
1212
 
        sleep(1); /* Make sure new timestamp won't equal old one even after update */
1213
 
        $gallery->performShutdownActions();
1214
 
        list ($ret, $refreshedAlbum) = $album->refresh();
1215
 
        if ($ret) {
1216
 
            return $this->failWithStatus($ret);
1217
 
        }
1218
 
        $this->assert(
1219
 
            $refreshedAlbum->getModificationTimestamp() > $album->getModificationTimestamp(),
1220
 
            'destination album modification timestamp');
1221
 
        list ($ret, $refreshedAlbum) = $this->_album->refresh();
1222
 
        if ($ret) {
1223
 
            return $this->failWithStatus($ret);
1224
 
        }
1225
 
        $this->assert(
1226
 
            $refreshedAlbum->getModificationTimestamp() > $this->_album->getModificationTimestamp(),
1227
 
            'source album modification timestamp');
1228
 
    }
1229
 
 
1230
 
    /**
1231
 
     * Add a small (dimension wise) photo to an album and make sure that we don't create
1232
 
     * resizes that are larger (dimension wise) than the original.
1233
 
     */
1234
 
    function testAddSmallPhotoToAlbum() {
1235
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
1236
 
        if ($ret) {
1237
 
            return $this->failWithStatus($ret);
1238
 
        }
1239
 
 
1240
 
        /*
1241
 
         * Set the album preferences such that we have one resize that's smaller than the
1242
 
         * original, and one that's larger.  We know that the image we're using is 62x75
1243
 
         */
1244
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($this->_album->getId());
1245
 
        if ($ret) {
1246
 
            return $this->failWithStatus($ret);
1247
 
        }
1248
 
 
1249
 
        $ret = GalleryCoreApi::addDerivativePreference(1, $this->_album->getId(),
1250
 
                                                       DERIVATIVE_TYPE_IMAGE_RESIZE,
1251
 
                                                       'scale|50');
1252
 
        if ($ret) {
1253
 
            return $this->failWithStatus($ret);
1254
 
        }
1255
 
 
1256
 
        $ret = GalleryCoreApi::addDerivativePreference(2, $this->_album->getId(),
1257
 
                                                       DERIVATIVE_TYPE_IMAGE_RESIZE,
1258
 
                                                       'scale|100');
1259
 
        if ($ret) {
1260
 
            return $this->failWithStatus($ret);
1261
 
        }
1262
 
 
1263
 
        $ret = GalleryCoreApi::addDerivativePreference(3, $this->_album->getId(),
1264
 
                                                       DERIVATIVE_TYPE_IMAGE_RESIZE,
1265
 
                                                       'scale|65,100');
1266
 
        if ($ret) {
1267
 
            return $this->failWithStatus($ret);
1268
 
        }
1269
 
 
1270
 
        $ret = GalleryCoreApi::addDerivativePreference(4, $this->_album->getId(),
1271
 
                                                       DERIVATIVE_TYPE_IMAGE_THUMBNAIL,
1272
 
                                                       'thumbnail|25');
1273
 
        if ($ret) {
1274
 
            return $this->failWithStatus($ret);
1275
 
        }
1276
 
 
1277
 
        list ($ret, $photo) = GalleryCoreApi::newItemByMimeType('image/jpeg');
1278
 
        if ($ret) {
1279
 
            return $this->failWithStatus($ret);
1280
 
        }
1281
 
 
1282
 
        $ret = $photo->create($this->_album->getId(),
1283
 
                              dirname(__FILE__) . '/../data/test1.gif',
1284
 
                              'image/gif',
1285
 
                              'test image');
1286
 
        if ($ret) {
1287
 
            return $this->failWithStatus($ret);
1288
 
        }
1289
 
 
1290
 
        $ret = GalleryCoreApi::addExistingItemToAlbum($photo, $this->_album->getId());
1291
 
        if ($ret) {
1292
 
            return $this->failWithStatus($ret);
1293
 
        }
1294
 
 
1295
 
        $ret = $photo->save();
1296
 
        if ($ret) {
1297
 
            return $this->failWithStatus($ret);
1298
 
        }
1299
 
 
1300
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
1301
 
        if ($ret) {
1302
 
            return $this->failWithStatus($ret);
1303
 
        }
1304
 
 
1305
 
        /* Now verify that we didn't upsample our resizes */
1306
 
        list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds(array($photo->getId()));
1307
 
        if ($ret) {
1308
 
            return $this->failWithStatus($ret);
1309
 
        }
1310
 
 
1311
 
        $this->assert(isset($resizes[$photo->getId()]), 'Missing resizes for new photo');
1312
 
        if (isset($resizes[$photo->getId()])) {
1313
 
            $resizes = $resizes[$photo->getId()];
1314
 
            $this->assertEquals(1, sizeof($resizes), 'There should only be one resize');
1315
 
            $this->assertEquals('scale|50', $resizes[0]->getDerivativeOperations(),
1316
 
                                'The resize operation should be scale|50');
1317
 
        }
1318
 
 
1319
 
        /* Now verify the photo has been made the highlight of the album.. */
1320
 
        list ($ret, $thumbTable) = GalleryCoreApi::fetchThumbnailsByItemIds(
1321
 
                                   array($this->_album->getId(), $photo->getId()));
1322
 
        if ($ret) {
1323
 
            return $this->failWithStatus($ret);
1324
 
        }
1325
 
        $this->assert(isset($thumbTable[$this->_album->getId()]), 'Missing highlight for album');
1326
 
        $this->assert(isset($thumbTable[$photo->getId()]), 'Missing thumbnail for photo');
1327
 
        if (count($thumbTable) == 2) {
1328
 
            $this->assertEquals(
1329
 
                $thumbTable[$photo->getId()]->getId(),
1330
 
                $thumbTable[$this->_album->getId()]->getDerivativeSourceId(), 'highlight id');
1331
 
        }
1332
 
 
1333
 
        /* Make sure setting the same highlight doesn't throw an error.. */
1334
 
        list ($ret, $success) =
1335
 
            GalleryCoreApi::setThumbnailFromItem($this->_album->getId(), $photo->getId());
1336
 
        if ($ret) {
1337
 
            return $this->failWithStatus($ret);
1338
 
        }
1339
 
        $this->assert($success, 'should be able to set highlight');
1340
 
    }
1341
 
 
1342
 
    function testAddNonViewableImage() {
1343
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
1344
 
        if ($ret) {
1345
 
            return $this->failWithStatus($ret);
1346
 
        }
1347
 
 
1348
 
        /* Get rid of all album preferences */
1349
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($this->_album->getId());
1350
 
        if ($ret) {
1351
 
            return $this->failWithStatus($ret);
1352
 
        }
1353
 
 
1354
 
        /* Register operations to convert from our test types */
1355
 
        $ret = GalleryCoreApi::registerToolkitOperation(
1356
 
            'ItemTestToolkit', array('image/nonviewable'), 'convert-to-image/x-portable-pixmap',
1357
 
            array(), 'itemtest-description', 'image/x-portable-pixmap');
1358
 
        if ($ret) {
1359
 
            return $this->failWithStatus($ret);
1360
 
        }
1361
 
        $ret = GalleryCoreApi::registerToolkitOperation(
1362
 
            'ItemTestToolkit', array('test/document'), 'convert-to-image/jpeg',
1363
 
            array(), 'itemtest-description', 'image/jpeg');
1364
 
        if ($ret) {
1365
 
            return $this->failWithStatus($ret);
1366
 
        }
1367
 
 
1368
 
        /* Test one image type and one document type */
1369
 
        foreach (array('image/nonviewable', 'test/document') as $i => $mimeType) {
1370
 
            list ($ret, $item) = GalleryCoreApi::newItemByMimeType($mimeType);
1371
 
            if ($ret) {
1372
 
                return $this->failWithStatus($ret);
1373
 
            }
1374
 
 
1375
 
            $ret = $item->create($this->_album->getId(), __FILE__, $mimeType, 'test item');
1376
 
            if ($ret) {
1377
 
                return $this->failWithStatus($ret);
1378
 
            }
1379
 
            if (method_exists($item, 'setwidth')) {
1380
 
                $item->setWidth(1234);
1381
 
                $item->setHeight(567);
1382
 
            }
1383
 
 
1384
 
            $ret = GalleryCoreApi::addExistingItemToAlbum($item, $this->_album->getId());
1385
 
            if ($ret) {
1386
 
                return $this->failWithStatus($ret);
1387
 
            }
1388
 
 
1389
 
            $ret = $item->save();
1390
 
            if ($ret) {
1391
 
                return $this->failWithStatus($ret);
1392
 
            }
1393
 
 
1394
 
            /* Make sure that we have a preferred that contains the proper convert operations */
1395
 
            $itemId = $item->getId();
1396
 
            list ($ret, $preferreds) = GalleryCoreApi::fetchPreferredsByItemIds(array($itemId));
1397
 
            if ($ret) {
1398
 
                $this->failWithStatus($ret);
1399
 
            }
1400
 
            if (isset($preferreds[$itemId])) {
1401
 
                $this->assertEquals(
1402
 
                        ($i ? '' : 'convert-to-image/x-portable-pixmap;') . 'convert-to-image/jpeg',
1403
 
                        $preferreds[$itemId]->getDerivativeOperations(), $mimeType . ' operations');
1404
 
                if (!$i) {
1405
 
                    $this->assertEquals(1234, $preferreds[$itemId]->getWidth(), 'width');
1406
 
                    $this->assertEquals(567, $preferreds[$itemId]->getHeight(), 'height');
1407
 
                }
1408
 
            } else {
1409
 
                $this->assert(false, 'missing preferred for ' . $mimeType);
1410
 
            }
1411
 
        }
1412
 
 
1413
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
1414
 
        if ($ret) {
1415
 
            return $this->failWithStatus($ret);
1416
 
        }
1417
 
    }
1418
 
 
1419
 
    function testAddNonViewableImageWithPreExistingPreferred() {
1420
 
        list ($ret, $lockId[]) = GalleryCoreApi::acquireReadLock($this->_album->getId());
1421
 
        if ($ret) {
1422
 
            return $this->failWithStatus($ret);
1423
 
        }
1424
 
 
1425
 
        /* Get rid of all album preferences */
1426
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($this->_album->getId());
1427
 
        if ($ret) {
1428
 
            return $this->failWithStatus($ret);
1429
 
        }
1430
 
 
1431
 
        /* Register an operation for our preferred to perform */
1432
 
        $ret = GalleryCoreApi::registerToolkitOperation(
1433
 
            'ItemTestToolkit', array('image/nonviewable'), 'test-operation',
1434
 
            array(), 'itemtest-description');
1435
 
        if ($ret) {
1436
 
            return $this->failWithStatus($ret);
1437
 
        }
1438
 
 
1439
 
        /* Register an operation to convert from our test type */
1440
 
        $ret = GalleryCoreApi::registerToolkitOperation(
1441
 
            'ItemTestToolkit', array('image/nonviewable'), 'convert-to-image/x-portable-pixmap',
1442
 
            array(), 'itemtest-description', 'image/x-portable-pixmap');
1443
 
        if ($ret) {
1444
 
            return $this->failWithStatus($ret);
1445
 
        }
1446
 
 
1447
 
        list ($ret, $photo) = GalleryCoreApi::newItemByMimeType('image/nonviewable');
1448
 
        if ($ret) {
1449
 
            return $this->failWithStatus($ret);
1450
 
        }
1451
 
 
1452
 
        $ret = $photo->create($this->_album->getId(), __FILE__, 'image/nonviewable', 'test image');
1453
 
        if ($ret) {
1454
 
            return $this->failWithStatus($ret);
1455
 
        }
1456
 
 
1457
 
        $ret = $photo->save();
1458
 
        if ($ret) {
1459
 
            return $this->failWithStatus($ret);
1460
 
        }
1461
 
 
1462
 
        /* Add a preferred */
1463
 
        list ($ret, $preferred) = $this->_createDerivative(
1464
 
            $photo, $photo->getId(), DERIVATIVE_TYPE_IMAGE_PREFERRED,
1465
 
            'test-operation', 'image/nonviewable');
1466
 
        if ($ret) {
1467
 
            return $this->failWithStatus($ret);
1468
 
        }
1469
 
 
1470
 
        list ($ret, $lockId[]) = GalleryCoreApi::acquireWriteLock($photo->getId());
1471
 
        if ($ret) {
1472
 
            return $this->failWithStatus($ret);
1473
 
        }
1474
 
 
1475
 
        $ret = GalleryCoreApi::addExistingItemToAlbum($photo, $this->_album->getId());
1476
 
        if ($ret) {
1477
 
            return $this->failWithStatus($ret);
1478
 
        }
1479
 
 
1480
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
1481
 
        if ($ret) {
1482
 
            return $this->failWithStatus($ret);
1483
 
        }
1484
 
 
1485
 
        /* Make sure that we have a preferred that contains the proper convert operations */
1486
 
        list ($ret, $preferreds) = GalleryCoreApi::fetchPreferredsByItemIds(array($photo->getId()));
1487
 
        if ($ret) {
1488
 
            return $this->failWithStatus($ret);
1489
 
        }
1490
 
        $preferred = $preferreds[$photo->getId()];
1491
 
        $this->assertEquals(
1492
 
            'test-operation;convert-to-image/x-portable-pixmap;convert-to-image/jpeg',
1493
 
            $preferred->getDerivativeOperations());
1494
 
    }
1495
 
 
1496
 
    function testSymlinkTwice() {
1497
 
        global $gallery;
1498
 
        $platform =& $gallery->getPlatform();
1499
 
        if (!$platform->isSymlinkSupported()) {
1500
 
            /* Skip this test if symlink is not supported */
1501
 
            return;
1502
 
        }
1503
 
 
1504
 
        /* Register the dimensions property for our mock data item */
1505
 
        $ret = GalleryCoreApi::registerToolkitProperty('ItemTestToolkit',
1506
 
            array('image/test'), 'dimensions', 'int,int', 'Get the dimensions');
1507
 
        if ($ret) {
1508
 
            return $this->failWithStatus($ret);
1509
 
        }
1510
 
 
1511
 
        /*
1512
 
         * Ensure we don't error out if we add-with-symlink and a problem with the datafile
1513
 
         * requires falling back to GalleryUnknownItem (so we try the symlink twice).
1514
 
         */
1515
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
1516
 
        if ($ret) {
1517
 
            return $this->failWithStatus($ret);
1518
 
        }
1519
 
 
1520
 
        list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum(
1521
 
            __FILE__, 'itemName', 'title', 'caption', 'description',
1522
 
            'image/test', $this->_album->getId(), true);
1523
 
        if ($ret) {
1524
 
            GalleryCoreApi::releaseLocks($lockId);
1525
 
            return $this->failWithStatus($ret);
1526
 
        }
1527
 
 
1528
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
1529
 
        if ($ret) {
1530
 
            return $this->failWithStatus($ret);
1531
 
        }
1532
 
 
1533
 
        /* Just verify a GalleryUnknownItem was created */
1534
 
        $this->assert(GalleryUtilities::isA($newItem, 'GalleryUnknownItem'),
1535
 
                      'newItem is not a GalleryUnknownItem');
1536
 
    }
1537
 
 
1538
 
    function testDeleteSortOrderFromAlbums() {
1539
 
        foreach (array('ItemTestOrder', 'ItemTestOrder|something', 'something|ItemTestOrder')
1540
 
                 as $order) {
1541
 
            list ($ret, $albums[$order]) = $this->_createRandomAlbum(
1542
 
                $this->_album->getId(),
1543
 
                array('orderBy' => $order, 'orderDirection' => ORDER_ASCENDING));
1544
 
            if ($ret) {
1545
 
                return $this->failWithStatus($ret);
1546
 
            }
1547
 
        }
1548
 
 
1549
 
        $ret = GalleryCoreApi::deleteSortOrder('ItemTestOrder');
1550
 
        if ($ret) {
1551
 
            return $this->failWithStatus($ret);
1552
 
        }
1553
 
 
1554
 
        foreach ($albums as $order => $album) {
1555
 
            list ($ret, $album) = $album->refresh();
1556
 
            if ($ret) {
1557
 
                return $this->failWithStatus($ret);
1558
 
            }
1559
 
 
1560
 
            $this->assertEquals('', $album->getOrderBy(), "orderBy not removed for '$order'");
1561
 
            $this->assertEquals(ORDER_ASCENDING, $album->getOrderDirection(),
1562
 
                                "orderDirection changed for '$order'");
1563
 
        }
1564
 
    }
1565
 
 
1566
 
    function testDeleteSortOrderFromDefault() {
1567
 
        $ret = $this->_markPluginParametersForCleanup('module', 'core');
1568
 
        if ($ret) {
1569
 
            return $this->failWithStatus($ret);
1570
 
        }
1571
 
 
1572
 
        foreach (array(array('sort' => array('MyTestSortOrder', 'orderWeight'),
1573
 
                             'order' => array(ORDER_ASCENDING, ORDER_ASCENDING)),
1574
 
                       array('sort' => array('MyPresort|MyTestSortOrder', 'orderWeight'),
1575
 
                             'order' => array(ORDER_DESCENDING, ORDER_DESCENDING)),
1576
 
                       array('sort' => array('SomeOtherSortOrder', 'SomeOtherSortOrder'),
1577
 
                             'order' => array(ORDER_ASCENDING, ORDER_ASCENDING)))
1578
 
                 as $index => $test) {
1579
 
            $ret = GalleryCoreApi::setPluginParameter(
1580
 
                'module', 'core', 'default.orderBy', $test['sort'][0]);
1581
 
            if ($ret) {
1582
 
                return $this->failWithStatus($ret);
1583
 
            }
1584
 
 
1585
 
            $ret = GalleryCoreApi::setPluginParameter(
1586
 
                'module', 'core', 'default.orderDirection', $test['order'][0]);
1587
 
            if ($ret) {
1588
 
                return $this->failWithStatus($ret);
1589
 
            }
1590
 
 
1591
 
            $ret = GalleryCoreApi::deleteSortOrder('MyTestSortOrder');
1592
 
            if ($ret) {
1593
 
                return $this->failWithStatus($ret);
1594
 
            }
1595
 
 
1596
 
            list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'core');
1597
 
            if ($ret) {
1598
 
                return $this->failWithStatus($ret);
1599
 
            }
1600
 
 
1601
 
            $this->assertEquals($test['sort'][1], $params['default.orderBy'],
1602
 
                                "orderBy: $index test case");
1603
 
            $this->assertEquals($test['order'][1], $params['default.orderDirection'],
1604
 
                                "orderDirection: $index test case");
1605
 
        }
1606
 
    }
1607
 
 
1608
 
    function testMultipleThumbnailPreferences() {
1609
 
        $ret = GalleryCoreApi::removeDerivativePreferencesForItem($this->_album->getId());
1610
 
        if ($ret) {
1611
 
            return $this->failWithStatus($ret);
1612
 
        }
1613
 
 
1614
 
        /* Add 2 thumbnail preferences */
1615
 
        for ($i = 0; $i < 2; $i++) {
1616
 
            $ret = GalleryCoreApi::addDerivativePreference(
1617
 
                0, $this->_album->getId(), DERIVATIVE_TYPE_IMAGE_THUMBNAIL, "thumbnail|[$i]");
1618
 
            if ($ret) {
1619
 
                return $this->failWithStatus($ret);
1620
 
            }
1621
 
        }
1622
 
 
1623
 
        list ($ret, $lockId) = GalleryCoreApi::acquireReadLock($this->_album->getId());
1624
 
        if ($ret) {
1625
 
            return $this->failWithStatus($ret);
1626
 
        }
1627
 
 
1628
 
        list ($ret, $newItem) = GalleryCoreApi::addItemToAlbum(
1629
 
            dirname(__FILE__) . '/../data/test1.jpg', 'itemName',
1630
 
            'title', 'caption', 'description', 'image/jpeg', $this->_album->getId());
1631
 
        if ($ret) {
1632
 
            return $this->failWithStatus($ret);
1633
 
        }
1634
 
 
1635
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
1636
 
        if ($ret) {
1637
 
            return $this->failWithStatus($ret);
1638
 
        }
1639
 
 
1640
 
        /* We should have ignored the duplicate preference  */
1641
 
        list ($ret, $thumbTable) = GalleryCoreApi::fetchThumbnailsByItemIds(
1642
 
            array($newItem->getId()));
1643
 
        if ($ret) {
1644
 
            return $this->failWithStatus($ret);
1645
 
        }
1646
 
 
1647
 
        $this->assert(!empty($thumbTable[$newItem->getId()]), 'missing thumbnail');
1648
 
        $operation = $thumbTable[$newItem->getId()]->getDerivativeOperations();
1649
 
        $this->assert(($operation == 'thumbnail|[0]' ||
1650
 
                       $operation == 'thumbnail|[1]'), 'Wrong operation');
1651
 
    }
1652
 
 
1653
 
    function testDeleteRenderer() {
1654
 
        foreach (array('ItemTestRendererToDelete', 'ItemTestRendererToKeep') as $rendererClass) {
1655
 
            list ($ret, $items[]) = $this->_createRandomDataItem(
1656
 
                $this->_album->getId(), 'application/unknown', array('renderer' => $rendererClass));
1657
 
            if ($ret) {
1658
 
                return $this->failWithStatus($ret);
1659
 
            }
1660
 
        }
1661
 
 
1662
 
        $ret = GalleryCoreApi::deleteRenderer('ItemTestRendererToDelete');
1663
 
        if ($ret) {
1664
 
            return $this->failWithStatus($ret);
1665
 
        }
1666
 
 
1667
 
        for ($i = 0; $i < 2; $i++) {
1668
 
            list ($ret,  $items[$i]) = $items[$i]->refresh();
1669
 
            if ($ret) {
1670
 
                return $this->failWithStatus($ret);
1671
 
            }
1672
 
        }
1673
 
 
1674
 
        /* For Oracle, null is replaced with '' when loading data from the database */
1675
 
        $this->assert(in_array($items[0]->getRenderer(), array(null, '')));
1676
 
        $this->assertEquals('ItemTestRendererToKeep', $items[1]->getRenderer());
1677
 
    }
1678
 
 
1679
 
    function testCreateLinkCopiesRenderer() {
1680
 
        /* Create target item */
1681
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_album->getId(),
1682
 
                'application/unknown', array('renderer' => 'TEST_RENDERER'));
1683
 
        if ($ret) {
1684
 
            return $this->failWithStatus($ret);
1685
 
        }
1686
 
 
1687
 
        list ($ret, $linkedItem) = $this->_createLinkedItem($this->_album->getId(), $item);
1688
 
        if ($ret) {
1689
 
            return $this->failWithStatus($ret);
1690
 
        }
1691
 
 
1692
 
        $this->assertEquals('TEST_RENDERER', $linkedItem->getRenderer());
1693
 
    }
1694
 
}
1695
 
 
1696
 
/**
1697
 
 * Test toolkit
1698
 
 *
1699
 
 * @package GalleryCore
1700
 
 * @subpackage PHPUnit
1701
 
 */
1702
 
class ItemTestToolkit extends GalleryToolkit {
1703
 
 
1704
 
    function getProperty($mimeType, $propertyName, $sourceFilename) {
1705
 
        /* This will be called by addItemToAlbum */
1706
 
        if ($propertyName == 'originationTimestamp') {
1707
 
            $results = null;
1708
 
            if (basename($sourceFilename) == 'itemNameWts') {
1709
 
                $results = array(12345);
1710
 
            }
1711
 
            return array(null, $results);
1712
 
        } else if ($propertyName == 'dimensions'
1713
 
                && in_array($mimeType, array('image/gif', 'image/jpeg'))) {
1714
 
            return array(null, array(62, 75));
1715
 
        }
1716
 
        /* We'll reach this point in testSymlinkTwice, but we want to return an error */
1717
 
        return array(GalleryCoreApi::error(ERROR_UNIMPLEMENTED), null);
1718
 
    }
1719
 
 
1720
 
}
1721
 
 
1722
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryMovieItem.class');
1723
 
GalleryCoreApi::requireOnce('modules/core/classes/GalleryRenderer.class');
1724
 
 
1725
 
/**
1726
 
 * Test movie item
1727
 
 */
1728
 
class ItemTestMovieItem extends GalleryMovieItem {
1729
 
    /**
1730
 
     * @see GalleryEntity::getClassName
1731
 
     */
1732
 
    function getClassName() {
1733
 
        return 'ItemTestMovieItem';
1734
 
    }
1735
 
}
1736
 
 
1737
 
/**
1738
 
 * Test renderer
1739
 
 */
1740
 
class ItemTestRenderer extends GalleryRenderer {
1741
 
    /**
1742
 
     * @see GalleryRenderer::canBeViewedInline
1743
 
     */
1744
 
    function canBeViewedInline($item) {
1745
 
        return (substr($item->getMimeType(), 0, 5) == 'test/');
1746
 
    }
1747
 
 
1748
 
    /**
1749
 
     * @see GalleryRenderer::render
1750
 
     */
1751
 
    function render($format, $entity, $item, $params) {
1752
 
        return null;
1753
 
    }
1754
 
}
1755
 
?>