From e92a0a74236a8993b288964a8333b8aca3c397cc Mon Sep 17 00:00:00 2001 From: Petro Vashchuk Date: Thu, 31 Mar 2022 13:58:02 +0300 Subject: [PATCH] Bug 30409: barcodedecode() should always trim barcode Barcode is trimmed of leading/trailing whitespaces in many instances before the barcodedecode sub was called. This patch instead makes that barcodedecode sub is going to trim it itself and removes unnecessary, and repetitive code that was used before barcodedecode was called. With this addition, filet "whitespace" was removed from barcodedecode sub, as it's going to be always trimmed from now on anyway. Also changed tests to fit the way barcodedecode works currently. --- C4/Circulation.pm | 6 +++--- circ/circulation.pl | 1 - circ/renew.pl | 1 - circ/returns.pl | 1 - t/Circulation_barcodedecode.t | 6 ++---- 5 files changed, 5 insertions(+), 10 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index c6baf4892f..abe1f88078 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -153,6 +153,7 @@ Will do some manipulation of the barcode for systems that deliver a barcode to circulation.pl that differs from the barcode stored for the item. For proper functioning of this filter, calling the function on the correct barcode string (items.barcode) should return an unaltered barcode. +Barcode is going to be automatically trimmed of leading/trailing whitespaces. The optional $filter argument is to allow for testing or explicit behavior that ignores the System Pref. Valid values are the same as the @@ -166,12 +167,11 @@ System Pref options. sub barcodedecode { my ($barcode, $filter) = @_; my $branch = C4::Context::mybranch(); + $barcode =~ s/^\s+|\s+$//g; $filter = C4::Context->preference('itemBarcodeInputFilter') unless $filter; Koha::Plugins->call('item_barcode_transform', \$barcode ); $filter or return $barcode; # ensure filter is defined, else return untouched barcode - if ($filter eq 'whitespace') { - $barcode =~ s/\s//g; - } elsif ($filter eq 'cuecat') { + if ($filter eq 'cuecat') { chomp($barcode); my @fields = split( /\./, $barcode ); my @results = map( C4::Circulation::_decode($_), @fields[ 1 .. $#fields ] ); diff --git a/circ/circulation.pl b/circ/circulation.pl index ac8ef1e430..c876e41e93 100755 --- a/circ/circulation.pl +++ b/circ/circulation.pl @@ -159,7 +159,6 @@ if (C4::Context->preference("DisplayClearScreenButton")) { } for my $barcode ( @$barcodes ) { - $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace $barcode = barcodedecode( $barcode ) if $barcode; } diff --git a/circ/renew.pl b/circ/renew.pl index 97b6317067..41c6afe48c 100755 --- a/circ/renew.pl +++ b/circ/renew.pl @@ -43,7 +43,6 @@ my $schema = Koha::Database->new()->schema(); my $barcode = $cgi->param('barcode') // ''; my $unseen = $cgi->param('unseen') || 0; -$barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespae $barcode = barcodedecode($barcode) if $barcode; my $override_limit = $cgi->param('override_limit'); my $override_holds = $cgi->param('override_holds'); diff --git a/circ/returns.pl b/circ/returns.pl index f98907187a..84b5248230 100755 --- a/circ/returns.pl +++ b/circ/returns.pl @@ -119,7 +119,6 @@ foreach ( $query->param ) { $counter++; # decode barcode ## Didn't we already decode them before passing them back last time?? - $barcode =~ s/^\s*|\s*$//g; # remove leading/trailing whitespace $barcode = barcodedecode($barcode) if $barcode; ###################### diff --git a/t/Circulation_barcodedecode.t b/t/Circulation_barcodedecode.t index 8a57dcd984..87a534152a 100755 --- a/t/Circulation_barcodedecode.t +++ b/t/Circulation_barcodedecode.t @@ -29,19 +29,17 @@ t::lib::Mocks::mock_userenv({ branchcode => 'IMS' }); our %inputs = ( cuecat => ["26002315", '.C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.', ".C3nZC3nZC3nYD3b6ENnZCNnY.fHmc.C3D1Dxr2C3nZE3n7.\r\n", 'q.C3nZC3nZC3nWDNzYDxf2CNnY.fHmc.C3DWC3nZCNjXD3nW.', '.C3nZC3nZC3nWCxjWE3D1C3nX.cGf2.ENr7C3v7D3T3ENj3C3zYDNnZ.' ], - whitespace => [" 26002315", "26002315 ", "\n\t26002315\n"], 'T-prefix' => [qw(T0031472 T32)], 'libsuite8' => ['b000126', 'b12', 'B0126', 'IMS-B-126', 'ims-b-126','CD0000024','00123','11998'], EAN13 => [qw(892685001928 695152)], - other => [qw(26002315 T0031472 T32 Alphanum123), "Alpha Num 345"], + other => [qw(26002315 T0031472 T32 Alphanum123), "Alpha Num 345", " 26002315", "26002315 ", "\n\t26002315\n"], ); our %outputs = ( cuecat => ["26002315", "046675000808", "046675000808", "043000112403", "978068484914051500"], - whitespace => [qw(26002315 26002315 26002315)], 'T-prefix' => [qw(T0031472 T0000002 )], 'libsuite8' => ['IMS-b-126', 'IMS-b-12', 'IMS-B-126', 'IMS-B-126', 'ims-b-126','IMS-CD-24','IMS-b-123','IMS-b-11998'], EAN13 => [qw(0892685001928 0000000695152)], - other => [qw(26002315 T0031472 T32 Alphanum123), "Alpha Num 345"], + other => [qw(26002315 T0031472 T32 Alphanum123), "Alpha Num 345", "26002315", "26002315", "26002315"], ); my @filters = sort keys %inputs; -- 2.31.1