From 248f77f3a9bc7af08d10634580b9e8aba4bb4874 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 13 Mar 2006 04:43:05 +0000 Subject: [PATCH] Added more test in preparation for changing Group SQL query work. Update documentation in User.pm and Group.pm. Refactor Group.pm to use placholders. Fix typo in getGroupsIn where wrong key in hash was accessed. --- lib/WebGUI/Group.pm | 25 +++++++++++++------------ lib/WebGUI/User.pm | 5 +++-- t/Group.t | 31 ++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/lib/WebGUI/Group.pm b/lib/WebGUI/Group.pm index 6618591ba..ea623d060 100755 --- a/lib/WebGUI/Group.pm +++ b/lib/WebGUI/Group.pm @@ -56,18 +56,14 @@ This package provides an object-oriented way of managing WebGUI groups and group $g->addGroups(\@arr); - $g->addUsers(\@arr); + $g->addUsers(\@arr, $expireOffset); $g->deleteGroups(\@arr); $g->deleteUsers(\@arr); $g->delete; - $group->addGroups(\@groups, \@toGroups); - $group->addUsers(\@users, \@toGroups); - $group->deleteGroups(\@groups, \@fromGroups); - $group->deleteUsers(\@users, \@fromGroups); - $arrayRef = $group->getGroupsFor($groupId); + $arrayRef = $group->getGroupsFor(); $arrayRef = $self->session->user->getGroups($userId); - $arrayRef = $group->getGroupsIn($groupId); + $arrayRef = $group->getGroupsIn($recursive); $arrayRef = $group->getUsers($groupId); $boolean = $self->session->user->isInGroup($groupId, $userId); $boolean = $group->userIsAdmin($userId,$groupId); @@ -119,10 +115,10 @@ sub addGroups { $self->session->stow->delete("isInGroup"); foreach my $gid (@{$groups}) { next if ($gid eq '1'); - my ($isIn) = $self->session->db->quickArray("select count(*) from groupGroupings where groupId=".$self->session->db->quote($gid)." and inGroup=".$self->session->db->quote($self->getId)); + my ($isIn) = $self->session->db->quickArray("select count(*) from groupGroupings where groupId=? and inGroup=?", [$gid, $self->getId]); my $recursive = isIn($self->getId, @{$self->getGroupsIn($gid,1)}); unless ($isIn || $recursive) { - $self->session->db->write("insert into groupGroupings (groupId,inGroup) values (".$self->session->db->quote($gid).",".$self->session->db->quote($self->getId).")"); + $self->session->db->write("insert into groupGroupings (groupId,inGroup) values (?,?)",[$gid, $self->getId]); } } } @@ -474,13 +470,13 @@ Returns an array reference containing a list of groups this group is in. sub getGroupsFor { my $self = shift; - return $self->session->db->buildArrayRef("select inGroup from groupGroupings where groupId=".$self->session->db->quote($self->getId)); + return $self->session->db->buildArrayRef("select inGroup from groupGroupings where groupId=?",[$self->getId]); } #------------------------------------------------------------------- -=head2 getGroupsIn ( [ recursive ] ) +=head2 getGroupsIn ( [ recursive , loopCount ] ) Returns an array reference containing a list of groups that belong to this group. @@ -488,6 +484,11 @@ Returns an array reference containing a list of groups that belong to this group A boolean value to determine whether the method should return the groups directly in the group, or to follow the entire groups of groups hierarchy. Defaults to "0". +=head3 loopCount + +If recursive is set, how many times this subroutine should recurse before it +determines that it is in an infinite loop. The loop in incremented by 1. + =cut @@ -498,7 +499,7 @@ sub getGroupsIn { my $gotGroupsInGroup = $self->session->stow->get("gotGroupsInGroup"); if ($isRecursive && exists $gotGroupsInGroup->{recursive}{$self->getId}) { return $gotGroupsInGroup->{recursive}{$self->getId}; - } elsif (exists $gotGroupsInGroup->{recursive}{$self->getId}) { + } elsif (exists $gotGroupsInGroup->{direct}{$self->getId}) { return $gotGroupsInGroup->{direct}{$self->getId}; } my $groups = $self->session->db->buildArrayRef("select groupId from groupGroupings where inGroup=".$self->session->db->quote($self->getId)); diff --git a/lib/WebGUI/User.pm b/lib/WebGUI/User.pm index 1309a3ce0..16013b661 100644 --- a/lib/WebGUI/User.pm +++ b/lib/WebGUI/User.pm @@ -38,8 +38,9 @@ This package provides an object-oriented way of managing WebGUI users as well as $languagePreference = $u->profileField("language",1); $referringAffiliate = $u->referringAffiliate; $status = $u->status("somestatus"); - $username = $u->username("jonboy"); - $arrayRef = $u->getGroups; + $username = $u->username("jonboy"); + $arrayRef = $u->getGroups; + $member = $u->isInGroup($groupId); $u->addToGroups(\@arr); $u->deleteFromGroups(\@arr); diff --git a/t/Group.t b/t/Group.t index ec9f661b3..d89bbc077 100644 --- a/t/Group.t +++ b/t/Group.t @@ -18,7 +18,8 @@ use WebGUI::Utility; use WebGUI::User; use WebGUI::Group; -use Test::More tests => 27; # increment this value for each test you create +use Test::More tests => 33; # increment this value for each test you create +use Test::Deep; my $session = WebGUI::Test->session; @@ -62,12 +63,36 @@ undef $g2; delete $g->{_group}; ok( !exists $g->{_group}, 'deleted group property hash'); -is( $g->name, $gname, 'group name restored after ->get'); +is( $g->name, $gname, 'group name restored after ->get through ->name'); ok( exists $g->{_group}, 'group property hash restored'); $g->delete(); -my $matchingGroups = $session->db->quickArray("select groupId from groups where groupId=".$session->db->quote($gid)); +my $matchingGroups = $session->db->quickArray("select groupId from groups where groupId=?",[$gid]); is ( $matchingGroups, 0, 'group was removed'); +my $gA = WebGUI::Group->new($session, "new"); +my $gB = WebGUI::Group->new($session, "new"); +$gA->name('Group A'); +$gB->name('Group B'); +ok( ($gA->name eq 'Group A' and $gB->name eq 'Group B'), 'object name assignment, multiple objects'); + +$gB->addGroups([$gA->getId]); + +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'); + +$gA->addGroups([$gB->getId]); +cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to create recursive group loops'); + +$gA->addGroups([1]); +cmp_bag([3], $gA->getGroupsIn() ,'Not allowed to add group Visitor to a group'); + +END { + (defined $gA and ref $gA eq 'WebGUI::Group') and $gA->delete; + (defined $gB and ref $gB eq 'WebGUI::Group') and $gB->delete; + (defined $g2 and ref $g2 eq 'WebGUI::Group') and $g2->delete; + (defined $g and ref $g eq 'WebGUI::Group') and $g->delete; +}