3
* Gallery - a web based photo album viewer and editor
4
* Copyright (C) 2000-2007 Bharat Mediratta
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or (at
9
* your option) any later version.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22
* Test User and Group functionality
23
* @package GalleryCore
25
* @author Bharat Mediratta <bharat@menalto.com>
26
* @version $Revision: 15513 $
28
class UserAndGroupTest extends GalleryTestCase {
30
function UserAndGroupTest($methodName) {
31
$this->GalleryTestCase($methodName);
37
list ($ret, $this->_siteAdminGroupId) =
38
GalleryCoreApi::getPluginParameter('module', 'core', 'id.adminGroup');
40
print $ret->getAsHtml();
41
return $this->failWithStatus($ret);
46
$ret = GalleryCoreApi::setPluginParameter('module', 'core',
47
'id.adminGroup', $this->_siteAdminGroupId);
49
$this->failWithStatus($ret);
56
* Create user, delete user
58
function testCreateUser() {
61
/* Create a new user */
62
list ($ret, $user) = $this->_createRandomUser();
64
return $this->failWithStatus($ret);
67
GalleryDataCache::reset();
70
$ret = $this->_verifyEntity($user);
72
return $this->failWithStatus($ret);
76
$ret = $this->_deleteAndVerifyEntity($user->getId());
78
return $this->failWithStatus($ret);
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
87
function testDeleteUser() {
90
/* Create a new group */
91
list ($ret, $group) = $this->_createRandomGroup();
93
return $this->failWithStatus($ret);
95
$this->_markForCleanup($group);
96
$groupId = $group->getId();
98
/* Create a new user */
99
list ($ret, $user) = $this->_createRandomUser();
101
return $this->failWithStatus($ret);
103
$userId = $user->getId();
105
/* Put the user into the group */
106
$ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
108
$this->_markForCleanup($user);
109
return $this->failWithStatus($ret);
112
list ($ret, $inGroup) = GalleryCoreApi::isUserInGroup($userId, $groupId);
114
$this->_markForCleanup($user);
115
return $this->failWithStatus($ret);
117
$this->assert($inGroup, 'failed to add the user to a group');
119
/* Add a permission to the user */
120
$ret = GalleryCoreApi::addUserPermission($this->_getRootId(), $userId,
121
'core.addDataItem', false);
123
GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
124
$this->_markForCleanup($user);
125
return $this->failWithStatus($ret);
127
/* Verify the user has the permission */
128
list ($ret, $hasPermission) =
129
GalleryCoreApi::hasItemPermission($this->_getRootId(), 'core.addDataItem');
131
GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
132
$this->_markForCleanup($user);
133
return $this->failWithStatus($ret);
135
$this->assert($hasPermission, 'failed to add a permission to the user');
137
$this->_activeUserBackup = $gallery->getActiveUser();
138
$gallery->setActiveUser($user);
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());
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);
149
$this->_markForCleanup($item);
151
/* Restore the activeUser, should be a Site Admin */
152
$gallery->setActiveUser($this->_activeUserBackup);
154
/* Delete the user */
155
$ret = GalleryCoreApi::deleteEntityById($userId);
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);
162
GalleryDataCache::reset();
164
/* Verify the user is gone */
165
$ret = $this->_verifyMissing($userId);
167
$this->assert(false, 'The user was not actually deleted');
170
/* Verify the item still exists and new owner is a site admin */
171
list ($ret, $item) = $item->refresh();
173
if ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT) {
174
$this->assert(false, 'An item was deleted that still should exist.');
176
GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
177
GalleryCoreApi::removeMapEntry(
178
'GalleryAccessMap', array('userOrGroupId' => $userId));
179
return $this->failWithStatus($ret);
182
/* Verify the new owner of the item */
183
list ($ret, $groupIds) = GalleryCoreApi::fetchGroupsForUser($item->getOwnerId());
185
GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
186
GalleryCoreApi::removeMapEntry('GalleryAccessMap', array('userOrGroupId' => $userId));
187
return $this->failWithStatus($ret);
189
$this->assert(isset($groupIds[$this->_siteAdminGroupId]),
190
'The new ownerId of the item is wrong.');
192
/* Verify there are no mappings left */
193
list ($ret, $groupIds) = GalleryCoreApi::fetchGroupsForUser($userId);
195
GalleryCoreApi::removeMapEntry('GalleryUserGroupMap', array('userId' => $userId));
196
GalleryCoreApi::removeMapEntry('GalleryAccessMap', array('userOrGroupId' => $userId));
197
return $this->failWithStatus($ret);
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));
203
$this->failWithStatus($ret);
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);
211
$this->failWithStatus($ret);
213
$this->assertEquals(array(), $permissions,
214
'Not all permissions of the deleted user were removed');
216
/* Make sure there are no mappings left (garbage) */
217
$ret = GalleryCoreApi::removeMapEntry('GalleryAccessMap',
218
array('userOrGroupId' => $userId));
220
$this->failWithStatus($ret);
224
/* Verify we can't delete the active user */
225
function testDeleteSelf() {
228
list ($ret, $user) = $this->_createRandomUser();
230
return $this->failWithStatus($ret);
232
$this->_markForCleanup($user);
233
$gallery->setActiveUser($user);
235
$ret = GalleryCoreApi::deleteEntityById($user->getId());
236
$this->assertEquals(ERROR_BAD_PARAMETER | GALLERY_ERROR, $ret->getErrorCode());
239
/* Verify we can't delete the anonymous user */
240
function testDeleteAnonymousUser() {
241
list ($ret, $anonymousUserId) =
242
GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
244
$ret = GalleryCoreApi::deleteEntityById($anonymousUserId);
245
$this->assertEquals(ERROR_BAD_PARAMETER | GALLERY_ERROR, $ret->getErrorCode());
249
* Verify we can't delete the last site admin.
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();
255
return $this->failWithStatus($ret);
257
$this->_markForCleanup($user);
258
$userId = $user->getId();
259
list ($ret, $group) = $this->_createRandomGroup();
261
return $this->failWithStatus($ret);
263
$this->_markForCleanup($group);
264
$groupId = $group->getId();
266
$ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
268
return $this->failWithStatus($ret);
270
$ret = GalleryCoreApi::setPluginParameter('module', 'core', 'id.adminGroup', $groupId);
272
return $this->failWithStatus($ret);
275
/* Attempt to delete user */
276
$ret = GalleryCoreApi::deleteEntityById($userId);
277
$this->assertEquals(ERROR_BAD_PARAMETER | GALLERY_ERROR, $ret->getErrorCode());
281
* Verify item owners are remapped ok if we delete a site admin.
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();
287
return $this->failWithStatus($ret);
289
$userId = $user->getId();
290
list ($ret, $otherUser) = $this->_createRandomUser();
292
$this->_markForCleanup($user);
293
return $this->failWithStatus($ret);
295
$this->_markForCleanup($otherUser);
296
$otherUserId = $otherUser->getId();
297
list ($ret, $group) = $this->_createRandomGroup();
299
$this->_markForCleanup($user);
300
return $this->failWithStatus($ret);
302
$this->_markForCleanup($group);
303
$groupId = $group->getId();
304
$ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
306
$this->_markForCleanup($user);
307
return $this->failWithStatus($ret);
309
$ret = GalleryCoreApi::addUserToGroup($otherUserId, $groupId);
311
$this->_markForCleanup($user);
312
return $this->failWithStatus($ret);
314
$ret = GalleryCoreApi::setPluginParameter('module', 'core', 'id.adminGroup', $groupId);
316
$this->_markForCleanup($user);
317
return $this->failWithStatus($ret);
320
/* Create an album and item owned by this admin */
321
list ($ret, $album) = $this->_createRandomAlbum($this->_getRootId(),
322
array('ownerId' => $userId));
324
$this->_markForCleanup($user);
325
return $this->failWithStatus($ret);
327
$this->_markForCleanup($album);
328
list ($ret, $item) = $this->_createRandomDataItem($album->getId(), 'test/file',
329
array('ownerId' => $userId));
331
$this->_markForCleanup($user);
332
return $this->failWithStatus($ret);
335
/* Delete this admin */
336
$ret = GalleryCoreApi::deleteEntityById($userId);
338
$this->assert(false, 'The user could not be deleted.');
339
return $this->failWithStatus($ret);
341
GalleryDataCache::reset();
342
$ret = $this->_verifyMissing($userId);
344
$this->assert(false, 'The user was not actually deleted');
347
/* Verify owner was remapped to our other admin */
348
list ($ret, $album) = $album->refresh();
350
return $this->failWithStatus($ret);
352
$this->assertEquals($otherUserId, $album->getOwnerId(), 'album owner');
353
list ($ret, $item) = $item->refresh();
355
return $this->failWithStatus($ret);
357
$this->assertEquals($otherUserId, $item->getOwnerId(), 'item owner');
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);
366
return $this->failWithStatus($ret);
369
$ret = GalleryCoreApi::deleteEntityById($groupId);
370
if ($ret && !($ret->getErrorCode() & ERROR_BAD_PARAMETER)) {
371
return $this->failWithStatus($ret);
374
$this->assert($ret && ($ret->getErrorCode() & ERROR_BAD_PARAMETER),
375
"$paramName: expect an error");
377
/* Make sure the entity still exists */
378
list ($ret, $entity) = GalleryCoreApi::loadEntitiesById($groupId);
380
return $this->failWithStatus($ret);
382
$groupName = $entity->getGroupName();
383
$this->assert(!empty($groupName), "$paramName: group name should not be empty");
388
* Create user, modify user, delete user
390
function testCreateAndModifyUser() {
393
/* Create a new user */
394
list ($ret, $user) = $this->_createRandomUser();
396
return $this->failWithStatus($ret);
399
/* Lock and refresh it */
400
list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($user->getId());
402
return $this->failWithStatus($ret);
405
list ($ret, $user) = $user->refresh();
407
return $this->failWithStatus($ret);
411
$user->setFullName('newname-' . rand());
414
$ret = $user->save();
416
return $this->failWithStatus($ret);
420
$ret = GalleryCoreApi::releaseLocks($lockId);
422
return $this->failWithStatus($ret);
426
$ret = $this->_verifyEntity($user);
428
return $this->failWithStatus($ret);
432
$ret = $this->_deleteAndVerifyEntity($user->getId());
434
return $this->failWithStatus($ret);
439
* Create user, fetch user names, make sure it's in there, delete user
441
function testFetchUserNames() {
444
list ($ret, $user) = $this->_createRandomUser();
446
return $this->failWithStatus($ret);
449
GalleryDataCache::reset();
451
list ($ret, $names) = GalleryCoreApi::fetchUserNames();
453
return $this->failWithStatus($ret);
456
$this->assert(in_array($user->getUserName(), $names));
459
$ret = $this->_deleteAndVerifyEntity($user->getId());
461
return $this->failWithStatus($ret);
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
470
function testDeleteUserItems() {
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
477
$deletedItems = array();
478
$remainingItems = array();
480
* These items get delete permission.
481
* Delete permission is assigned AFTER creating all items, just before
484
$deletePermissionItems = array();
486
/* Create test user 1, the owner of most items in the new item tree. */
487
list ($ret, $user1) = $this->_createRandomUser();
489
return $this->failWithStatus($ret);
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();
495
return $this->failWithStatus($ret);
497
$this->_markForCleanup($user2);
499
/* Add a test container album. All test items/albums will be in this container. */
500
list ($ret, $this->_album) = $this->_createRandomAlbum($this->_getRootId());
502
return $this->failWithStatus($ret);
505
$this->_markForCleanup($this->_album);
506
$remainingItems[] = $this->_album;
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);
513
return $this->failWithStatus($ret);
515
/* Change the active user to our test user1. */
516
$this->_activeUserBackup = $gallery->getActiveUser();
517
$gallery->setActiveUser($user1);
519
/* Add a test album with user1 */
520
list ($ret, $albumWithDeletePermission) = $this->_createRandomAlbum($this->_album->getId());
522
return $this->failWithStatus($ret);
524
$remainingItems[] = $albumWithDeletePermission;
525
$deletePermissionItems[] = $albumWithDeletePermission;
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
535
list ($ret, $subAlbumWithDeletePermission) =
536
$this->_createRandomAlbum($albumWithDeletePermission->getId());
538
return $this->failWithStatus($ret);
540
$deletedItems[] = $subAlbumWithDeletePermission;
541
$deletePermissionItems[] = $subAlbumWithDeletePermission;
542
/* Add item to album and subalbum */
543
list ($ret, $subItemWithDeletePermission) =
544
$this->_createRandomDataItem($albumWithDeletePermission->getId());
546
return $this->failWithStatus($ret);
548
$deletedItems[] = $subItemWithDeletePermission;
549
$deletePermissionItems[] = $subItemWithDeletePermission;
550
list ($ret, $subSubItemWithDeletePermission) =
551
$this->_createRandomDataItem($subAlbumWithDeletePermission->getId());
553
return $this->failWithStatus($ret);
555
$deletedItems[] = $subSubItemWithDeletePermission;
556
$deletePermissionItems[] = $subSubItemWithDeletePermission;
557
list ($ret, $subSubAlbumWithDeletePermission) =
558
$this->_createRandomAlbum($subAlbumWithDeletePermission->getId());
560
return $this->failWithStatus($ret);
562
$deletedItems[] = $subSubAlbumWithDeletePermission;
563
$deletePermissionItems[] = $subSubAlbumWithDeletePermission;
564
list ($ret, $subSubSubItemWithDeletePermission) =
565
$this->_createRandomDataItem($subSubAlbumWithDeletePermission->getId());
567
return $this->failWithStatus($ret);
569
$deletedItems[] = $subSubSubItemWithDeletePermission;
570
$deletePermissionItems[] = $subSubSubItemWithDeletePermission;
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
581
list ($ret, $subAlbumWithDeletePermission2) =
582
$this->_createRandomAlbum($albumWithDeletePermission->getId());
584
return $this->failWithStatus($ret);
587
$remainingItems[] = $subAlbumWithDeletePermission2;
588
$deletePermissionItems[] = $subAlbumWithDeletePermission2;
589
/* A subSub album without delete permission */
590
list ($ret, $subSubAlbumWithoutDeletePermission2) =
591
$this->_createRandomAlbum($subAlbumWithDeletePermission2->getId());
593
return $this->failWithStatus($ret);
595
$remainingItems[] = $subSubAlbumWithoutDeletePermission2;
597
/* A subSub album with delete permission */
598
list ($ret, $subSubAlbumWithDeletePermission2) =
599
$this->_createRandomAlbum($subAlbumWithDeletePermission2->getId());
601
return $this->failWithStatus($ret);
603
$remainingItems[] = $subSubAlbumWithDeletePermission2;
604
$deletePermissionItems[] = $subSubAlbumWithDeletePermission2;
606
* A subSubSubItem with delete permission
607
* (but is in a subSubalbum without delete permission)
609
list ($ret, $subSubSubItemWithDeletePermission2) =
610
$this->_createRandomDataItem($subSubAlbumWithoutDeletePermission2->getId());
612
return $this->failWithStatus($ret);
614
$deletedItems[] = $subSubSubItemWithDeletePermission2;
615
$deletePermissionItems[] = $subSubSubItemWithDeletePermission2;
616
/* A subSubItem without delete permission */
617
list ($ret, $subSubSubItemWithoutDeletePermission2) =
618
$this->_createRandomDataItem($subSubAlbumWithDeletePermission2->getId());
620
return $this->failWithStatus($ret);
622
$remainingItems[] = $subSubSubItemWithoutDeletePermission2;
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
631
list ($ret, $subAlbumWithDeletePermission3) =
632
$this->_createRandomAlbum($albumWithDeletePermission->getId());
634
return $this->failWithStatus($ret);
636
$remainingItems[] = $subAlbumWithDeletePermission3;
637
$deletePermissionItems[] = $subAlbumWithDeletePermission3;
638
list ($ret, $subSubItemWithDeletePermission3) =
639
$this->_createRandomDataItem($subAlbumWithDeletePermission3->getId());
641
return $this->failWithStatus($ret);
643
$deletedItems[] = $subSubItemWithDeletePermission3;
644
$deletePermissionItems[] = $subSubItemWithDeletePermission3;
646
/* Now change the activeUser to the other user (from user1 to user2) */
647
$gallery->setActiveUser($user2);
649
/* Add an item with user2 in user1's item tree */
650
list ($ret, $subSubItemWithDeletePermissionDifferentOwner3) =
651
$this->_createRandomDataItem($subAlbumWithDeletePermission3->getId());
653
return $this->failWithStatus($ret);
655
$remainingItems[] = $subSubItemWithDeletePermissionDifferentOwner3;
656
$deletePermissionItems[] = $subSubItemWithDeletePermissionDifferentOwner3;
658
/* Restore the activeUser, should be a Site Admin(the user who runs the test) */
659
$gallery->setActiveUser($this->_activeUserBackup);
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);
666
return $this->failWithStatus($ret);
671
* Verify the owner id of the items, all test items/albums but the single subitem of user2
672
* should be owned by user1.
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');
681
/* Verify the site admin has the needed permissions */
682
$ret = GalleryCoreApi::assertHasItemPermission($item->getId(), 'core.viewResizes');
684
return $this->failWithStatus($ret);
688
/* Execute the "delete user1" call */
689
$ret = GalleryCoreApi::deleteUserItems($user1->getId());
691
return $this->failWithStatus($ret);
693
GalleryDataCache::reset();
695
/* Verify the correct items were deleted */
696
foreach ($deletedItems as $item) {
697
$ret = $this->_verifyMissing($item->getId());
699
return $this->failWithStatus($ret);
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.
708
foreach ($remainingItems as $item) {
709
$id = $item->getId();
710
list ($ret, $item) = $item->refresh();
712
if ($ret && $ret->getErrorCode() & ERROR_MISSING_OBJECT) {
713
$this->assert(false, "Wrongly deleted item ($id)");
715
return $this->failWithStatus($ret);
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');
727
* Count users, Create user, count users again, delete user
729
function testFetchUserCount() {
732
list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount();
734
return $this->failWithStatus($ret);
737
list ($ret, $user) = $this->_createRandomUser();
739
return $this->failWithStatus($ret);
742
GalleryDataCache::reset();
744
list ($ret, $countAfter) = GalleryCoreApi::fetchUserCount();
746
return $this->failWithStatus($ret);
749
$this->assertEquals($countBefore+1, $countAfter);
752
$ret = $this->_deleteAndVerifyEntity($user->getId());
754
return $this->failWithStatus($ret);
759
* Verify that we can fetch all users with a given substring in their username
761
function testFetchUserCountBySubstring() {
764
$unique = rand(0, 65535);
765
list ($ret, $count) = GalleryCoreApi::fetchUserCount($unique, null);
767
return $this->failWithStatus($ret);
770
/* Nobody matches our unique tag */
771
$this->assertEquals(0, $count);
773
list ($ret, $user) = $this->_createRandomUser($unique);
775
return $this->failWithStatus($ret);
778
GalleryDataCache::reset();
780
list ($ret, $count) = GalleryCoreApi::fetchUserCount($unique, null);
782
return $this->failWithStatus($ret);
785
/* We now have one user that matches the description */
786
$this->assertEquals(1, $count);
789
$ret = $this->_deleteAndVerifyEntity($user->getId());
791
return $this->failWithStatus($ret);
796
* Verify that we can fetch a count of the users from any particular group
798
function testFetchUserCountByGroupId() {
802
list ($ret, $user) = $this->_createRandomUser();
804
return $this->failWithStatus($ret);
808
list ($ret, $group) = $this->_createRandomGroup();
810
return $this->failWithStatus($ret);
813
/* fetchUserCount should return 0 */
814
list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount(null, $group->getId());
816
return $this->failWithStatus($ret);
818
$this->assertEquals(0, $countBefore);
820
/* Put the user into the group */
821
$ret = GalleryCoreApi::addUserToGroup($user->getId(), $group->getId());
823
return $this->failWithStatus($ret);
826
/* fetchUserCount should return 1 */
827
list ($ret, $countAfter) = GalleryCoreApi::fetchUserCount(null, $group->getId());
829
return $this->failWithStatus($ret);
831
$this->assertEquals(1, $countAfter);
834
$ret = $this->_deleteAndVerifyEntity($user->getId());
836
return $this->failWithStatus($ret);
840
$ret = $this->_deleteAndVerifyEntity($group->getId());
842
return $this->failWithStatus($ret);
847
* Verify that we can fetch all users from a group with a given substring in their username
849
function testFetchUserCountBySubstringAndGroupId() {
853
$uniqueFirst = rand(0, 65535);
854
$uniqueSecond = rand(0, 65535);
857
list ($ret, $group) = $this->_createRandomGroup();
859
return $this->failWithStatus($ret);
862
/* fetchUserCount should return 0 */
863
list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount($strSearch, $group->getId());
865
return $this->failWithStatus($ret);
867
$this->assertEquals(0, $countBefore);
869
/* Create the first user */
870
list ($ret, $userFirst) = $this->_createRandomUser($strSearch . $uniqueFirst);
872
return $this->failWithStatus($ret);
875
/* Create the second user */
876
list ($ret, $userSecond) = $this->_createRandomUser($strSearch . $uniqueSecond);
878
return $this->failWithStatus($ret);
881
/* Put the first user into the group */
882
$ret = GalleryCoreApi::addUserToGroup($userFirst->getId(), $group->getId());
884
return $this->failWithStatus($ret);
887
/* Put the second user into the group */
888
$ret = GalleryCoreApi::addUserToGroup($userSecond->getId(), $group->getId());
890
return $this->failWithStatus($ret);
893
/* fetchUserCount should return 1 */
894
list ($ret, $countAfter) = GalleryCoreApi::fetchUserCount($strSearch . $uniqueFirst,
897
return $this->failWithStatus($ret);
899
$this->assertEquals(1, $countAfter);
901
/* Delete first user */
902
$ret = $this->_deleteAndVerifyEntity($userFirst->getId());
904
return $this->failWithStatus($ret);
907
/* Delete second user */
908
$ret = $this->_deleteAndVerifyEntity($userSecond->getId());
910
return $this->failWithStatus($ret);
914
$ret = $this->_deleteAndVerifyEntity($group->getId());
916
return $this->failWithStatus($ret);
921
* Verify that we can fetch all users from a group with a given substring in their username
923
function testFetchUsersFromGroupBySubstringAndGroupId() {
927
$uniqueFirst = rand(0, 65535);
928
$uniqueSecond = rand(0, 65535);
931
list ($ret, $group) = $this->_createRandomGroup();
933
return $this->failWithStatus($ret);
936
/* fetchUserCount should return 0 */
937
list ($ret, $countBefore) = GalleryCoreApi::fetchUserCount($strSearch, $group->getId());
939
return $this->failWithStatus($ret);
941
$this->assertEquals(0, $countBefore);
943
/* Create the first user */
944
list ($ret, $userFirst) = $this->_createRandomUser($strSearch . $uniqueFirst);
946
return $this->failWithStatus($ret);
949
/* Create the second user */
950
list ($ret, $userSecond) = $this->_createRandomUser($strSearch . $uniqueSecond);
952
return $this->failWithStatus($ret);
955
/* Put the first user into the group */
956
$ret = GalleryCoreApi::addUserToGroup($userFirst->getId(), $group->getId());
958
return $this->failWithStatus($ret);
961
/* Put the second user into the group */
962
$ret = GalleryCoreApi::addUserToGroup($userSecond->getId(), $group->getId());
964
return $this->failWithStatus($ret);
967
/* fetchUsersForGroup should return one user */
968
list ($ret, $users) = GalleryCoreApi::fetchUsersForGroup(
972
$strSearch . $uniqueFirst
975
return $this->failWithStatus($ret);
977
$this->assertEquals(1, count($users));
979
/* Delete first user */
980
$ret = $this->_deleteAndVerifyEntity($userFirst->getId());
982
return $this->failWithStatus($ret);
985
/* Delete second user */
986
$ret = $this->_deleteAndVerifyEntity($userSecond->getId());
988
return $this->failWithStatus($ret);
992
$ret = $this->_deleteAndVerifyEntity($group->getId());
994
return $this->failWithStatus($ret);
999
* Create user, fetch it by name, delete user
1001
function testFetchUserByName() {
1004
list ($ret, $user) = $this->_createRandomUser();
1006
return $this->failWithStatus($ret);
1009
GalleryDataCache::reset();
1011
list ($ret, $newUser) = GalleryCoreApi::fetchUserByUserName($user->getUserName());
1013
return $this->failWithStatus($ret);
1016
$this->assertEquals($user, $newUser);
1019
$ret = $this->_deleteAndVerifyEntity($user->getId());
1021
return $this->failWithStatus($ret);
1026
* Create group, delete group
1028
function testCreateGroup() {
1031
list ($ret, $group) = $this->_createRandomGroup();
1033
return $this->failWithStatus($ret);
1036
GalleryDataCache::reset();
1039
$ret = $this->_verifyEntity($group);
1041
return $this->failWithStatus($ret);
1045
$ret = $this->_deleteAndVerifyEntity($group->getId());
1047
return $this->failWithStatus($ret);
1052
* Create duplicate group
1054
function testCreateDuplicateGroup() {
1059
list ($ret, $group) = $this->_createRandomGroup($groupTag);
1061
return $this->failWithStatus($ret);
1064
GalleryDataCache::reset();
1067
$ret = $this->_verifyEntity($group);
1069
return $this->failWithStatus($ret);
1072
GalleryDataCache::reset();
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));
1080
$ret = $this->_deleteAndVerifyEntity($group->getId());
1082
return $this->failWithStatus($ret);
1087
* Create duplicate user
1089
function testCreateDuplicateUser() {
1094
list ($ret, $user) = $this->_createRandomUser($userTag);
1096
return $this->failWithStatus($ret);
1099
GalleryDataCache::reset();
1102
$ret = $this->_verifyEntity($user);
1104
return $this->failWithStatus($ret);
1107
GalleryDataCache::reset();
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));
1115
$ret = $this->_deleteAndVerifyEntity($user->getId());
1117
return $this->failWithStatus($ret);
1122
* Create group, modify group, delete group
1124
function testCreateAndModifyGroup() {
1127
/* Create a new group */
1128
list ($ret, $group) = $this->_createRandomGroup();
1130
return $this->failWithStatus($ret);
1133
/* Lock and refresh it */
1134
list ($ret, $lockId) = GalleryCoreApi::acquireWriteLock($group->getId());
1136
return $this->failWithStatus($ret);
1139
list ($ret, $group) = $group->refresh();
1141
return $this->failWithStatus($ret);
1145
$group->setGroupName('newname-' . rand());
1148
$ret = $group->save();
1150
return $this->failWithStatus($ret);
1154
$ret = GalleryCoreApi::releaseLocks($lockId);
1156
return $this->failWithStatus($ret);
1160
$ret = $this->_verifyEntity($group);
1162
return $this->failWithStatus($ret);
1166
$ret = $this->_deleteAndVerifyEntity($group->getId());
1168
return $this->failWithStatus($ret);
1173
* Create user, create group, add user to group, remove user from group,
1174
* delete user, delete group
1176
function testAddAndRemoveUserFromGroup() {
1180
list ($ret, $user) = $this->_createRandomUser();
1182
return $this->failWithStatus($ret);
1185
/* Create a group */
1186
list ($ret, $group) = $this->_createRandomGroup();
1188
return $this->failWithStatus($ret);
1191
/* Put the user into the group */
1192
$ret = GalleryCoreApi::addUserToGroup($user->getId(), $group->getId());
1194
return $this->failWithStatus($ret);
1198
list ($ret, $inGroup) = GalleryCoreApi::isUserInGroup($user->getId(), $group->getId());
1200
return $this->failWithStatus($ret);
1202
$this->assert($inGroup);
1204
/* Remove user from group */
1205
$ret = GalleryCoreApi::removeUserFromGroup($user->getId(), $group->getId());
1207
return $this->failWithStatus($ret);
1211
list ($ret, $inGroup) = GalleryCoreApi::isUserInGroup($user->getId(), $group->getId());
1213
return $this->failWithStatus($ret);
1215
$this->assert(!$inGroup);
1218
$ret = $this->_deleteAndVerifyEntity($user->getId());
1220
return $this->failWithStatus($ret);
1224
$ret = $this->_deleteAndVerifyEntity($group->getId());
1226
return $this->failWithStatus($ret);
1231
* Create group, fetch group names, make sure it's in there, delete group
1233
function testFetchGroupNames() {
1236
list ($ret, $group) = $this->_createRandomGroup();
1238
return $this->failWithStatus($ret);
1241
GalleryDataCache::reset();
1243
list ($ret, $names) = GalleryCoreApi::fetchGroupNames();
1245
return $this->failWithStatus($ret);
1248
$this->assert(in_array($group->getGroupName(), $names));
1251
$ret = $this->_deleteAndVerifyEntity($group->getId());
1253
return $this->failWithStatus($ret);
1258
* Count groups, Create group, count groups again, delete group
1260
function testFetchGroupCount() {
1263
list ($ret, $countBefore) = GalleryCoreApi::fetchGroupCount();
1265
return $this->failWithStatus($ret);
1268
list ($ret, $group) = $this->_createRandomGroup();
1270
return $this->failWithStatus($ret);
1273
GalleryDataCache::reset();
1275
list ($ret, $countAfter) = GalleryCoreApi::fetchGroupCount();
1277
return $this->failWithStatus($ret);
1280
$this->assertEquals($countBefore+1, $countAfter);
1283
$ret = $this->_deleteAndVerifyEntity($group->getId());
1285
return $this->failWithStatus($ret);
1290
* Create group, fetch it by name, delete group
1292
function testFetchGroupByName() {
1295
list ($ret, $group) = $this->_createRandomGroup();
1297
return $this->failWithStatus($ret);
1300
GalleryDataCache::reset();
1302
list ($ret, $newGroup) = GalleryCoreApi::fetchGroupByGroupName($group->getGroupName());
1304
return $this->failWithStatus($ret);
1307
$this->assertEquals($group, $newGroup);
1310
$ret = $this->_deleteAndVerifyEntity($group->getId());
1312
return $this->failWithStatus($ret);
1316
function testAssertSiteAdministrator() {
1317
$ret = GalleryCoreApi::assertUserIsSiteAdministrator();
1319
return $this->failWithStatus($ret);
1323
function testAssertHasItemPermission() {
1326
$ret = GalleryCoreApi::assertHasItemPermission($this->_getRootId(), 'core.view');
1328
return $this->failWithStatus($ret);
1332
function testHasItemPermission() {
1335
GalleryDataCache::clearPermissionCache();
1336
list ($ret, $hasPermission) =
1337
GalleryCoreApi::hasItemPermission($this->_getRootId(), 'core.view');
1339
return $this->failWithStatus($ret);
1341
$this->assert($hasPermission, 'activeUser should have core.view on root');
1343
$userId = $gallery->getActiveUserId();
1344
/* Get id of anonymous id */
1345
list ($ret, $anonymousUserId) =
1346
GalleryCoreApi::getPluginParameter('module', 'core', 'id.anonymousUser');
1348
return $this->failWithStatus($ret);
1351
/* Add an item such that we can set permissions to check hasItemPermission */
1352
list ($ret, $item) = $this->_createRandomDataItem($this->_getRootId());
1354
return $this->failWithStatus($ret);
1356
$this->_markForCleanup($item);
1358
/* Reset permissions */
1359
$ret = GalleryCoreApi::removeItemPermissions($item->getId());
1361
return $this->failWithStatus($ret);
1364
/* Add a permission to the active user */
1365
$ret = GalleryCoreApi::addUserPermission($item->getId(), $userId, 'core.view', false);
1367
return $this->failWithStatus($ret);
1370
/* Now test activeUser with and without userId param */
1371
list ($ret, $hasPermission) =
1372
GalleryCoreApi::hasItemPermission($item->getId(), 'core.view');
1374
return $this->failWithStatus($ret);
1376
$this->assert($hasPermission, 'default user should have core.view on item');
1378
list ($ret, $hasPermission) =
1379
GalleryCoreApi::hasItemPermission($item->getId(), 'core.view', $userId);
1381
return $this->failWithStatus($ret);
1383
$this->assert($hasPermission, 'userId should have core.view on item');
1385
list ($ret, $hasPermission) =
1386
GalleryCoreApi::hasItemPermission($item->getId(), 'core.view', $anonymousUserId);
1388
return $this->failWithStatus($ret);
1390
$this->assert(!$hasPermission, 'guest user shouldn\'t have core.view on item');
1393
function testAddDuplicateUserToGroup() {
1396
/* Create a new group */
1397
list ($ret, $group) = $this->_createRandomGroup();
1399
return $this->failWithStatus($ret);
1401
$this->_markForCleanup($group);
1402
$groupId = $group->getId();
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();
1408
return $this->failWithStatus($ret);
1410
$this->_markForCleanup($user);
1411
$userId = $user->getId();
1413
/* Put the user into the group */
1414
$ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
1416
return $this->failWithStatus($ret);
1419
list ($ret, $count) = GalleryCoreApi::fetchUserCount(null, $groupId);
1421
return $this->failWithStatus($ret);
1423
$this->assertEquals(1, $count, 'first');
1425
GalleryDataCache::reset();
1427
/* Put the same user into the group again */
1428
$ret = GalleryCoreApi::addUserToGroup($userId, $groupId);
1430
return $this->failWithStatus($ret);
1433
list ($ret, $count) = GalleryCoreApi::fetchUserCount(null, $groupId);
1435
return $this->failWithStatus($ret);
1437
$this->assertEquals(1, $count, 'second');
1440
function testCheckUserPassword() {
1441
list ($ret, $user) = GalleryCoreApi::newFactoryInstance('GalleryEntity', 'GalleryUser');
1443
return $this->failWithStatus($ret);
1445
$this->assert(!empty($user), 'factory error');
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>');
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');
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.
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');