From 6cde33e05d598ff0f2780f2b4928123068038214 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 22 Dec 2021 15:20:16 +0000
Subject: [PATCH] Bug 29754: Unit test

This adds a unit test to the SIP code

It also adds some spacing and dividers to make the tests for 'relationships_debt' easier to read

To test:
prove -v t/db_dependent/Koha/Patron.t - passes and is readable
prove -v t/db_dependent/SIP/Patron.t - fails

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 t/db_dependent/Koha/Patron.t | 11 +++++-----
 t/db_dependent/SIP/Patron.t  | 39 +++++++++++++++++++++++++++++++++++-
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t
index ce114cab0e..8c73e7aa0c 100755
--- a/t/db_dependent/Koha/Patron.t
+++ b/t/db_dependent/Koha/Patron.t
@@ -90,8 +90,8 @@ subtest 'relationships_debt() tests' => sub {
 
     my $parent_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Parent 1" } });
     my $parent_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Parent 2" } });
-    my $child_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Child 1" } });
-    my $child_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => "Child 2" } });
+    my $child_1 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => " Child 1" } });
+    my $child_2 = $builder->build_object({ class => 'Koha::Patrons', value => { firstname => " Child 2" } });
 
     $child_1->add_guarantor({ guarantor_id => $parent_1->borrowernumber, relationship => 'parent' });
     $child_1->add_guarantor({ guarantor_id => $parent_2->borrowernumber, relationship => 'parent' });
@@ -135,7 +135,7 @@ subtest 'relationships_debt() tests' => sub {
 
 sub _test_combinations {
     my ( $patrons, $parent1_debt, $parent2_debt, $child1_debt, $child2_debt ) = @_;
-
+    diag("Testing with parent 1 debt $parent1_debt | Parent 2 debt $parent2_debt | Child 1 debt $child1_debt | Child 2 debt $child2_debt");
     # Options
     # P1 => P1 + C1 + C2 ( - P1 ) ( + P2 )
     # P2 => P2 + C1 + C2 ( - P2 ) ( + P1 )
@@ -146,6 +146,7 @@ sub _test_combinations {
     for my $i ( 0 .. 7 ) {
         my ( $only_this_guarantor, $include_guarantors, $include_this_patron )
           = split '', sprintf( "%03b", $i );
+        diag("---------------------");
         for my $patron ( @$patrons ) {
             if ( $only_this_guarantor
                 && !$patron->guarantee_relationships->count )
@@ -176,7 +177,7 @@ sub _test_combinations {
                     $debt += $child1_debt + $child2_debt;
                     $debt += $parent1_debt unless ($only_this_guarantor || !$include_guarantors);
                 }
-                elsif ( $patron->firstname eq 'Child 1' ) {
+                elsif ( $patron->firstname eq ' Child 1' ) {
                     $debt += $child1_debt if ($include_this_patron);
                     $debt += $child2_debt;
                     $debt += $parent1_debt + $parent2_debt if ($include_guarantors);
@@ -197,7 +198,7 @@ sub _test_combinations {
                     ),
                     $debt,
                     $patron->firstname
-                      . " debt of $debt calculated correctly for ( only_this_guarantor: $only_this_guarantor, include_guarantors: $include_guarantors, include_this_patron: $include_this_patron)"
+                      . " debt of " . sprintf('%02d',$debt) . " calculated correctly for ( only_this_guarantor: $only_this_guarantor, include_guarantors: $include_guarantors, include_this_patron: $include_this_patron)"
                 );
             }
         }
diff --git a/t/db_dependent/SIP/Patron.t b/t/db_dependent/SIP/Patron.t
index 9106b1e739..f0a1f26dce 100755
--- a/t/db_dependent/SIP/Patron.t
+++ b/t/db_dependent/SIP/Patron.t
@@ -4,7 +4,7 @@
 # This needs to be extended! Your help is appreciated..
 
 use Modern::Perl;
-use Test::More tests => 8;
+use Test::More tests => 9;
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
@@ -272,4 +272,41 @@ subtest "fine_items tests" => sub {
     is( @$fine_items, 0, "Got zero fine items" );
 };
 
+subtest "NoIssuesChargeGuarantorsWithGuarantees tests" => sub {
+
+    plan tests => 1;
+
+    t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' );
+
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $child  = $builder->build_object({ class => 'Koha::Patrons' });
+    $child->add_guarantor({ guarantor_id => $patron->borrowernumber, relationship => 'parent' });
+
+    t::lib::Mocks::mock_preference('NoIssuesChargeGuarantorsWithGuarantees', 1);
+
+    my $fee1 = $builder->build_object(
+        {
+            class => 'Koha::Account::Lines',
+            value  => {
+                borrowernumber => $patron->borrowernumber,
+                amountoutstanding => 11,
+            }
+        }
+    )->store;
+
+    my $fee2 = $builder->build_object(
+        {
+            class => 'Koha::Account::Lines',
+            value  => {
+                borrowernumber => $child->borrowernumber,
+                amountoutstanding => 0.11,
+            }
+        }
+    )->store;
+
+    my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
+
+    is( $sip_patron->fines_amount, 11.11,"Guarantee fines correctly included");
+
+};
 $schema->storage->txn_rollback;
-- 
2.32.0