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 <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Martin Renvoize 2024-10-23 11:07:52 +01:00 committed by Katrin Fischer
parent f1f790f51f
commit 48331bf28a
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
1 changed files with 46 additions and 3 deletions

View File

@ -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 {