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:
parent
1a278fed6c
commit
1412f023f1
4 changed files with 63 additions and 68 deletions
|
|
@ -45,17 +45,18 @@ Package for interfacing with SQL databases. This package implements Perl DBI fun
|
|||
$db->commit;
|
||||
$db->rollback;
|
||||
|
||||
@arr = $db->buildArray($sql);
|
||||
@arr = $db->buildArray($sql);
|
||||
$arrayRef = $db->buildArrayRef($sql);
|
||||
%hash = $db->buildHash($sql);
|
||||
$hashRef = $db->buildHashRef($sql);
|
||||
@arr = $db->quickArray($sql);
|
||||
$text = $db->quickCSV($sql);
|
||||
%hash = $db->quickHash($sql);
|
||||
$hashRef = $db->quickHashRef($sql);
|
||||
$text = $db->quickTab($sql);
|
||||
%hash = $db->buildHash($sql);
|
||||
$hashRef = $db->buildHashRef($sql);
|
||||
@arr = $db->quickArray($sql);
|
||||
$scalar = $db->quickScalar($sql);
|
||||
$text = $db->quickCSV($sql);
|
||||
%hash = $db->quickHash($sql);
|
||||
$hashRef = $db->quickHashRef($sql);
|
||||
$text = $db->quickTab($sql);
|
||||
|
||||
$id = $db->getNextId("someId");
|
||||
$id = $db->getNextId("someId");
|
||||
$string = $db->quote($string);
|
||||
$string = $db->quoteAndJoin(\@array);
|
||||
|
||||
|
|
@ -620,6 +621,35 @@ sub quickHashRef {
|
|||
}
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 quickScalar ( sql, params )
|
||||
|
||||
Executes a query and returns the first column from a single row of data as a scalar.
|
||||
|
||||
=head3 sql
|
||||
|
||||
An SQL query.
|
||||
|
||||
=head3 params
|
||||
|
||||
An array reference containing values for any placeholder params used in the SQL query.
|
||||
|
||||
=cut
|
||||
|
||||
sub quickScalar {
|
||||
my $self = shift;
|
||||
my $sql = shift;
|
||||
my $params = shift;
|
||||
my ($sth, @data);
|
||||
$sth = $self->prepare($sql);
|
||||
$sth->execute($params);
|
||||
@data = $sth->array;
|
||||
$sth->finish;
|
||||
return $data[0];
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 quickTab ( sql, params )
|
||||
|
|
|
|||
|
|
@ -383,6 +383,7 @@ sub new {
|
|||
my $cache = WebGUI::Cache->new($session,["user",$userId]);
|
||||
my $userData = $cache->get;
|
||||
unless ($userData->{_userId} && $userData->{_user}{username}) {
|
||||
$session->errorHandler->warn('Cache invalid');
|
||||
my %user;
|
||||
tie %user, 'Tie::CPHash';
|
||||
%user = $session->db->quickHash("select * from users where userId=?",[$userId]);
|
||||
|
|
@ -462,7 +463,7 @@ sub profileField {
|
|||
$self = shift;
|
||||
$fieldName = shift;
|
||||
$value = shift;
|
||||
if (!exists $self->{_profile}{$fieldName} && !$self->session->db->quickArray("SELECT COUNT(*) FROM userProfileField WHERE fieldName = ?", [$fieldName])) {
|
||||
if (!exists $self->{_profile}{$fieldName} && !$self->session->db->quickScalar("SELECT COUNT(*) FROM userProfileField WHERE fieldName = ?", [$fieldName]) ) {
|
||||
$self->session->errorHandler->warn("No such profile field: $fieldName");
|
||||
return undef;
|
||||
}
|
||||
|
|
|
|||
7
t/SQL.t
7
t/SQL.t
|
|
@ -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)");
|
||||
|
|
|
|||
73
t/User.t
73
t/User.t
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue