From 44f1aff476c991cbfb115693e5b97044de7f77c4 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 23 Jan 2020 02:37:19 +0000
Subject: [PATCH] Bug 24476: Change CanBookBeRenewed and adjust tests

Signed-off-by: Andrew <andrew@bywatersolutions.com>

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
---
 C4/Circulation.pm            |  6 +++---
 t/db_dependent/Circulation.t | 26 +++++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index cca192c6b0..a529b2ebbe 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2751,7 +2751,7 @@ sub CanBookBeRenewed {
             return ( 0, 'overdue');
         }
 
-        if ( $issue->auto_renew ) {
+        if ( $issue->auto_renew && $patron->autorenewal ) {
 
             if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) {
                 return ( 0, 'auto_account_expired' );
@@ -2807,10 +2807,10 @@ sub CanBookBeRenewed {
 
             if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) )
             {
-                return ( 0, "auto_too_soon" ) if $issue->auto_renew;
+                return ( 0, "auto_too_soon" ) if $issue->auto_renew && $patron->autorenewal;
                 return ( 0, "too_soon" );
             }
-            elsif ( $issue->auto_renew ) {
+            elsif ( $issue->auto_renew && $patron->autorenewal ) {
                 $auto_renew = 1;
             }
         }
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index 58ec966c17..f0d46df0ed 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -355,7 +355,8 @@ subtest "CanBookBeRenewed tests" => sub {
     my $restricted_borrowernumber = Koha::Patron->new(\%restricted_borrower_data)->store->borrowernumber;
     my $expired_borrowernumber = Koha::Patron->new(\%expired_borrower_data)->store->borrowernumber;
 
-    my $renewing_borrower = Koha::Patrons->find( $renewing_borrowernumber )->unblessed;
+    my $renewing_borrower_obj = Koha::Patrons->find( $renewing_borrowernumber );
+    my $renewing_borrower = $renewing_borrower_obj->unblessed;
     my $restricted_borrower = Koha::Patrons->find( $restricted_borrowernumber )->unblessed;
     my $expired_borrower = Koha::Patrons->find( $expired_borrowernumber )->unblessed;
 
@@ -656,6 +657,12 @@ subtest "CanBookBeRenewed tests" => sub {
 
 
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 1, 'No renewal before is undef, but patron opted out of auto_renewal' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
+
     # Bug 7413
     # Test premature manual renewal
     Koha::CirculationRules->set_rule(
@@ -699,6 +706,12 @@ subtest "CanBookBeRenewed tests" => sub {
         'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
     );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 0, 'No renewal before is 7, patron opted out of auto_renewal still cannot renew early' );
+    is( $error, 'too_soon', 'Error is too_soon, no auto' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
     # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
     # and test automatic renewal again
     $dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'norenewalbefore'});
@@ -709,6 +722,12 @@ subtest "CanBookBeRenewed tests" => sub {
         'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
     );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 0, 'No renewal before is 0, patron opted out of auto_renewal still cannot renew early' );
+    is( $error, 'too_soon', 'Error is too_soon, no auto' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
     # Change policy so that loans can be renewed 99 days prior to the due date
     # and test automatic renewal again
     $dbh->do(q{UPDATE circulation_rules SET rule_value = '99' WHERE rule_name = 'norenewalbefore'});
@@ -719,6 +738,11 @@ subtest "CanBookBeRenewed tests" => sub {
         'Bug 14101: Cannot renew, renewal is automatic (returned code is auto_renew)'
     );
 
+    $renewing_borrower_obj->autorenewal(0)->store;
+    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
+    is( $renewokay, 1, 'No renewal before is 99, patron opted out of auto_renewal so can renew' );
+    $renewing_borrower_obj->autorenewal(1)->store;
+
     subtest "too_late_renewal / no_auto_renewal_after" => sub {
         plan tests => 14;
         my $item_to_auto_renew = $builder->build(
-- 
2.11.0