Bug 22144: Add method metadata() to Koha::Biblio
authorKyle M Hall <kyle@bywatersolutions.com>
Wed, 16 Jan 2019 19:25:07 +0000 (14:25 -0500)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 30 Jan 2019 11:50:48 +0000 (11:50 +0000)
It would be very convenient to be able to retrieve a bib's attached metadata objects directly from the biblio object. This would be very useful for Template Toolkit syntax in slips and notices where a library would like to use arbitrary metadata fields that aren't stored in the database.

Test Plan:
1) Apply this patch
2) prove t/db_dependent/Koha/Biblio.t

Signed-off-by: Te Rahui Tunua <terahuitunua@catalyst.net.nz>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

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

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

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

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

index 1dca4d9..d2e4d4d 100644 (file)
@@ -28,12 +28,13 @@ use Koha::DateUtils qw( dt_from_string );
 
 use base qw(Koha::Object);
 
-use Koha::Items;
-use Koha::Biblioitems;
-use Koha::ArticleRequests;
 use Koha::ArticleRequest::Status;
+use Koha::ArticleRequests;
+use Koha::Biblio::Metadatas;
+use Koha::Biblioitems;
 use Koha::IssuingRules;
 use Koha::Item::Transfer::Limits;
+use Koha::Items;
 use Koha::Libraries;
 use Koha::Subscriptions;
 
@@ -61,6 +62,22 @@ sub store {
     return $self->SUPER::store;
 }
 
+=head3 metadata
+
+my $metadata = $biblio->metadata();
+
+Returns a Koha::Biblio::Metadata object
+
+=cut
+
+sub metadata {
+    my ( $self ) = @_;
+
+    $self->{_metadata} ||= Koha::Biblio::Metadatas->find( { biblionumber => $self->id } );
+
+    return $self->{_metadata};
+}
+
 =head3 subtitles
 
 my @subtitles = $biblio->subtitles();
index f271f6f..cf55373 100644 (file)
@@ -19,6 +19,8 @@ use Modern::Perl;
 
 use Carp;
 
+use C4::Biblio qw();
+
 use Koha::Database;
 
 use base qw(Koha::Object);
@@ -33,6 +35,28 @@ Koha::Metadata - Koha Metadata Object class
 
 =cut
 
+=head3 record
+
+my @record = $biblio->record($params);
+
+Returns a MARC::Record object for a record.
+
+This method accepts the same paramters as C4::Biblio::GetMarcBiblio,
+but does not require the 'biblionumber' parameter.
+
+=cut
+
+sub record {
+    my ( $self, $params ) = @_;
+
+    $params->{biblionumber} = $self->biblionumber;
+
+    my $record = C4::Biblio::GetMarcBiblio($params);
+
+    return $record;
+}
+
+
 =head3 type
 
 =cut
diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t
new file mode 100644 (file)
index 0000000..281ad65
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+
+# 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 => 3;
+
+use C4::Biblio;
+use Koha::Database;
+
+BEGIN {
+    use_ok('Koha::Biblio');
+    use_ok('Koha::Biblios');
+}
+
+my $schema = Koha::Database->new->schema;
+
+subtest 'metadata() tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $title = 'Oranges and Peaches';
+
+    my $record = MARC::Record->new();
+    my $field = MARC::Field->new('245','','','a' => $title);
+    $record->append_fields( $field );
+    my ($biblionumber) = C4::Biblio::AddBiblio($record, '');
+
+    my $biblio = Koha::Biblios->find( $biblionumber );
+    is( ref $biblio, 'Koha::Biblio', 'Found a Koha::Biblio object' );
+
+    my $metadata = $biblio->metadata;
+    is( ref $metadata, 'Koha::Biblio::Metadata', 'Method metadata() returned a Koha::Biblio::Metadata object' );
+
+    my $record2 = $metadata->record;
+    is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
+
+    is( $record2->field('245')->subfield("a"), $title, 'Title in 245$a matches title from original record object' );
+
+    $schema->storage->txn_rollback;
+};