From cac4c01342e235c5521d5bc93f135a8c22d08f6a Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Thu, 4 Sep 2025 14:20:55 +0000 Subject: [PATCH] Bug 40644: Add tests If you apply this patch before the other one: prove t/db_dependent/Koha/Biblio.t Will fail. Apply both patches. The above now passes. This highlighted something. Following the test plan I can't reproduce this anymore. In order to reproduce, the check-outs need to happen before the bookings, otherwise when the check-outs happen on an existing booking it'll 'complete' or 'cancel' the booking thus not causing the error. --- t/db_dependent/Koha/Biblio.t | 73 +++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index f0d32ee3009..c4b485390c9 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -2194,7 +2194,7 @@ sub host_record { } subtest 'check_booking tests' => sub { - plan tests => 5; + plan tests => 6; $schema->storage->txn_begin; @@ -2309,5 +2309,76 @@ subtest 'check_booking tests' => sub { "Koha::Item->check_booking takes account of cancelled status in bookings check" ); + my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } ); + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + t::lib::Mocks::mock_userenv( { branchcode => $library->id } ); + + # Create a a test biblio with 8 items + my $new_biblio = $builder->build_object( + { + class => 'Koha::Biblios', + value => { title => 'Test biblio with items' } + } + ); + my @new_items; + for ( 1 .. 8 ) { + my $item = $builder->build_object( + { + class => 'Koha::Items', + value => { + homebranch => $library->branchcode, + holdingbranch => $library->branchcode, + biblionumber => $new_biblio->biblionumber, + bookable => 1 + } + } + ); + push @new_items, $item; + } + + my @item_numbers = map { $_->itemnumber } @new_items; + my @item_barcodes = map { $_->barcode } @new_items; + + # Check-out all of those 6 items + @item_barcodes = splice @item_barcodes, 0, 6; + for my $item_barcode (@item_barcodes) { + AddIssue( $patron_1, $item_barcode ); + } + + @item_numbers = splice @item_numbers, 0, 6; + my @new_bookings; + for my $itemnumber (@item_numbers) { + my $booking = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + biblio_id => $new_biblio->biblionumber, + item_id => $itemnumber, + start_date => $start_2, + end_date => $end_2, + status => 'new' + } + } + ); + push @new_bookings, $booking; + } + + # Place a booking on one of the 2 remaining items + my $item = ( grep { $_->itemnumber ne $new_bookings[0]->item_id } @new_items )[0]; + + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + + my $testttt = $new_biblio->bookings( { '-and' => [ { status => { '-not_in' => ['new'] } } ] } ); + + my $check_booking = $new_biblio->get_from_storage->check_booking( + { + start_date => $start_2, + end_date => $end_2, + item_id => $item->itemnumber + } + ); + + is( $check_booking, 1, "Koha::Biblio->check_booking returns true when we can book on an item" ); + $schema->storage->txn_rollback; }; -- 2.39.5