Bug 32781: Prevent import from list to fail if package does not exist

This is theorical (caught when writting tests) but we need to deal with
it.

Can't call method "resources" on an undefined value at /kohadevbox/koha/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm line 94.

Test plan:
  prove t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t
must return green

Signed-off-by: Pedro Amorim <pedro.amorim@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2023-02-01 16:23:59 +01:00 committed by Tomas Cohen Arazi
parent c3325f815f
commit 71bf715596
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
2 changed files with 73 additions and 25 deletions

View file

@ -66,6 +66,11 @@ sub process {
my @record_ids = @{ $args->{record_ids} };
my $package_id = $args->{package_id};
my $report = {
total_records => scalar @record_ids,
total_success => 0,
};
my $package = Koha::ERM::EHoldings::Packages->find($package_id);
unless ( $package ) {
push @messages, {
@ -73,12 +78,14 @@ sub process {
code => 'package_do_not_exist',
package_id => $package_id,
};
my $data = $self->decoded_data;
$data->{messages} = \@messages;
$data->{report} = $report;
return $self->finish( $data );
}
my $report = {
total_records => scalar @record_ids,
total_success => 0,
};
my $fix_coverage = sub {
my $coverage = shift || q{};
my @coverages = split '-', $coverage;

View file

@ -49,12 +49,52 @@ subtest 'enqueue' => sub {
};
subtest 'process' => sub {
plan tests => 2;
subtest 'package_do_not_exist' => sub {
plan tests => 2;
$schema->storage->txn_begin;
my $biblio = $builder->build_sample_biblio;
my $package =
Koha::ERM::EHoldings::Package->new( { name => 'a package' } )->store;
my $job = Koha::BackgroundJob::CreateEHoldingsFromBiblios->new(
{
status => 'new',
type => 'create_eholdings_from_biblios',
size => 1,
}
)->store;
$job = Koha::BackgroundJobs->find( $job->id );
my $data = {
record_ids => [ $biblio->biblionumber ],
package_id => $package->package_id,
};
my $json = $job->json->encode($data);
$job->data($json)->store;
$package->delete; # Delete the package
$job->process($data);
is( $job->report->{total_success}, 0 );
is_deeply(
$job->messages,
[
{
code => "package_do_not_exist",
package_id => $package->package_id,
type => "error"
}
]
);
};
subtest 'all good' => sub {
plan tests => 1;
$schema->storage->txn_begin;
my $biblio = $builder->build_sample_biblio;
my $package =
Koha::ERM::EHoldings::Package->new( { name => 'a package' } )->store;
@ -76,4 +116,5 @@ subtest 'process' => sub {
is( $job->report->{total_success}, 1 );
$schema->storage->txn_rollback;
};
};