From 924530a7529140dc3a6821901310bed511b5ff7a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 1 Feb 2023 16:23:59 +0100 Subject: [PATCH] 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 Signed-off-by: Kyle M Hall Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 71bf715596769577766cbf8c21972826e766e97f) Signed-off-by: Matt Blenkinsop --- .../CreateEHoldingsFromBiblios.pm | 15 +++- .../CreateEHoldingsFromBiblios.t | 88 ++++++++++++++----- 2 files changed, 76 insertions(+), 27 deletions(-) diff --git a/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm b/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm index c48181f34f..ae2ab9672e 100644 --- a/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm +++ b/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm @@ -71,6 +71,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, { @@ -78,12 +83,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; diff --git a/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t b/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t index b18a3bfb6e..aa829aa708 100755 --- a/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t +++ b/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t @@ -49,30 +49,72 @@ subtest 'enqueue' => sub { }; subtest 'process' => sub { - plan tests => 1; - - $schema->storage->txn_begin; + plan tests => 2; - 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', - } - )->store; - $job = Koha::BackgroundJobs->find( $job->id ); - my $data = { - record_ids => [ $biblio->biblionumber ], - package_id => $package->package_id, + 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" + } + ] + ); }; - my $json = $job->json->encode($data); - $job->data($json)->store; - $job->process($data); - is( $job->report->{total_success}, 1 ); - $schema->storage->txn_rollback; + 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; + + 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; + $job->process($data); + is( $job->report->{total_success}, 1 ); + + $schema->storage->txn_rollback; + }; }; -- 2.39.5