From 0f4228bca87131be707d7588d72aedb1e961dbcf Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 23 Mar 2022 11:22:52 -0300 Subject: [PATCH] Bug 29346: Biblio actions triggers This patch adds the trigger for the holds queue update on teh following methods: - C4::Biblio::DelBiblio - C4::Biblio::ModBiblio The ModBiblio use case could be improved by checking if itemtype is one of the updated attributes... but it felt there was no way to do it without some overhead. So I leave it as-is. It also mocks the ->enqueue method in the tests that call DelBiblio and ModBiblio to avoid breakages [1] Tests are added to check the trigger is called. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Biblio.t => SUCCESS: Tests pass! It means the trigger is triggered :-D 3. Sign off :-D [1] This breakages could be avoided if we solve Koha/BackgrounJob.pm:101 FIXME. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- C4/Biblio.pm | 16 ++++++++++++++- t/db_dependent/Biblio.t | 45 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index c4de6b064db..d438688e2d4 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -106,6 +106,7 @@ use Koha::Logger; use Koha::Caches; use Koha::Authority::Types; use Koha::Acquisition::Currencies; +use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; use Koha::Biblio::Metadatas; use Koha::Holds; use Koha::ItemTypes; @@ -428,6 +429,12 @@ sub ModBiblio { C4::OAI::Sets::UpdateOAISetsBiblio($biblionumber, $record); } + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( + { + biblio_ids => [ $biblionumber ] + } + ); + return 1; } @@ -490,7 +497,8 @@ sub DelBiblio { # We delete any existing holds my $holds = $biblio->holds; while ( my $hold = $holds->next ) { - $hold->cancel; + # no need to update the holds queue on each step, we'll do it at the end + $hold->cancel({ skip_holds_queue => 1 }); } unless ( $params->{skip_record_index} ){ @@ -519,6 +527,12 @@ sub DelBiblio { logaction( "CATALOGUING", "DELETE", $biblionumber, "biblio" ) if C4::Context->preference("CataloguingLog"); + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( + { + biblio_ids => [ $biblionumber ] + } + ); + return; } diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index ff0857a2963..1287aa57c53 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -251,6 +251,9 @@ sub run_tests { # roll back ES index changes. t::lib::Mocks::mock_preference('SearchEngine', 'Zebra'); + my $bgj_mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); + $bgj_mock->mock( 'enqueue', undef ); + my $isbn = '0590353403'; my $title = 'Foundation'; my $subtitle1 = 'Research'; @@ -636,6 +639,9 @@ subtest 'IsMarcStructureInternal' => sub { subtest 'deletedbiblio_metadata' => sub { plan tests => 2; + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); + $mock->mock( 'enqueue', undef ); + my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, ''); my $biblio_metadata = C4::Biblio::GetXmlBiblio( $biblionumber ); C4::Biblio::DelBiblio( $biblionumber ); @@ -646,7 +652,11 @@ subtest 'deletedbiblio_metadata' => sub { }; subtest 'DelBiblio' => sub { - plan tests => 5; + + plan tests => 6; + + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); + $mock->mock( 'enqueue', undef ); my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio(MARC::Record->new, ''); my $deleted = C4::Biblio::DelBiblio( $biblionumber ); @@ -684,6 +694,39 @@ subtest 'DelBiblio' => sub { is( $subscription->get_from_storage, undef, 'subscription should be deleted on biblio deletion' ); is( $serial->get_from_storage, undef, 'serial should be deleted on biblio deletion' ); is( $subscription_history->get_from_storage, undef, 'subscription history should be deleted on biblio deletion' ); + + subtest 'holds_queue update tests' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); + $mock->mock( 'enqueue', sub { + my ( $self, $args ) = @_; + is_deeply( + $args->{biblio_ids}, + [ $biblio->id ], + '->cancel triggers a holds queue update for the related biblio' + ); + } ); + + # add a hold + $builder->build_object( + { + class => 'Koha::Holds', + value => { + biblionumber => $biblio->id, + } + } + ); + + C4::Biblio::DelBiblio( $biblio->id ); + + $schema->storage->txn_rollback; + }; }; subtest 'MarcFieldForCreatorAndModifier' => sub { -- 2.36.0