From dd818e19eed4ee073ddf4bac8dd6908e54c186ae Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Sun, 1 Jan 2017 11:50:44 +0100 Subject: [PATCH] Bug 9988: Add Koha objects for table need_merge_authorities Content-Type: text/plain; charset=utf-8 This patch adds two Koha objects: MergeRequest(s). A method add_merge_request is added to Koha::Authority. A class method reporting_tag_xml is added to MergeRequests.pm. Both routines are tested in Authorities.t. Test plan: Run t/db_dependent/Koha/Authorities.t Signed-off-by: Marcel de Rooy --- Koha/Authority.pm | 35 ++++++++++--- Koha/Authority/MergeRequest.pm | 60 +++++++++++++++++++++++ Koha/Authority/MergeRequests.pm | 100 ++++++++++++++++++++++++++++++++++++++ t/db_dependent/Koha/Authorities.t | 51 ++++++++++++++++++- 4 files changed, 238 insertions(+), 8 deletions(-) create mode 100644 Koha/Authority/MergeRequest.pm create mode 100644 Koha/Authority/MergeRequests.pm diff --git a/Koha/Authority.pm b/Koha/Authority.pm index b533fd7..6664995 100644 --- a/Koha/Authority.pm +++ b/Koha/Authority.pm @@ -19,13 +19,9 @@ package Koha::Authority; use Modern::Perl; -use Carp; - -use Koha::Database; -use C4::Context; -use MARC::Record; - use base qw(Koha::Object); +use Koha::Authority::MergeRequest; +use Koha::Authority::Types; =head1 NAME @@ -33,6 +29,33 @@ Koha::Authority - Koha Authority Object class =head1 API +=head2 Instance Methods + +=cut + +=head3 add_merge_request + + $self->add_merge_request({ [ authid_to => $to ], record_from => $marc }); + +=cut + +sub add_merge_request { + my ( $self, $params ) = @_; + if( $self->in_storage ) { + my $type = Koha::Authority::Types->find( $self->authtypecode ); + my $tag = $type ? $type->auth_tag_to_report : undef; + my $xml = Koha::Authority::MergeRequests->reporting_tag_xml({ + record => $params->{record_from}, + tag => $tag, + }); + my $req = Koha::Authority::MergeRequest->new({ + authid_from => $self->authid, + authid_to => $params->{authid_to} // $self->authid, + reportxml => $xml, + })->store; + } +} + =head2 Class Methods =cut diff --git a/Koha/Authority/MergeRequest.pm b/Koha/Authority/MergeRequest.pm new file mode 100644 index 0000000..6795a08 --- /dev/null +++ b/Koha/Authority/MergeRequest.pm @@ -0,0 +1,60 @@ +package Koha::Authority::MergeRequest; + +# Copyright Rijksmuseum 2017 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use parent qw(Koha::Object); + +=head1 NAME + +Koha::Authority::MergeRequest - Koha::Object class for single need_merge_authorities record + +=head1 SYNOPSIS + +use Koha::Authority::MergeRequest; + +=head1 DESCRIPTION + +Description + +=head1 METHODS + +=head2 INSTANCE METHODS + +=head2 CLASS METHODS + +=head3 _type + +Returns name of corresponding DBIC resultset + +=cut + +sub _type { + return 'NeedMergeAuthority'; +} + +=head1 AUTHOR + +Marcel de Rooy (Rijksmuseum) + +Koha Development Team + +=cut + +1; diff --git a/Koha/Authority/MergeRequests.pm b/Koha/Authority/MergeRequests.pm new file mode 100644 index 0000000..469982b --- /dev/null +++ b/Koha/Authority/MergeRequests.pm @@ -0,0 +1,100 @@ +package Koha::Authority::MergeRequests; + +# Copyright Rijksmuseum 2017 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use MARC::File::XML; +use MARC::Record; + +use C4::Context; +use Koha::Authority::MergeRequest; + +use parent qw(Koha::Objects); + +=head1 NAME + +Koha::Authority::MergeRequests - Koha::Objects class for need_merge_authorities + +=head1 SYNOPSIS + +use Koha::Authority::MergeRequests; + +=head1 DESCRIPTION + +Description + +=head1 METHODS + +=head2 INSTANCE METHODS + +=head2 CLASS METHODS + +=head3 reporting_tag_xml + + my $xml = Koha::Authority::MergeRequests->reporting_tag_xml({ + record => $record, tag => $tag, + }); + +=cut + +sub reporting_tag_xml { + my ( $class, $params ) = @_; + return if !$params->{record} || !$params->{tag}; + + my $newrecord = MARC::Record->new; + $newrecord->encoding( 'UTF-8' ); + my $reportfield = $params->{record}->field( $params->{tag} ); + return if !$reportfield; + + $newrecord->append_fields( $reportfield ); + return $newrecord->as_xml( + C4::Context->preference('marcflavour') eq 'UNIMARC' ? + 'UNIMARCAUTH' : + 'MARC21' + ); +} + +=head3 _type + +Returns name of corresponding DBIC resultset + +=cut + +sub _type { + return 'NeedMergeAuthority'; +} + +=head3 object_class + +Returns name of corresponding Koha object class + +=cut + +sub object_class { + return 'Koha::Authority::MergeRequest'; +} + +=head1 AUTHOR + +Marcel de Rooy (Rijksmuseum) + +Koha Development Team + +=cut + +1; diff --git a/t/db_dependent/Koha/Authorities.t b/t/db_dependent/Koha/Authorities.t index 890929d..a3a0625 100644 --- a/t/db_dependent/Koha/Authorities.t +++ b/t/db_dependent/Koha/Authorities.t @@ -19,10 +19,15 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 5; +use MARC::Field; +use MARC::File::XML; +use MARC::Record; +use Test::Deep; use Koha::Authority; use Koha::Authorities; +use Koha::Authority::MergeRequests; use Koha::Authority::Type; use Koha::Authority::Types; use Koha::Database; @@ -51,5 +56,47 @@ is( Koha::Authorities->search->count, $nb_of_authorities + 2, 'The 2 au $new_authority_1->delete; is( Koha::Authorities->search->count, $nb_of_authorities + 1, 'Delete should have deleted the authority' ); +subtest 'Add merge request' => sub { + plan tests => 2; + + my $count = Koha::Authority::MergeRequests->count; + $new_authority_2->add_merge_request; + is( Koha::Authority::MergeRequests->count, + $count+1, + 'Merge request added' ); + my $new_authority_3 = Koha::Authority->new; + $new_authority_3->add_merge_request; + is( Koha::Authority::MergeRequests->count, + $count+1, + 'No additional merge request added (not in storage)' ); +}; + +subtest 'Testing reporting_tag_xml in MergeRequests' => sub { + plan tests => 2; + + my $record = MARC::Record->new; + $record->append_fields( + MARC::Field->new( '024', '', '', a => 'aaa' ), + MARC::Field->new( '100', '', '', a => 'Best author' ), + MARC::Field->new( '234', '', '', a => 'Just a field' ), + ); + my $xml = Koha::Authority::MergeRequests->reporting_tag_xml({ + record => $record, tag => '110', + }); + is( $xml, undef, 'Expected no result for wrong tag' ); + $xml = Koha::Authority::MergeRequests->reporting_tag_xml({ + record => $record, tag => '100', + }); + my $newrecord = MARC::Record->new_from_xml( + $xml, 'UTF-8', + C4::Context->preference('marcflavour') eq 'UNIMARC' ? + 'UNIMARCAUTH' : + 'MARC21', + ); # MARC format does not actually matter here + cmp_deeply( $record->field('100')->subfields, + $newrecord->field('100')->subfields, + 'Compare reporting tag in both records', + ); +}; + $schema->storage->txn_rollback; -1; -- 2.1.4