@@ -, +, @@ set --- C4/Stats.pm | 3 +++ t/db_dependent/Koha/Pseudonymization.t | 36 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) --- a/C4/Stats.pm +++ a/C4/Stats.pm @@ -124,6 +124,9 @@ sub UpdateStats { my $location = exists $params->{location} ? $params->{location} : undef; my $ccode = exists $params->{ccode} ? $params->{ccode} : ''; + # If $branch is set to an invalid branchcode, like 'OPACRenew' + $branch = undef if $branch && ! Koha::Libraries->find($branch); + my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $statistic = Koha::Statistic->new( { --- 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,36 @@ subtest 'PseudonymizedBorrowerAttributes tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'invalid branchcode' => sub { + plan tests => 1; + + $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, undef, 'UpdateStats does not explode if branchcode is invalid' ); + + $schema->storage->txn_rollback; +}; --