Add method to clean up test sessions. This has to be done

after ALL other types of objects have been cleaned up.
More test clean-up for users, groups and sessions.
This commit is contained in:
Colin Kuskie 2009-05-01 02:02:12 +00:00
parent d7f2aca1e4
commit 004b8095a1
5 changed files with 47 additions and 33 deletions

View file

@ -77,12 +77,10 @@ sub addUser {
##so the test will not fail in the summer
$user->profileField("timeZone","America/Hermosillo");
$user->username("Time Zone");
WebGUI::Test->usersToDelete($user);
return $user;
}
END { ##Clean-up after yourself, always
foreach my $dude ($timeZoneUser) {
$dude->delete if (defined $dude and ref $dude eq 'WebGUI::User');
}
}

View file

@ -307,6 +307,7 @@ cmp_bag($gY->getAllGroupsFor(), [ map {$_->getId} ($gZ, $gA, $gB) ], 'getAllGrou
cmp_bag($gZ->getAllGroupsFor(), [ map {$_->getId} ($gA, $gB) ], 'getAllGroupsFor Z');
my $user = WebGUI::User->new($session, "new");
WebGUI::Test->usersToDelete($user);
$gX->userIsAdmin($user->userId, "yes");
ok(!$gX->userIsAdmin($user->userId), "userIsAdmin: User who isn't secondary admin can't be group admin");
isnt($gX->userIsAdmin($user->userId), 'yes', "userIsAdmin returns 1 or 0, not value");
@ -365,11 +366,13 @@ $user->delete;
################################################################
my @crowd = map { WebGUI::User->new($session, "new") } 0..7;
WebGUI::Test->usersToDelete(@crowd);
my @mob;
foreach my $idx (0..2) {
$mob[$idx] = WebGUI::User->new($session, "new");
$mob[$idx]->username("mob$idx");
}
WebGUI::Test->usersToDelete(@mob);
my @bUsers = map { $_->userId } @crowd[0,1];
my @aUsers = map { $_->userId } @crowd[2,3];
@ -459,6 +462,7 @@ foreach my $idx (0..3) {
$chameleons[$idx] = WebGUI::User->new($session, "new");
$chameleons[$idx]->username("chameleon$idx");
}
WebGUI::Test->usersToDelete(@chameleons);
foreach my $idx (0..3) {
$chameleons[$idx]->karma(5*$idx, 'testCode', 'testable karma, dude');
@ -540,6 +544,8 @@ foreach my $idx (0..$#scratchTests) {
$sessionBank[$idx]->scratch->set($name, $value);
}
}
WebGUI::Test->usersToDelete(@itchies);
WebGUI::Test->sessionsToDelete(@sessionBank);
foreach my $scratchTest (@scratchTests) {
is($scratchTest->{user}->isInGroup($gS->getId), $scratchTest->{expect}, $scratchTest->{comment});
@ -557,10 +563,6 @@ cmp_bag(
'getAllUsers for group with scratch'
);
foreach my $subSession (@sessionBank) {
$subSession->db->write("DELETE FROM userSessionScratch WHERE sessionId=?",[ $subSession->getId]);
}
@sessionBank = ();
my @tcps = ();
@ -582,6 +584,8 @@ foreach my $idx (0..$#ipTests) {
##Assign this user to this test to be fetched later
$ipTests[$idx]->{user} = $tcps[$idx];
}
WebGUI::Test->usersToDelete(@tcps);
WebGUI::Test->sessionsToDelete(@sessionBank);
my $gI = WebGUI::Group->new($session, "new");
WebGUI::Test->groupsToDelete($gI);
@ -607,6 +611,7 @@ foreach my $ipTest (@ipTests) {
##Cache check.
my $cacheDude = WebGUI::User->new($session, "new");
WebGUI::Test->usersToDelete($cacheDude);
$cacheDude->username('Cache Dude');
$gY->addUsers([$cacheDude->userId]);
@ -656,17 +661,6 @@ SKIP: {
################################################################
END {
foreach my $dude ($user, @crowd, @mob, @chameleons, @itchies, @tcps, $cacheDude) {
$dude->delete if (defined $dude and ref $dude eq 'WebGUI::User');
}
$session->db->dbh->do('DROP TABLE IF EXISTS myUserTable');
foreach my $subSession (@sessionBank) {
$subSession->db->write("DELETE FROM userSession WHERE sessionId=?",[ $subSession->getId]);
$subSession->db->write("DELETE FROM userSessionScratch WHERE sessionId=?",[ $subSession->getId]);
$subSession->scratch->deleteAll;
$subSession->close() if (defined $subSession and ref $subSession eq 'WebGUI::Session');
}
$testCache->flush;
}

View file

@ -93,16 +93,19 @@ is ($dt->getTimeZone(), 'America/Chicago', 'getTimeZone: fetching cached version
my $buster = WebGUI::User->new($session, "new");
$buster->profileField('timeZone', 'Amerigo/Vespucci');
$session->user({user => $buster});
WebGUI::Test->usersToDelete($buster);
is ($dt->getTimeZone(), 'America/Chicago', 'getTimeZone: time zones not in the approved list get reset to the default');
my $dude = WebGUI::User->new($session, "new");
$dude->profileField('timeZone', 'Australia/Perth');
$session->user({user => $dude});
WebGUI::Test->usersToDelete($dude);
is ($dt->getTimeZone(), 'Australia/Perth', 'getTimeZone: valid time zones are allowed');
my $bud = WebGUI::User->new($session, "new");
$bud->profileField('timeZone', '');
$session->user({user => $bud});
WebGUI::Test->usersToDelete($bud);
is ($dt->getTimeZone(), 'America/Chicago', q|getTimeZone: if user's time zone doesn't exist, then return America/Chicago|);
$session->user({userId => 1}); ##back to Visitor
@ -284,7 +287,4 @@ sub installBadLocale {
END {
unlink File::Spec->catfile(WebGUI::Test->lib, qw/WebGUI i18n BadLocale.pm/);
foreach my $account ($buster, $dude, $bud) {
(defined $account and ref $account eq 'WebGUI::User') and $account->delete;
}
}

View file

@ -20,7 +20,7 @@ use WebGUI::Cache;
use WebGUI::User;
use WebGUI::ProfileField;
use Test::More tests => 196; # increment this value for each test you create
use Test::More tests => 194; # increment this value for each test you create
use Test::Deep;
use Data::Dumper;
@ -29,15 +29,13 @@ my $session = WebGUI::Test->session;
my $testCache = WebGUI::Cache->new($session, 'myTestKey');
$testCache->flush;
my $numberOfUsers = $session->db->quickScalar('select count(*) from users');
my $numberOfGroups = $session->db->quickScalar('select count(*) from groups');
my $user;
my $lastUpdate;
#Let's try to create a new user and make sure we get an object back
my $userCreationTime = time();
ok(defined ($user = WebGUI::User->new($session,"new")), 'new("new") -- object reference is defined');
WebGUI::Test->usersToDelete($user);
#New does not return undef if something breaks, so we'll see if the _profile hash was set.
ok(exists $user->{_profile}, 'new("new") -- profile subhash exists');
@ -250,6 +248,7 @@ $cm->ipFilter(defined $origFilter ? $origFilter : '');
##Test for group membership
$user = WebGUI::User->new($session, "new");
WebGUI::Test->usersToDelete($user);
ok($user->isInGroup(7), "addToGroups: New user is in group 7(Everyone)");
ok(!$user->isInGroup(1), "New user not in group 1 (Visitors)");
ok($user->isRegistered, "User is not a visitor");
@ -304,6 +303,7 @@ ok($visitor->isInGroup(7), "Visitor added back to group Everyone");
################################################################
my $dude = WebGUI::User->new($session, "new");
WebGUI::Test->usersToDelete($dude);
ok(!$dude->canUseAdminMode, 'canUseAdminMode: newly created users cannot');
@ -402,6 +402,7 @@ is($useru->userId, $dude->userId, '... and it is the right user object');
################################################################
my $buster = WebGUI::User->new($session, "new");
WebGUI::Test->usersToDelete($buster);
is( $buster->profileField('timeZone'), 'America/Chicago', 'buster received original user profile on user creation');
my $profileField = WebGUI::ProfileField->new($session, 'timeZone');
@ -430,6 +431,7 @@ $copiedAliasProfile{'dataDefault'} = "'aliasAlias'"; ##Non word characters;
$aliasProfile->set(\%copiedAliasProfile);
my $buster3 = WebGUI::User->new($session, $buster->userId);
WebGUI::Test->usersToDelete($buster);
is($buster3->profileField('alias'), 'aliasAlias', 'default alias set');
$copiedAliasProfile{'dataDefault'} = "'....^^^^....'"; ##Non word characters;
@ -476,6 +478,7 @@ is($session->scratch->get('hack'), undef, 'userProfile dataDefault is not execut
##Set up a group that has expired.
my $expiredGroup = WebGUI::Group->new($session, 'new');
WebGUI::Test->groupsToDelete($expiredGroup);
$expiredGroup->name('Group that expires users automatically');
$expiredGroup->expireOffset(-1000);
@ -504,6 +507,7 @@ cmp_bag($dude->getGroups(1), [2, 7], 'Accessing the cached list of groups does n
################################################################
my $friend = WebGUI::User->new($session, 'new');
WebGUI::Test->usersToDelete($friend);
is($friend->getFirstName, undef, 'with no profile settings, getFirstName returns undef');
$friend->username('friend');
@ -520,6 +524,7 @@ is($friend->getFirstName, 'Mr', 'firstName is the highest priority profile setti
################################################################
my $neighbor = WebGUI::User->new($session, 'new');
WebGUI::Test->usersToDelete($neighbor);
is($neighbor->getWholeName, undef, 'with no profile settings, getWholeName returns undef');
$neighbor->username('neighbor');
@ -611,6 +616,7 @@ foreach my $groupName (qw/red pink orange blue turquoise lightBlue purple/) {
$groupSet{$groupName} = WebGUI::Group->new($session, 'new');
$groupSet{$groupName}->name($groupName);
}
WebGUI::Test->groupsToDelete(values %groupSet);
$groupSet{blue}->expireOffset(-1500);
@ -621,6 +627,7 @@ $groupSet{pink}->addGroups( [ map { $groupSet{$_}->getId } qw/red/ ] );
$groupSet{orange}->addGroups( [ map { $groupSet{$_}->getId } qw/red/ ] );
my $newFish = WebGUI::User->new($session, 'new');
WebGUI::Test->usersToDelete($newFish);
$newFish->addToGroups([ $groupSet{red}->getId, $groupSet{blue}->getId ]);
cmp_bag(
@ -654,6 +661,7 @@ SKIP: {
ok( my $newCreateUser = WebGUI::User->create( $session ),
'create() returns something'
);
WebGUI::Test->usersToDelete($newCreateUser);
isa_ok( $newCreateUser, 'WebGUI::User', 'create() returns a WebGUI::User' );
################################################################
@ -797,10 +805,6 @@ ok(! $neighbor->canViewField('email', $buster), "... returns 0 when the field's
$friend->deleteFromGroups([$neighbor->friends->getId]);
END {
foreach my $account ($user, $dude, $buster, $buster3, $neighbor, $friend, $newFish, $newCreateUser) {
(defined $account and ref $account eq 'WebGUI::User') and $account->delete;
}
foreach my $testGroup ($expiredGroup, values %groupSet) {
if (defined $testGroup and ref $testGroup eq 'WebGUI::Group') {
$testGroup->delete;
@ -819,9 +823,5 @@ END {
$newProfileField->delete() if $newProfileField;
$testCache->flush;
my $newNumberOfUsers = $session->db->quickScalar('select count(*) from users');
my $newNumberOfGroups = $session->db->quickScalar('select count(*) from groups');
is ($newNumberOfUsers, $numberOfUsers, 'no new additional users were leaked by this test');
is ($newNumberOfGroups, $numberOfGroups, 'no new additional groups were leaked by this test');
}

View file

@ -54,6 +54,7 @@ my $originalSetting;
my @groupsToDelete;
my @usersToDelete;
my @sessionsToDelete;
my @storagesToDelete;
BEGIN {
@ -157,6 +158,10 @@ END {
$stor->delete;
}
}
SESSION: foreach my $session (@sessionsToDelete) {
$session->var->end;
$session->close;
}
if ($ENV{WEBGUI_TEST_DEBUG}) {
$Test->diag('Sessions: '.$SESSION->db->quickScalar('select count(*) from userSession'));
$Test->diag('Scratch : '.$SESSION->db->quickScalar('select count(*) from userSessionScratch'));
@ -437,6 +442,23 @@ sub storagesToDelete {
#----------------------------------------------------------------------------
=head2 sessionsToDelete ( $session, [$session, ...] )
Push a list of session objects onto the stack of groups to be automatically deleted
at the end of the test. Note, this will be the last group of objects to be
cleaned up.
This is a class method.
=cut
sub sessionsToDelete {
my $class = shift;
push @sessionsToDelete, @_;
}
#----------------------------------------------------------------------------
=head2 usersToDelete ( $user, [$user, ...] )
Push a list of user objects onto the stack of groups to be automatically deleted