From 4c9bdc96643774b602f6df8895cf1450b5aae259 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Mon, 8 Apr 2024 09:50:37 +0100 Subject: [PATCH] Bug 22740: Write unit tests for automatic lost status switching This patch adds the unit tests for the new system preferences that will automatically change an items lost status to an authorised value defined in these preferences on payment or write off of outstanding balance. Signed-off-by: Martin Renvoize Signed-off-by: Laura Escamilla Signed-off-by: Nick Clemens --- t/db_dependent/Circulation.t | 229 ++++++++++++++++++++++++++++++++++- 1 file changed, 228 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 5e88a29a00a..ea6cc0774d6 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -18,7 +18,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 70; +use Test::More tests => 73; use Test::Exception; use Test::MockModule; use Test::Deep qw( cmp_deeply ); @@ -5429,6 +5429,233 @@ subtest 'Do not return on renewal (LOST charge)' => sub { ); }; +subtest 'Lost status does not change when preferences are set to 0' => sub { + plan tests => 2; + + my $library = $builder->build_object( { class => "Koha::Libraries" } ); + my $manager = $builder->build_object( { class => "Koha::Patrons" } ); + t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } ); + + my $biblio = $builder->build_sample_biblio; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $item = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + library => $library->branchcode, + replacementprice => 99, + itype => $itemtype, + itemlost => 1, + } + ); + + my $debitCharge = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + debit_type_code => 'LOST', + status => undef, + itemnumber => $item->itemnumber, + amount => 12, + amountoutstanding => 12, + interface => 'something', + } + )->store(); + + # Test for UpdateItemLostStatusWhenPaid + t::lib::Mocks::mock_preference( 'UpdateItemLostStatusWhenPaid', 0 ); + + my $paymentLine = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + credit_type_code => 'CREDIT', + status => undef, + itemnumber => $item->itemnumber, + amountoutstanding => 0 - 12, + amount => 0 - 12, + interface => 'something', + } + )->store(); + + my $offset = Koha::Account::Offset->new( + { + credit_id => $paymentLine->id, + debit_id => $debitCharge->id, + type => 'APPLY', + amount => 0 + } + )->store(); + + $paymentLine->apply( { debits => [$debitCharge] } ); + + is( + Koha::Items->find( $item->itemnumber )->itemlost, 1, + "Payment should not change itemlost status when UpdateItemLostStatusWhenPaid is 0" + ); + + # Test for UpdateItemLostStatusWhenWriteOff + t::lib::Mocks::mock_preference( 'UpdateItemLostStatusWhenWriteOff', 0 ); + + my $writeOffLine = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + credit_type_code => 'WRITEOFF', + status => undef, + itemnumber => $item->itemnumber, + amountoutstanding => 0 - 12, + amount => 0 - 12, + interface => 'something', + } + )->store(); + + $offset = Koha::Account::Offset->new( + { + credit_id => $writeOffLine->id, + debit_id => $debitCharge->id, + type => 'APPLY', + amount => 0 + } + )->store(); + + $writeOffLine->apply( { debits => [$debitCharge] } ); + + is( + Koha::Items->find( $item->itemnumber )->itemlost, 1, + "Write-off should not change itemlost status when UpdateItemLostStatusWhenWriteOff is 0" + ); +}; + +# Test for UpdateItemLostStatusWhenPaid +subtest 'Update lost item to authorized value on payment of balance' => sub { + plan tests => 5; + + my $library = $builder->build_object( { class => "Koha::Libraries" } ); + my $manager = $builder->build_object( { class => "Koha::Patrons" } ); + t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } ); + + my $biblio = $builder->build_sample_biblio; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + for my $status ( 2 .. 6 ) { + t::lib::Mocks::mock_preference( 'UpdateItemLostStatusWhenPaid', $status ); + + my $item = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + library => $library->branchcode, + replacementprice => 99, + itype => $itemtype, + itemlost => 1, + } + ); + + my $debitCharge = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + debit_type_code => 'LOST', + status => undef, + itemnumber => $item->itemnumber, + amount => 12, + amountoutstanding => 12, + interface => 'something', + } + )->store(); + + my $paymentLine = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + credit_type_code => 'CREDIT', + status => undef, + itemnumber => $item->itemnumber, + amountoutstanding => 0 - 12, + amount => 0 - 12, + interface => 'something', + } + )->store(); + + my $offset = Koha::Account::Offset->new( + { + credit_id => $paymentLine->id, + debit_id => $debitCharge->id, + type => 'APPLY', + amount => 0 + } + )->store(); + + $paymentLine->apply( { debits => [$debitCharge] } ); + + is( + Koha::Items->find( $item->itemnumber )->itemlost, $status, + "Payment should set itemlost to status $status" + ); + } +}; + +# Test for UpdateItemLostStatusWhenWriteOff +subtest 'Update lost item to authorized value on write-off of balance' => sub { + plan tests => 5; + + my $library = $builder->build_object( { class => "Koha::Libraries" } ); + my $manager = $builder->build_object( { class => "Koha::Patrons" } ); + t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } ); + + my $biblio = $builder->build_sample_biblio; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + for my $status ( 2 .. 6 ) { + t::lib::Mocks::mock_preference( 'UpdateItemLostStatusWhenWriteOff', $status ); + + my $item = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + library => $library->branchcode, + replacementprice => 99, + itype => $itemtype, + itemlost => 1, + } + ); + + my $debitCharge = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + debit_type_code => 'LOST', + status => undef, + itemnumber => $item->itemnumber, + amount => 12, + amountoutstanding => 12, + interface => 'something', + } + )->store(); + + my $writeOffLine = Koha::Account::Line->new( + { + borrowernumber => $patron->borrowernumber, + credit_type_code => 'WRITEOFF', + status => undef, + itemnumber => $item->itemnumber, + amountoutstanding => 0 - 12, + amount => 0 - 12, + interface => 'something', + } + )->store(); + + my $offset = Koha::Account::Offset->new( + { + credit_id => $writeOffLine->id, + debit_id => $debitCharge->id, + type => 'APPLY', + amount => 0 + } + )->store(); + + $writeOffLine->apply( { debits => [$debitCharge] } ); + + is( + Koha::Items->find( $item->itemnumber )->itemlost, $status, + "Write-off should set itemlost to status $status" + ); + } +}; + subtest 'Filling a hold should cancel existing transfer' => sub { plan tests => 4; -- 2.39.2