From a5cc6ba4e50624259ac8217ddb0b8369ff717e76 Mon Sep 17 00:00:00 2001
From: Arthur Suzuki <arthur.suzuki@biblibre.com>
Date: Wed, 8 Jan 2020 14:20:38 +0100
Subject: [PATCH] Bug 22806: Unit Tests

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: David Nind <david@davidnind.com>
---
 t/db_dependent/ILSDI_Services.t | 44 +++++++++++++++++++++++++++++++----------
 t/db_dependent/Reserves.t       | 37 ++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+), 10 deletions(-)

diff --git a/t/db_dependent/ILSDI_Services.t b/t/db_dependent/ILSDI_Services.t
index 3b599b67f0..f8ab8921ca 100755
--- a/t/db_dependent/ILSDI_Services.t
+++ b/t/db_dependent/ILSDI_Services.t
@@ -300,7 +300,7 @@ subtest 'LookupPatron test' => sub {
 
 subtest 'Holds test' => sub {
 
-    plan tests => 5;
+    plan tests => 7;
 
     $schema->storage->txn_begin;
 
@@ -371,19 +371,28 @@ subtest 'Holds test' => sub {
     $reply = C4::ILSDI::Services::HoldItem( $query );
     is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
 
-    # Adding a holdable item to biblio 3.
-    my $item3 = $builder->build_sample_item(
+    my $origin_branch = $builder->build(
         {
-            damaged => 0,
+            source => 'Branch',
+            value  => {
+                pickup_location => 1,
+            }
         }
     );
 
+    # Adding a holdable item.
+    my $item3 = $builder->build_sample_item(
+       {
+           barcode => '123456789',
+           library => $origin_branch->{branchcode}
+       });
+
     my $item4 = $builder->build_sample_item(
         {
-            biblionumber => $item3->biblionumber,
-            damaged      => 1,
-        }
-    );
+           biblionumber => $item3->biblionumber,
+           damaged => 1,
+           library => $origin_branch->{branchcode}
+       });
 
     Koha::CirculationRules->set_rule(
         {
@@ -397,12 +406,27 @@ subtest 'Holds test' => sub {
 
     $query = new CGI;
     $query->param( 'patron_id', $patron->{borrowernumber});
-    $query->param( 'bib_id', $item3->biblionumber);
+    $query->param( 'bib_id', $item4->biblionumber);
     $query->param( 'item_id', $item4->itemnumber);
 
     $reply = C4::ILSDI::Services::HoldItem( $query );
     is( $reply->{code}, 'damaged', "Item is damaged" );
 
+    my $module = new Test::MockModule('C4::Context');
+    $module->mock('userenv', sub { { patron => $patron } });
+    my $issue = C4::Circulation::AddIssue($patron, $item3->barcode);
+    t::lib::Mocks::mock_preference( 'AllowHoldsOnPatronsPossessions', '0' );
+
+    $query = new CGI;
+    $query->param( 'patron_id', $patron->{borrowernumber});
+    $query->param( 'bib_id', $item3->biblionumber);
+    $query->param( 'item_id', $item3->itemnumber);
+    $query->param( 'pickup_location', $origin_branch->{branchcode});
+    $reply = C4::ILSDI::Services::HoldItem( $query );
+
+    is( $reply->{code}, 'itemAlreadyOnLoan', "Patron has issued same book" );
+    is( $reply->{pickup_location}, undef, "No reserve placed");
+
     $schema->storage->txn_rollback;
 };
 
@@ -642,7 +666,7 @@ subtest 'GetPatronInfo paginated loans' => sub {
     });
     my $module = new Test::MockModule('C4::Context');
     $module->mock('userenv', sub { { branch => $library->branchcode } });
-    my $date_due = DateTime->now->add(weeks => 2);
+    my $date_due = Koha::DateUtils::dt_from_string()->add(weeks => 2);
     my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
     my $date_due1 = Koha::DateUtils::dt_from_string( $issue1->date_due );
     my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $date_due);
diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t
index c1c4758c86..52c779510b 100755
--- a/t/db_dependent/Reserves.t
+++ b/t/db_dependent/Reserves.t
@@ -1150,7 +1150,44 @@ subtest 'CheckReserves additional test' => sub {
     is( $matched_reserve->{reserve_id},
         $reserve1->reserve_id, "We got the Transit reserve" );
     is( scalar @$possible_reserves, 2, 'We do get both reserves' );
+};
+
+subtest 'AllowHoldOnPatronPossession test' => sub {
+
+    plan tests => 4;
 
+    # Create the items and patrons we need
+    my $biblio = $builder->build_sample_biblio();
+    my $itype = $builder->build_object({ class => "Koha::ItemTypes", value => { notforloan => 0 } });
+    my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber,notforloan => 0, itype => $itype->itemtype });
+    my $patron = $builder->build_object({ class => "Koha::Patrons",
+                                          value => { branchcode => $item->homebranch }});
+
+    C4::Circulation::AddIssue($patron->unblessed,
+                              $item->barcode);
+    t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 0);
+
+    is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
+                                       $item->biblionumber)->{status},
+       'itemAlreadyOnLoan',
+       'Patron cannot place hold on a book loaned to itself');
+
+    is(C4::Reserves::CanItemBeReserved($patron->borrowernumber,
+                                       $item->itemnumber)->{status},
+       'itemAlreadyOnLoan',
+       'Patron cannot place hold on an item loaned to itself');
+
+    t::lib::Mocks::mock_preference('AllowHoldsOnPatronsPossessions', 1);
+
+    is(C4::Reserves::CanBookBeReserved($patron->borrowernumber,
+                                       $item->biblionumber)->{status},
+       'OK',
+       'Patron can place hold on a book loaned to itself');
+
+    is(C4::Reserves::CanItemBeReserved($patron->borrowernumber,
+                                       $item->itemnumber)->{status},
+       'OK',
+       'Patron can place hold on an item loaned to itself');
 };
 
 sub count_hold_print_messages {
-- 
2.11.0