From 33d5ab614c3fc66cc16b1910017baf4e876054d9 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 22 Aug 2024 11:02:58 +0100 Subject: [PATCH] Bug 34440: Unit test for change to Koha::Item This adds a unit test for the new preparation_period handling introduced into the find_booking method of Koha::Item. Sponsored-by: Cuyahoga County Public Library Signed-off-by: Paul Derscheid Signed-off-by: Katrin Fischer --- Koha/Item.pm | 4 ++- t/db_dependent/Koha/Item.t | 50 +++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index d2e475df02..05d65d1da3 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -586,7 +586,9 @@ sub bookings { my $booking = $item->find_booking( { checkout_date => $now, due_date => $future_date } ); -Find the first booking that would conflict with the passed checkout dates for this item. +Find the first booking that would conflict with the passed checkout dates for this item. If a booking +lead period is configured for the itemtype we will also take that into account here, counting bookings +that fall in that lead period as conflicts too. FIXME: This can be simplified, it was originally intended to iterate all biblio level bookings to catch cases where this item may be the last available to satisfy a biblio level only booking. diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 9e3824842a..5d3d780f35 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -2600,7 +2600,7 @@ subtest 'bookings' => sub { }; subtest 'find_booking' => sub { - plan tests => 6; + plan tests => 7; $schema->storage->txn_begin; @@ -2707,6 +2707,54 @@ subtest 'find_booking' => sub { "Koha::Item->find_booking returns the current booking not a future one" ); + subtest "Preparation period handling" => sub { + plan tests => 3; + + # Delete current booking, is the future booking returned? + $booking2->delete(); + $found_booking = $item->find_booking( + { + checkout_date => dt_from_string(), + due_date => dt_from_string()->add( days => 7 ), + } + ); + + is( + $found_booking, + undef, + "Koha::Item->find_booking returns undefined when the current booking is deleted and the future booking is out of range and there's no lead period rule" + ); + + # Adding lead period rule + Koha::CirculationRules->set_rules( + { + branchcode => '*', + itemtype => $item->effective_itemtype, + rules => { + bookings_lead_period => 3, + }, + } + ); + + $found_booking = $item->find_booking( + { + checkout_date => dt_from_string(), + due_date => dt_from_string()->add( days => 7 ), + } + ); + + is( + ref($found_booking), + 'Koha::Booking', + "Koha::Item->find_booking returns a Koha::Booking if one exists that would clash with the passed dates including lead period" + ); + is( + $found_booking->booking_id, $booking3->booking_id, + "Koha::Item->find_booking returns the future booking when lead period is included" + ); + + }; + $schema->storage->txn_rollback; }; -- 2.39.5