From fcf728958e595cdaa450c117d403b447ee2c3589 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Tue, 20 Feb 2024 18:04:49 +0000
Subject: [PATCH] Bug 35944: Unit tests

Signed-off-by: David Nind <david@davidnind.com>
---
 t/db_dependent/Circulation.t | 38 +++++++++++++++++++++++++++++++++++-
 1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index c5d0b2a60b..e9af7f2121 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -18,7 +18,7 @@
 use Modern::Perl;
 use utf8;
 
-use Test::More tests => 69;
+use Test::More tests => 70;
 use Test::Exception;
 use Test::MockModule;
 use Test::Deep qw( cmp_deeply );
@@ -1690,6 +1690,42 @@ subtest "CanBookBeRenewed tests" => sub {
     $recall->set_cancelled;
 };
 
+subtest "CanBookBeRenewed | bookings" => sub {
+    plan tests => 3;
+
+    my $schema = Koha::Database->schema;
+    $schema->storage->txn_begin;
+
+    t::lib::Mocks::mock_preference('RenewalPeriodBase', 'date_due');
+
+    my $renewing_patron = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $booked_patron   = $builder->build_object( { class => 'Koha::Patrons' } );
+    my $item            = $builder->build_sample_item( { bookable => 1 } );
+
+    # issue
+    my $issue   = AddIssue( $renewing_patron, $item->barcode );
+    my $datedue = dt_from_string( $issue->date_due() );
+    is( defined $issue->date_due(), 1, "Item checked out, due date: " . $issue->date_due() );
+
+    # item-level booking
+    my $booking = Koha::Booking->new(
+        {
+            patron_id  => $booked_patron->borrowernumber,
+            item_id    => $item->itemnumber,
+            biblio_id  => $item->biblio->biblionumber,
+            start_date => $datedue->clone()->add( days => 2 ),
+            end_date   => $datedue->clone()->add( days => 10 ),
+        }
+    )->store();
+
+    # Proposed renewal would encroach on booking
+    my ( $renewok, $error ) = CanBookBeRenewed( $renewing_patron, $issue, 0 );
+    is( $renewok, 0,  "Renewal not allowed as it would mean the item was not returned before the next booking" );
+    is( $error,   'booked', "Error is 'booked'" );
+
+    $schema->storage->txn_rollback;
+};
+
 subtest "GetUpcomingDueIssues" => sub {
     plan tests => 12;
 
-- 
2.30.2