From f900fe7a9bbf0b6dcb6c6967b7df173b1bf31730 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 21 Jul 2020 18:45:37 +0200 Subject: [PATCH] Bug 20815: Tests refactoring Some code were duplicated. --- t/db_dependent/Circulation.t | 369 ++++++++++++----------------------- 1 file changed, 128 insertions(+), 241 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index b886a7b8db..eb7b54102e 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 => 51; +use Test::More tests => 48; use Test::MockModule; use Test::Deep qw( cmp_deeply ); @@ -4062,34 +4062,24 @@ subtest 'Filling a hold should cancel existing transfer' => sub { is( Koha::Item::Transfers->search({ itemnumber => $item->itemnumber, datearrived => undef })->count, 0, "No outstanding transfers when hold is waiting"); }; -subtest 'Tests for NoRefundOnLostReturnedItemsAge = undef' => sub { - plan tests => 3; - - t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); - t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', undef ); +subtest 'Tests for NoRefundOnLostReturnedItemsAge' => sub { - my $lost_on = dt_from_string->subtract( days => 7 )->date; + plan tests => 4; - my $library = $builder->build( { source => 'Branch' } ); - my $patron = $builder->build( + t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0); + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron = $builder->build_object( { - source => 'Borrower', - value => { categorycode => $patron_category->{categorycode} } + class => 'Koha::Patrons', + value => { categorycode => $patron_category->{categorycode} } } ); my $biblionumber = $builder->build_sample_biblio( { - branchcode => $library->{branchcode}, + branchcode => $library->branchcode, } )->biblionumber; - my $item = $builder->build_sample_item( - { - biblionumber => $biblionumber, - library => $library->{branchcode}, - replacementprice => '42.00', - } - ); # And the circulation rule Koha::CirculationRules->search->delete; @@ -4117,249 +4107,146 @@ subtest 'Tests for NoRefundOnLostReturnedItemsAge = undef' => sub { } ); - # Test with NoRefundOnLostReturnedItemsAge disabled - my $issue = AddIssue( $patron, $item->barcode ); - LostItem( $item->itemnumber, 'cli', 0 ); - $item->_result->itemlost(1); - $item->_result->itemlost_on( $lost_on ); - $item->_result->update(); - - my $a = Koha::Account::Lines->find( - { - itemnumber => $item->id, - borrowernumber => $patron->{borrowernumber} - } - ); - ok( $a, "Found accountline for lost fee" ); - is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); - my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->{branchcode}, undef, dt_from_string ); - $a = Koha::Account::Lines->find( $a->id ); - is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" ); -}; - -subtest 'Tests for NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub { - plan tests => 3; - - t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); - t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 ); - - my $lost_on = dt_from_string->subtract( days => 6 )->date; + subtest 'NoRefundOnLostReturnedItemsAge = undef' => sub { + plan tests => 3; - my $library = $builder->build( { source => 'Branch' } ); - my $patron = $builder->build( - { - source => 'Borrower', - value => { categorycode => $patron_category->{categorycode} } - } - ); + t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); + t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', undef ); - my $biblionumber = $builder->build_sample_biblio( - { - branchcode => $library->{branchcode}, - } - )->biblionumber; - my $item = $builder->build_sample_item( - { - biblionumber => $biblionumber, - library => $library->{branchcode}, - replacementprice => '42.00', - } - ); + my $lost_on = dt_from_string->subtract( days => 7 )->date; - # And the circulation rule - Koha::CirculationRules->search->delete; - Koha::CirculationRules->set_rules( - { - categorycode => undef, - itemtype => undef, - branchcode => undef, - rules => { - issuelength => 14, - lengthunit => 'days', - } - } - ); - $builder->build( - { - source => 'CirculationRule', - value => { - branchcode => undef, - categorycode => undef, - itemtype => undef, - rule_name => 'refund', - rule_value => 1 + my $item = $builder->build_sample_item( + { + biblionumber => $biblionumber, + library => $library->branchcode, + replacementprice => '42', } - } - ); - - # Test with NoRefundOnLostReturnedItemsAge disabled - my $issue = AddIssue( $patron, $item->barcode ); - LostItem( $item->itemnumber, 'cli', 0 ); - $item->_result->itemlost(1); - $item->_result->itemlost_on( $lost_on ); - $item->_result->update(); - - my $a = Koha::Account::Lines->find( - { - itemnumber => $item->id, - borrowernumber => $patron->{borrowernumber} - } - ); - ok( $a, "Found accountline for lost fee" ); - is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); - my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->{branchcode}, undef, dt_from_string ); - $a = Koha::Account::Lines->find( $a->id ); - is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" ); -}; - -subtest 'Tests for NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub { - plan tests => 3; + ); + my $issue = AddIssue( $patron->unblessed, $item->barcode ); + LostItem( $item->itemnumber, 'cli', 0 ); + $item->_result->itemlost(1); + $item->_result->itemlost_on( $lost_on ); + $item->_result->update(); - t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); - t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 ); + my $a = Koha::Account::Lines->search( + { + itemnumber => $item->id, + borrowernumber => $patron->borrowernumber + } + )->next; + ok( $a, "Found accountline for lost fee" ); + is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); + my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string ); + $a = $a->get_from_storage; + is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" ); + $a->delete; + }; - my $lost_on = dt_from_string->subtract( days => 7 )->date; + subtest 'NoRefundOnLostReturnedItemsAge > length of days item has been lost' => sub { + plan tests => 3; - my $library = $builder->build( { source => 'Branch' } ); - my $patron = $builder->build( - { - source => 'Borrower', - value => { categorycode => $patron_category->{categorycode} } - } - ); + t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); + t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 ); - my $biblionumber = $builder->build_sample_biblio( - { - branchcode => $library->{branchcode}, - } - )->biblionumber; - my $item = $builder->build_sample_item( - { - biblionumber => $biblionumber, - library => $library->{branchcode}, - replacementprice => '42.00', - } - ); + my $lost_on = dt_from_string->subtract( days => 6 )->date; - # And the circulation rule - Koha::CirculationRules->search->delete; - Koha::CirculationRules->set_rules( - { - categorycode => undef, - itemtype => undef, - branchcode => undef, - rules => { - issuelength => 14, - lengthunit => 'days', + my $item = $builder->build_sample_item( + { + biblionumber => $biblionumber, + library => $library->branchcode, + replacementprice => '42', } - } - ); - $builder->build( - { - source => 'CirculationRule', - value => { - branchcode => undef, - categorycode => undef, - itemtype => undef, - rule_name => 'refund', - rule_value => 1 + ); + my $issue = AddIssue( $patron->unblessed, $item->barcode ); + LostItem( $item->itemnumber, 'cli', 0 ); + $item->_result->itemlost(1); + $item->_result->itemlost_on( $lost_on ); + $item->_result->update(); + + my $a = Koha::Account::Lines->search( + { + itemnumber => $item->id, + borrowernumber => $patron->borrowernumber } - } - ); + )->next; + ok( $a, "Found accountline for lost fee" ); + is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); + my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string ); + $a = $a->get_from_storage; + is( $a->amountoutstanding + 0, 0, "Lost fee was refunded" ); + $a->delete; + }; - # Test with NoRefundOnLostReturnedItemsAge disabled - my $issue = AddIssue( $patron, $item->barcode ); - LostItem( $item->itemnumber, 'cli', 0 ); - $item->_result->itemlost(1); - $item->_result->itemlost_on( $lost_on ); - $item->_result->update(); + subtest 'NoRefundOnLostReturnedItemsAge = length of days item has been lost' => sub { + plan tests => 3; - my $a = Koha::Account::Lines->find( - { - itemnumber => $item->id, - borrowernumber => $patron->{borrowernumber} - } - ); - ok( $a, "Found accountline for lost fee" ); - is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); - my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->{branchcode}, undef, dt_from_string ); - $a = Koha::Account::Lines->find( $a->id ); - is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" ); -}; + t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); + t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 ); -subtest 'Tests for NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub { - plan tests => 3; + my $lost_on = dt_from_string->subtract( days => 7 )->date; - t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); - t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 ); + my $item = $builder->build_sample_item( + { + biblionumber => $biblionumber, + library => $library->branchcode, + replacementprice => '42', + } + ); + my $issue = AddIssue( $patron->unblessed, $item->barcode ); + LostItem( $item->itemnumber, 'cli', 0 ); + $item->_result->itemlost(1); + $item->_result->itemlost_on( $lost_on ); + $item->_result->update(); - my $lost_on = dt_from_string->subtract( days => 8 )->date; + my $a = Koha::Account::Lines->search( + { + itemnumber => $item->id, + borrowernumber => $patron->borrowernumber + } + )->next; + ok( $a, "Found accountline for lost fee" ); + is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); + my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string ); + $a = $a->get_from_storage; + is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" ); + $a->delete; + }; - my $library = $builder->build( { source => 'Branch' } ); - my $patron = $builder->build( - { - source => 'Borrower', - value => { categorycode => $patron_category->{categorycode} } - } - ); + subtest 'NoRefundOnLostReturnedItemsAge < length of days item has been lost' => sub { + plan tests => 3; - my $biblionumber = $builder->build_sample_biblio( - { - branchcode => $library->{branchcode}, - } - )->biblionumber; - my $item = $builder->build_sample_item( - { - biblionumber => $biblionumber, - library => $library->{branchcode}, - replacementprice => '42.00', - } - ); + t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 ); + t::lib::Mocks::mock_preference( 'NoRefundOnLostReturnedItemsAge', 7 ); - # And the circulation rule - Koha::CirculationRules->search->delete; - Koha::CirculationRules->set_rules( - { - categorycode => undef, - itemtype => undef, - branchcode => undef, - rules => { - issuelength => 14, - lengthunit => 'days', - } - } - ); - $builder->build( - { - source => 'CirculationRule', - value => { - branchcode => undef, - categorycode => undef, - itemtype => undef, - rule_name => 'refund', - rule_value => 1 - } - } - ); + my $lost_on = dt_from_string->subtract( days => 8 )->date; - # Test with NoRefundOnLostReturnedItemsAge disabled - my $issue = AddIssue( $patron, $item->barcode ); - LostItem( $item->itemnumber, 'cli', 0 ); - $item->_result->itemlost(1); - $item->_result->itemlost_on( $lost_on ); - $item->_result->update(); + my $item = $builder->build_sample_item( + { + biblionumber => $biblionumber, + library => $library->branchcode, + replacementprice => '42', + } + ); + my $issue = AddIssue( $patron->unblessed, $item->barcode ); + LostItem( $item->itemnumber, 'cli', 0 ); + $item->_result->itemlost(1); + $item->_result->itemlost_on( $lost_on ); + $item->_result->update(); - my $a = Koha::Account::Lines->find( - { - itemnumber => $item->id, - borrowernumber => $patron->{borrowernumber} - } - ); - ok( $a, "Found accountline for lost fee" ); - is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); - my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->{branchcode}, undef, dt_from_string ); - $a = Koha::Account::Lines->find( $a->id ); - is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" ); + my $a = Koha::Account::Lines->search( + { + itemnumber => $item->id, + borrowernumber => $patron->borrowernumber + } + ); + $a = $a->next; + ok( $a, "Found accountline for lost fee" ); + is( $a->amountoutstanding + 0, 42, "Lost fee charged correctly" ); + my ( $doreturn, $messages ) = AddReturn( $item->barcode, $library->branchcode, undef, dt_from_string ); + $a = $a->get_from_storage; + is( $a->amountoutstanding + 0, 42, "Lost fee was not refunded" ); + $a->delete; + }; }; $schema->storage->txn_rollback; -- 2.20.1