From a05db213215567619e2fca44158401f1907922fe Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 20 Dec 2016 16:54:44 +0200 Subject: [PATCH] Bug 17783: Prevent crash when providing an undefined value When calling the proposed version of get_effective_issuing_rule with undefined parameter values, a following crash occurs: SQL::Abstract::puke(): [SQL::Abstract::__ANON__] Fatal: SQL::Abstract before v1.75 used to generate incorrect SQL when the -IN operator was given an undef-containing list: !!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based version of SQL::Abstract will emit the logically correct SQL instead of raising this exception) at /home/ubuntu/kohaclone/Koha/Objects.pm line 182 This patch adds a test to cover this problem and fixes the issue. To test: 1. Run t/db_dependent/Koha/IsssuingRules.t Signed-off-by: Josef Moravec Signed-off-by: Jonathan Druart --- Koha/IssuingRules.pm | 20 +++++++++++++++++--- t/db_dependent/Koha/IssuingRules.t | 31 ++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Koha/IssuingRules.pm b/Koha/IssuingRules.pm index c0c69be..e3664df 100644 --- a/Koha/IssuingRules.pm +++ b/Koha/IssuingRules.pm @@ -44,10 +44,24 @@ sub get_effective_issuing_rule { my $itemtype = $params->{itemtype}; my $branchcode = $params->{branchcode}; + my $search_categorycode = $default; + my $search_itemtype = $default; + my $search_branchcode = $default; + + if ($categorycode) { + $search_categorycode = { 'in' => [ $categorycode, $default ] }; + } + if ($itemtype) { + $search_itemtype = { 'in' => [ $itemtype, $default ] }; + } + if ($branchcode) { + $search_branchcode = { 'in' => [ $branchcode, $default ] }; + } + my $rule = $self->search({ - categorycode => { 'in' => [ $categorycode, $default ] }, - itemtype => { 'in' => [ $itemtype, $default ] }, - branchcode => { 'in' => [ $branchcode, $default ] }, + categorycode => $search_categorycode, + itemtype => $search_itemtype, + branchcode => $search_branchcode, }, { order_by => { -desc => ['branchcode', 'categorycode', 'itemtype'] diff --git a/t/db_dependent/Koha/IssuingRules.t b/t/db_dependent/Koha/IssuingRules.t index 158e3cf..847f971 100644 --- a/t/db_dependent/Koha/IssuingRules.t +++ b/t/db_dependent/Koha/IssuingRules.t @@ -33,7 +33,7 @@ $schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; subtest 'get_effective_issuing_rule' => sub { - plan tests => 2; + plan tests => 3; my $patron = $builder->build({ source => 'Borrower' }); my $item = $builder->build({ source => 'Item' }); @@ -42,6 +42,35 @@ subtest 'get_effective_issuing_rule' => sub { my $itemtype = $item->{'itype'}; my $branchcode = $item->{'homebranch'}; + subtest 'Call with undefined values' => sub { + plan tests => 4; + + my $rule; + Koha::IssuingRules->delete; + ok(!Koha::IssuingRules->search->count, 'There are no issuing rules.'); + $rule = Koha::IssuingRules->get_effective_issuing_rule({ + branchcode => undef, + categorycode => undef, + itemtype => undef, + }); + is($rule, undef, 'When I attempt to get effective issuing rule by' + .' providing undefined values, then undef is returned.'); + ok(Koha::IssuingRule->new({ + branchcode => '*', + categorycode => '*', + itemtype => '*', + })->store, 'Given I added an issuing rule branchcode => *,' + .' categorycode => *, itemtype => *,'); + $rule = Koha::IssuingRules->get_effective_issuing_rule({ + branchcode => undef, + categorycode => undef, + itemtype => undef, + }); + ok(_row_match($rule, '*', '*', '*'), 'When I attempt to get effective' + .' issuing rule by providing undefined values, then the above one is' + .' returned.'); + }; + subtest 'Get effective issuing rule in correct order' => sub { plan tests => 18; -- 2.9.3