From 484add49def5b314606f612800b61038e85e2725 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 1 Jul 2022 10:21:58 -0300 Subject: [PATCH] Bug 30889: (follow-up) Warn if context is not defined Signed-off-by: Tomas Cohen Arazi (cherry picked from commit de90b39b4c8a77ea3a911cf611e406770de5cf26) Signed-off-by: Lucas Gass --- Koha/BackgroundJob.pm | 29 +++++++++++++++++------------ t/db_dependent/Koha/BackgroundJob.t | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index 97b1bd7cbc..76bed24426 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -166,18 +166,23 @@ sub process { $args ||= {}; - my $context = decode_json($self->context); - C4::Context->_new_userenv(-1); - C4::Context->interface( $context->{interface} ); - C4::Context->set_userenv( - $context->{number}, $context->{id}, - $context->{cardnumber}, $context->{firstname}, - $context->{surname}, $context->{branch}, - $context->{branchname}, $context->{flags}, - $context->{emailaddress}, undef, - $context->{desk_id}, $context->{desk_name}, - $context->{register_id}, $context->{register_name} - ); + if ( $self->context ) { + my $context = decode_json($self->context); + C4::Context->_new_userenv(-1); + C4::Context->interface( $context->{interface} ); + C4::Context->set_userenv( + $context->{number}, $context->{id}, + $context->{cardnumber}, $context->{firstname}, + $context->{surname}, $context->{branch}, + $context->{branchname}, $context->{flags}, + $context->{emailaddress}, undef, + $context->{desk_id}, $context->{desk_name}, + $context->{register_id}, $context->{register_name} + ); + } + else { + Koha::Logger->get->warn("A background job didn't have context defined (" . $self->id . ")"); + } return $derived_class->process( $args ); } diff --git a/t/db_dependent/Koha/BackgroundJob.t b/t/db_dependent/Koha/BackgroundJob.t index 06b425bad1..2c368383f5 100755 --- a/t/db_dependent/Koha/BackgroundJob.t +++ b/t/db_dependent/Koha/BackgroundJob.t @@ -28,9 +28,11 @@ use Koha::BackgroundJob::BatchUpdateItem; use JSON qw( decode_json encode_json ); use t::lib::Mocks; +use t::lib::Mocks::Logger; use t::lib::TestBuilder; use t::lib::Koha::BackgroundJob::BatchTest; +my $logger = t::lib::Mocks::Logger->new; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -176,7 +178,7 @@ subtest 'start(), step() and finish() tests' => sub { subtest 'process tests' => sub { - plan tests => 4; + plan tests => 5; $schema->storage->txn_begin; @@ -221,6 +223,23 @@ subtest 'process tests' => sub { is_deeply( C4::Context->userenv, $job_context, "Userenv set from job context on process" ); is_deeply( C4::Context->interface, 'intranet', "Interface set from job context on process" ); + # Manually add a job (->new->store) without context + my $incomplete_job = t::lib::Koha::BackgroundJob::BatchTest->new( + { status => 'new', + size => 1, + borrowernumber => $patron->borrowernumber, + type => 'batch_test', + data => encode_json { + a => 'a', + b => 'b', + }, + } + )->store; + + $incomplete_job = Koha::BackgroundJobs->find( $incomplete_job->id ); + $incomplete_job->process(); + $logger->warn_is( "A background job didn't have context defined (" . $incomplete_job->id . ")" ); + $schema->storage->txn_rollback; }; -- 2.39.5