Bug 30889: (follow-up) Warn if context is not defined

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomas Cohen Arazi 2022-07-01 10:21:58 -03:00
parent fd385d90b8
commit de90b39b4c
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
2 changed files with 37 additions and 13 deletions

View File

@ -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 );
}

View File

@ -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;
};