Bug 15548: Move new patron related code to Patron*
[koha.git] / t / db_dependent / Accounts.t
index 991de12..ee92a48 100644 (file)
@@ -18,7 +18,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 8;
+use Test::More tests => 19;
 use Test::MockModule;
 use Test::Warn;
 
@@ -27,7 +27,7 @@ use t::lib::TestBuilder;
 BEGIN {
     use_ok('C4::Accounts');
     use_ok('Koha::Object');
-    use_ok('Koha::Borrower');
+    use_ok('Koha::Patron');
     use_ok('Data::Dumper');
 }
 
@@ -44,7 +44,8 @@ can_ok( 'C4::Accounts',
         ReversePayment
         recordpayment_selectaccts
         makepartialpayment
-        WriteOffFee )
+        WriteOffFee
+        purge_zero_balance_fees )
 );
 
 my $schema  = Koha::Database->new->schema;
@@ -73,6 +74,67 @@ $context->mock( 'userenv', sub {
     };
 });
 
+# Testing purge_zero_balance_fees
+
+# The 3rd value in the insert is 'days ago' --
+# 0 => today
+# 1 => yesterday
+# etc.
+
+my $sth = $dbh->prepare(
+    "INSERT INTO accountlines (
+         borrowernumber,
+         amountoutstanding,
+         date,
+         description
+     )
+     VALUES ( ?, ?, (select date_sub(CURRENT_DATE, INTERVAL ? DAY) ), ? )"
+);
+
+my $days = 5;
+
+my @test_data = (
+    { amount => 0     , days_ago => 0         , description =>'purge_zero_balance_fees should not delete 0 balance fees with date today'                     , delete => 0 } ,
+    { amount => 0     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete 0 balance fees with date before threshold day'      , delete => 0 } ,
+    { amount => 0     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete 0 balance fees with date on threshold day'          , delete => 0 } ,
+    { amount => 0     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete 0 balance fees with date after threshold day'           , delete => 1 } ,
+    { amount => undef , days_ago => $days + 1 , description =>'purge_zero_balance_fees should delete NULL balance fees with date after threshold day'        , delete => 1 } ,
+    { amount => 5     , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed before threshold day'  , delete => 0 } ,
+    { amount => 5     , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with positive amout owed on threshold day'      , delete => 0 } ,
+    { amount => 5     , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with positive amout owed after threshold day'   , delete => 0 } ,
+    { amount => -5    , days_ago => $days - 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed before threshold day' , delete => 0 } ,
+    { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
+    { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
+);
+
+my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => 'MPL' } )->store();
+
+for my $data ( @test_data ) {
+    $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
+}
+
+purge_zero_balance_fees( $days );
+
+$sth = $dbh->prepare(
+            "select count(*) = 0 as deleted
+             from accountlines
+             where description = ?"
+       );
+
+#
+sub is_delete_correct {
+    my $should_delete = shift;
+    my $description = shift;
+    $sth->execute( $description );
+    my $test = $sth->fetchrow_hashref();
+    is( $test->{deleted}, $should_delete, $description )
+}
+
+for my $data  (@test_data) {
+    is_delete_correct( $data->{delete}, $data->{description});
+}
+
+$dbh->do(q|DELETE FROM accountlines|);
 
 subtest "recordpayment() tests" => sub {
 
@@ -82,7 +144,7 @@ subtest "recordpayment() tests" => sub {
     my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
     my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
 
-    my $borrower = Koha::Borrower->new( {
+    my $borrower = Koha::Patron->new( {
         cardnumber => '1234567890',
         surname => 'McFly',
         firstname => 'Marty',