Cleaned up Group.t so that diagnostics when failing are correct.
Fixed two bugs in Group.pm 1) If recursive was set, and no recursive cache was present but a direct cache was available, it would be returned. That's wrong. 2) Added a check in addGroups so that self cannot be added to itself.
This commit is contained in:
parent
726f7c61af
commit
0197f6f1bd
2 changed files with 18 additions and 13 deletions
|
|
@ -105,7 +105,8 @@ Adds groups to this group.
|
|||
|
||||
=head3 groups
|
||||
|
||||
An array reference containing the list of group ids to add.
|
||||
An array reference containing the list of group ids to add. Group Visitor may
|
||||
not be added to any group. Groups may not be added to themselves.
|
||||
|
||||
=cut
|
||||
|
||||
|
|
@ -115,6 +116,7 @@ sub addGroups {
|
|||
$self->session->stow->delete("isInGroup");
|
||||
foreach my $gid (@{$groups}) {
|
||||
next if ($gid eq '1');
|
||||
next if ($gid eq $self->getId);
|
||||
my ($isIn) = $self->session->db->quickArray("select count(*) from groupGroupings where groupId=? and inGroup=?", [$gid, $self->getId]);
|
||||
my $group = WebGUI::Group->new($self->session, $gid);
|
||||
my $recursive = isIn($self->getId, @{$group->getGroupsIn(1)});
|
||||
|
|
@ -122,6 +124,7 @@ sub addGroups {
|
|||
$self->session->db->write("insert into groupGroupings (groupId,inGroup) values (?,?)",[$gid, $self->getId]);
|
||||
}
|
||||
}
|
||||
$self->session->stow->delete("gotGroupsInGroup");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -501,7 +504,8 @@ sub getGroupsIn {
|
|||
my $gotGroupsInGroup = $self->session->stow->get("gotGroupsInGroup");
|
||||
if ($isRecursive && exists($gotGroupsInGroup->{recursive}{$self->getId})) {
|
||||
return $gotGroupsInGroup->{recursive}{$self->getId};
|
||||
} elsif (exists $gotGroupsInGroup->{direct}{$self->getId}) {
|
||||
}
|
||||
elsif (!$isRecursive and exists($gotGroupsInGroup->{direct}{$self->getId})) {
|
||||
return $gotGroupsInGroup->{direct}{$self->getId};
|
||||
}
|
||||
my $groups = $self->session->db->buildArrayRef("select groupId from groupGroupings where inGroup=?",[$self->getId]);
|
||||
|
|
@ -518,6 +522,7 @@ sub getGroupsIn {
|
|||
my %unique = map { $_ => 1 } @groupsOfGroups;
|
||||
@groupsOfGroups = keys %unique;
|
||||
$gotGroupsInGroup->{recursive}{$self->getId} = \@groupsOfGroups;
|
||||
$self->session->stow->set("gotGroupsInGroup", $gotGroupsInGroup);
|
||||
return \@groupsOfGroups;
|
||||
}
|
||||
$gotGroupsInGroup->{direct}{$self->getId} = $groups;
|
||||
|
|
|
|||
22
t/Group.t
22
t/Group.t
|
|
@ -80,28 +80,28 @@ ok( ($gA->name eq 'Group A' and $gB->name eq 'Group B'), 'object name assignment
|
|||
|
||||
$gB->addGroups([$gA->getId]);
|
||||
|
||||
cmp_bag([$gA->getId, 3], $gB->getGroupsIn(1) ,'Group A is in Group B, recursively');
|
||||
cmp_bag([$gA->getId, 3], $gB->getGroupsIn() ,'Group A is in Group B');
|
||||
cmp_bag([$gB->getId], $gA->getGroupsFor() ,'Group B contains Group A');
|
||||
cmp_bag([3], $gA->getGroupsIn() ,'Admin added to group A automatically');
|
||||
cmp_bag($gB->getGroupsIn(1), [$gA->getId, 3], 'Group A is in Group B, recursively');
|
||||
cmp_bag($gB->getGroupsIn(), [$gA->getId, 3], 'Group A is in Group B');
|
||||
cmp_bag($gA->getGroupsFor(), [$gB->getId], 'Group B contains Group A');
|
||||
cmp_bag($gA->getGroupsIn(), [3], 'Admin added to group A automatically');
|
||||
|
||||
$gA->addGroups([$gB->getId]);
|
||||
cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to create recursive group loops');
|
||||
cmp_bag($gA->getGroupsIn(), [3], 'Not allowed to create recursive group loops');
|
||||
|
||||
$gA->addGroups([1]);
|
||||
cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to add group Visitor to a group');
|
||||
cmp_bag($gA->getGroupsIn(), [3], 'Not allowed to add group Visitor to a group');
|
||||
|
||||
$gA->addGroups([$gA->getId]);
|
||||
cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to add myself to my group');
|
||||
cmp_bag($gA->getGroupsIn(), [3], 'Not allowed to add myself to my group');
|
||||
|
||||
my $gC = WebGUI::Group->new($session, "new");
|
||||
$gC->name('Group C');
|
||||
$gA->addGroups([$gC->getId]);
|
||||
|
||||
cmp_bag([$gA->getId], $gC->getGroupsFor() ,'Group A contains Group C');
|
||||
cmp_bag([3], $gA->getGroupsIn() ,'Group C is a member of Group A, cached');
|
||||
cmp_bag([$gA->getId, 3], $gB->getGroupsIn() ,'Group A is in Group B, cached result');
|
||||
cmp_bag([$gA->getId, 3], $gB->getGroupsIn(1) ,'Group C is in Group B, recursively, cached result');
|
||||
cmp_bag($gC->getGroupsFor(), [$gA->getId], 'Group A contains Group C');
|
||||
cmp_bag($gA->getGroupsIn(), [$gC->getId, 3], 'Group C is a member of Group A, cached');
|
||||
cmp_bag($gB->getGroupsIn(1), [$gC->getId, $gA->getId, 3], 'Group C is in Group B, recursively, cached result');
|
||||
cmp_bag($gB->getGroupsIn(), [$gA->getId, 3], 'Group A is in Group B, cached result');
|
||||
|
||||
END {
|
||||
(defined $gA and ref $gA eq 'WebGUI::Group') and $gA->delete;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue