From 8748718c5bd238fb9536c5c1630cdf22055800e8 Mon Sep 17 00:00:00 2001
From: David Gustafsson <david.gustafsson@ub.gu.se>
Date: Fri, 23 Feb 2024 19:41:26 +0100
Subject: [PATCH] Bug 36160: Use $builder->build_object for patron objects in
 Circulation.t

1) Set TrackLastPatronActivityTriggers to at least "Renew an item"
2) Run tests in t/db_dependent/Circulation.t and verify that
   failes with "Invalid value passed, borrowers.updated_on..."
3) Apply patch
4) Run Circulatoint.t tests again and verify that no longer
   produces this error
---
 t/db_dependent/Circulation.t | 240 ++++++++++++++++++++++-------------
 1 file changed, 149 insertions(+), 91 deletions(-)

diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t
index 5095afde735..bfc3ecfbcb4 100755
--- a/t/db_dependent/Circulation.t
+++ b/t/db_dependent/Circulation.t
@@ -469,52 +469,71 @@ subtest "CanBookBeRenewed tests" => sub {
     );
 
     # Create borrowers
-    my %renewing_borrower_data = (
-        firstname =>  'John',
-        surname => 'Renewal',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-        autorenew_checkouts => 1,
-    );
-
-    my %reserving_borrower_data = (
-        firstname =>  'Katrin',
-        surname => 'Reservation',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-    );
 
-    my %hold_waiting_borrower_data = (
-        firstname =>  'Kyle',
-        surname => 'Reservation',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-    );
+    my $renewing_borrower_obj = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'John',
+                surname => 'Renewal',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+                autorenew_checkouts => 1,
+            }
+        }
+    )->store;
+    my $renewing_borrowernumber = $renewing_borrower_obj->borrowernumber;
 
-    my %restricted_borrower_data = (
-        firstname =>  'Alice',
-        surname => 'Reservation',
-        categorycode => $patron_category->{categorycode},
-        debarred => '3228-01-01',
-        branchcode => $branch,
-    );
+    my $reserving_borrowernumber = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'Katrin',
+                surname => 'Reservation',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+            }
+        }
+    )->store->borrowernumber;
 
-    my %expired_borrower_data = (
-        firstname =>  'Ça',
-        surname => 'Glisse',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-        dateexpiry => dt_from_string->subtract( months => 1 ),
-        autorenew_checkouts => 1,
-    );
+    my $hold_waiting_borrowernumber = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'Kyle',
+                surname => 'Reservation',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+            }
+        }
+    )->store->borrowernumber;
 
-    my $renewing_borrower_obj = Koha::Patron->new(\%renewing_borrower_data)->store;
-    my $renewing_borrowernumber = $renewing_borrower_obj->borrowernumber;
-    my $reserving_borrowernumber = Koha::Patron->new(\%reserving_borrower_data)->store->borrowernumber;
-    my $hold_waiting_borrowernumber = Koha::Patron->new(\%hold_waiting_borrower_data)->store->borrowernumber;
-    my $restricted_borrower_obj = Koha::Patron->new(\%restricted_borrower_data)->store;
+    my $restricted_borrower_obj = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'Alice',
+                surname => 'Reservation',
+                categorycode => $patron_category->{categorycode},
+                debarred => '3228-01-01',
+                branchcode => $branch,
+            }
+        }
+    )->store;
 
-    my $expired_borrower_obj = Koha::Patron->new(\%expired_borrower_data)->store;
+    my $expired_borrower_obj = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'Ça',
+                surname => 'Glisse',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+                dateexpiry => dt_from_string->subtract( months => 1 ),
+                autorenew_checkouts => 1,
+            }
+        }
+    );
 
     my $bibitems       = '';
     my $priority       = '1';
@@ -1851,19 +1870,31 @@ subtest "AllowRenewalIfOtherItemsAvailable tests" => sub {
         }
     );
 
+    my $borrower1 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname    => 'Kyle',
+                surname      => 'Hall',
+                categorycode => $patron_category->{categorycode},
+                branchcode   => $library2->{branchcode},
+            }
+        }
+    );
+
+    my $borrowernumber2 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname    => 'Chelsea',
+                surname      => 'Hall',
+                categorycode => $patron_category->{categorycode},
+                branchcode   => $library2->{branchcode},
+
+            }
+        }
+    )->borrowernumber;
 
-    my $borrower1 = Koha::Patron->new({
-        firstname    => 'Kyle',
-        surname      => 'Hall',
-        categorycode => $patron_category->{categorycode},
-        branchcode   => $library2->{branchcode},
-    })->store;
-    my $borrowernumber2 = Koha::Patron->new({
-        firstname    => 'Chelsea',
-        surname      => 'Hall',
-        categorycode => $patron_category->{categorycode},
-        branchcode   => $library2->{branchcode},
-    })->store->borrowernumber;
     my $patron_category_2 = $builder->build(
         {
             source => 'Category',
@@ -1874,12 +1905,18 @@ subtest "AllowRenewalIfOtherItemsAvailable tests" => sub {
             }
         }
     );
-    my $borrowernumber3 = Koha::Patron->new({
-        firstname    => 'Carnegie',
-        surname      => 'Hall',
-        categorycode => $patron_category_2->{categorycode},
-        branchcode   => $library2->{branchcode},
-    })->store->borrowernumber;
+
+    my $borrowernumber3 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname    => 'Carnegie',
+                surname      => 'Hall',
+                categorycode => $patron_category_2->{categorycode},
+                branchcode   => $library2->{branchcode},
+            }
+        }
+    )->borrowernumber;
 
     my $issue = AddIssue( $borrower1, $item_1->barcode );
 
@@ -2041,12 +2078,17 @@ subtest "AllowRenewalIfOtherItemsAvailable tests" => sub {
         }
     );
 
-    my $borrower = Koha::Patron->new({
-        firstname =>  'fn',
-        surname => 'dn',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-    })->store;
+    my $borrower = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'fn',
+                surname => 'dn',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+            }
+        }
+    );
 
     my $issue = AddIssue( $borrower, $item->barcode, undef, undef, undef, undef, { onsite_checkout => 1 } );
     my ( $renewed, $error ) = CanBookBeRenewed( $borrower, $issue );
@@ -2522,25 +2564,36 @@ subtest 'MultipleReserves' => sub {
     my $checkitem      = undef;
     my $found          = undef;
 
-    my %renewing_borrower_data = (
-        firstname =>  'John',
-        surname => 'Renewal',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
+    # Renewing borrower
+    my $patron = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'John',
+                surname => 'Renewal',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+            }
+        }
     );
-    my $patron = Koha::Patron->new(\%renewing_borrower_data)->store;
+
     my $issue = AddIssue( $patron, $item_1->barcode);
     my $datedue = dt_from_string( $issue->date_due() );
     is (defined $issue->date_due(), 1, "item 1 checked out");
     my $borrowing_borrowernumber = Koha::Checkouts->find({ itemnumber => $item_1->itemnumber })->borrowernumber;
 
-    my %reserving_borrower_data1 = (
-        firstname =>  'Katrin',
-        surname => 'Reservation',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-    );
-    my $reserving_borrowernumber1 = Koha::Patron->new(\%reserving_borrower_data1)->store->borrowernumber;
+    my $reserving_borrowernumber1 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'Katrin',
+                surname => 'Reservation',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+            }
+        }
+    )->borrowernumber;
+
     AddReserve(
         {
             branchcode       => $branch,
@@ -2555,13 +2608,18 @@ subtest 'MultipleReserves' => sub {
         }
     );
 
-    my %reserving_borrower_data2 = (
-        firstname =>  'Kirk',
-        surname => 'Reservation',
-        categorycode => $patron_category->{categorycode},
-        branchcode => $branch,
-    );
-    my $reserving_borrowernumber2 = Koha::Patron->new(\%reserving_borrower_data2)->store->borrowernumber;
+    my $reserving_borrowernumber2 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => {
+                firstname =>  'Kirk',
+                surname => 'Reservation',
+                categorycode => $patron_category->{categorycode},
+                branchcode => $branch,
+            }
+        }
+    )->borrowernumber;
+
     AddReserve(
         {
             branchcode       => $branch,
@@ -4406,7 +4464,7 @@ subtest 'CanBookBeIssued | item-level_itypes=biblio' => sub {
 
     t::lib::Mocks::mock_preference('item-level_itypes', 0); # biblio
     my $library = $builder->build( { source => 'Branch' } );
-    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
 
     my $item = $builder->build_sample_item(
         {
@@ -4425,7 +4483,7 @@ subtest 'CanBookBeIssued | notforloan' => sub {
     t::lib::Mocks::mock_preference('AllowNotForLoanOverride', 0);
 
     my $library = $builder->build( { source => 'Branch' } );
-    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } )->store;
+    my $patron  = $builder->build_object( { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
 
     my $itemtype = $builder->build(
         {
@@ -4892,7 +4950,7 @@ subtest 'Incremented fee tests' => sub {
             class => 'Koha::Patrons',
             value => { categorycode => $patron_category->{categorycode} }
         }
-    )->store;
+    );
 
     my $itemtype = $builder->build_object(
         {
@@ -5157,7 +5215,7 @@ subtest 'CanBookBeIssued & RentalFeesCheckoutConfirmation' => sub {
             class => 'Koha::Patrons',
             value => { categorycode => $patron_category->{categorycode} }
         }
-    )->store;
+    );
 
     my $itemtype = $builder->build_object(
         {
@@ -5204,7 +5262,7 @@ subtest 'CanBookBeIssued & CircConfirmItemParts' => sub {
             class => 'Koha::Patrons',
             value => { categorycode => $patron_category->{categorycode} }
         }
-    )->store;
+    );
 
     my $item = $builder->build_sample_item(
         {
@@ -5275,7 +5333,7 @@ subtest 'Filling a hold should cancel existing transfer' => sub {
                 branchcode => $libraryA->branchcode,
             }
         }
-    )->store;
+    );
 
     my $item = $builder->build_sample_item({
         homebranch => $libraryB->branchcode,
-- 
2.43.0