diff --git a/Koha/HoldGroup.pm b/Koha/HoldGroup.pm index a924696b8d..9f6c8c7786 100644 --- a/Koha/HoldGroup.pm +++ b/Koha/HoldGroup.pm @@ -22,6 +22,8 @@ use Modern::Perl; use C4::Context; use C4::Log qw( logaction ); +use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; + use base qw(Koha::Object); =head1 NAME @@ -77,6 +79,11 @@ sub store { } } + if ( $result && @$associated_holds && C4::Context->preference('RealTimeHoldsQueue') ) { + my @biblio_ids = map { $_->biblionumber } @$associated_holds; + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { biblio_ids => \@biblio_ids } ); + } + return $result; } @@ -114,6 +121,11 @@ sub delete { } } + if ( $result && @associated_holds && C4::Context->preference('RealTimeHoldsQueue') ) { + my @biblio_ids = map { $_->biblionumber } @associated_holds; + Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { biblio_ids => \@biblio_ids } ); + } + return $result; } diff --git a/t/db_dependent/Reserves/HoldGroup.t b/t/db_dependent/Reserves/HoldGroup.t index 93506e7f98..d87b00af99 100755 --- a/t/db_dependent/Reserves/HoldGroup.t +++ b/t/db_dependent/Reserves/HoldGroup.t @@ -20,11 +20,13 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 3; +use Test::More tests => 4; use t::lib::TestBuilder; use t::lib::Mocks; +use Test::MockModule; + use C4::Reserves qw( AddReserve ModReserveAffect ); use C4::Circulation qw( AddReturn ); @@ -130,6 +132,89 @@ subtest "target_hold_id tests" => sub { $schema->storage->txn_rollback; }; +subtest 'RealTimeHoldsQueue tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $item_1 = $builder->build_sample_item; + my $item_2 = $builder->build_sample_item; + + my $hold_1 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron->borrowernumber, + biblionumber => $item_1->biblionumber, + hold_group_id => undef, + found => undef, + } + } + ); + my $hold_2 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron->borrowernumber, + biblionumber => $item_2->biblionumber, + hold_group_id => undef, + found => undef, + } + } + ); + + my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); + $mock->mock( + 'enqueue', + sub { + my ( $self, $args ) = @_; + is_deeply( + [ sort @{ $args->{biblio_ids} } ], + [ sort( $item_1->biblionumber, $item_2->biblionumber ) ], + 'store/delete with RealTimeHoldsQueue triggers a holds queue update for the related biblios' + ); + } + ); + + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); + + my $hold_group = $patron->create_hold_group( [ $hold_1->id, $hold_2->id ] ); + + $hold_group->delete; + + t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 ); + + my $hold_3 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron->borrowernumber, + biblionumber => $item_1->biblionumber, + hold_group_id => undef, + found => undef, + } + } + ); + my $hold_4 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron->borrowernumber, + biblionumber => $item_2->biblionumber, + hold_group_id => undef, + found => undef, + } + } + ); + + my $hold_group_2 = $patron->create_hold_group( [ $hold_3->id, $hold_4->id ] ); + $hold_group_2->delete; + + $schema->storage->txn_rollback; +}; + sub set_userenv { my ($library) = @_; my $staff = $builder->build_object( { class => "Koha::Patrons" } );