~ubuntu-branches/ubuntu/lucid/gallery2/lucid

« back to all changes in this revision

Viewing changes to modules/core/test/phpunit/UserAndGroupTest.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 User and Group functionality
23
 
 * @package GalleryCore
24
 
 * @subpackage PHPUnit
25
 
 * @author Bharat Mediratta <bharat@menalto.com>
26
 
 * @version $Revision: 15513 $
27
 
 */
28
 
class UserAndGroupTest extends GalleryTestCase {
29
 
 
30
 
    function UserAndGroupTest($methodName) {
31
 
        $this->GalleryTestCase($methodName);
32
 
    }
33
 
 
34
 
    function setUp() {
35
 
        parent::setUp();
36
 
 
37
 
        list ($ret, $this->_siteAdminGroupId) =
38
 
            GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
39
 
        if ($ret) {
40
 
            print $ret->getAsHtml();
41
 
            return $this->failWithStatus($ret);
42
 
        }
43
 
    }
44
 
 
45
 
    function tearDown() {
46
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core',
47
 
                                                  'id.adminGroup', $this->_siteAdminGroupId);
48
 
        if ($ret) {
49
 
            $this->failWithStatus($ret);
50
 
        }
51
 
 
52
 
        parent::tearDown();
53
 
    }
54
 
 
55
 
    /**
56
 
     * Create user, delete user
57
 
     */
58
 
    function testCreateUser() {
59
 
        global $gallery;
60
 
 
61
 
        /* Create a new user */
62
 
        list ($ret, $user) = $this->_createRandomUser();
63
 
        if ($ret) {
64
 
            return $this->failWithStatus($ret);
65
 
        }
66
 
 
67
 
        GalleryDataCache::reset();
68
 
 
69
 
        /* Verify it */
70
 
        $ret = $this->_verifyEntity($user);
71
 
        if ($ret) {
72
 
            return $this->failWithStatus($ret);
73
 
        }
74
 
 
75
 
        /* Delete it */
76
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
77
 
        if ($ret) {
78
 
            return $this->failWithStatus($ret);
79
 
        }
80
 
    }
81
 
 
82
 
    /**
83
 
     * Create group, create user, add user to group, assign
84
 
     * permission to user, add item to user, delete user. check if permission
85
 
     * and group maps are gone, check if item still exists with correct owner, delete group
86
 
     */
87
 
    function testDeleteUser() {
88
 
        global $gallery;
89
 
 
90
 
        /* Create a new group */
91
 
        list ($ret, $group) = $this->_createRandomGroup();
92
 
        if ($ret) {
93
 
            return $this->failWithStatus($ret);
94
 
        }
95
 
        $this->_markForCleanup($group);
96
 
        $groupId = $group->getId();
97
 
 
98
 
        /* Create a new user */
99
 
        list ($ret, $user) = $this->_createRandomUser();
100
 
        if ($ret) {
101
 
            return $this->failWithStatus($ret);
102
 
        }
103
 
        $userId = $user->getId();
104
 
 
105
 
        /* Put the user into the group */
106
 
        $ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
107
 
        if ($ret) {
108
 
            $this->_markForCleanup($user);
109
 
            return $this->failWithStatus($ret);
110
 
        }
111
 
        /* Verify */
112
 
        list ($ret, $inGroup) = GalleryCoreApi::isUserInGroup($userId, $groupId);
113
 
        if ($ret) {
114
 
            $this->_markForCleanup($user);
115
 
            return $this->failWithStatus($ret);
116
 
        }
117
 
        $this->assert($inGroup, 'failed to add the user to a group');
118
 
 
119
 
        /* Add a permission to the user */
120
 
        $ret = GalleryCoreApi::addUserPermission($this->_getRootId(), $userId,
121
 
                                                'core.addDataItem', false);
122
 
        if ($ret) {
123
 
            GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
124
 
            $this->_markForCleanup($user);
125
 
            return $this->failWithStatus($ret);
126
 
        }
127
 
        /* Verify the user has the permission  */
128
 
        list ($ret, $hasPermission) =
129
 
            GalleryCoreApi::hasItemPermission($this->_getRootId(), 'core.addDataItem');
130
 
        if ($ret) {
131
 
            GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
132
 
            $this->_markForCleanup($user);
133
 
            return $this->failWithStatus($ret);
134
 
        }
135
 
        $this->assert($hasPermission, 'failed to add a permission to the user');
136
 
 
137
 
        $this->_activeUserBackup = $gallery->getActiveUser();
138
 
        $gallery->setActiveUser($user);
139
 
 
140
 
        /* Add an item to the user, this item should be remapped to a new owner automatically */
141
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_getRootId());
142
 
        if ($ret) {
143
 
            GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $user->getId));
144
 
            GalleryCoreApi::removeMapEntry(
145
 
                'GalleryAccessMap', array('userOrGroupId' => $user->getId));
146
 
            $this->_markForCleanup($user);
147
 
            return $this->failWithStatus($ret);
148
 
        }
149
 
        $this->_markForCleanup($item);
150
 
 
151
 
        /* Restore the activeUser, should be a Site Admin */
152
 
        $gallery->setActiveUser($this->_activeUserBackup);
153
 
 
154
 
        /* Delete the user */
155
 
        $ret = GalleryCoreApi::deleteEntityById($userId);
156
 
        if ($ret) {
157
 
            $this->assert(false, 'The user could not be deleted.');
158
 
            GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
159
 
            GalleryCoreApi::removeMapEntry('GalleryAccessMap', array('userOrGroupId' => $userId));
160
 
            return $this->failWithStatus($ret);
161
 
        }
162
 
        GalleryDataCache::reset();
163
 
 
164
 
        /* Verify the user is gone */
165
 
        $ret = $this->_verifyMissing($userId);
166
 
        if ($ret) {
167
 
            $this->assert(false, 'The user was not actually deleted');
168
 
        }
169
 
 
170
 
        /* Verify the item still exists and new owner is a site admin */
171
 
        list ($ret, $item) = $item->refresh();
172
 
        if ($ret) {
173
 
            if ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT) {
174
 
                $this->assert(false, 'An item was deleted that still should exist.');
175
 
            } else {
176
 
                GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
177
 
                GalleryCoreApi::removeMapEntry(
178
 
                    'GalleryAccessMap', array('userOrGroupId' => $userId));
179
 
                return $this->failWithStatus($ret);
180
 
            }
181
 
        }
182
 
        /* Verify the new owner of the item */
183
 
        list ($ret, $groupIds) = GalleryCoreApi::fetchGroupsForUser($item->getOwnerId());
184
 
        if ($ret) {
185
 
            GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
186
 
            GalleryCoreApi::removeMapEntry('GalleryAccessMap', array('userOrGroupId' => $userId));
187
 
            return $this->failWithStatus($ret);
188
 
        }
189
 
        $this->assert(isset($groupIds[$this->_siteAdminGroupId]),
190
 
                      'The new ownerId of the item is wrong.');
191
 
 
192
 
        /* Verify there are no mappings left */
193
 
        list ($ret, $groupIds) = GalleryCoreApi::fetchGroupsForUser($userId);
194
 
        if ($ret) {
195
 
            GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
196
 
            GalleryCoreApi::removeMapEntry('GalleryAccessMap', array('userOrGroupId' => $userId));
197
 
            return $this->failWithStatus($ret);
198
 
        }
199
 
        $this->assertEquals(0, count($groupIds), 'There are still some group maps.');
200
 
        /* Make sure there are no mappings left (garbage) */
201
 
        $ret = GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
202
 
        if ($ret) {
203
 
            $this->failWithStatus($ret);
204
 
        }
205
 
 
206
 
        /* Verify all permission maps for this user are gone */
207
 
        $checkItemIds = array($item->getId(), $this->_getRootId());
208
 
        list ($ret, $permissions) =
209
 
            GalleryCoreApi::fetchPermissionsForItems($checkItemIds, $userId);
210
 
        if ($ret) {
211
 
            $this->failWithStatus($ret);
212
 
        }
213
 
        $this->assertEquals(array(), $permissions,
214
 
                            'Not all permissions of the deleted user were removed');
215
 
 
216
 
        /* Make sure there are no mappings left (garbage) */
217
 
        $ret = GalleryCoreApi::removeMapEntry('GalleryAccessMap',
218
 
                                              array('userOrGroupId' => $userId));
219
 
        if ($ret) {
220
 
            $this->failWithStatus($ret);
221
 
        }
222
 
    }
223
 
 
224
 
    /* Verify we can't delete the active user */
225
 
    function testDeleteSelf() {
226
 
        global $gallery;
227
 
 
228
 
        list ($ret, $user) = $this->_createRandomUser();
229
 
        if ($ret) {
230
 
            return $this->failWithStatus($ret);
231
 
        }
232
 
        $this->_markForCleanup($user);
233
 
        $gallery->setActiveUser($user);
234
 
 
235
 
        $ret = GalleryCoreApi::deleteEntityById($user->getId());
236
 
        $this->assertEquals(ERROR_BAD_PARAMETER | GALLERY_ERROR, $ret->getErrorCode());
237
 
    }
238
 
 
239
 
     /* Verify we can't delete the anonymous user */
240
 
    function testDeleteAnonymousUser() {
241
 
        list ($ret, $anonymousUserId) =
242
 
            GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
243
 
 
244
 
        $ret = GalleryCoreApi::deleteEntityById($anonymousUserId);
245
 
        $this->assertEquals(ERROR_BAD_PARAMETER | GALLERY_ERROR, $ret->getErrorCode());
246
 
    }
247
 
 
248
 
    /**
249
 
     * Verify we can't delete the last site admin.
250
 
     */
251
 
    function testDeleteLastAdmin() {
252
 
        /* Create a new user in a new group, and make that the Site Admins group */
253
 
        list ($ret, $user) = $this->_createRandomUser();
254
 
        if ($ret) {
255
 
            return $this->failWithStatus($ret);
256
 
        }
257
 
        $this->_markForCleanup($user);
258
 
        $userId = $user->getId();
259
 
        list ($ret, $group) = $this->_createRandomGroup();
260
 
        if ($ret) {
261
 
            return $this->failWithStatus($ret);
262
 
        }
263
 
        $this->_markForCleanup($group);
264
 
        $groupId = $group->getId();
265
 
 
266
 
        $ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
267
 
        if ($ret) {
268
 
            return $this->failWithStatus($ret);
269
 
        }
270
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'id.adminGroup', $groupId);
271
 
        if ($ret) {
272
 
            return $this->failWithStatus($ret);
273
 
        }
274
 
 
275
 
        /* Attempt to delete user */
276
 
        $ret = GalleryCoreApi::deleteEntityById($userId);
277
 
        $this->assertEquals(ERROR_BAD_PARAMETER | GALLERY_ERROR, $ret->getErrorCode());
278
 
    }
279
 
 
280
 
    /**
281
 
     * Verify item owners are remapped ok if we delete a site admin.
282
 
     */
283
 
    function testDeleteAdmin() {
284
 
        /* Create 2 new users in a new group, and make that the Site Admins group */
285
 
        list ($ret, $user) = $this->_createRandomUser();
286
 
        if ($ret) {
287
 
            return $this->failWithStatus($ret);
288
 
        }
289
 
        $userId = $user->getId();
290
 
        list ($ret, $otherUser) = $this->_createRandomUser();
291
 
        if ($ret) {
292
 
            $this->_markForCleanup($user);
293
 
            return $this->failWithStatus($ret);
294
 
        }
295
 
        $this->_markForCleanup($otherUser);
296
 
        $otherUserId = $otherUser->getId();
297
 
        list ($ret, $group) = $this->_createRandomGroup();
298
 
        if ($ret) {
299
 
            $this->_markForCleanup($user);
300
 
            return $this->failWithStatus($ret);
301
 
        }
302
 
        $this->_markForCleanup($group);
303
 
        $groupId = $group->getId();
304
 
        $ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
305
 
        if ($ret) {
306
 
            $this->_markForCleanup($user);
307
 
            return $this->failWithStatus($ret);
308
 
        }
309
 
        $ret = GalleryCoreApi::addUserToGroup($otherUserId, $groupId);
310
 
        if ($ret) {
311
 
            $this->_markForCleanup($user);
312
 
            return $this->failWithStatus($ret);
313
 
        }
314
 
        $ret = GalleryCoreApi::setPluginParameter('module', 'core', 'id.adminGroup', $groupId);
315
 
        if ($ret) {
316
 
            $this->_markForCleanup($user);
317
 
            return $this->failWithStatus($ret);
318
 
        }
319
 
 
320
 
        /* Create an album and item owned by this admin */
321
 
        list ($ret, $album) = $this->_createRandomAlbum($this->_getRootId(),
322
 
                                                        array('ownerId' => $userId));
323
 
        if ($ret) {
324
 
            $this->_markForCleanup($user);
325
 
            return $this->failWithStatus($ret);
326
 
        }
327
 
        $this->_markForCleanup($album);
328
 
        list ($ret, $item) = $this->_createRandomDataItem($album->getId(), 'test/file',
329
 
                                                          array('ownerId' => $userId));
330
 
        if ($ret) {
331
 
            $this->_markForCleanup($user);
332
 
            return $this->failWithStatus($ret);
333
 
        }
334
 
 
335
 
        /* Delete this admin */
336
 
        $ret = GalleryCoreApi::deleteEntityById($userId);
337
 
        if ($ret) {
338
 
            $this->assert(false, 'The user could not be deleted.');
339
 
            return $this->failWithStatus($ret);
340
 
        }
341
 
        GalleryDataCache::reset();
342
 
        $ret = $this->_verifyMissing($userId);
343
 
        if ($ret) {
344
 
            $this->assert(false, 'The user was not actually deleted');
345
 
        }
346
 
 
347
 
        /* Verify owner was remapped to our other admin */
348
 
        list ($ret, $album) = $album->refresh();
349
 
        if ($ret) {
350
 
            return $this->failWithStatus($ret);
351
 
        }
352
 
        $this->assertEquals($otherUserId, $album->getOwnerId(), 'album owner');
353
 
        list ($ret, $item) = $item->refresh();
354
 
        if ($ret) {
355
 
            return $this->failWithStatus($ret);
356
 
        }
357
 
        $this->assertEquals($otherUserId, $item->getOwnerId(), 'item owner');
358
 
    }
359
 
 
360
 
    /* Try to delete the all users group (this will fail) */
361
 
    function testDeleteSpecialGroups() {
362
 
        foreach (array('id.allUserGroup', 'id.everybodyGroup', 'id.adminGroup') as $paramName) {
363
 
            list ($ret, $groupId) =
364
 
                GalleryCoreApi::getPluginParameter('module', 'core', $paramName);
365
 
            if ($ret) {
366
 
                return $this->failWithStatus($ret);
367
 
            }
368
 
 
369
 
            $ret = GalleryCoreApi::deleteEntityById($groupId);
370
 
            if ($ret && !($ret->getErrorCode() & ERROR_BAD_PARAMETER)) {
371
 
                return $this->failWithStatus($ret);
372
 
            }
373
 
 
374
 
            $this->assert($ret && ($ret->getErrorCode() & ERROR_BAD_PARAMETER),
375
 
                          "$paramName: expect an error");
376
 
 
377
 
            /* Make sure the entity still exists */
378
 
            list ($ret, $entity) = GalleryCoreApi::loadEntitiesById($groupId);
379
 
            if ($ret) {
380
 
                return $this->failWithStatus($ret);
381
 
            }
382
 
            $groupName = $entity->getGroupName();
383
 
            $this->assert(!empty($groupName), "$paramName: group name should not be empty");
384
 
        }
385
 
    }
386
 
 
387
 
    /**
388
 
     * Create user, modify user, delete user
389
 
     */
390
 
    function testCreateAndModifyUser() {
391
 
        global $gallery;
392
 
 
393
 
        /* Create a new user */
394
 
        list ($ret, $user) = $this->_createRandomUser();
395
 
        if ($ret) {
396
 
            return $this->failWithStatus($ret);
397
 
        }
398
 
 
399
 
        /* Lock and refresh it */
400
 
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($user->getId());
401
 
        if ($ret) {
402
 
            return $this->failWithStatus($ret);
403
 
        }
404
 
 
405
 
        list ($ret, $user) = $user->refresh();
406
 
        if ($ret) {
407
 
            return $this->failWithStatus($ret);
408
 
        }
409
 
 
410
 
        /* Modify it */
411
 
        $user->setFullName('newname-' . rand());
412
 
 
413
 
        /* Save it */
414
 
        $ret = $user->save();
415
 
        if ($ret) {
416
 
            return $this->failWithStatus($ret);
417
 
        }
418
 
 
419
 
        /* Unlock it */
420
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
421
 
        if ($ret) {
422
 
            return $this->failWithStatus($ret);
423
 
        }
424
 
 
425
 
        /* Verify it */
426
 
        $ret = $this->_verifyEntity($user);
427
 
        if ($ret) {
428
 
            return $this->failWithStatus($ret);
429
 
        }
430
 
 
431
 
        /* Delete it */
432
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
433
 
        if ($ret) {
434
 
            return $this->failWithStatus($ret);
435
 
        }
436
 
    }
437
 
 
438
 
    /**
439
 
     * Create user, fetch user names, make sure it's in there, delete user
440
 
     */
441
 
    function testFetchUserNames() {
442
 
        global $gallery;
443
 
 
444
 
        list ($ret, $user) = $this->_createRandomUser();
445
 
        if ($ret) {
446
 
            return $this->failWithStatus($ret);
447
 
        }
448
 
 
449
 
        GalleryDataCache::reset();
450
 
 
451
 
        list ($ret, $names) = GalleryCoreApi::fetchUserNames();
452
 
        if ($ret) {
453
 
            return $this->failWithStatus($ret);
454
 
        }
455
 
 
456
 
        $this->assert(in_array($user->getUserName(), $names));
457
 
 
458
 
        /* Delete it */
459
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
460
 
        if ($ret) {
461
 
            return $this->failWithStatus($ret);
462
 
        }
463
 
    }
464
 
 
465
 
    /**
466
 
      * Create user1, add some items, some with delete permission,
467
 
      * create another user2, add an item of user2 in user1's item tree
468
 
      * delete user1, check if the right items were deleted, clean up
469
 
      */
470
 
     function testDeleteUserItems() {
471
 
        global $gallery;
472
 
 
473
 
        /*
474
 
         * An array for all items that should be gone and an
475
 
         * array for all items that should not be gone after the test
476
 
         */
477
 
        $deletedItems = array();
478
 
        $remainingItems = array();
479
 
        /*
480
 
         * These items get delete permission.
481
 
         * Delete permission is assigned AFTER creating all items, just before
482
 
         * user1 is deleted.
483
 
         */
484
 
        $deletePermissionItems = array();
485
 
 
486
 
        /* Create test user 1, the owner of most items in the new item tree. */
487
 
        list ($ret, $user1) = $this->_createRandomUser();
488
 
        if ($ret) {
489
 
            return $this->failWithStatus($ret);
490
 
        }
491
 
        $this->_markForCleanup($user1);
492
 
        /* Create test user 2, the owner of a few subitems in the new item tree */
493
 
        list ($ret, $user2) = $this->_createRandomUser();
494
 
        if ($ret) {
495
 
            return $this->failWithStatus($ret);
496
 
        }
497
 
        $this->_markForCleanup($user2);
498
 
 
499
 
        /* Add a test container album. All test items/albums will be in this container. */
500
 
        list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
501
 
        if ($ret) {
502
 
            return $this->failWithStatus($ret);
503
 
        }
504
 
 
505
 
        $this->_markForCleanup($this->_album);
506
 
        $remainingItems[] = $this->_album;
507
 
 
508
 
        /* Give User1 enough permission such that he can add items. */
509
 
        $ret = GalleryCoreApi::addUserPermission(
510
 
            $this->_album->getId(), $user1->getId(),
511
 
            array('core.addDataItem','core.addAlbumItem'), false);
512
 
        if ($ret) {
513
 
            return $this->failWithStatus($ret);
514
 
        }
515
 
        /* Change the active user to our test user1. */
516
 
        $this->_activeUserBackup = $gallery->getActiveUser();
517
 
        $gallery->setActiveUser($user1);
518
 
 
519
 
        /* Add a test album with user1 */
520
 
        list ($ret, $albumWithDeletePermission) = $this->_createRandomAlbum($this->_album->getId());
521
 
        if ($ret) {
522
 
            return $this->failWithStatus($ret);
523
 
        }
524
 
        $remainingItems[] = $albumWithDeletePermission;
525
 
        $deletePermissionItems[] = $albumWithDeletePermission;
526
 
 
527
 
        /*
528
 
         * Add a test subalbum with user1 as the owner and delete permissions (assigned later)
529
 
         * subAlbumWithDeletePermission                       | with delete perm. -> deleted
530
 
         *      |- subSubItemWithDeletePermission             | with delete perm. -> deleted
531
 
         *      |- subSubAlbumWithDeletePermission            | with delete perm. -> deleted
532
 
         *              |- subSubSubItemWithDeletePermission  | with delete perm. -> deleted
533
 
         * subItemWithDeletePermission                        | with delete perm. -> deleted
534
 
         */
535
 
        list ($ret, $subAlbumWithDeletePermission) =
536
 
            $this->_createRandomAlbum($albumWithDeletePermission->getId());
537
 
        if ($ret) {
538
 
            return $this->failWithStatus($ret);
539
 
        }
540
 
        $deletedItems[] = $subAlbumWithDeletePermission;
541
 
        $deletePermissionItems[] = $subAlbumWithDeletePermission;
542
 
        /* Add item to album and subalbum */
543
 
        list ($ret, $subItemWithDeletePermission) =
544
 
            $this->_createRandomDataItem($albumWithDeletePermission->getId());
545
 
        if ($ret) {
546
 
            return $this->failWithStatus($ret);
547
 
        }
548
 
        $deletedItems[] = $subItemWithDeletePermission;
549
 
        $deletePermissionItems[] = $subItemWithDeletePermission;
550
 
        list ($ret, $subSubItemWithDeletePermission) =
551
 
            $this->_createRandomDataItem($subAlbumWithDeletePermission->getId());
552
 
        if ($ret) {
553
 
            return $this->failWithStatus($ret);
554
 
        }
555
 
        $deletedItems[] = $subSubItemWithDeletePermission;
556
 
        $deletePermissionItems[] = $subSubItemWithDeletePermission;
557
 
        list ($ret, $subSubAlbumWithDeletePermission) =
558
 
            $this->_createRandomAlbum($subAlbumWithDeletePermission->getId());
559
 
        if ($ret) {
560
 
            return $this->failWithStatus($ret);
561
 
        }
562
 
        $deletedItems[] = $subSubAlbumWithDeletePermission;
563
 
        $deletePermissionItems[] = $subSubAlbumWithDeletePermission;
564
 
        list ($ret, $subSubSubItemWithDeletePermission) =
565
 
            $this->_createRandomDataItem($subSubAlbumWithDeletePermission->getId());
566
 
        if ($ret) {
567
 
            return $this->failWithStatus($ret);
568
 
        }
569
 
        $deletedItems[] = $subSubSubItemWithDeletePermission;
570
 
        $deletePermissionItems[] = $subSubSubItemWithDeletePermission;
571
 
 
572
 
 
573
 
        /*
574
 
         * Now add another subalbum tree, this time with some items without delete permission
575
 
         * subAlbumWithDeletePermission2                        | with delete permission
576
 
         *    |- subSubAlbumWithoutDeletePermission2            | no delete permission
577
 
         *              |- subSubSubItemWithDeletePermission2   | with delete perm -> deleted
578
 
         *         |- subSubAlbumWithDeletePermission2          | with delete permission
579
 
         *              |- subSubSubItemWithoutDeletePermission2| no delete permission
580
 
         */
581
 
        list ($ret, $subAlbumWithDeletePermission2) =
582
 
            $this->_createRandomAlbum($albumWithDeletePermission->getId());
583
 
        if ($ret) {
584
 
            return $this->failWithStatus($ret);
585
 
        }
586
 
 
587
 
        $remainingItems[] = $subAlbumWithDeletePermission2;
588
 
        $deletePermissionItems[] = $subAlbumWithDeletePermission2;
589
 
        /* A subSub album without delete permission */
590
 
        list ($ret, $subSubAlbumWithoutDeletePermission2) =
591
 
            $this->_createRandomAlbum($subAlbumWithDeletePermission2->getId());
592
 
        if ($ret) {
593
 
            return $this->failWithStatus($ret);
594
 
        }
595
 
        $remainingItems[] = $subSubAlbumWithoutDeletePermission2;
596
 
 
597
 
        /* A subSub album with delete permission */
598
 
        list ($ret, $subSubAlbumWithDeletePermission2) =
599
 
            $this->_createRandomAlbum($subAlbumWithDeletePermission2->getId());
600
 
        if ($ret) {
601
 
            return $this->failWithStatus($ret);
602
 
        }
603
 
        $remainingItems[] = $subSubAlbumWithDeletePermission2;
604
 
        $deletePermissionItems[] = $subSubAlbumWithDeletePermission2;
605
 
        /*
606
 
         * A subSubSubItem with delete permission
607
 
         * (but is in a subSubalbum without delete permission)
608
 
         */
609
 
        list ($ret, $subSubSubItemWithDeletePermission2) =
610
 
            $this->_createRandomDataItem($subSubAlbumWithoutDeletePermission2->getId());
611
 
        if ($ret) {
612
 
            return $this->failWithStatus($ret);
613
 
        }
614
 
        $deletedItems[] = $subSubSubItemWithDeletePermission2;
615
 
        $deletePermissionItems[] = $subSubSubItemWithDeletePermission2;
616
 
        /* A subSubItem without delete permission */
617
 
        list ($ret, $subSubSubItemWithoutDeletePermission2) =
618
 
            $this->_createRandomDataItem($subSubAlbumWithDeletePermission2->getId());
619
 
        if ($ret) {
620
 
            return $this->failWithStatus($ret);
621
 
        }
622
 
        $remainingItems[] = $subSubSubItemWithoutDeletePermission2;
623
 
 
624
 
        /*
625
 
         * Add another subalbum tree, with some items of another user (user2) in it
626
 
         * Also check for a new thumbnail here
627
 
         * subAlbumWithDeletePermission3                         | with delete permission
628
 
         *      |- subSubItemWithDeletePermission                | with delete perm.-> deleted
629
 
         *      |- subSubItemWithDeletePermissionDifferentOwner3 | with delete permission
630
 
         */
631
 
        list ($ret, $subAlbumWithDeletePermission3) =
632
 
            $this->_createRandomAlbum($albumWithDeletePermission->getId());
633
 
        if ($ret) {
634
 
            return $this->failWithStatus($ret);
635
 
        }
636
 
        $remainingItems[] = $subAlbumWithDeletePermission3;
637
 
        $deletePermissionItems[] = $subAlbumWithDeletePermission3;
638
 
        list ($ret, $subSubItemWithDeletePermission3) =
639
 
            $this->_createRandomDataItem($subAlbumWithDeletePermission3->getId());
640
 
        if ($ret) {
641
 
            return $this->failWithStatus($ret);
642
 
        }
643
 
        $deletedItems[] = $subSubItemWithDeletePermission3;
644
 
        $deletePermissionItems[] = $subSubItemWithDeletePermission3;
645
 
 
646
 
        /* Now change the activeUser to the other user (from user1 to user2) */
647
 
        $gallery->setActiveUser($user2);
648
 
 
649
 
        /* Add an item with user2 in user1's item tree */
650
 
        list ($ret, $subSubItemWithDeletePermissionDifferentOwner3) =
651
 
            $this->_createRandomDataItem($subAlbumWithDeletePermission3->getId());
652
 
        if ($ret) {
653
 
            return $this->failWithStatus($ret);
654
 
        }
655
 
        $remainingItems[] = $subSubItemWithDeletePermissionDifferentOwner3;
656
 
        $deletePermissionItems[] = $subSubItemWithDeletePermissionDifferentOwner3;
657
 
 
658
 
        /* Restore the activeUser, should be a Site Admin(the user who runs the test) */
659
 
        $gallery->setActiveUser($this->_activeUserBackup);
660
 
 
661
 
        /* Add delete perm. to all items/albums that should have delete permission for user1 */
662
 
        foreach ($deletePermissionItems as $item) {
663
 
            $ret = GalleryCoreApi::addUserPermission(
664
 
                $item->getId(), $user1->getId(), array('core.delete'), false);
665
 
            if ($ret) {
666
 
                return $this->failWithStatus($ret);
667
 
            }
668
 
        }
669
 
 
670
 
        /*
671
 
         * Verify the owner id of the items, all test items/albums but the single subitem of user2
672
 
         * should be owned by user1.
673
 
         */
674
 
        $allItems = $deletedItems;
675
 
        array_splice($allItems, count($allItems), 0, $remainingItems);
676
 
        foreach ($allItems as $item) {
677
 
            if ($item->getId() != $this->_album->getId() &&
678
 
                    $item->getId() != $subSubItemWithDeletePermissionDifferentOwner3->getId()) {
679
 
                $this->assert($item->getOwnerId() == $user1->getId(), 'owner Id is wrong');
680
 
            }
681
 
            /* Verify the site admin has the needed permissions */
682
 
            $ret = GalleryCoreApi::assertHasItemPermission($item->getId(), 'core.viewResizes');
683
 
            if ($ret) {
684
 
                return $this->failWithStatus($ret);
685
 
            }
686
 
        }
687
 
 
688
 
        /* Execute the "delete user1" call */
689
 
        $ret = GalleryCoreApi::deleteUserItems($user1->getId());
690
 
        if ($ret) {
691
 
            return $this->failWithStatus($ret);
692
 
        }
693
 
        GalleryDataCache::reset();
694
 
 
695
 
        /* Verify the correct items were deleted */
696
 
        foreach ($deletedItems as $item) {
697
 
            $ret = $this->_verifyMissing($item->getId());
698
 
            if ($ret) {
699
 
                return $this->failWithStatus($ret);
700
 
            }
701
 
        }
702
 
 
703
 
        /*
704
 
         * Verify the correct items still exist. (sub)albums with delete permission for user1,
705
 
         * that are not empty, i.e. that contain items without delete permission for user1 or
706
 
         * that have another owner, should still exist after a "delete user1" call.
707
 
         */
708
 
        foreach ($remainingItems as $item) {
709
 
            $id = $item->getId();
710
 
            list ($ret, $item) = $item->refresh();
711
 
            if ($ret) {
712
 
                if ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT) {
713
 
                    $this->assert(false, "Wrongly deleted item ($id)");
714
 
                }
715
 
                return $this->failWithStatus($ret);
716
 
            }
717
 
            /* Verify the owner is still user1 */
718
 
            if ($item->getId() != $this->_album->getId()
719
 
                    && $item->getId() != $subSubItemWithDeletePermissionDifferentOwner3->getId()) {
720
 
                $this->assert($item->getOwnerId() == $user1->getId(),
721
 
                              'The ownerId for an item was changed');
722
 
            }
723
 
        }
724
 
    }
725
 
 
726
 
    /**
727
 
     * Count users, Create user, count users again, delete user
728
 
     */
729
 
    function testFetchUserCount() {
730
 
        global $gallery;
731
 
 
732
 
        list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount();
733
 
        if ($ret) {
734
 
            return $this->failWithStatus($ret);
735
 
        }
736
 
 
737
 
        list ($ret, $user) = $this->_createRandomUser();
738
 
        if ($ret) {
739
 
            return $this->failWithStatus($ret);
740
 
        }
741
 
 
742
 
        GalleryDataCache::reset();
743
 
 
744
 
        list ($ret, $countAfter) = GalleryCoreApi::fetchUserCount();
745
 
        if ($ret) {
746
 
            return $this->failWithStatus($ret);
747
 
        }
748
 
 
749
 
        $this->assertEquals($countBefore+1, $countAfter);
750
 
 
751
 
        /* Delete it */
752
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
753
 
        if ($ret) {
754
 
            return $this->failWithStatus($ret);
755
 
        }
756
 
    }
757
 
 
758
 
    /**
759
 
     * Verify that we can fetch all users with a given substring in their username
760
 
     */
761
 
    function testFetchUserCountBySubstring() {
762
 
        global $gallery;
763
 
 
764
 
        $unique = rand(0, 65535);
765
 
        list ($ret, $count) = GalleryCoreApi::fetchUserCount($unique, null);
766
 
        if ($ret) {
767
 
            return $this->failWithStatus($ret);
768
 
        }
769
 
 
770
 
        /* Nobody matches our unique tag */
771
 
        $this->assertEquals(0, $count);
772
 
 
773
 
        list ($ret, $user) = $this->_createRandomUser($unique);
774
 
        if ($ret) {
775
 
            return $this->failWithStatus($ret);
776
 
        }
777
 
 
778
 
        GalleryDataCache::reset();
779
 
 
780
 
        list ($ret, $count) = GalleryCoreApi::fetchUserCount($unique, null);
781
 
        if ($ret) {
782
 
            return $this->failWithStatus($ret);
783
 
        }
784
 
 
785
 
        /* We now have one user that matches the description */
786
 
        $this->assertEquals(1, $count);
787
 
 
788
 
        /* Clean up */
789
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
790
 
        if ($ret) {
791
 
            return $this->failWithStatus($ret);
792
 
        }
793
 
    }
794
 
 
795
 
    /*
796
 
     * Verify that we can fetch a count of the users from any particular group
797
 
     */
798
 
    function testFetchUserCountByGroupId() {
799
 
        global $gallery;
800
 
 
801
 
        /* Create a user */
802
 
        list ($ret, $user) = $this->_createRandomUser();
803
 
        if ($ret) {
804
 
            return $this->failWithStatus($ret);
805
 
        }
806
 
 
807
 
        /* Create a group */
808
 
        list ($ret, $group) = $this->_createRandomGroup();
809
 
        if ($ret) {
810
 
            return $this->failWithStatus($ret);
811
 
        }
812
 
 
813
 
        /* fetchUserCount should return 0 */
814
 
        list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount(null, $group->getId());
815
 
        if ($ret) {
816
 
            return $this->failWithStatus($ret);
817
 
        }
818
 
        $this->assertEquals(0, $countBefore);
819
 
 
820
 
        /* Put the user into the group */
821
 
        $ret = GalleryCoreApi::addUserToGroup($user->getId(), $group->getId());
822
 
        if ($ret) {
823
 
            return $this->failWithStatus($ret);
824
 
        }
825
 
 
826
 
        /* fetchUserCount should return 1 */
827
 
        list ($ret, $countAfter) = GalleryCoreApi::fetchUserCount(null, $group->getId());
828
 
        if ($ret) {
829
 
            return $this->failWithStatus($ret);
830
 
        }
831
 
        $this->assertEquals(1, $countAfter);
832
 
 
833
 
        /* Delete user */
834
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
835
 
        if ($ret) {
836
 
            return $this->failWithStatus($ret);
837
 
        }
838
 
 
839
 
        /* Delete group */
840
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
841
 
        if ($ret) {
842
 
            return $this->failWithStatus($ret);
843
 
        }
844
 
    }
845
 
 
846
 
    /**
847
 
     * Verify that we can fetch all users from a group with a given substring in their username
848
 
     */
849
 
    function testFetchUserCountBySubstringAndGroupId() {
850
 
        global $gallery;
851
 
 
852
 
        $strSearch = 'rand';
853
 
        $uniqueFirst = rand(0, 65535);
854
 
        $uniqueSecond = rand(0, 65535);
855
 
 
856
 
        /* Create a group */
857
 
        list ($ret, $group) = $this->_createRandomGroup();
858
 
        if ($ret) {
859
 
            return $this->failWithStatus($ret);
860
 
        }
861
 
 
862
 
        /* fetchUserCount should return 0 */
863
 
        list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount($strSearch, $group->getId());
864
 
        if ($ret) {
865
 
            return $this->failWithStatus($ret);
866
 
        }
867
 
        $this->assertEquals(0, $countBefore);
868
 
 
869
 
        /* Create the first user */
870
 
        list ($ret, $userFirst) = $this->_createRandomUser($strSearch . $uniqueFirst);
871
 
        if ($ret) {
872
 
            return $this->failWithStatus($ret);
873
 
        }
874
 
 
875
 
        /* Create the second user */
876
 
        list ($ret, $userSecond) = $this->_createRandomUser($strSearch . $uniqueSecond);
877
 
        if ($ret) {
878
 
            return $this->failWithStatus($ret);
879
 
        }
880
 
 
881
 
        /* Put the first user into the group */
882
 
        $ret = GalleryCoreApi::addUserToGroup($userFirst->getId(), $group->getId());
883
 
        if ($ret) {
884
 
            return $this->failWithStatus($ret);
885
 
        }
886
 
 
887
 
        /* Put the second user into the group */
888
 
        $ret = GalleryCoreApi::addUserToGroup($userSecond->getId(), $group->getId());
889
 
        if ($ret) {
890
 
            return $this->failWithStatus($ret);
891
 
        }
892
 
 
893
 
        /* fetchUserCount should return 1 */
894
 
        list ($ret, $countAfter) = GalleryCoreApi::fetchUserCount($strSearch . $uniqueFirst,
895
 
                                                                  $group->getId());
896
 
        if ($ret) {
897
 
            return $this->failWithStatus($ret);
898
 
        }
899
 
        $this->assertEquals(1, $countAfter);
900
 
 
901
 
        /* Delete first user */
902
 
        $ret = $this->_deleteAndVerifyEntity($userFirst->getId());
903
 
        if ($ret) {
904
 
            return $this->failWithStatus($ret);
905
 
        }
906
 
 
907
 
        /* Delete second user */
908
 
        $ret = $this->_deleteAndVerifyEntity($userSecond->getId());
909
 
        if ($ret) {
910
 
            return $this->failWithStatus($ret);
911
 
        }
912
 
 
913
 
        /* Delete group */
914
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
915
 
        if ($ret) {
916
 
            return $this->failWithStatus($ret);
917
 
        }
918
 
    }
919
 
 
920
 
    /**
921
 
     * Verify that we can fetch all users from a group with a given substring in their username
922
 
     */
923
 
    function testFetchUsersFromGroupBySubstringAndGroupId() {
924
 
        global $gallery;
925
 
 
926
 
        $strSearch = 'rand';
927
 
        $uniqueFirst = rand(0, 65535);
928
 
        $uniqueSecond = rand(0, 65535);
929
 
 
930
 
        /* Create a group */
931
 
        list ($ret, $group) = $this->_createRandomGroup();
932
 
        if ($ret) {
933
 
            return $this->failWithStatus($ret);
934
 
        }
935
 
 
936
 
        /* fetchUserCount should return 0 */
937
 
        list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount($strSearch, $group->getId());
938
 
        if ($ret) {
939
 
            return $this->failWithStatus($ret);
940
 
        }
941
 
        $this->assertEquals(0, $countBefore);
942
 
 
943
 
        /* Create the first user */
944
 
        list ($ret, $userFirst) = $this->_createRandomUser($strSearch . $uniqueFirst);
945
 
        if ($ret) {
946
 
            return $this->failWithStatus($ret);
947
 
        }
948
 
 
949
 
        /* Create the second user */
950
 
        list ($ret, $userSecond) = $this->_createRandomUser($strSearch . $uniqueSecond);
951
 
        if ($ret) {
952
 
            return $this->failWithStatus($ret);
953
 
        }
954
 
 
955
 
        /* Put the first user into the group */
956
 
        $ret = GalleryCoreApi::addUserToGroup($userFirst->getId(), $group->getId());
957
 
        if ($ret) {
958
 
            return $this->failWithStatus($ret);
959
 
        }
960
 
 
961
 
        /* Put the second user into the group */
962
 
        $ret = GalleryCoreApi::addUserToGroup($userSecond->getId(), $group->getId());
963
 
        if ($ret) {
964
 
            return $this->failWithStatus($ret);
965
 
        }
966
 
 
967
 
        /* fetchUsersForGroup should return one user */
968
 
        list ($ret, $users) = GalleryCoreApi::fetchUsersForGroup(
969
 
                                        $group->getId(),
970
 
                                        null,
971
 
                                        null,
972
 
                                        $strSearch . $uniqueFirst
973
 
                              );
974
 
        if ($ret) {
975
 
            return $this->failWithStatus($ret);
976
 
        }
977
 
        $this->assertEquals(1, count($users));
978
 
 
979
 
        /* Delete first user */
980
 
        $ret = $this->_deleteAndVerifyEntity($userFirst->getId());
981
 
        if ($ret) {
982
 
            return $this->failWithStatus($ret);
983
 
        }
984
 
 
985
 
        /* Delete second user */
986
 
        $ret = $this->_deleteAndVerifyEntity($userSecond->getId());
987
 
        if ($ret) {
988
 
            return $this->failWithStatus($ret);
989
 
        }
990
 
 
991
 
        /* Delete group */
992
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
993
 
        if ($ret) {
994
 
            return $this->failWithStatus($ret);
995
 
        }
996
 
    }
997
 
 
998
 
    /**
999
 
     * Create user, fetch it by name, delete user
1000
 
     */
1001
 
    function testFetchUserByName() {
1002
 
        global $gallery;
1003
 
 
1004
 
        list ($ret, $user) = $this->_createRandomUser();
1005
 
        if ($ret) {
1006
 
            return $this->failWithStatus($ret);
1007
 
        }
1008
 
 
1009
 
        GalleryDataCache::reset();
1010
 
 
1011
 
        list ($ret, $newUser) = GalleryCoreApi::fetchUserByUserName($user->getUserName());
1012
 
        if ($ret) {
1013
 
            return $this->failWithStatus($ret);
1014
 
        }
1015
 
 
1016
 
        $this->assertEquals($user, $newUser);
1017
 
 
1018
 
        /* Delete it */
1019
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
1020
 
        if ($ret) {
1021
 
            return $this->failWithStatus($ret);
1022
 
        }
1023
 
    }
1024
 
 
1025
 
    /**
1026
 
     * Create group, delete group
1027
 
     */
1028
 
    function testCreateGroup() {
1029
 
        global $gallery;
1030
 
 
1031
 
        list ($ret, $group) = $this->_createRandomGroup();
1032
 
        if ($ret) {
1033
 
            return $this->failWithStatus($ret);
1034
 
        }
1035
 
 
1036
 
        GalleryDataCache::reset();
1037
 
 
1038
 
        /* Verify it */
1039
 
        $ret = $this->_verifyEntity($group);
1040
 
        if ($ret) {
1041
 
            return $this->failWithStatus($ret);
1042
 
        }
1043
 
 
1044
 
        /* Delete it */
1045
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1046
 
        if ($ret) {
1047
 
            return $this->failWithStatus($ret);
1048
 
        }
1049
 
    }
1050
 
 
1051
 
    /**
1052
 
     * Create duplicate group
1053
 
     */
1054
 
    function testCreateDuplicateGroup() {
1055
 
        global $gallery;
1056
 
 
1057
 
        $groupTag = rand();
1058
 
 
1059
 
        list ($ret, $group) = $this->_createRandomGroup($groupTag);
1060
 
        if ($ret) {
1061
 
            return $this->failWithStatus($ret);
1062
 
        }
1063
 
 
1064
 
        GalleryDataCache::reset();
1065
 
 
1066
 
        /* Verify it */
1067
 
        $ret = $this->_verifyEntity($group);
1068
 
        if ($ret) {
1069
 
            return $this->failWithStatus($ret);
1070
 
        }
1071
 
 
1072
 
        GalleryDataCache::reset();
1073
 
 
1074
 
        /* Create it again */
1075
 
        list ($ret, $duplicateGroup) = $this->_createRandomGroup($groupTag);
1076
 
        $this->assert($ret->getErrorCode() & ERROR_COLLISION, 'There should have been a collision');
1077
 
        $this->assert(!isset($duplicateGroup));
1078
 
 
1079
 
        /* Delete it */
1080
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1081
 
        if ($ret) {
1082
 
            return $this->failWithStatus($ret);
1083
 
        }
1084
 
    }
1085
 
 
1086
 
    /**
1087
 
     * Create duplicate user
1088
 
     */
1089
 
    function testCreateDuplicateUser() {
1090
 
        global $gallery;
1091
 
 
1092
 
        $userTag = rand();
1093
 
 
1094
 
        list ($ret, $user) = $this->_createRandomUser($userTag);
1095
 
        if ($ret) {
1096
 
            return $this->failWithStatus($ret);
1097
 
        }
1098
 
 
1099
 
        GalleryDataCache::reset();
1100
 
 
1101
 
        /* Verify it */
1102
 
        $ret = $this->_verifyEntity($user);
1103
 
        if ($ret) {
1104
 
            return $this->failWithStatus($ret);
1105
 
        }
1106
 
 
1107
 
        GalleryDataCache::reset();
1108
 
 
1109
 
        /* Create it again */
1110
 
        list ($ret, $duplicateUser) = $this->_createRandomUser($userTag);
1111
 
        $this->assert($ret->getErrorCode() & ERROR_COLLISION, 'There should have been a collision');
1112
 
        $this->assert(!isset($duplicateUser));
1113
 
 
1114
 
        /* Delete it */
1115
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
1116
 
        if ($ret) {
1117
 
            return $this->failWithStatus($ret);
1118
 
        }
1119
 
    }
1120
 
 
1121
 
    /**
1122
 
     * Create group, modify group, delete group
1123
 
     */
1124
 
    function testCreateAndModifyGroup() {
1125
 
        global $gallery;
1126
 
 
1127
 
        /* Create a new group */
1128
 
        list ($ret, $group) = $this->_createRandomGroup();
1129
 
        if ($ret) {
1130
 
            return $this->failWithStatus($ret);
1131
 
        }
1132
 
 
1133
 
        /* Lock and refresh it */
1134
 
        list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($group->getId());
1135
 
        if ($ret) {
1136
 
            return $this->failWithStatus($ret);
1137
 
        }
1138
 
 
1139
 
        list ($ret, $group) = $group->refresh();
1140
 
        if ($ret) {
1141
 
            return $this->failWithStatus($ret);
1142
 
        }
1143
 
 
1144
 
        /* Modify it */
1145
 
        $group->setGroupName('newname-' . rand());
1146
 
 
1147
 
        /* Save it */
1148
 
        $ret = $group->save();
1149
 
        if ($ret) {
1150
 
            return $this->failWithStatus($ret);
1151
 
        }
1152
 
 
1153
 
        /* Unlock it */
1154
 
        $ret = GalleryCoreApi::releaseLocks($lockId);
1155
 
        if ($ret) {
1156
 
            return $this->failWithStatus($ret);
1157
 
        }
1158
 
 
1159
 
        /* Verify it */
1160
 
        $ret = $this->_verifyEntity($group);
1161
 
        if ($ret) {
1162
 
            return $this->failWithStatus($ret);
1163
 
        }
1164
 
 
1165
 
        /* Delete it */
1166
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1167
 
        if ($ret) {
1168
 
            return $this->failWithStatus($ret);
1169
 
        }
1170
 
    }
1171
 
 
1172
 
    /**
1173
 
     * Create user, create group, add user to group, remove user from group,
1174
 
     * delete user, delete group
1175
 
     */
1176
 
    function testAddAndRemoveUserFromGroup() {
1177
 
        global $gallery;
1178
 
 
1179
 
        /* Create a user */
1180
 
        list ($ret, $user) = $this->_createRandomUser();
1181
 
        if ($ret) {
1182
 
            return $this->failWithStatus($ret);
1183
 
        }
1184
 
 
1185
 
        /* Create a group */
1186
 
        list ($ret, $group) = $this->_createRandomGroup();
1187
 
        if ($ret) {
1188
 
            return $this->failWithStatus($ret);
1189
 
        }
1190
 
 
1191
 
        /* Put the user into the group */
1192
 
        $ret = GalleryCoreApi::addUserToGroup($user->getId(), $group->getId());
1193
 
        if ($ret) {
1194
 
            return $this->failWithStatus($ret);
1195
 
        }
1196
 
 
1197
 
        /* Verify */
1198
 
        list ($ret, $inGroup) = GalleryCoreApi::isUserInGroup($user->getId(), $group->getId());
1199
 
        if ($ret) {
1200
 
            return $this->failWithStatus($ret);
1201
 
        }
1202
 
        $this->assert($inGroup);
1203
 
 
1204
 
        /* Remove user from group */
1205
 
        $ret = GalleryCoreApi::removeUserFromGroup($user->getId(), $group->getId());
1206
 
        if ($ret) {
1207
 
            return $this->failWithStatus($ret);
1208
 
        }
1209
 
 
1210
 
        /* Verify */
1211
 
        list ($ret, $inGroup) = GalleryCoreApi::isUserInGroup($user->getId(), $group->getId());
1212
 
        if ($ret) {
1213
 
            return $this->failWithStatus($ret);
1214
 
        }
1215
 
        $this->assert(!$inGroup);
1216
 
 
1217
 
        /* Delete user */
1218
 
        $ret = $this->_deleteAndVerifyEntity($user->getId());
1219
 
        if ($ret) {
1220
 
            return $this->failWithStatus($ret);
1221
 
        }
1222
 
 
1223
 
        /* Delete group */
1224
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1225
 
        if ($ret) {
1226
 
            return $this->failWithStatus($ret);
1227
 
        }
1228
 
    }
1229
 
 
1230
 
    /**
1231
 
     * Create group, fetch group names, make sure it's in there, delete group
1232
 
     */
1233
 
    function testFetchGroupNames() {
1234
 
        global $gallery;
1235
 
 
1236
 
        list ($ret, $group) = $this->_createRandomGroup();
1237
 
        if ($ret) {
1238
 
            return $this->failWithStatus($ret);
1239
 
        }
1240
 
 
1241
 
        GalleryDataCache::reset();
1242
 
 
1243
 
        list ($ret, $names) = GalleryCoreApi::fetchGroupNames();
1244
 
        if ($ret) {
1245
 
            return $this->failWithStatus($ret);
1246
 
        }
1247
 
 
1248
 
        $this->assert(in_array($group->getGroupName(), $names));
1249
 
 
1250
 
        /* Delete it */
1251
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1252
 
        if ($ret) {
1253
 
            return $this->failWithStatus($ret);
1254
 
        }
1255
 
    }
1256
 
 
1257
 
    /**
1258
 
     * Count groups, Create group, count groups again, delete group
1259
 
     */
1260
 
    function testFetchGroupCount() {
1261
 
        global $gallery;
1262
 
 
1263
 
        list ($ret, $countBefore) = GalleryCoreApi::fetchGroupCount();
1264
 
        if ($ret) {
1265
 
            return $this->failWithStatus($ret);
1266
 
        }
1267
 
 
1268
 
        list ($ret, $group) = $this->_createRandomGroup();
1269
 
        if ($ret) {
1270
 
            return $this->failWithStatus($ret);
1271
 
        }
1272
 
 
1273
 
        GalleryDataCache::reset();
1274
 
 
1275
 
        list ($ret, $countAfter) = GalleryCoreApi::fetchGroupCount();
1276
 
        if ($ret) {
1277
 
            return $this->failWithStatus($ret);
1278
 
        }
1279
 
 
1280
 
        $this->assertEquals($countBefore+1, $countAfter);
1281
 
 
1282
 
        /* Delete it */
1283
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1284
 
        if ($ret) {
1285
 
            return $this->failWithStatus($ret);
1286
 
        }
1287
 
    }
1288
 
 
1289
 
    /**
1290
 
     * Create group, fetch it by name, delete group
1291
 
     */
1292
 
    function testFetchGroupByName() {
1293
 
        global $gallery;
1294
 
 
1295
 
        list ($ret, $group) = $this->_createRandomGroup();
1296
 
        if ($ret) {
1297
 
            return $this->failWithStatus($ret);
1298
 
        }
1299
 
 
1300
 
        GalleryDataCache::reset();
1301
 
 
1302
 
        list ($ret, $newGroup) = GalleryCoreApi::fetchGroupByGroupName($group->getGroupName());
1303
 
        if ($ret) {
1304
 
            return $this->failWithStatus($ret);
1305
 
        }
1306
 
 
1307
 
        $this->assertEquals($group, $newGroup);
1308
 
 
1309
 
        /* Delete it */
1310
 
        $ret = $this->_deleteAndVerifyEntity($group->getId());
1311
 
        if ($ret) {
1312
 
            return $this->failWithStatus($ret);
1313
 
        }
1314
 
    }
1315
 
 
1316
 
    function testAssertSiteAdministrator() {
1317
 
        $ret = GalleryCoreApi::assertUserIsSiteAdministrator();
1318
 
        if ($ret) {
1319
 
            return $this->failWithStatus($ret);
1320
 
        }
1321
 
    }
1322
 
 
1323
 
    function testAssertHasItemPermission() {
1324
 
        global $gallery;
1325
 
 
1326
 
        $ret = GalleryCoreApi::assertHasItemPermission($this->_getRootId(), 'core.view');
1327
 
        if ($ret) {
1328
 
            return $this->failWithStatus($ret);
1329
 
        }
1330
 
    }
1331
 
 
1332
 
    function testHasItemPermission() {
1333
 
        global $gallery;
1334
 
 
1335
 
        GalleryDataCache::clearPermissionCache();
1336
 
        list ($ret, $hasPermission) =
1337
 
            GalleryCoreApi::hasItemPermission($this->_getRootId(), 'core.view');
1338
 
        if ($ret) {
1339
 
            return $this->failWithStatus($ret);
1340
 
        }
1341
 
        $this->assert($hasPermission, 'activeUser should have core.view on root');
1342
 
 
1343
 
        $userId = $gallery->getActiveUserId();
1344
 
        /* Get id of anonymous id */
1345
 
        list ($ret, $anonymousUserId) =
1346
 
            GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
1347
 
        if ($ret) {
1348
 
            return $this->failWithStatus($ret);
1349
 
        }
1350
 
 
1351
 
        /* Add an item such that we can set permissions to check hasItemPermission */
1352
 
        list ($ret, $item) = $this->_createRandomDataItem($this->_getRootId());
1353
 
        if ($ret) {
1354
 
            return $this->failWithStatus($ret);
1355
 
        }
1356
 
        $this->_markForCleanup($item);
1357
 
 
1358
 
        /* Reset permissions */
1359
 
        $ret = GalleryCoreApi::removeItemPermissions($item->getId());
1360
 
        if ($ret) {
1361
 
            return $this->failWithStatus($ret);
1362
 
        }
1363
 
 
1364
 
        /* Add a permission to the active user */
1365
 
        $ret = GalleryCoreApi::addUserPermission($item->getId(), $userId, 'core.view', false);
1366
 
        if ($ret) {
1367
 
            return $this->failWithStatus($ret);
1368
 
        }
1369
 
 
1370
 
        /* Now test activeUser with and without userId param */
1371
 
        list ($ret, $hasPermission) =
1372
 
            GalleryCoreApi::hasItemPermission($item->getId(), 'core.view');
1373
 
        if ($ret) {
1374
 
            return $this->failWithStatus($ret);
1375
 
        }
1376
 
        $this->assert($hasPermission, 'default user should have core.view on item');
1377
 
 
1378
 
        list ($ret, $hasPermission) =
1379
 
            GalleryCoreApi::hasItemPermission($item->getId(), 'core.view', $userId);
1380
 
        if ($ret) {
1381
 
            return $this->failWithStatus($ret);
1382
 
        }
1383
 
        $this->assert($hasPermission, 'userId should have core.view on item');
1384
 
 
1385
 
        list ($ret, $hasPermission) =
1386
 
            GalleryCoreApi::hasItemPermission($item->getId(), 'core.view', $anonymousUserId);
1387
 
        if ($ret) {
1388
 
            return $this->failWithStatus($ret);
1389
 
        }
1390
 
        $this->assert(!$hasPermission, 'guest user shouldn\'t have core.view on item');
1391
 
    }
1392
 
 
1393
 
    function testAddDuplicateUserToGroup() {
1394
 
        global $gallery;
1395
 
 
1396
 
        /* Create a new group */
1397
 
        list ($ret, $group) = $this->_createRandomGroup();
1398
 
        if ($ret) {
1399
 
            return $this->failWithStatus($ret);
1400
 
        }
1401
 
        $this->_markForCleanup($group);
1402
 
        $groupId = $group->getId();
1403
 
 
1404
 
        /* Create a new user */
1405
 
        /* If the unit test aborts to some reason, this user will still be in the db */
1406
 
        list ($ret, $user) = $this->_createRandomUser();
1407
 
        if ($ret) {
1408
 
            return $this->failWithStatus($ret);
1409
 
        }
1410
 
        $this->_markForCleanup($user);
1411
 
        $userId = $user->getId();
1412
 
 
1413
 
        /* Put the user into the group */
1414
 
        $ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
1415
 
        if ($ret) {
1416
 
            return $this->failWithStatus($ret);
1417
 
        }
1418
 
        /* Verify */
1419
 
        list ($ret, $count) = GalleryCoreApi::fetchUserCount(null, $groupId);
1420
 
        if ($ret) {
1421
 
            return $this->failWithStatus($ret);
1422
 
        }
1423
 
        $this->assertEquals(1, $count, 'first');
1424
 
 
1425
 
        GalleryDataCache::reset();
1426
 
 
1427
 
        /* Put the same user into the group again */
1428
 
        $ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
1429
 
        if ($ret) {
1430
 
            return $this->failWithStatus($ret);
1431
 
        }
1432
 
        /* Verify */
1433
 
        list ($ret, $count) = GalleryCoreApi::fetchUserCount(null, $groupId);
1434
 
        if ($ret) {
1435
 
            return $this->failWithStatus($ret);
1436
 
        }
1437
 
        $this->assertEquals(1, $count, 'second');
1438
 
    }
1439
 
 
1440
 
    function testCheckUserPassword() {
1441
 
        list ($ret, $user) = GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryUser');
1442
 
        if ($ret) {
1443
 
            return $this->failWithStatus($ret);
1444
 
        }
1445
 
        $this->assert(!empty($user), 'factory error');
1446
 
 
1447
 
        $user->changePassword('password');
1448
 
        $this->assert($user->isCorrectPassword('password'), 'password');
1449
 
        $user->changePassword('Ab1#+$_ ;]\?.,');
1450
 
        $this->assert($user->isCorrectPassword('Ab1#+$_ ;]\?.,'), 'Ab1#+$_ ;]\?.,');
1451
 
        $user->changePassword('pass&word>');
1452
 
        $this->assert($user->isCorrectPassword('pass&word>'), 'pass&word>');
1453
 
 
1454
 
        /* We still support old style simple md5 (G1 thru 1.4.0, G2 thru alpha-4) */
1455
 
        $user->setHashedPassword(md5('password'));
1456
 
        $this->assert($user->isCorrectPassword('password'), 'password');
1457
 
 
1458
 
        /*
1459
 
         * 2.0 was released with installer not entitizing password prior to hashing, while
1460
 
         * site admin(edit/create), user admin and registration all did.  Rather than simply
1461
 
         * make the installer match the others we've opted for cleaner data moving forward:
1462
 
         * hash the actual password, not entitized value.  To keep old hashed passwords
1463
 
         * working we check against both entitized and plain text in isCorrectPassword.
1464
 
         * If this is ever changed, ensure all these interfaces set the password correctly.
1465
 
         */
1466
 
        $password = $entityPassword = 'pass<word"';
1467
 
        GalleryUtilities::sanitizeInputValues($entityPassword, false);
1468
 
        $user->changePassword($entityPassword);
1469
 
        $this->assert($user->isCorrectPassword($entityPassword), 'check password entitized');
1470
 
        $this->assert($user->isCorrectPassword($password), 'check password original');
1471
 
    }
1472
 
}
1473
 
?>