diff --git a/Supporters/lib/Supporters.pm b/Supporters/lib/Supporters.pm index 983dc28..e3631f0 100644 --- a/Supporters/lib/Supporters.pm +++ b/Supporters/lib/Supporters.pm @@ -682,22 +682,27 @@ Arguments: =item type - A string describing the request. + A string describing the request. Argument is optional. =back -Returns the id value of the request_type entry. undef is returned if there -is no request of that type. +If type is given, returns a scalar the id value of the request_type entry. +undef is returned if there is no request of that type. + +If type is not given, a list of all known request types is returned. =cut -sub getRequestType($$) { +sub getRequestType($;$) { my($self, $type) = @_; - return undef if not defined $type; - my $val = $self->dbh()->selectall_hashref("SELECT id, type FROM request_type WHERE type = '$type'", 'type'); - return $val->{$type}{id} if (defined $val and defined $val->{$type} and defined $val->{$type}{id}); - return undef; + if (not defined $type) { + return @{$self->dbh()->selectcol_arrayref("SELECT type, id FROM request_type ORDER BY id", { Columns=>[1] })}; + } else { + my $val = $self->dbh()->selectall_hashref("SELECT id, type FROM request_type WHERE type = '$type'", 'type'); + return $val->{$type}{id} if (defined $val and defined $val->{$type} and defined $val->{$type}{id}); + return undef; + } } ###################################################################### diff --git a/Supporters/t/Supporters.t b/Supporters/t/Supporters.t index 283398d..7086e71 100644 --- a/Supporters/t/Supporters.t +++ b/Supporters/t/Supporters.t @@ -8,7 +8,7 @@ use strict; use warnings; -use Test::More tests => 331; +use Test::More tests => 334; use Test::Exception; use Sub::Override; use File::Temp qw/tempfile/; @@ -413,6 +413,10 @@ lives_ok { $tShirt0RequestTypeId = $sp->addRequestType('t-shirt-0'); } ok( (defined $tShirt0RequestTypeId and looks_like_number($tShirt0RequestTypeId) and $tShirt0RequestTypeId > 0), "addRequestType: id is a number"); +my @allRequestsList = $sp->getRequestType(); + +is_deeply(\@allRequestsList, ['t-shirt-0' ], "getRequestType: no argument returns full list of request types (1)"); + my $testSameRequestType; lives_ok { $testSameRequestType = $sp->addRequestType('t-shirt-0'); } @@ -490,6 +494,11 @@ my $joinEmailListRequestId = $sp->getRequestType("join-announce-email-list"); ok((defined $joinEmailListRequestId and looks_like_number($joinEmailListRequestId) and $joinEmailListRequestId > 0), "addRequest: underlying call to addRequestType works properly, per getRequestType"); +@allRequestsList = $sp->getRequestType(); + +is_deeply(\@allRequestsList, ['t-shirt-0', 'join-announce-email-list'], + "getRequestType: no argument returns full list of request types (2)"); + my $tshirtSmallRequestId; lives_ok { $tshirtSmallRequestId = @@ -501,7 +510,13 @@ lives_ok { $tshirtSmallRequestId = ok( (defined $tshirtSmallRequestId and looks_like_number($tshirtSmallRequestId) and $tshirtSmallRequestId > 0), "addRequest: successful call returns an integer id."); +@allRequestsList = $sp->getRequestType(); + +is_deeply(\@allRequestsList, ['t-shirt-0', 'join-announce-email-list', 't-shirt-small-only' ], + "getRequestType: no argument returns full list of request types (3)"); + my $tShirt0RequestId; + lives_ok { $tShirt0RequestId = $sp->addRequest({ donorId => $drapperId, requestTypeId => $tShirt0RequestTypeId, requestConfigurationId => $tShirt0Data->{$tShirt0RequestTypeId}{'MenL'} }); }