From 97316c0b8743217b89f7b7cb1743df5971dffc4c 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). MergeRequest has a new method and an oldmarc method. A class method reporting_tag_xml is added to MergeRequests.pm. All new routines are tested in Authorities.t. Removes a few unneeded modules from Koha::Authority. Test plan: Run t/db_dependent/Koha/Authorities.t Signed-off-by: Marcel de Rooy --- Koha/Authority.pm | 6 --- Koha/Authority/MergeRequest.pm | 109 ++++++++++++++++++++++++++++++++++++++ Koha/Authority/MergeRequests.pm | 100 ++++++++++++++++++++++++++++++++++ t/db_dependent/Koha/Authorities.t | 62 +++++++++++++++++++++- 4 files changed, 269 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..b94d560 100644 --- a/Koha/Authority.pm +++ b/Koha/Authority.pm @@ -19,12 +19,6 @@ package Koha::Authority; use Modern::Perl; -use Carp; - -use Koha::Database; -use C4::Context; -use MARC::Record; - use base qw(Koha::Object); =head1 NAME diff --git a/Koha/Authority/MergeRequest.pm b/Koha/Authority/MergeRequest.pm new file mode 100644 index 0000000..20c138c --- /dev/null +++ b/Koha/Authority/MergeRequest.pm @@ -0,0 +1,109 @@ +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); + +use Koha::Authorities; +use Koha::Authority::MergeRequests; +use Koha::Authority::Types; + +=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 + +=head3 new + + $self->new({ + authid => $id, + [ authid_new => $new, ] + [ oldrecord => $marc, ] + }); + + authid refers to the old authority id, + authid_new optionally refers to a new different authority id + + oldrecord is the MARC record belonging to the original authority record + + This method returns an object and initializes the reportxml property. + +=cut + +sub new { + my ( $class, $params ) = @_; + my $oldrecord = delete $params->{oldrecord}; + delete $params->{reportxml}; # just making sure it is empty + my $self = $class->SUPER::new( $params ); + + if( $self->authid && $oldrecord ) { + my $auth = Koha::Authorities->find( $self->authid ); + my $type = $auth ? Koha::Authority::Types->find($auth->authtypecode) : undef; + $self->reportxml( Koha::Authority::MergeRequests->reporting_tag_xml({ record => $oldrecord, tag => $type->auth_tag_to_report })) if $type; + } + return $self; +} + +=head3 oldmarc + + my $record = $self->oldmarc; + + Convert reportxml back to MARC::Record. + +=cut + +sub oldmarc { + my ( $self ) = @_; + return if !$self->reportxml; + return MARC::Record->new_from_xml( $self->reportxml, 'UTF-8' ); +} + +=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..8bc98fd 100644 --- a/t/db_dependent/Koha/Authorities.t +++ b/t/db_dependent/Koha/Authorities.t @@ -19,10 +19,16 @@ 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::MergeRequest; +use Koha::Authority::MergeRequests; use Koha::Authority::Type; use Koha::Authority::Types; use Koha::Database; @@ -51,5 +57,57 @@ 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 'New merge request, method oldmarc' => sub { + plan tests => 4; + + my $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '100', '', '', a => 'a', b => 'b_findme' ), + MARC::Field->new( '200', '', '', a => 'aa' ), + ); + my $req = Koha::Authority::MergeRequest->new({ + authid => $new_authority_2->authid, + reportxml => 'Should be discarded', + }); + is( $req->reportxml, undef, 'Reportxml is undef without oldrecord' ); + + $req = Koha::Authority::MergeRequest->new({ + authid => $new_authority_2->authid, + oldrecord => $marc, + }); + like( $req->reportxml, qr/b_findme/, 'Reportxml initialized' ); + + # Check if oldmarc is a MARC::Record and has one field + is( ref( $req->oldmarc ), 'MARC::Record', 'Check oldmarc method' ); + is( scalar $req->oldmarc->fields, 1, 'Contains one field' ); +}; + +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