From a82f7399b5ded50f41b5b79736abfc496de1a769 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 16 Jan 2024 12:47:18 +0000 Subject: [PATCH] Bug 35248: Add unit tests for Koha::Item->find_booking This patch adds basic unit tests for the Koha::Item->find_booking method. Test plan 1) Run t/db_dependant/Koha/Item.t Signed-off-by: David Nind Signed-off-by: Nick Clemens Signed-off-by: Katrin Fischer (cherry picked from commit dab5694dad54616367d0ecba365a37711e86ad2f) Signed-off-by: Fridolin Somers --- Koha/Item.pm | 15 +++-- t/db_dependent/Koha/Item.t | 113 ++++++++++++++++++++++++++++++++++++- 2 files changed, 123 insertions(+), 5 deletions(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 22eaceac94..ab7f269db3 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -528,7 +528,14 @@ sub bookings { =head3 find_booking -Find the first booking that would conflict with the passed checkout dates + 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. + +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. +However, we dropped the biblio level functionality prior to push as bugs were found in it's +implimentation. =cut @@ -542,7 +549,7 @@ sub find_booking { my $dtf = Koha::Database->new->schema->storage->datetime_parser; my $bookings = $biblio->bookings( [ - # Checkout starts during booked period + # Proposed checkout starts during booked period start_date => { '-between' => [ $dtf->format_datetime($checkout_date), @@ -550,7 +557,7 @@ sub find_booking { ] }, - # Checkout is due during booked period + # Proposed checkout is due during booked period end_date => { '-between' => [ $dtf->format_datetime($checkout_date), @@ -558,7 +565,7 @@ sub find_booking { ] }, - # Checkout contains booked period + # Proposed checkout would contain the booked period { start_date => { '<' => $dtf->format_datetime($checkout_date) }, end_date => { '>' => $dtf->format_datetime($due_date) } diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 0e47c5d4cc..6016a6dc7b 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -20,7 +20,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 32; +use Test::More tests => 33; use Test::Exception; use Test::MockModule; @@ -2408,3 +2408,114 @@ subtest 'bookings' => sub { $schema->storage->txn_rollback; }; + +subtest 'find_booking' => sub { + plan tests => 6; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 1 } ); + my $found_booking = $item->find_booking( + { + checkout_date => dt_from_string(), + due_date => dt_from_string()->add( days => 7 ), + } + ); + + is( + $found_booking, undef, + "Nothing returned from Koha::Item->find_booking if there are no bookings" + ); + + my $start_1 = dt_from_string()->subtract( days => 7 ); + my $end_1 = dt_from_string()->subtract( days => 1 ); + my $start_2 = dt_from_string(); + my $end_2 = dt_from_string()->add( days => 7 ); + my $start_3 = dt_from_string()->add( days => 8 ); + my $end_3 = dt_from_string()->add( days => 16 ); + + # Past booking + my $booking1 = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + item_id => $item->itemnumber, + start_date => $start_1, + end_date => $end_1 + } + } + ); + + $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 if the passed dates do not conflict with any item bookings" + ); + + # Current booking + my $booking2 = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + item_id => $item->itemnumber, + start_date => $start_2, + end_date => $end_2 + } + } + ); + + $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" + ); + is( $found_booking->booking_id, $booking2->booking_id, "Koha::Item->find_booking returns the current booking" ); + + # Future booking + my $booking3 = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $biblio->biblionumber, + item_id => $item->itemnumber, + start_date => $start_3, + end_date => $end_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" + ); + is( + $found_booking->booking_id, $booking2->booking_id, + "Koha::Item->find_booking returns the current booking not a future one" + ); + + $schema->storage->txn_rollback; +}; -- 2.39.5