Bug 14591: Update unit tests
[koha.git] / t / db_dependent / Circulation.t
index 6b5d14b..c478a42 100755 (executable)
@@ -18,7 +18,7 @@
 use Modern::Perl;
 use utf8;
 
-use Test::More tests => 125;
+use Test::More tests => 126;
 use Test::MockModule;
 
 use Data::Dumper;
@@ -45,6 +45,7 @@ use Koha::CirculationRules;
 use Koha::Subscriptions;
 use Koha::Account::Lines;
 use Koha::Account::Offsets;
+use Koha::ActionLogs;
 
 my $schema = Koha::Database->schema;
 $schema->storage->txn_begin;
@@ -75,7 +76,7 @@ my $itemtype = $builder->build(
         value  => {
             notforloan          => undef,
             rentalcharge        => 0,
-            rental_charge_daily => 0,
+            rentalcharge_daily => 0,
             defaultreplacecost  => undef,
             processfee          => undef
         }
@@ -476,17 +477,27 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
     );
 
     t::lib::Mocks::mock_preference('RenewalLog', 0);
-    my $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
-    my $old_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
+    my $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
+    my %params_renewal = (
+        timestamp => { -like => $date . "%" },
+        module => "CIRCULATION",
+        action => "RENEWAL",
+    );
+    my %params_issue = (
+        timestamp => { -like => $date . "%" },
+        module => "CIRCULATION",
+        action => "ISSUE"
+    );
+    my $old_log_size = Koha::ActionLogs->count( \%params_renewal );
     AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
-    my $new_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
+    my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
     is ($new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog');
 
     t::lib::Mocks::mock_preference('RenewalLog', 1);
-    $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
-    $old_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
+    $date = output_pref( { dt => dt_from_string(), dateonly => 1, dateformat => 'iso' } );
+    $old_log_size = Koha::ActionLogs->count( \%params_renewal );
     AddRenewal( $renewing_borrower->{borrowernumber}, $item_7->itemnumber, $branch );
-    $new_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
+    $new_log_size = Koha::ActionLogs->count( \%params_renewal );
     is ($new_log_size, $old_log_size + 1, 'renew log successfully added');
 
     my $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
@@ -496,12 +507,12 @@ my ( $reused_itemnumber_1, $reused_itemnumber_2 );
     $fines->delete();
 
 
-    my $old_issue_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
-    my $old_renew_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
+    my $old_issue_log_size = Koha::ActionLogs->count( \%params_issue );
+    my $old_renew_log_size = Koha::ActionLogs->count( \%params_renewal );
     AddIssue( $renewing_borrower,$item_7->barcode,Koha::DateUtils::output_pref({str=>$datedue6->date_due, dateformat =>'iso'}),0,$date, 0, undef );
-    $new_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["RENEWAL"]) } );
+    $new_log_size = Koha::ActionLogs->count( \%params_renewal );
     is ($new_log_size, $old_renew_log_size + 1, 'renew log successfully added when renewed via issuing');
-    $new_log_size =  scalar(@{GetLogs( $date, $date, undef,["CIRCULATION"], ["ISSUE"]) } );
+    $new_log_size = Koha::ActionLogs->count( \%params_issue );
     is ($new_log_size, $old_issue_log_size, 'renew not logged as issue when renewed via issuing');
 
     $fines = Koha::Account::Lines->search( { borrowernumber => $renewing_borrower->{borrowernumber}, itemnumber => $item_7->itemnumber } );
@@ -1641,8 +1652,7 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
       ;    # Add another overdue
 
     t::lib::Mocks::mock_preference( 'CumulativeRestrictionPeriods', '0' );
-    AddReturn( $item_1->{barcode}, $library->{branchcode},
-        undef, undef, dt_from_string );
+    AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
     my $debarments = Koha::Patron::Debarments::GetDebarments(
         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
     is( scalar(@$debarments), 1 );
@@ -1658,8 +1668,7 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
     );
     is( $debarments->[0]->{expiration}, $expected_expiration );
 
-    AddReturn( $item_2->{barcode}, $library->{branchcode},
-        undef, undef, dt_from_string );
+    AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
     $debarments = Koha::Patron::Debarments::GetDebarments(
         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
     is( scalar(@$debarments), 1 );
@@ -1679,8 +1688,7 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
     AddIssue( $patron, $item_1->{barcode}, $five_days_ago );    # Add an overdue
     AddIssue( $patron, $item_2->{barcode}, $ten_days_ago )
       ;    # Add another overdue
-    AddReturn( $item_1->{barcode}, $library->{branchcode},
-        undef, undef, dt_from_string );
+    AddReturn( $item_1->{barcode}, $library->{branchcode}, undef, dt_from_string );
     $debarments = Koha::Patron::Debarments::GetDebarments(
         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
     is( scalar(@$debarments), 1 );
@@ -1693,8 +1701,7 @@ subtest 'AddReturn + CumulativeRestrictionPeriods' => sub {
     );
     is( $debarments->[0]->{expiration}, $expected_expiration );
 
-    AddReturn( $item_2->{barcode}, $library->{branchcode},
-        undef, undef, dt_from_string );
+    AddReturn( $item_2->{barcode}, $library->{branchcode}, undef, dt_from_string );
     $debarments = Koha::Patron::Debarments::GetDebarments(
         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
     is( scalar(@$debarments), 1 );
@@ -1923,25 +1930,25 @@ subtest 'AddReturn | is_overdue' => sub {
 
     # specify return date 5 days before => no overdue
     AddIssue( $patron->unblessed, $item->{barcode}, $five_days_ago ); # date due was 5d ago
-    AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $ten_days_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
     AddIssue( $patron->unblessed, $item->{barcode}, $ten_days_ago ); # date due was 10d ago
-    AddReturn( $item->{barcode}, $library->{branchcode}, undef, undef, $five_days_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}, undef, 1, undef, $ten_days_ago );
+    AddReturn( $item->{barcode}, $library->{branchcode}, $ten_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}, undef, 1, undef, $five_days_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 FU 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;
 };
@@ -1961,7 +1968,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
                 notforloan         => undef,
                 rentalcharge       => 0,
                 defaultreplacecost => undef,
-                processfee         => $processfee_amount
+                processfee         => $processfee_amount,
+                rentalcharge_daily => 0,
             }
         }
     );
@@ -2258,7 +2266,8 @@ subtest '_FixAccountForLostAndReturned' => sub {
                     notforloan         => undef,
                     rentalcharge       => 0,
                     defaultreplacecost => undef,
-                    processfee         => 0
+                    processfee         => 0,
+                    rentalcharge_daily => 0,
                 }
             }
         );
@@ -2322,7 +2331,7 @@ subtest '_FixAccountForLostAndReturned' => sub {
 };
 
 subtest '_FixOverduesOnReturn' => sub {
-    plan tests => 10;
+    plan tests => 6;
 
     my $biblio = $builder->build_sample_biblio({ author => 'Hall, Kylie' });
 
@@ -2376,24 +2385,6 @@ subtest '_FixOverduesOnReturn' => sub {
     is( $accountline->accounttype, 'FFOR', 'Open fine ( account type FU ) has been set to fine forgiven ( account type FFOR )');
     is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via forgiveness" );
     is( $offset->amount, '-99.000000', "Amount of offset is correct" );
-
-    ## Run again, with dropbox mode enabled
-    $accountline->set(
-        {
-            accounttype    => 'FU',
-            amountoutstanding => 99.00,
-        }
-    )->store();
-
-    C4::Circulation::_FixOverduesOnReturn( $patron->{borrowernumber}, $item->itemnumber, 0, 1 );
-
-    $accountline->_result()->discard_changes();
-    $offset = Koha::Account::Offsets->search({ debit_id => $accountline->id, type => 'Dropbox' })->next();
-
-    is( $accountline->amountoutstanding + 0, 90, 'Fine has been reduced to 90' );
-    is( $accountline->accounttype, 'F', 'Open fine ( account type FU ) has been closed out ( account type F )');
-    is( ref $offset, "Koha::Account::Offset", "Found matching offset for fine reduction via dropbox" );
-    is( $offset->amount, '-9.000000', "Amount of offset is correct" );
 };
 
 subtest 'Set waiting flag' => sub {
@@ -2846,6 +2837,8 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
 
     $schema->storage->txn_begin;
 
+    t::lib::Mocks::mock_preference('item-level_itypes', 1);
+
     my $issuing_charges = 15;
     my $title   = 'A title';
     my $author  = 'Author, An';
@@ -2860,7 +2853,7 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
     );
 
     my $library  = $builder->build_object({ class => 'Koha::Libraries' });
-    my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
+    my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes', value => { rentalcharge_daily => 0.00 }});
     my $patron   = $builder->build_object({
         class => 'Koha::Patrons',
         value => { branchcode => $library->id }
@@ -2884,12 +2877,16 @@ 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 $old_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
+    my %params_renewal = (
+        timestamp => { -like => $date . "%" },
+        module => "CIRCULATION",
+        action => "RENEWAL",
+    );
+    my $old_log_size = Koha::ActionLogs->count( \%params_renewal );;
     AddRenewal( $patron->id, $item->id, $library->id );
-    my $new_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
+    my $new_log_size = Koha::ActionLogs->count( \%params_renewal );
     is( $new_log_size, $old_log_size, 'renew log not added because of the syspref RenewalLog' );
 
     my $checkouts = $patron->checkouts;
@@ -2898,9 +2895,9 @@ subtest 'AddRenewal and AddIssuingCharge tests' => sub {
 
     t::lib::Mocks::mock_preference( 'RenewalLog', 1 );
     $date = output_pref( { dt => dt_from_string(), datenonly => 1, dateformat => 'iso' } );
-    $old_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
+    $old_log_size = Koha::ActionLogs->count( \%params_renewal );
     AddRenewal( $patron->id, $item->id, $library->id );
-    $new_log_size = scalar( @{ GetLogs( $date, $date, undef, ["CIRCULATION"], ["RENEWAL"] ) } );
+    $new_log_size = Koha::ActionLogs->count( \%params_renewal );
     is( $new_log_size, $old_log_size + 1, 'renew log successfully added' );
 
     my $lines = Koha::Account::Lines->search({
@@ -2988,8 +2985,7 @@ sub test_debarment_on_checkout {
     my $line_number = $caller[2];
     AddIssue( $patron, $item->{barcode}, $due_date );
 
-    my ( undef, $message ) = AddReturn( $item->{barcode}, $library->{branchcode},
-        undef, undef, $return_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(
@@ -3002,8 +2998,8 @@ sub test_debarment_on_checkout {
         { borrowernumber => $patron->{borrowernumber}, type => 'SUSPENSION' } );
 };
 
-subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
-    plan tests => 8;
+subtest 'Incremented fee tests' => sub {
+    plan tests => 11;
 
     t::lib::Mocks::mock_preference('item-level_itypes', 1);
 
@@ -3025,7 +3021,7 @@ subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
             value  => {
                 notforloan          => undef,
                 rentalcharge        => 0,
-                rental_charge_daily => 1.000000
+                rentalcharge_daily => 1.000000
             }
         }
     )->store;
@@ -3047,7 +3043,7 @@ subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
         }
     )->store;
 
-    is( $itemtype->rental_charge_daily, '1.000000', 'Daily rental charge stored and retreived correctly' );
+    is( $itemtype->rentalcharge_daily, '1.000000', 'Daily rental charge stored and retreived correctly' );
     is( $item->effective_itemtype, $itemtype->id, "Itemtype set correctly for item");
 
     my $dt_from = dt_from_string();
@@ -3092,4 +3088,72 @@ subtest 'Koha::ItemType::calc_rental_charge_daily tests' => sub {
     $accountline->delete();
     $issue->delete();
 
+    $itemtype->rentalcharge('2.000000')->store;
+    is( $itemtype->rentalcharge, '2.000000', 'Rental charge updated and retreived correctly' );
+    $issue = AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from);
+    my $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
+    is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly");
+    $accountlines->delete();
+    AddRenewal( $patron->id, $item->id, $library->id, $dt_to_renew, $dt_to );
+    $accountlines = Koha::Account::Lines->search({ itemnumber => $item->id });
+    is( $accountlines->count, '2', "Fixed charge and accrued charge recorded distinctly, for renewal");
+    $accountlines->delete();
+    $issue->delete();
+};
+
+subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
+    plan tests => 2;
+
+    t::lib::Mocks::mock_preference('RentalFeesCheckoutConfirmation', 1);
+    t::lib::Mocks::mock_preference('item-level_itypes', 1);
+
+    my $library =
+      $builder->build_object( { class => 'Koha::Libraries' } )->store;
+    my $patron = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { categorycode => $patron_category->{categorycode} }
+        }
+    )->store;
+
+    my $itemtype = $builder->build_object(
+        {
+            class => 'Koha::ItemTypes',
+            value => {
+                notforloan             => 0,
+                rentalcharge           => 0,
+                rentalcharge_daily => 0
+            }
+        }
+    );
+
+    my $biblioitem = $builder->build( { source => 'Biblioitem' } );
+    my $item = $builder->build_object(
+        {
+            class => 'Koha::Items',
+            value  => {
+                homebranch    => $library->id,
+                holdingbranch => $library->id,
+                notforloan    => 0,
+                itemlost      => 0,
+                withdrawn     => 0,
+                itype         => $itemtype->id,
+                biblionumber  => $biblioitem->{biblionumber},
+                biblioitemnumber => $biblioitem->{biblioitemnumber},
+            }
+        }
+    )->store;
+
+    my ( $issuingimpossible, $needsconfirmation );
+    my $dt_from = dt_from_string();
+    my $dt_due = dt_from_string()->add( days => 3 );
+
+    $itemtype->rentalcharge('1.000000')->store;
+    ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
+    is_deeply( $needsconfirmation, { RENTALCHARGE => '1' }, 'Item needs rentalcharge confirmation to be issued' );
+    $itemtype->rentalcharge('0')->store;
+    $itemtype->rentalcharge_daily('1.000000')->store;
+    ( $issuingimpossible, $needsconfirmation ) = CanBookBeIssued( $patron, $item->barcode, $dt_due, undef, undef, undef );
+    is_deeply( $needsconfirmation, { RENTALCHARGE => '3' }, 'Item needs rentalcharge confirmation to be issued, increment' );
+    $itemtype->rentalcharge_daily('0')->store;
 };