From d88c331cd6a54e3be236b292591455d9c93680fe Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Mon, 18 Aug 2025 15:41:32 +0000 Subject: [PATCH] Bug 40665: Add tests --- t/db_dependent/Circulation.t | 100 ++++++++++++++++++++++++++++++++- t/db_dependent/Koha/Booking.t | 86 +++++++++++++++++++++++++++- t/db_dependent/Koha/Checkout.t | 32 ++++++++++- 3 files changed, 215 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 48b4c02d2f0..fb4f0c0c019 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -19,7 +19,7 @@ use Modern::Perl; use utf8; use Test::NoWarnings; -use Test::More tests => 78; +use Test::More tests => 80; use Test::Exception; use Test::MockModule; use Test::Deep qw( cmp_deeply ); @@ -7120,6 +7120,104 @@ subtest "SendCirculationAlert" => sub { }; +subtest 'AddIssue | booking_id field linkage' => sub { + plan tests => 6; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } ); + my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } ); + my $item = $builder->build_sample_item( { bookable => 1 } ); + + # Patron checks out item with their own booking - booking_id should be set + my $booking1 = Koha::Booking->new( + { + patron_id => $patron1->borrowernumber, + pickup_library_id => $library->branchcode, + item_id => $item->itemnumber, + biblio_id => $item->biblio->biblionumber, + start_date => dt_from_string(), + end_date => dt_from_string()->add( days => 5 ), + } + )->store(); + + my $issue1 = AddIssue( $patron1, $item->barcode, dt_from_string()->add( days => 7 ) ); + is( $issue1->booking_id, $booking1->booking_id, "Checkout linked to patron's own booking via booking_id" ); + + # Verify the relationship accessor works + my $linked_booking = $issue1->booking; + is( $linked_booking->booking_id, $booking1->booking_id, "Booking relationship accessor returns correct booking" ); + + # Verify booking_id is preserved when moved to old_issues on return + my ( $returned, undef, undef, undef ) = AddReturn( $item->barcode, $library->branchcode ); + is( $returned, 1, "Item returned successfully" ); + + my $old_checkout = Koha::Old::Checkouts->find( { issue_id => $issue1->issue_id } ); + is( $old_checkout->booking_id, $booking1->booking_id, "booking_id preserved in old_issues after return" ); + + # Verify old_checkout booking relationship works + my $old_linked_booking = $old_checkout->booking; + is( $old_linked_booking->booking_id, $booking1->booking_id, "Old checkout booking relationship accessor works" ); + + # Another patron checks out - no booking_id should be set + $booking1->delete(); + my $issue2 = AddIssue( $patron2, $item->barcode, dt_from_string()->add( days => 7 ) ); + is( $issue2->booking_id, undef, "No booking_id set when checkout is not from a patron's own booking" ); +}; + +subtest 'AddRenewal | booking_id preservation' => sub { + plan tests => 3; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + my $item = $builder->build_sample_item( { library => $library->branchcode } ); + + # Set up renewal rules + Koha::CirculationRules->set_rules( + { + branchcode => $library->branchcode, + categorycode => $patron->categorycode, + itemtype => $item->effective_itemtype, + rules => { + renewalsallowed => 10, + renewalperiod => 7, + issuelength => 7, + } + } + ); + + # Create a simple booking using the builder + my $booking = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + patron_id => $patron->borrowernumber, + item_id => $item->itemnumber, + pickup_library_id => $library->branchcode, + status => 'completed' + } + } + ); + + # Create a checkout and manually set booking_id to simulate a booking-linked checkout + my $issue = AddIssue( $patron, $item->barcode ); + $issue->booking_id( $booking->booking_id )->store; + + # Renew the checkout + AddRenewal( + { + borrowernumber => $patron->borrowernumber, + itemnumber => $item->itemnumber, + branch => $library->branchcode, + } + ); + + # Refresh the issue object and test that booking_id is preserved + $issue = $issue->get_from_storage; + is( $issue->booking_id, $booking->booking_id, "booking_id preserved after renewal" ); + is( $issue->renewals_count, 1, "Renewal count incremented" ); + ok( defined $issue->booking_id, "booking_id field is not null after renewal" ); +}; + subtest "GetSoonestRenewDate tests" => sub { plan tests => 6; Koha::CirculationRules->set_rule( diff --git a/t/db_dependent/Koha/Booking.t b/t/db_dependent/Koha/Booking.t index a63d37b120f..95048b5a74b 100755 --- a/t/db_dependent/Koha/Booking.t +++ b/t/db_dependent/Koha/Booking.t @@ -38,7 +38,7 @@ my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; subtest 'Relation accessor tests' => sub { - plan tests => 4; + plan tests => 6; subtest 'biblio relation tests' => sub { plan tests => 3; @@ -125,6 +125,90 @@ subtest 'Relation accessor tests' => sub { $schema->storage->txn_rollback; }; + + subtest 'checkout relation tests' => sub { + plan tests => 4; + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => "Koha::Patrons" } ); + my $item = $builder->build_sample_item( { bookable => 1 } ); + my $booking = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + patron_id => $patron->borrowernumber, + item_id => $item->itemnumber, + status => 'completed' + } + } + ); + + my $checkout = $booking->checkout; + is( $checkout, undef, "Koha::Booking->checkout returns undef when no checkout exists" ); + + my $issue = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + borrowernumber => $patron->borrowernumber, + itemnumber => $item->itemnumber, + booking_id => $booking->booking_id + } + } + ); + + $checkout = $booking->checkout; + is( ref($checkout), 'Koha::Checkout', "Koha::Booking->checkout returns a Koha::Checkout object" ); + is( $checkout->issue_id, $issue->issue_id, "Koha::Booking->checkout returns the linked checkout" ); + is( $checkout->booking_id, $booking->booking_id, "The checkout is properly linked to the booking" ); + + $schema->storage->txn_rollback; + }; + + subtest 'old_checkout relation tests' => sub { + plan tests => 4; + $schema->storage->txn_begin; + + my $patron = $builder->build_object( { class => "Koha::Patrons" } ); + my $item = $builder->build_sample_item( { bookable => 1 } ); + my $booking = $builder->build_object( + { + class => 'Koha::Bookings', + value => { + patron_id => $patron->borrowernumber, + item_id => $item->itemnumber, + status => 'completed' + } + } + ); + + my $old_checkout = $booking->old_checkout; + is( $old_checkout, undef, "Koha::Booking->old_checkout returns undef when no old_checkout exists" ); + + my $old_issue = $builder->build_object( + { + class => 'Koha::Old::Checkouts', + value => { + borrowernumber => $patron->borrowernumber, + itemnumber => $item->itemnumber, + booking_id => $booking->booking_id + } + } + ); + + $old_checkout = $booking->old_checkout; + is( + ref($old_checkout), 'Koha::Old::Checkout', + "Koha::Booking->old_checkout returns a Koha::Old::Checkout object" + ); + is( + $old_checkout->issue_id, $old_issue->issue_id, + "Koha::Booking->old_checkout returns the linked old_checkout" + ); + is( $old_checkout->booking_id, $booking->booking_id, "The old_checkout is properly linked to the booking" ); + + $schema->storage->txn_rollback; + }; }; subtest 'store() tests' => sub { diff --git a/t/db_dependent/Koha/Checkout.t b/t/db_dependent/Koha/Checkout.t index 215c895acb3..57e232d3313 100755 --- a/t/db_dependent/Koha/Checkout.t +++ b/t/db_dependent/Koha/Checkout.t @@ -20,7 +20,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 5; +use Test::More tests => 6; use t::lib::TestBuilder; use Koha::Database; @@ -187,3 +187,33 @@ subtest 'public_read_list() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'booking() tests' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + my $booking = $builder->build_object( { class => 'Koha::Bookings' } ); + my $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { booking_id => $booking->booking_id } + } + ); + + my $linked_booking = $checkout->booking; + is( ref($linked_booking), 'Koha::Booking', 'booking() returns a Koha::Booking object' ); + is( $linked_booking->booking_id, $booking->booking_id, 'booking() returns the correct booking' ); + + my $checkout_no_booking = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { booking_id => undef } + } + ); + + my $no_booking = $checkout_no_booking->booking; + is( $no_booking, undef, 'booking() returns undef when no booking_id is set' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5