Bug 17630: Add the Koha::Biblio->holds method
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 15 Nov 2016 11:37:30 +0000 (11:37 +0000)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 16 Dec 2016 14:51:53 +0000 (14:51 +0000)
This method will be useful to get the current holds placed on a given
bibliographic record.

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

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Koha/Biblio.pm
t/db_dependent/Koha/Biblios.t [new file with mode: 0644]

index d9b26b0..d5ca370 100644 (file)
@@ -249,6 +249,21 @@ sub itemtype {
     return $self->biblioitem()->itemtype();
 }
 
+=head3 holds
+
+my $holds = $biblio->holds();
+
+return the current holds placed on this record
+
+=cut
+
+sub holds {
+    my ( $self ) = @_;
+
+    my $holds_rs = $self->_result->reserves;
+    return Koha::Holds->_new_from_dbic( $holds_rs );
+}
+
 =head3 biblioitem
 
 my $field = $self->biblioitem()->itemtype
diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t
new file mode 100644 (file)
index 0000000..2498b86
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+# Copyright 2016 Koha Development team
+#
+# This file is part of Koha
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+
+use C4::Reserves;
+
+use Koha::Biblios;
+use Koha::Patrons;
+use t::lib::TestBuilder;
+use t::lib::Mocks;
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+
+my $builder = t::lib::TestBuilder->new;
+my $patron = $builder->build( { source => 'Borrower' } );
+$patron = Koha::Patrons->find( $patron->{borrowernumber} );
+
+my $biblio = Koha::Biblio->new()->store();
+
+my $biblioitem = $schema->resultset('Biblioitem')->new(
+    {
+        biblionumber => $biblio->id
+    }
+)->insert();
+
+subtest 'holds' => sub {
+    plan tests => 3;
+    C4::Reserves::AddReserve( $patron->branchcode, $patron->borrowernumber, $biblio->biblionumber );
+    my $holds = $biblio->holds;
+    is( ref($holds), 'Koha::Holds', '->holds should return a Koha::Holds object' );
+    is( $holds->count, 1, '->holds should only return 1 hold' );
+    is( $holds->next->borrowernumber, $patron->borrowernumber, '->holds should return the correct hold' );
+};
+
+$schema->storage->txn_rollback;
+
+1;