Bug 17740: Add the Koha::Patron->holds method
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 7 Dec 2016 13:42:41 +0000 (14:42 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 13 Jan 2017 11:38:51 +0000 (11:38 +0000)
The goal of this method is to replace
C4::Reserves::GetReservesFromBorrowernumber but could be reused for
something else, that's why it has its own bug report.

Test plan:
  prove t/db_dependent/Koha/Patrons.t
should return green

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Rebased. Edited two test descriptions.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Koha/Patron.pm
t/db_dependent/Koha/Patrons.t

index 698134a..ffdd7e3 100644 (file)
@@ -569,6 +569,20 @@ sub account {
     return Koha::Account->new( { patron_id => $self->borrowernumber } );
 }
 
+=head3 holds
+
+my $holds = $patron->holds
+
+Return all the holds placed by this patron
+
+=cut
+
+sub holds {
+    my ($self) = @_;
+    my $holds_rs = $self->_result->reserves->search( {}, { order_by => 'reservedate' } );
+    return Koha::Holds->_new_from_dbic($holds_rs);
+}
+
 =head3 type
 
 =cut
index 98e1f40..e5fcba8 100644 (file)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 19;
+use Test::More tests => 20;
 use Test::Warn;
 use DateTime;
 
@@ -594,6 +594,68 @@ subtest 'search_upcoming_membership_expires' => sub {
     Koha::Patrons->search({ borrowernumber => { in => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber}, $patron_3->{borrowernumber} ] } })->delete;
 };
 
+subtest 'holds' => sub {
+    plan tests => 3;
+
+    my $library = $builder->build( { source => 'Branch' } );
+    my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
+    my $item_1 = $builder->build(
+        {
+            source => 'Item',
+            value  => {
+                homebranch    => $library->{branchcode},
+                holdingbranch => $library->{branchcode},
+                biblionumber  => $biblionumber_1
+            }
+        }
+    );
+    my $item_2 = $builder->build(
+        {
+            source => 'Item',
+            value  => {
+                homebranch    => $library->{branchcode},
+                holdingbranch => $library->{branchcode},
+                biblionumber  => $biblionumber_1
+            }
+        }
+    );
+    my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
+    my $item_3 = $builder->build(
+        {
+            source => 'Item',
+            value  => {
+                homebranch    => $library->{branchcode},
+                holdingbranch => $library->{branchcode},
+                biblionumber  => $biblionumber_2
+            }
+        }
+    );
+    my $patron = $builder->build(
+        {
+            source => 'Borrower',
+            value  => { branchcode => $library->{branchcode} }
+        }
+    );
+
+    $patron = Koha::Patrons->find( $patron->{borrowernumber} );
+    my $holds = $patron->holds;
+    is( ref($holds), 'Koha::Holds',
+        'Koha::Patron->holds should return a Koha::Holds objects' );
+    is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
+
+    C4::Reserves::AddReserve( $library->{branchcode},
+        $patron->borrowernumber, $biblionumber_1 );
+    # In the future
+    C4::Reserves::AddReserve( $library->{branchcode},
+        $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
+
+    $holds = $patron->holds;
+    is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
+
+    $holds->delete;
+    $patron->delete;
+};
+
 $retrieved_patron_1->delete;
 is( Koha::Patrons->search->count, $nb_of_patrons + 1, 'Delete should have deleted the patron' );