From 21089ffec5307491919551708f527058d0e9efff Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 16 Mar 2022 18:05:55 -0300 Subject: [PATCH] Bug 29346: Circulation actions triggers This patch introduces triggers for real-time updating the holds queue at check out and check in. The following high-level methods are involved: - C4::Circulation::AddIssue - C4::Circulation::AddReturn To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Circulation_holdsqueue.t => SUCCESS: Tests pass! Triggers are triggered 3. Sign off :-D Note: I put the tests on a separate file because the other one was too big already. Sponsored-by: Montgomery County Public Libraries Signed-off-by: Martin Renvoize --- C4/Circulation.pm | 13 +++++ t/db_dependent/Circulation_holdsqueue.t | 64 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100755 t/db_dependent/Circulation_holdsqueue.t diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 4f2e359b55..8d70f1ab18 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -41,6 +41,7 @@ use Algorithm::CheckDigits qw( CheckDigits ); use Data::Dumper qw( Dumper ); use Koha::Account; use Koha::AuthorisedValues; +use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; use Koha::Biblioitems; use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Calendar; @@ -1773,6 +1774,12 @@ sub AddIssue { checkout => $issue->get_from_storage } }); + + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( + { + biblio_ids => [ $item_object->biblionumber ] + } + ); } } return $issue; @@ -2433,6 +2440,12 @@ sub AddReturn { checkout=> $checkin } }); + + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( + { + biblio_ids => [ $item->biblionumber ] + } + ); } return ( $doreturn, $messages, $issue, ( $patron ? $patron->unblessed : {} )); diff --git a/t/db_dependent/Circulation_holdsqueue.t b/t/db_dependent/Circulation_holdsqueue.t new file mode 100755 index 0000000000..f8a82ffd19 --- /dev/null +++ b/t/db_dependent/Circulation_holdsqueue.t @@ -0,0 +1,64 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::MockModule; + +use C4::Circulation qw( AddIssue AddReturn ); + +use Koha::Database; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'AddIssue() and AddReturn() real-time holds queue tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $item = $builder->build_sample_item({ library => $library->id }); + + t::lib::Mocks::mock_userenv({ branchcode => $library->id }); + + my $action; + + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); + $mock->mock( 'enqueue', sub { + my ( $self, $args ) = @_; + is_deeply( + $args->{biblio_ids}, + [ $item->biblionumber ], + "$action triggers a holds queue update for the related biblio" + ); + } ); + + $action = 'AddIssue'; + AddIssue( $patron->unblessed, $item->barcode, ); + + $action = 'AddReturn'; + AddReturn( $item->barcode ); + + $schema->storage->txn_rollback; +}; -- 2.20.1