@@ -, +, @@ set --- C4/Stats.pm | 4 +++ t/db_dependent/Koha/Pseudonymization.t | 39 +++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) --- a/C4/Stats.pm +++ a/C4/Stats.pm @@ -140,6 +140,10 @@ sub UpdateStats { } )->store; + + # If $branch is set to an invalid branchcode, like 'OPACRenew' + $statistic->branch(undef) if $branch && ! Koha::Libraries->find($branch); + Koha::PseudonymizedTransaction->new_from_statistic($statistic)->store if C4::Context->preference('Pseudonymization') && $borrowernumber # Not a real transaction if the patron does not exist --- a/t/db_dependent/Koha/Pseudonymization.t +++ a/t/db_dependent/Koha/Pseudonymization.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Try::Tiny; use C4::Circulation qw( AddIssue AddReturn ); @@ -29,6 +29,7 @@ use Koha::Database; use Koha::DateUtils qw( dt_from_string ); use Koha::Patrons; use Koha::PseudonymizedTransactions; +use Koha::Statistics; use t::lib::TestBuilder; use t::lib::Mocks; @@ -209,3 +210,39 @@ subtest 'PseudonymizedBorrowerAttributes tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'invalid branchcode' => sub { # Test added for transaction_branchcode == 'OPACRenew' (BZ 29341) + plan tests => 2; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_config( 'bcrypt_settings', '$2a$08$9lmorEKnwQloheaCLFIfje' ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + t::lib::Mocks::mock_preference( 'Pseudonymization', 1 ); + my $item = $builder->build_sample_item; + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $branchcode = $library->branchcode; + $library->delete; + + C4::Stats::UpdateStats( + { + type => 'issue', + branch => $branchcode, + itemnumber => $item->itemnumber, + borrowernumber => $patron->borrowernumber, + itemtype => $item->effective_itemtype, + location => $item->location, + ccode => $item->ccode, + } + ); + + my $stat = Koha::Statistics->search({itemnumber => $item->itemnumber}); + is( $stat->next->branch, $branchcode, 'UpdateStats does not explode if branchcode is invalid, and stat logs the branchcode as is' ); + + my $p = Koha::PseudonymizedTransactions->search({itemnumber => $item->itemnumber}); + is( $p->next->transaction_branchcode, undef, 'Pseudonymized transactions logged branchcode=NULL if invalid branchcode passed' ); + + $schema->storage->txn_rollback; +}; --