From c802e4ddada649aa08c415dc2be7fbd4992c0650 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Mon, 3 Apr 2006 18:07:09 +0000 Subject: [PATCH] Add tests for database defined groups to Group.t Clear cache on set of databaseLinkId or dbQuery in WebGUI::Group.pm. Fix typos, missing module use lines and extra variables in WebGUI::User.pm. --- lib/WebGUI/Group.pm | 8 ++++++-- lib/WebGUI/User.pm | 5 +++-- t/Group.t | 21 +++++++++++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/WebGUI/Group.pm b/lib/WebGUI/Group.pm index 5b5b87871..e3f369742 100755 --- a/lib/WebGUI/Group.pm +++ b/lib/WebGUI/Group.pm @@ -714,7 +714,7 @@ If you specified "new" for groupId, you can use this property to specify an id y =cut sub new { - my ($class, $groupId, %default, $value, $key, %group, %profile); + my ($class, %group); tie %group, 'Tie::CPHash'; $class = shift; my $self = {}; @@ -787,6 +787,8 @@ sub dbQuery { my $value = shift; if (defined $value) { $self->set("dbQuery",$value); + $self->session->stow->delete("gotGroupsInGroup"); + $self->session->stow->delete("isInGroup"); } return $self->get("dbQuery"); } @@ -799,7 +801,7 @@ Returns the databaseLinkId for this group. =head3 value -If specified, the databaseLinkId is set to this value. +If specified, the databaseLinkId is set to this value and in-memory cached user and group data is cleared. =cut @@ -808,6 +810,8 @@ sub databaseLinkId { my $value = shift; if (defined $value) { $self->set("databaseLinkId",$value); + $self->session->stow->delete("gotGroupsInGroup"); + $self->session->stow->delete("isInGroup"); } return $self->get("databaseLinkId"); } diff --git a/lib/WebGUI/User.pm b/lib/WebGUI/User.pm index 1987e2ce4..447527cb0 100644 --- a/lib/WebGUI/User.pm +++ b/lib/WebGUI/User.pm @@ -17,6 +17,7 @@ package WebGUI::User; use strict; use WebGUI::Cache; use WebGUI::Group; +use WebGUI::DatabaseLink; =head1 NAME @@ -309,11 +310,11 @@ sub isInGroup { } } ### Check external database - if ($group->get("dbQuery") && $group->get("databaseLinkId")) { + if ($group->get("dbQuery") && defined $group->get("databaseLinkId")) { # skip if not logged in and query contains a User macro unless ($group->get("dbQuery") =~ /\^User/i && $uid eq '1') { my $dbLink = WebGUI::DatabaseLink->new($self->session,$group->get("databaseLinkId")); - my $dbh = $dbLink->dbh; + my $dbh = $dbLink->db; if (defined $dbh) { if ($group->get("dbQuery") =~ /select 1/i) { my $query = $group->get("dbQuery"); diff --git a/t/Group.t b/t/Group.t index 20eccbc3b..fdd6d3720 100644 --- a/t/Group.t +++ b/t/Group.t @@ -18,7 +18,7 @@ use WebGUI::Utility; use WebGUI::User; use WebGUI::Group; -use Test::More tests => 68; # increment this value for each test you create +use Test::More tests => 78; # increment this value for each test you create use Test::Deep; my $session = WebGUI::Test->session; @@ -161,7 +161,7 @@ ok($gX->userIsAdmin($user->userId), "userIsAdmin: Dude set to be group admin for sleep 5; $expireTime = time() + $expireOffset - $gX->userGroupExpireDate($user->userId) ; -ok( ($expireTime < 6 && $expireTime > 0), 'userGroupExpireDate: Default expire time ages'); +ok( ($expireTime < 7 && $expireTime > 0), 'userGroupExpireDate: Default expire time ages'); $gX->addUsers([$user->userId]); my $expireTime = abs($gX->userGroupExpireDate($user->userId) - $expireOffset - time()); @@ -216,10 +216,27 @@ foreach my $mob (@mob) { $sth->execute([ $mob->userId ]); } +ok( !$mob[0]->isInGroup($gY->getId), 'mob[0] is not in group Z'); + my $mobUsers = $session->db->buildArrayRef('select userId from myUserTable'); cmp_bag($mobUsers, [map {$_->userId} @mob], 'verify SQL table built correctly'); +is( $gY->databaseLinkId, 0, "Group Y's databaseLinkId is set to WebGUI"); +$gY->dbQuery(q!select 1 from myUserTable where userId='^#();'!); +is( $session->stow->get('isInGroup'), undef, 'setting dbQuery clears cached isInGroup'); + +$session->user({userId => $mob[0]->userId}); +is( $session->user->userId, $mob[0]->userId, 'mob[0] set to be current user'); +is( $session->stow->get('isInGroup'), undef, 'isInGroup cache still cleared'); +is( $gY->session->stow->get('isInGroup'), undef, 'group Y copy of isInGroup cache cleared, too'); +is( $mob[0]->session->stow->get('isInGroup'), undef, 'mob[0] copy of isInGroup cache cleared, too'); +$session->errorHandler->warn('checking mob[0] group membership'); +is( $mob[0]->isInGroup($gY->getId), 1, 'mob[0] is in group Y after setting dbQuery'); +$session->user({userId => $mob[1]->userId}); +is( $mob[0]->isInGroup($gZ->getId), 1, 'mob[0] is not in group Z'); +is( $mob[0]->isInGroup($gZ->getId,1), 1, 'mob[0] is in group Z'); + END { (defined $gX and ref $gX eq 'WebGUI::Group') and $gX->delete; (defined $gY and ref $gY eq 'WebGUI::Group') and $gY->delete;