From 7100d1df41901a301e735986eb68ac452a208c64 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 26 May 2006 16:43:47 +0000 Subject: [PATCH] Add a new method called getAllGroupsFor, which recursively returns ALL groups that a group belongs to. This is required to properly uncache group settings for groups with more than 2 levels of hierarchy. Add 7 tests to verify the working of the new method. Fix a typo in the POD for Operation/Group.pm --- lib/WebGUI/Group.pm | 23 +++++++++++++++++++++-- lib/WebGUI/Operation/Group.pm | 2 +- t/Group.t | 10 +++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/WebGUI/Group.pm b/lib/WebGUI/Group.pm index 9731c1d67..1fb982bcd 100755 --- a/lib/WebGUI/Group.pm +++ b/lib/WebGUI/Group.pm @@ -211,14 +211,14 @@ sub autoDelete { =head2 clearCaches ( ) -Clears caches for this group. +Clears all caches for this group and any ancestor groups of the group. =cut sub clearCaches { my $self = shift; ##Clear my cache and the cache of all groups above me. - my $groups = $self->getGroupsFor(); + my $groups = $self->getAllGroupsFor(); foreach my $group ( $self->getId, @{ $groups } ) { WebGUI::Cache->new($self->session, $group)->delete; } @@ -615,6 +615,25 @@ sub get { +#------------------------------------------------------------------- + +=head2 getAllGroupsFor ( ) + +Returns an array reference containing a list of all groups this group is in, recursively. + +=cut + +sub getAllGroupsFor { + my $self = shift; + my $groups = $self->getGroupsFor(); + foreach my $gid (@{ $groups }) { + push @{ $groups }, @{ WebGUI::Group->new($self->session, $gid)->getAllGroupsFor() }; + } + my %unique = map { $_ => 1 } @{ $groups }; + $groups = [ keys %unique ]; + return $groups; +} + #------------------------------------------------------------------- =head2 getGroupsFor ( ) diff --git a/lib/WebGUI/Operation/Group.pm b/lib/WebGUI/Operation/Group.pm index fc38c19da..59fc9d88c 100644 --- a/lib/WebGUI/Operation/Group.pm +++ b/lib/WebGUI/Operation/Group.pm @@ -238,7 +238,7 @@ sub www_deleteGroupGrouping { Deletes a set of users from a set of groups. Only Admins may perform this function. The user and group lists are expected to be found in form fields names uid and gid, respectively. Visitors are not allowed to -perform this operation, and the +perform this operation. =cut diff --git a/t/Group.t b/t/Group.t index fd34467b0..de2d12713 100644 --- a/t/Group.t +++ b/t/Group.t @@ -75,7 +75,7 @@ my @ipTests = ( ); -plan tests => (96 + scalar(@scratchTests) + scalar(@ipTests)); # increment this value for each test you create +plan tests => (103 + scalar(@scratchTests) + scalar(@ipTests)); # increment this value for each test you create my $session = WebGUI::Test->session; my $testCache = WebGUI::Cache->new($session, 'myTestKey'); @@ -198,6 +198,10 @@ cmp_bag($gB->getGroupsIn(1), [$gA->getId, $gC->getId, $gZ->getId, $gY->getId, $g $gX->addGroups([$gA->getId]); cmp_bag($gX->getGroupsIn(), [3], 'Not able to add B tree under Z tree under X'); +cmp_bag($gX->getAllGroupsFor(), [ map {$_->getId} ($gZ, $gA, $gB) ], 'getAllGroupsFor X'); +cmp_bag($gY->getAllGroupsFor(), [ map {$_->getId} ($gZ, $gA, $gB) ], 'getAllGroupsFor Y'); +cmp_bag($gZ->getAllGroupsFor(), [ map {$_->getId} ($gA, $gB) ], 'getAllGroupsFor Z'); + 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"); @@ -493,11 +497,15 @@ $gY->addGroups([$gCache->getId]); ok( $cacheDude->isInGroup($gY->getId), "Cache dude is a member of group Y by group membership"); ok( $cacheDude->isInGroup($gZ->getId), "Cache dude is a member of group Z by group membership"); +ok( $cacheDude->isInGroup($gA->getId), "Cache dude is a member of group A by group membership"); +ok( $cacheDude->isInGroup($gB->getId), "Cache dude is a member of group B by group membership"); $gY->deleteGroups([$gCache->getId]); ok( !$cacheDude->isInGroup($gY->getId), "Cache dude is not a member of group Y"); ok( !$cacheDude->isInGroup($gZ->getId), "Cache dude is not a member of group Z"); +ok( !$cacheDude->isInGroup($gA->getId), "Cache dude is not a member of group A"); +ok( !$cacheDude->isInGroup($gB->getId), "Cache dude is not a member of group B"); SKIP: { skip("need to test expiration date in groupings interacting with recursive or not", 1);