From fce186a5ea3b90b078200044682d295fd8d402bf Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 3 Jun 2025 15:28:16 -0300 Subject: [PATCH] Bug 40062: Trying to understand priority calculation --- t/db_dependent/Reserves.t | 189 +++++++++++++++++++++++++++++++++++++- 1 file changed, 188 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index b66706dcfc2..f9aed1a4771 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 69; +use Test::More tests => 70; use Test::MockModule; use Test::Warn; @@ -2091,3 +2091,190 @@ subtest 'HOLDDGST tests' => sub { $schema->txn_rollback; }; + +subtest 'Hold priority tests' => sub { + + plan tests => 2; + + subtest 'Using priority=1' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + # Create the items and patrons we need + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $itype = $builder->build_object( { class => "Koha::ItemTypes", value => { notforloan => 0 } } ); + my $item = $builder->build_sample_item( + { + itype => $itype->itemtype, + library => $library->branchcode + } + ); + my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } ); + my $patron_2 = $builder->build_object( { class => "Koha::Patrons" } ); + my $patron_3 = $builder->build_object( { class => "Koha::Patrons" } ); + my $patron_4 = $builder->build_object( { class => "Koha::Patrons" } ); + + # Place a hold on the title for both patrons + my $hold_1 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_1->borrowernumber, + biblionumber => $item->biblionumber, + priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + my $hold_2 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_2->borrowernumber, + biblionumber => $item->biblionumber, + priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + my $hold_3 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_3->borrowernumber, + biblionumber => $item->biblionumber, + priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + my $hold_4 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_4->borrowernumber, + biblionumber => $item->biblionumber, + priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + + # Each AddReserve call has an impact on the other hold's priorities + # Refresh just in case + $hold_1->discard_changes; + $hold_2->discard_changes; + $hold_3->discard_changes; + $hold_4->discard_changes; + + is( $item->biblio->holds->count, 4, '4 holds on the biblio' ); + + use DDP; + p( $hold_1->priority ); + p( $hold_2->priority ); + p( $hold_3->priority ); + p( $hold_4->priority ); + + is( $hold_1->priority, 1, 'Priority is correct (1)' ); + is( $hold_2->priority, 2, 'Priority is correct (2)' ); + is( $hold_3->priority, 3, 'Priority is correct (3)' ); + is( $hold_4->priority, 4, 'Priority is correct (4)' ); + + $schema->storage->txn_rollback; + }; + + subtest 'Using priority=undef' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + # Create the items and patrons we need + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $itype = $builder->build_object( { class => "Koha::ItemTypes", value => { notforloan => 0 } } ); + my $item = $builder->build_sample_item( + { + itype => $itype->itemtype, + library => $library->branchcode + } + ); + my $patron_1 = $builder->build_object( { class => "Koha::Patrons" } ); + my $patron_2 = $builder->build_object( { class => "Koha::Patrons" } ); + my $patron_3 = $builder->build_object( { class => "Koha::Patrons" } ); + my $patron_4 = $builder->build_object( { class => "Koha::Patrons" } ); + + # Place a hold on the title for both patrons + my $hold_1 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_1->borrowernumber, + biblionumber => $item->biblionumber, + + # priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + my $hold_2 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_2->borrowernumber, + biblionumber => $item->biblionumber, + + # priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + my $hold_3 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_3->borrowernumber, + biblionumber => $item->biblionumber, + + # priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + my $hold_4 = Koha::Holds->find( + C4::Reserves::AddReserve( + { + branchcode => $library->branchcode, + borrowernumber => $patron_4->borrowernumber, + biblionumber => $item->biblionumber, + + # priority => 1, + itemnumber => $item->itemnumber, + } + ) + ); + + # Each AddReserve call has an impact on the other hold's priorities + # Refresh just in case + $hold_1->discard_changes; + $hold_2->discard_changes; + $hold_3->discard_changes; + $hold_4->discard_changes; + + is( $item->biblio->holds->count, 4, '4 holds on the biblio' ); + + use DDP; + p( $hold_1->priority ); + p( $hold_2->priority ); + p( $hold_3->priority ); + p( $hold_4->priority ); + + is( $hold_1->priority, 1, 'Priority is correct (1)' ); + is( $hold_2->priority, 2, 'Priority is correct (2)' ); + is( $hold_3->priority, 3, 'Priority is correct (3)' ); + is( $hold_4->priority, 4, 'Priority is correct (4)' ); + + $schema->storage->txn_rollback; + }; +}; -- 2.49.0