From 6b4d340d2c7656fd4c4a25bccccc2adab006ea15 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Mon, 5 Jun 2023 13:54:40 -0300
Subject: [PATCH] Bug 21983: Make DelBiblio update linked ILL requests

This patch makes DelBiblio update the biblio linked ILL requests so the
value in biblio_id is moved to the deleted_biblio_id.

The change is covered by tests.

To test:
1. Apply this patchset
2. Run:
   $ ktd --shell
  k$ updatedatabase
  k$ qa --run-tests
=> SUCCESS: Tests pass! All green/good
3. Sign off :-D

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 C4/Biblio.pm            |  2 ++
 t/db_dependent/Biblio.t | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index cc88b20871..879044b0e4 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -523,6 +523,8 @@ sub DelBiblio {
     # We update any existing orders
     my $orders = $biblio->orders;
     $orders->update({ deleted_biblionumber => $biblionumber}, { no_triggers => 1 });
+    # Update related ILL requests
+    $biblio->ill_requests->update({ deleted_biblio_id => $biblio->id, biblio_id => undef });
 
     unless ( $params->{skip_record_index} ){
         my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t
index 3d6f96e759..c176885efe 100755
--- a/t/db_dependent/Biblio.t
+++ b/t/db_dependent/Biblio.t
@@ -670,7 +670,7 @@ subtest 'deletedbiblio_metadata' => sub {
 
 subtest 'DelBiblio' => sub {
 
-    plan tests => 6;
+    plan tests => 10;
 
     t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 0 );
 
@@ -716,11 +716,22 @@ subtest 'DelBiblio' => sub {
     my $order = $builder->build_object(
         { class => 'Koha::Acquisition::Orders', value => $orderinfo } );
 
+    # Add some ILL requests
+    my $ill_req_1 = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => $biblio->id, deleted_biblio_id => undef } });
+    my $ill_req_2 = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => $biblio->id, deleted_biblio_id => undef } });
+
     C4::Biblio::DelBiblio($biblio->biblionumber); # Or $biblio->delete
     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' );
     is( $order->get_from_storage->deleted_biblionumber, $biblio->biblionumber, 'biblionumber of order has been moved to deleted_biblionumber column' );
+
+    $ill_req_1 = $ill_req_1->get_from_storage;
+    $ill_req_2 = $ill_req_2->get_from_storage;
+    is( $ill_req_1->biblio_id, undef, 'biblio_id cleared on biblio deletion' );
+    is( $ill_req_1->deleted_biblio_id, $biblio->id, 'biblio_id is kept on the deleted_biblio_id column' );
+    is( $ill_req_2->biblio_id, undef, 'biblio_id cleared on biblio deletion' );
+    is( $ill_req_2->deleted_biblio_id, $biblio->id, 'biblio_id is kept on the deleted_biblio_id column' );
 };
 
 subtest 'MarcFieldForCreatorAndModifier' => sub {
-- 
2.41.0