Group.t: group karma tests, better clean-up at end

User.t: better clean-up at end
Scratch.t: no newlines in test output
Session/Scratch.pm: convert to use placeholders
Ad.pm: Add missing line of POD.
Group.pm: add method to get all users in a group by karma.
User.pm: isInGroup to use getKarmaUsers.
This commit is contained in:
Colin Kuskie 2006-04-06 16:52:34 +00:00
parent fd5be2ce2d
commit 3e179fa4ad
7 changed files with 120 additions and 47 deletions

View file

@ -18,7 +18,7 @@ use WebGUI::Utility;
use WebGUI::User;
use WebGUI::Group;
use Test::More tests => 78; # increment this value for each test you create
use Test::More tests => 82; # increment this value for each test you create
use Test::Deep;
my $session = WebGUI::Test->session;
@ -186,7 +186,11 @@ $user->delete;
##Build a group of users and add them to various groups to test fetching users
my @crowd = map { WebGUI::User->new($session, "new") } 0..7;
my @mob = map { WebGUI::User->new($session, "new") } 0..2;
my @mob;
foreach my $idx (0..2) {
$mob[$idx] = WebGUI::User->new($session, "new");
$mob[$idx]->username("mob$idx");
}
my @bUsers = map { $_->userId } @crowd[0,1];
my @aUsers = map { $_->userId } @crowd[2,3];
@ -208,6 +212,8 @@ cmp_bag($gA->getUsers(1), [@aUsers, @zUsers, 3], 'users in group A, recursively'
cmp_bag($gC->getUsers(1), [@cUsers, 3], 'users in group C, recursively');
cmp_bag($gZ->getUsers(1), [@zUsers, 3], 'users in group Z, recursively');
##Database based user membership in groups
$session->db->dbh->do('DROP TABLE IF EXISTS myUserTable');
$session->db->dbh->do(q!CREATE TABLE myUserTable (userId varchar(22) binary NOT NULL default '', PRIMARY KEY(userId)) TYPE=InnoDB!);
@ -226,35 +232,80 @@ is( $gY->databaseLinkId, 0, "Group Y's databaseLinkId is set to WebGUI");
$gY->dbQuery(q!select userId from myUserTable!);
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');
$session->errorHandler->warn('checking mob[0] group membership');
my @mobIds = map { $_->userId } @mob;
cmp_bag(
$gY->getDatabaseUsers(),
\@mobIds,
'all mob users in list of group Y users from database'
);
is( $mob[0]->isInGroup($gY->getId), 1, 'mob[0] is in group Y after setting dbQuery');
is( $mob[0]->isInGroup($gZ->getId), 1, 'mob[0] isInGroup Z');
ok( isIn($mob[0]->userId, @{ $gY->getUsers() }), 'mob[0] in list of group Y users');
ok( !isIn($mob[0]->userId, @{ $gZ->getUsers() }), 'mob[0] not in list of group Z users');
$session->errorHandler->warn('getUsers checks');
$session->errorHandler->warn('mob[0] userId = '. $mob[0]->userId);
ok( isIn($mob[0]->userId, @{ $gZ->getUsers(1) }), 'mob[0] in list of group Z users, recursively');
my $gK = WebGUI::Group->new($session, "new");
$gK->name('Group K');
$gC->addGroups([$gK->getId]);
$gK->karmaThreshold(5);
my @chameleons = ();
foreach my $idx (0..3) {
$chameleons[$idx] = WebGUI::User->new($session, "new");
$chameleons[$idx]->username("chameleon$idx");
}
foreach my $idx (0..3) {
$chameleons[$idx]->karma(5*$idx, 'testCode', 'testable karma, dude');
}
is_deeply(
[ (map { $_->karma() } @chameleons) ],
[0, 5, 10, 15],
'karma level checks'
);
my $defaultKarmaSetting = $session->setting->get('useKarma');
$session->setting->set('useKarma', 0) if $defaultKarmaSetting;
is_deeply(
[ (map { $_->isInGroup($gK->getId) } @chameleons) ],
[0, 0, 0, 0],
'karma disabled in settings, no users in group'
);
$session->setting->set('useKarma', 1);
$session->stow->delete('isInGroup'); ##Clear cache since previous data is wrong
is_deeply(
[ (map { $_->isInGroup($gK->getId) } @chameleons) ],
[0, 1, 1, 1],
'chameleons 1, 2 and 3 are in group K via karma threshold'
);
is_deeply(
$gK->getKarmaUsers,
[ (map { $_->userId() } @chameleons[1..3]) ],
'chameleons 1, 2 and 3 are group K karma users'
);
$session->setting->set('useKarma', $defaultKarmaSetting);
SKIP: {
skip("need to test expiration date in groupings interacting with recursive or not", 1);
ok(undef, "expiration date in groupings for getUser");
}
END {
(defined $gX and ref $gX eq 'WebGUI::Group') and $gX->delete;
(defined $gY and ref $gY eq 'WebGUI::Group') and $gY->delete;
(defined $gZ and ref $gZ eq 'WebGUI::Group') and $gZ->delete;
(defined $gA and ref $gA eq 'WebGUI::Group') and $gA->delete;
(defined $gB and ref $gB eq 'WebGUI::Group') and $gB->delete;
(defined $gC and ref $gC eq 'WebGUI::Group') and $gC->delete;
(defined $g2 and ref $g2 eq 'WebGUI::Group') and $g2->delete;
(defined $g and ref $g eq 'WebGUI::Group') and $g->delete;
(defined $user and ref $g eq 'WebGUI::User') and $g->delete;
foreach my $dude (@crowd, @mob) {
foreach my $testGroup ($gX, $gY, $gZ, $gA, $gB, $gC, $g, $gK) {
$testGroup->delete if (defined $testGroup and ref $testGroup eq 'WebGUI::Group');
}
foreach my $dude (@crowd, @mob, @chameleons, $user) {
$dude->delete if (defined $dude and ref $dude eq 'WebGUI::User');
}
$session->db->dbh->do('DROP TABLE IF EXISTS myUserTable');

View file

@ -31,7 +31,7 @@ for (my $count = 1; $count <= $maxCount; $count++){
for (my $count = 1; $count <= $maxCount; $count++){
is($scratch->get("Test$count"), $count, "Passed set/get $count\n");
is($scratch->get("Test$count"), $count, "Passed set/get $count");
}

View file

@ -289,3 +289,8 @@ WebGUI::Group->new($session, '7')->addUsers([1]);
ok($visitor->isInGroup(1), "Visitor added back to group Visitor");
ok($visitor->isInGroup(7), "Visitor added back to group Everyone");
END {
(defined $user and ref $user eq 'WebGUI::User') and $user->delete;
}