From 48331bf28ab220f2c8c9d3d94c94eda4cd7a618c Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 23 Oct 2024 11:07:52 +0100 Subject: [PATCH] Bug 38175: Unit tests for assign_item_for_booking This patch adds more comprehensive tests to the item autoassignment for 'Any item' handling in bookings. We check for both random assignment, which was missing before now but always intended, and that we take cancelled status into account. Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- t/db_dependent/Koha/Booking.t | 49 ++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Koha/Booking.t b/t/db_dependent/Koha/Booking.t index 1c4ec2debc..19aa536b12 100755 --- a/t/db_dependent/Koha/Booking.t +++ b/t/db_dependent/Koha/Booking.t @@ -314,13 +314,56 @@ subtest 'store() tests' => sub { ok( $booking->item_id, 'An item was assigned to the booking' ); subtest '_assign_item_for_booking() tests' => sub { - plan tests => 1; - is( $booking->item_id, $item_1->itemnumber, "Item 1 was assigned to the booking" ); + plan tests => 5; # Bookings # ✓ Item 1 |----| # ✓ Item 2 |--| - # ✓ Any (1) |--| + # ✓ Any (X) |--| + my $valid_items = [ $item_1->itemnumber, $item_2->itemnumber ]; + my $assigned_item = $booking->item_id; + is( + ( scalar grep { $_ == $assigned_item } @$valid_items ), 1, + 'The item assigned was one of the valid, bookable items' + ); + + my $second_booking = Koha::Booking->new( + { + patron_id => $patron->borrowernumber, + biblio_id => $biblio->biblionumber, + pickup_library_id => $item_2->homebranch, + start_date => $start_1, + end_date => $end_1 + } + )->store(); + isnt( $second_booking->item_id, $assigned_item, "The subsequent booking picks the only other available item" ); + + # Cancel both bookings so we can check that cancelled bookings are allowed in the auto-assign + $booking->status('cancelled')->store(); + $second_booking->status('cancelled')->store(); + is($booking->status, 'cancelled', "Booking is cancelled"); + is($second_booking->status, 'cancelled', "Second booking is cancelled"); + + # Test randomness of selection + my %seen_items; + foreach my $i ( 1 .. 10 ) { + my $new_booking = Koha::Booking->new( + { + patron_id => $patron->borrowernumber, + biblio_id => $biblio->biblionumber, + pickup_library_id => $item_1->homebranch, + start_date => $start_1, + end_date => $end_1 + } + ); + $new_booking->store(); + $seen_items{ $new_booking->item_id }++; + $new_booking->delete(); + } + ok( + scalar( keys %seen_items ) > 1, + 'Multiple different items were selected randomly across bookings, and a cancelled booking is allowed in the selection' + ); }; subtest 'confirmation notice trigger' => sub { -- 2.39.5