From 71bf715596769577766cbf8c21972826e766e97f 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 --- .../CreateEHoldingsFromBiblios.pm | 15 +++- .../CreateEHoldingsFromBiblios.t | 89 ++++++++++++++----- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm b/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm index 807b85fdab..3f071281a3 100644 --- a/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm +++ b/Koha/BackgroundJob/CreateEHoldingsFromBiblios.pm @@ -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; diff --git a/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t b/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t index d39a204079..aa829aa708 100755 --- a/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t +++ b/t/db_dependent/Koha/BackgroundJob/CreateEHoldingsFromBiblios.t @@ -49,31 +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', - size => 1, - } - )->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