diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index dea1224..aa58ce0 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -548,6 +548,37 @@ sub _verifyId($$) { } +=item _verifyRequestTypeId() + +Parameters: + +=over + +=item $self: current object. + +=item $requestTypeId: A scalar numeric argument that is the request type id to lookup + + +=back + +Returns: scalar boolean, which is true iff. the $requestTypeId is valid and +already in the supporter database's request_type table. + + +=cut + + +sub _verifyRequestTypeId($$) { + my($self, $requestTypeId) = @_; + + die "_verifyRequestTypeId() called with a non-numeric id" unless defined $requestTypeId and looks_like_number($requestTypeId); + + my $val = $self->dbh()->selectall_hashref("SELECT id FROM request_type WHERE id = " . + $self->dbh->quote($requestTypeId, 'SQL_INTEGER'), 'id'); + return (defined $val and defined $val->{$requestTypeId}); + +} + =item _getOrCreateRequestType Arguments: diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 31b6462..fc097d4 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -5,7 +5,7 @@ use strict; use warnings; -use Test::More tests => 110; +use Test::More tests => 124; use Test::Exception; use Scalar::Util qw(looks_like_number reftype); @@ -423,6 +423,22 @@ lives_ok { $tempSP->_getOrCreateRequestType(\%hh); } is_deeply(\%hh, { requestTypeId => $rr }, "_getOrCreateRequestType: deletes requestType if both are provided."); +dies_ok { $tempSP->_verifyRequestTypeId(undef); } + "_verifyRequestTypeId: dies for undefined requestTypeId"; + +dies_ok { $tempSP->_verifyRequestTypeId("NoStringsPlease"); } + "_verifyRequestTypeId: dies for a string requestTypeId"; + +ok( (not $tempSP->_verifyRequestTypeId(0)), "_verifyRequestTypeId: returns false for id lookup for 0"); + +# Assumption here: that id number one more than the last added would never be in db. +ok( (not $tempSP->_verifyRequestTypeId($rr + 1)), + "_verifyRequestTypeId: returns false for id one greater than last added"); + +ok( ($tempSP->_verifyRequestTypeId($rr)), + "_verifyRequestTypeId: returns true for id known to be in database"); + + =item _getOrCreateRequestConfiguration =cut