make isUserAdmin do what the POD says (returns 0, 1), and clarify it

(deliberately passed null string or undef causes query instead of set).
Add tests to Group.t to check that behavior.
This commit is contained in:
Colin Kuskie 2006-03-19 06:10:44 +00:00
parent 0eb6ec7b01
commit abfa6ed1ac
3 changed files with 39 additions and 18 deletions

View file

@ -133,7 +133,8 @@ sub addGroups {
=head2 addUsers ( users [, expireOffset ] )
Adds users to this group.
Adds users to this group. If a user is already a member of a group, their expiration date
is updated.
=head3 users
@ -155,6 +156,7 @@ sub addUsers {
my ($isIn) = $self->session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [$self->getId, $uid]);
unless ($isIn) {
$self->session->db->write("insert into groupings (groupId,userId,expireDate) values (?,?,?)", [$self->getId, $uid, ($self->session->datetime->time()+$expireOffset)]);
$self->session->stow->delete("gotGroupsForUser");
} else {
$self->userGroupExpireDate($uid,($self->session->datetime->time()+$expireOffset));
}
@ -281,6 +283,7 @@ sub deleteUsers {
foreach my $uid (@{$users}) {
$self->session->db->write("delete from groupings where groupId=? and userId=?",[$self->getId, $uid]);
}
$self->session->stow->delete("gotGroupsForUser");
}
#-------------------------------------------------------------------
@ -937,7 +940,7 @@ sub set {
=head2 userIsAdmin ( [ userId, value ] )
Returns a 1 or 0 depending upon whether the user is a sub-admin for this group.
Sets or returns $userid's status as a group admin in this group.
=head3 userId
@ -945,7 +948,8 @@ A guid that is the unique identifier for a user. Defaults to the currently logge
=head3 value
If specified the admin flag will be set to this value.
If defined and not the empty string, the admin flag will be set to this value. Otherwise,
returns 1 if the user has been set as a group admin and 0 if the user hasn't.
=cut
@ -958,7 +962,7 @@ sub userIsAdmin {
return $value;
} else {
my ($admin) = $self->session->db->quickArray("select groupAdmin from groupings where groupId=? and userId=?", [$self->getId, $userId]);
return $admin;
return ($admin ? 1 : 0);
}
}

View file

@ -647,7 +647,7 @@ Be aware that any database links you create here will be available to all conten
},
'977 description' => {
message => q|Set this to yes to make this user a secondary admin. Secondary admins have the ability
message => q|Set this to yes to make this user a group admin. Group admins have the ability
to add or remove users from their groups.
|,
lastUpdated => 1132359856,
@ -1036,7 +1036,7 @@ removed from the system after a set period of time. That period of time is set
},
'977' => {
message => q|Is secondary admin?|,
message => q|Is group admin?|,
lastUpdated => 1053803387
},
@ -1280,9 +1280,7 @@ When users are added to the system they are put into the registered users group.
<p>
<b>Secondary Admins</b><br> Users in the Secondary Admins group may
add new users, but cannot edit users. Also, if you are a Secondary
Admin and are a member of a different group, you can be set as an admin for that group. This
will allow you to add or remove members from that group.
add new users, but cannot edit users.
<p>
<b>Style Managers</b><br>

View file

@ -18,7 +18,7 @@ use WebGUI::Utility;
use WebGUI::User;
use WebGUI::Group;
use Test::More tests => 51; # increment this value for each test you create
use Test::More tests => 59; # increment this value for each test you create
use Test::Deep;
my $session = WebGUI::Test->session;
@ -143,24 +143,43 @@ cmp_bag($gX->getGroupsIn(), [3], 'Not able to add B tree under Z tree under X');
my $user = WebGUI::User->new($session, "new");
$gX->userIsAdmin($user->userId, "yes");
ok(!$gX->userIsAdmin($user->userId), "userIsAdmin: User who isn't secondary admin can't be group admin");
$user->addToGroups([12]);
ok($user->isInGroup(12), "userIsAdmin: Added dude to Secondary Admins");
isnt($gX->userIsAdmin($user->userId), 'yes', "userIsAdmin returns 1 or 0, not value");
$gX->userIsAdmin($user->userId, 1);
ok(!$gX->userIsAdmin($user->userId), "userIsAdmin: User must be member of group to be group admin");
my $addedUserTime = time();
my $expireOffset = $gX->expireOffset;
$user->addToGroups([$gX->getId]);
ok($user->isInGroup($gX->getId), "userIsAdmin: Added dude to gX");
##User expire time is calculated correctly
my $expireTime = abs($gX->userGroupExpireDate($user->userId) - $expireOffset - time());
ok( $expireTime < 1, 'userGroupExpireDate: Default expire time');
ok($user->isInGroup($gX->getId), "addToGroups: Added dude to gX");
$gX->userIsAdmin($user->userId, 1);
ok($gX->userIsAdmin($user->userId), "userIsAdmin: Dude set to be group admin for gX");
my $expireOffset = $gX->expireOffset;
my $expireTime = $addedUserTime+$expireOffset;
sleep 5;
$expireTime = time() + $expireOffset - $gX->userGroupExpireDate($user->userId) ;
ok( ($expireTime < 6 && $expireTime > 0), 'userGroupExpireDate: Default expire time ages');
ok( abs($gX->userGroupExpireDate($user->userId) - $expireTime) < 1, 'userGroupExpireDate: Default expire time');
$gX->addUsers([$user->userId]);
my $expireTime = abs($gX->userGroupExpireDate($user->userId) - $expireOffset - time());
ok( $expireTime < 1, 'adding exising user to group resets expire date');
ok($gX->userIsAdmin($user->userId), "userIsAdmin: adding existing user to group does not change group admin status");
##undef and the empty string will return the set value because they're
##interpreted as the empty string.
is($gX->userIsAdmin($user->userId, ''), 1, "userIsAdmin: empty string returns status");
is($gX->userIsAdmin($user->userId, undef), 1, "userIsAdmin: undef returns status");
##Now we'll try various settings and see how they work with userIsAdmin.
$gX->userIsAdmin($user->userId, 0);
ok(!$gX->userIsAdmin($user->userId), "userIsAdmin: trying 0 as value");
$gX->userIsAdmin($user->userId, '0E0');
ok(!$gX->userIsAdmin($user->userId), "userIsAdmin: trying '0E0'(string) as value");
$gX->userIsAdmin($user->userId, 0E0);
ok(!$gX->userIsAdmin($user->userId), "userIsAdmin: trying 0E0 as value");
$user->delete;