From fc07e267e060fbbe721d7d019d50beebe549d418 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 15 Aug 2024 09:27:13 +0100
Subject: [PATCH] Bug 10190: Migrate MarkIssueReturned test to circulation
 rules

Sponsored-by: Glasgow Colleges Library Group <https://library.cityofglasgowcollege.ac.uk>
Signed-off-by: George Harkins <George.Harkins@cityofglasgowcollege.ac.uk>
---
 .../Circulation/MarkIssueReturned.t           | 65 +++++++++++--------
 1 file changed, 37 insertions(+), 28 deletions(-)

diff --git a/t/db_dependent/Circulation/MarkIssueReturned.t b/t/db_dependent/Circulation/MarkIssueReturned.t
index 9c43132a256..c4fdac6f51a 100755
--- a/t/db_dependent/Circulation/MarkIssueReturned.t
+++ b/t/db_dependent/Circulation/MarkIssueReturned.t
@@ -134,7 +134,8 @@ subtest 'Manually pass a return date' => sub {
     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
     my $patron  = $builder->build_object(
-        {   class => 'Koha::Patrons',
+        {
+            class => 'Koha::Patrons',
             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
         }
     );
@@ -182,21 +183,26 @@ subtest 'AutoRemoveOverduesRestrictions' => sub {
     my $categorycode = $patron->categorycode;
     my $branchcode   = $patron->branchcode;
 
-    $dbh->do(
-        qq{
-        INSERT INTO `overduerules` (
-            `categorycode`,
-            `delay1`,
-            `letter1`,
-            `debarred1`,
-            `delay2`,
-            `letter2`,
-            `debarred2`
-        )
-        VALUES ('$categorycode', 6, 'ODUE', 0, 10, 'ODUE2', 1)
-    }
+    Koha::CirculationRules->search->delete;
+    Koha::CirculationRules->set_rules(
+        {
+            categorycode => $categorycode,
+            itemtype     => undef,
+            branchcode   => undef,
+            rules        => {
+                overdue_1_delay    => 6,
+                overdue_1_notice   => 'ODUE',
+                overdue_1_mtt      => 'email',
+                overdue_1_restrict => 0,
+                overdue_2_delay    => 10,
+                overdue_2_notice   => 'ODUE2',
+                overdue_2_mtt      => 'email',
+                overdue_2_restrict => 1
+            }
+        }
     );
 
+    note("Testing when_no_overdue");
     t::lib::Mocks::mock_preference( 'AutoRemoveOverduesRestrictions', 'when_no_overdue' );
 
     t::lib::Mocks::mock_userenv( { branchcode => $branchcode } );
@@ -230,6 +236,7 @@ subtest 'AutoRemoveOverduesRestrictions' => sub {
     $restrictions = $patron->restrictions;
     is( $restrictions->count, 0, 'OVERDUES debarment is removed if patron does not have overdues' );
 
+    note("Testing when_no_overdue_causing_debarment");
     t::lib::Mocks::mock_preference( 'AutoRemoveOverduesRestrictions', 'when_no_overdue_causing_debarment' );
 
     my $ten_days_ago = dt_from_string->subtract( days => 10 );
@@ -278,20 +285,22 @@ subtest 'AutoRemoveOverduesRestrictions' => sub {
     $checkout_2 = AddIssue( $patron, $item_2->barcode, $eleven_days_ago );
 
     # $checkout_1 should now not trigger debarment with this new rule for specific branchcode
-    $dbh->do(
-        qq{
-        INSERT INTO `overduerules` (
-            `branchcode`,
-            `categorycode`,
-            `delay1`,
-            `letter1`,
-            `debarred1`,
-            `delay2`,
-            `letter2`,
-            `debarred2`
-        )
-        VALUES ('$branchcode', '$categorycode', 6, 'ODUE', 0, 11, 'ODUE2', 1)
-    }
+    Koha::CirculationRules->set_rules(
+        {
+            categorycode => $categorycode,
+            itemtype     => undef,
+            branchcode   => $branchcode,
+            rules        => {
+                overdue_1_delay    => 6,
+                overdue_1_notice   => 'ODUE',
+                overdue_1_mtt      => 'email',
+                overdue_1_restrict => 0,
+                overdue_2_delay    => 11,
+                overdue_2_notice   => 'ODUE2',
+                overdue_2_mtt      => 'email',
+                overdue_2_restrict => 1
+            }
+        }
     );
 
     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
-- 
2.47.0