Bug 23177: (QA follow-up) Remove subtest txn_begin and rollback
[koha-equinox.git] / t / db_dependent / Circulation.t
index 37ab7a5..6219ada 100755 (executable)
@@ -48,6 +48,53 @@ use Koha::Account::Lines;
 use Koha::Account::Offsets;
 use Koha::ActionLogs;
 
+sub set_userenv {
+    my ( $library ) = @_;
+    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
+}
+
+sub str {
+    my ( $error, $question, $alert ) = @_;
+    my $s;
+    $s  = %$error    ? ' (error: '    . join( ' ', keys %$error    ) . ')' : '';
+    $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
+    $s .= %$alert    ? ' (alert: '    . join( ' ', keys %$alert    ) . ')' : '';
+    return $s;
+}
+
+sub test_debarment_on_checkout {
+    my ($params) = @_;
+    my $item     = $params->{item};
+    my $library  = $params->{library};
+    my $patron   = $params->{patron};
+    my $due_date = $params->{due_date} || dt_from_string;
+    my $return_date = $params->{return_date} || dt_from_string;
+    my $expected_expiration_date = $params->{expiration_date};
+
+    $expected_expiration_date = output_pref(
+        {
+            dt         => $expected_expiration_date,
+            dateformat => 'sql',
+            dateonly   => 1,
+        }
+    );
+    my @caller      = caller;
+    my $line_number = $caller[2];
+    AddIssue( $patron, $item->{barcode}, $due_date );
+
+    my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode}, undef, $return_date );
+    is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
+        or diag('AddReturn returned message ' . Dumper $message );
+    my $debarments = Koha::Patron::Debarments::GetDebarments(
+        { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
+    is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
+
+    is( $debarments->[0]->{expiration},
+        $expected_expiration_date, 'Test at line ' . $line_number );
+    Koha::Patron::Debarments::DelUniqueDebarment(
+        { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
+};
+
 my $schema = Koha::Database->schema;
 $schema->storage->txn_begin;
 my $builder = t::lib::TestBuilder->new;
@@ -1997,6 +2044,17 @@ subtest 'AddReturn | is_overdue' => sub {
     t::lib::Mocks::mock_userenv({ patron => $manager, branchcode => $manager->branchcode });
 
     my $biblioitem = $builder->build( { source => 'Biblioitem' } );
+    my $item_type          = $builder->build_object(
+        {   class => 'Koha::ItemTypes',
+            value => {
+                notforloan         => undef,
+                rentalcharge       => 0,
+                defaultreplacecost => undef,
+                processfee         => 0,
+                rentalcharge_daily => 0,
+            }
+        }
+    );
     my $item = $builder->build(
         {
             source => 'Item',
@@ -2007,6 +2065,8 @@ subtest 'AddReturn | is_overdue' => sub {
                 itemlost      => 0,
                 withdrawn     => 0,
                 biblionumber  => $biblioitem->{biblionumber},
+                replacementprice => 7,
+                itype         => $item_type->itemtype
             }
         }
     );
@@ -2025,40 +2085,68 @@ subtest 'AddReturn | is_overdue' => sub {
     );
     $rule->store();
 
+    my $now   = dt_from_string;
     my $one_day_ago   = dt_from_string->subtract( days => 1 );
     my $five_days_ago = dt_from_string->subtract( days => 5 );
     my $ten_days_ago  = dt_from_string->subtract( days => 10 );
     $patron = Koha::Patrons->find( $patron->{borrowernumber} );
 
-    # No date specify, today will be used
+    # No return date specified, today will be used => 10 days overdue charged
     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
     AddReturn( $item->{barcode}, $library->{branchcode} );
     is( int($patron->account->balance()), 10, 'Patron should have a charge of 10 (10 days x 1)' );
     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
 
-    # specify return date 5 days before => no overdue
+    # specify return date 5 days before => no overdue charged
     AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
     AddReturn( $item->{barcode}, $library->{branchcode}, undef, $ten_days_ago );
     is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
 
-    # specify return date 5 days later => overdue
+    # specify return date 5 days later => 5 days overdue charged
     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
     AddReturn( $item->{barcode}, $library->{branchcode}, undef, $five_days_ago );
     is( int($patron->account->balance()), 5, 'AddReturn: pass return_date => overdue' );
     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
 
-    # specify dropbox date 5 days before => no overdue
-    AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
-    AddReturn( $item->{barcode}, $library->{branchcode}, $ten_days_ago );
+    # specify return date 5 days later, specify exemptfine => no overdue charge
+    AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
+    AddReturn( $item->{barcode}, $library->{branchcode}, 1, $five_days_ago );
     is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue' );
     Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
 
-    # specify dropbox date 5 days later => overdue, or... not
-    AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
-    AddReturn( $item->{barcode}, $library->{branchcode}, $five_days_ago );
-    is( int($patron->account->balance()), 0, 'AddReturn: pass return_date => no overdue in dropbox mode' ); # FIXME? This is weird, the OVERDUE fine is created ( _CalculateAndUpdateFine > C4::Overdues::UpdateFine ) then remove later (in _FixOverduesOnReturn). Looks like it is a feature
-    Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber })->delete;
+    subtest 'bug 22877' => sub {
+
+        plan tests => 3;
+
+        my $issue = AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago );    # date due was 10d ago
+
+        # Fake fines cronjob on this checkout
+        my ($fine) =
+          CalcFine( $item, $patron->categorycode, $library->{branchcode},
+            $ten_days_ago, $now );
+        UpdateFine(
+            {
+                issue_id       => $issue->issue_id,
+                itemnumber     => $item->{itemnumber},
+                borrowernumber => $patron->borrowernumber,
+                amount         => $fine,
+                due            => output_pref($ten_days_ago)
+            }
+        );
+        is( int( $patron->account->balance() ),
+            10, "Overdue fine of 10 days overdue" );
+
+        # Fake longoverdue with charge and not marking returned
+        LostItem( $item->{itemnumber}, 'cronjob', 0 );
+        is( int( $patron->account->balance() ),
+            17, "Lost fine of 7 plus 10 days overdue" );
+
+        # Now we return it today
+        AddReturn( $item->{barcode}, $library->{branchcode} );
+        is( int( $patron->account->balance() ),
+            17, "Should have a single 10 days overdue fine and lost charge" );
+      }
 };
 
 subtest '_FixAccountForLostAndReturned' => sub {
@@ -2949,15 +3037,11 @@ subtest 'AddReturn should clear items.onloan for unissued items' => sub {
     is( $item->onloan, undef, 'AddReturn did clear items.onloan' );
 };
 
-$schema->storage->txn_rollback;
-C4::Context->clear_syspref_cache();
-$cache->clear_from_cache('single_holidays');
 
 subtest 'AddRenewal and AddIssuingCharge tests' => sub {
 
     plan tests => 13;
 
-    $schema->storage->txn_begin;
 
     t::lib::Mocks::mock_preference('item-level_itypes', 1);
 
@@ -3000,7 +3084,7 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
     # Check the item out
     AddIssue( $patron->unblessed, $item->barcode );
     t::lib::Mocks::mock_preference( 'RenewalLog', 0 );
-    my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
+    my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
     my %params_renewal = (
         timestamp => { -like => $date . "%" },
         module => "CIRCULATION",
@@ -3016,7 +3100,7 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
     unlike ( $checkouts->next->lastreneweddate, qr/00:00:00/, 'AddRenewal should set the renewal date with the time part');
 
     t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
-    $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
+    $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
     $old_log_size = Koha::ActionLogs->count( \%params_renewal );
     AddRenewal( $patron->id, $item->id, $library->id );
     $new_log_size = Koha::ActionLogs->count( \%params_renewal );
@@ -3044,14 +3128,12 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
     is( $line->branchcode,  $library->id, 'AddRenewal correctly sets branchcode' );
     is( $line->description, "Renewal of Rental Item $title $barcode", 'AddRenewal set a hardcoded description for the accountline' );
 
-    $schema->storage->txn_rollback;
 };
 
 subtest 'ProcessOfflinePayment() tests' => sub {
 
     plan tests => 4;
 
-    $schema->storage->txn_begin;
 
     my $amount = 123;
 
@@ -3068,56 +3150,6 @@ subtest 'ProcessOfflinePayment() tests' => sub {
     is( $line->amount+0, $amount * -1, 'amount picked from params' );
     is( $line->branchcode, $library->id, 'branchcode set correctly' );
 
-    $schema->storage->txn_rollback;
-};
-
-
-
-sub set_userenv {
-    my ( $library ) = @_;
-    t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
-}
-
-sub str {
-    my ( $error, $question, $alert ) = @_;
-    my $s;
-    $s  = %$error    ? ' (error: '    . join( ' ', keys %$error    ) . ')' : '';
-    $s .= %$question ? ' (question: ' . join( ' ', keys %$question ) . ')' : '';
-    $s .= %$alert    ? ' (alert: '    . join( ' ', keys %$alert    ) . ')' : '';
-    return $s;
-}
-
-sub test_debarment_on_checkout {
-    my ($params) = @_;
-    my $item     = $params->{item};
-    my $library  = $params->{library};
-    my $patron   = $params->{patron};
-    my $due_date = $params->{due_date} || dt_from_string;
-    my $return_date = $params->{return_date} || dt_from_string;
-    my $expected_expiration_date = $params->{expiration_date};
-
-    $expected_expiration_date = output_pref(
-        {
-            dt         => $expected_expiration_date,
-            dateformat => 'sql',
-            dateonly   => 1,
-        }
-    );
-    my @caller      = caller;
-    my $line_number = $caller[2];
-    AddIssue( $patron, $item->{barcode}, $due_date );
-
-    my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode}, undef, $return_date );
-    is( $message->{WasReturned} && exists $message->{Debarred}, 1, 'AddReturn must have debarred the patron' )
-        or diag('AddReturn returned message ' . Dumper $message );
-    my $debarments = Koha::Patron::Debarments::GetDebarments(
-        { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
-    is( scalar(@$debarments), 1, 'Test at line ' . $line_number );
-
-    is( $debarments->[0]->{expiration},
-        $expected_expiration_date, 'Test at line ' . $line_number );
-    Koha::Patron::Debarments::DelUniqueDebarment(
-        { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
 };
 
 subtest 'Incremented fee tests' => sub {
@@ -3279,3 +3311,7 @@ subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
     is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
     $itemtype->rentalcharge_daily('0')->store;
 };
+
+$schema->storage->txn_rollback;
+C4::Context->clear_syspref_cache();
+$cache->clear_from_cache('single_holidays');