From c2cc6869dd1730168bc1c13bb3e0dcaac89395bb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johanna=20R=C3=A4is=C3=A4?= Date: Tue, 8 Oct 2024 13:46:24 +0300 Subject: [PATCH] Bug 32413: Fix repeated param names for JSON report This patch fixes repeated param names for JSON report. To test: 1) Create a report SELECT count(*) from items where homebranch = <> UNION ALL SELECT count(*) from deleteditems where homebranch = <> 2) Run the report as JSON, cgi-bin/koha/svc/report?id=&sql_params= 3) Check that the second value is 0 4) Apply the patch 5) Run the report as JSON again 6) Check that the second value has the correct value 7) prove t/db_dependent/Koha/Reports.t Sponsored-by: Koha-Suomi Oy Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Report.pm | 6 ++++++ t/db_dependent/Koha/Reports.t | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Koha/Report.pm b/Koha/Report.pm index 4ac42ffa47..0a36f81b90 100644 --- a/Koha/Report.pm +++ b/Koha/Report.pm @@ -194,6 +194,12 @@ sub prep_report { } my %lookup; + unless ( scalar @$param_names ) { + my @placeholders = $sql =~ /<<([^<>]+)>>/g; + foreach my $placeholder (@placeholders) { + push @$param_names, $placeholder unless grep { $_ eq $placeholder } @$param_names; + } + } @lookup{@$param_names} = @$sql_params; @split = split /<<|>>/, $sql; for ( my $i = 0 ; $i < $#split / 2 ; $i++ ) { diff --git a/t/db_dependent/Koha/Reports.t b/t/db_dependent/Koha/Reports.t index 941a89ce28..19bee5bd27 100755 --- a/t/db_dependent/Koha/Reports.t +++ b/t/db_dependent/Koha/Reports.t @@ -66,8 +66,8 @@ subtest 'prep_report' => sub { ($sql, undef) = $report->prep_report( ['Test|list','Another'],["1\n12\n\r243",'the other'] ); is( $sql, qq{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('1','12','243') /* saved_sql.id: $id */},'Expected sql generated correctly with multiple params and names'); - ($sql, undef) = $report->prep_report( [],["1\n12\n\r243",'the other',"42\n32\n22\n12"] ); - is( $sql, qq{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('42','32','22','12') /* saved_sql.id: $id */},'Expected sql generated correctly with multiple params and no names'); + ($sql, undef) = $report->prep_report( [],["1\n12\n\r243",'the other'] ); + is( $sql, qq{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('1','12','243') /* saved_sql.id: $id */},'Expected sql generated correctly with multiple params and no names'); $report->savedsql( q{SELECT i.itemnumber, i.itemnumber as Exemplarnummber, [[i.itemnumber| itemnumber for batch]] FROM items}) -- 2.39.5