Fixed a bug in WebGUI::User::profileField, where the check for whether or not

a profile field existed in the db always passed because it used quickArray in
scalar context.  Since quickArray always returns 1 element, this was always true
and it never returned undef like it was supposed to.

To fix this, I added a new method called quickScalar that returns the first column
from the row as a scalar.  It has a basic test in SQL.t.

Then there's the new test in User.t for coverage that exposed this problem, and
I also removed a whole slew of duplicate tests.
This commit is contained in:
Colin Kuskie 2007-03-04 04:18:59 +00:00
parent 1a278fed6c
commit 1412f023f1
4 changed files with 63 additions and 68 deletions

View file

@ -17,7 +17,7 @@ use WebGUI::Session;
use Data::Dumper;
use Test::Deep;
use Test::More tests => 50; # increment this value for each test you create
use Test::More tests => 52; # increment this value for each test you create
my $session = WebGUI::Test->session;
@ -82,6 +82,11 @@ $sth->finish;
my ($value) = $session->db->quickArray("select value from settings where name='authMethod'");
ok($value, "quickArray()");
# quickScalar
my $quickScalar = $session->db->quickScalar("SELECT COUNT(*) from userProfileField where fieldName='email'");
is(ref $quickScalar, '', 'quickScalar returns a scalar');
is($quickScalar, 1, 'quickScalar returns the correct scalar');
# write
$session->db->write("delete from incrementer where incrementerId='theBigTest'"); # clean up previous failures
$session->db->write("insert into incrementer (incrementerId, nextValue) values ('theBigTest',25)");

View file

@ -20,7 +20,7 @@ use WebGUI::Cache;
use WebGUI::User;
use WebGUI::ProfileField;
use Test::More tests => 102; # increment this value for each test you create
use Test::More tests => 90; # increment this value for each test you create
use Test::Deep;
my $session = WebGUI::Test->session;
@ -64,12 +64,21 @@ is($user->status, "Selfdestructed", 'status("Selfdestructed")');
$user->status('Deactivated');
is($user->status, "Deactivated", 'status("Deactivated")');
################################################################
#
# profileField
#
################################################################
#Let's get/set a profile field
$user->profileField("firstName", "Bill");
$lastUpdate = time();
is($user->profileField("firstName"), "Bill", 'profileField() get/set');
cmp_ok(abs($user->lastUpdated-$lastUpdate), '<=', 1, 'lastUpdated() -- profileField');
#Fetching a non-existant profile field returns undef
is($user->profileField('notAProfileField'), undef, 'getting non-existant profile fields returns undef');
#Let's check the auth methods
#Default should be WebGUI
@ -118,7 +127,8 @@ cmp_ok(abs($user->lastUpdated-$lastUpdate), '<=', 1, 'lastUpdated() -- referring
my @groups = qw|6 4|;
$user->addToGroups(\@groups);
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [6, $user->userId]);
my $result;
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [6, $user->userId]);
ok($result, 'addToGroups() -- added to first test group');
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [4, $user->userId]);
@ -127,10 +137,10 @@ ok($result, 'addToGroups() -- added to second test group');
#Let's delete this user from our test groups
$user->deleteFromGroups(\@groups);
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [6, $user->userId]);
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [6, $user->userId]);
is($result, '0', 'deleteFromGroups() -- removed from first test group');
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [4, $user->userId]);
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [4, $user->userId]);
is($result, '0', 'deleteFromGroups() -- removed from second test group');
#Let's delete this user
@ -166,64 +176,13 @@ is($user->userId, "ROYSUNIQUEUSERID000002", 'new() -- override user id');
$user->authMethod("LDAP");
is($user->authMethod, "LDAP", 'authMethod() -- set to LDAP');
#get/set karma
my $oldKarma = $user->karma;
$user->karma('69', 'peter gibbons', 'test karma');
is($user->karma, $oldKarma+69, 'karma() -- get/set add amount');
my ($source, $description) = $session->db->quickArray("select source, description from karmaLog where userId=?",[$user->userId]);
is($source, 'peter gibbons', 'karma() -- get/set source');
is($description, 'test karma', 'karma() -- get/set description');
$oldKarma = $user->karma;
$user->karma('-69', 'peter gibbons', 'lumberg took test karma away');
is($user->karma, $oldKarma-69, 'karma() -- get/set subtract amount');
#Let's test referringAffiliate
$lastUpdate = time();
$user->referringAffiliate(10);
is($user->referringAffiliate, '10', 'referringAffiliate() -- get/set');
cmp_ok(abs($user->lastUpdated-$lastUpdate), '<=', 1, 'lastUpdated() -- referringAffiliate');
#Let's try adding this user to some groups
my @groups = qw|2 4|;
$user->addToGroups(\@groups);
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('2')." and userId=".$session->db->quote($user->userId));
ok($result, 'addToGroups() -- added to first test group');
($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('4')." and userId=".$session->db->quote($user->userId));
ok($result, 'addToGroups() -- added to second test group');
#Let's delete this user from our test groups
$user->deleteFromGroups(\@groups);
($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('2')." and userId=".$session->db->quote($user->userId));
is($result, '0', 'deleteFromGroups() -- removed from first test group');
($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('4')." and userId=".$session->db->quote($user->userId));
is($result, '0', 'deleteFromGroups() -- removed from second test group');
#Let's delete this user
my $userId = $user->userId;
$user->delete;
my ($count) = $session->db->quickArray("select count(*) from users where userId=".$session->db->quote($userId));
is($count, '0', 'delete() -- users table');
($count) = $session->db->quickArray("select count(*) from userProfileData where userId=".$session->db->quote($userId));
is($count, '0', 'delete() -- userProfileData table');
($count) = $session->db->quickArray("select count(*) from inbox where userId=".$session->db->quote($userId));
is($count, '0', 'delete() -- inbox table');
ok(WebGUI::User->validUserId($session, 1), 'Visitor has a valid userId');
ok(WebGUI::User->validUserId($session, 3), 'Admin has a valid userId');
ok(!WebGUI::User->validUserId($session, 'eeee'), 'random illegal Id #1');
ok(!WebGUI::User->validUserId($session, 37), 'random illegal Id #2');
$user->delete;
#identifier() and uncache()
SKIP: {
skip("identifier() -- deprecated",1);