From 204eca096c4921dd20452e0d173a3fc7ebd6bb4e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 12 Dec 2022 18:13:25 +0000 Subject: [PATCH] Bug 32437: Add replace method to Koha::Import::Record objects This patch adds a replace routine to Koha::Import::Record - largely copying and updating code from C4::ImportBatch To test: prove t/db_dependent/Koha/Import/Records.t Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi (cherry picked from commit ecc92f92ca1569f20abbc4aa0cea3944d1cb4973) Signed-off-by: Jacob O'Mara --- Koha/Import/Record.pm | 73 ++++++++++++++++++++++++++++ t/db_dependent/Koha/Import/Records.t | 56 ++++++++++++++++++++- 2 files changed, 128 insertions(+), 1 deletion(-) diff --git a/Koha/Import/Record.pm b/Koha/Import/Record.pm index 1e209df428..c85c533d40 100644 --- a/Koha/Import/Record.pm +++ b/Koha/Import/Record.pm @@ -21,8 +21,11 @@ use Carp; use MARC::Record; use C4::Context; +use C4::Biblio qw(ModBiblio); +use C4::AuthoritiesMarc qw(GuessAuthTypeCode ModAuthority); use Koha::Database; use Koha::Import::Record::Biblios; +use Koha::Import::Record::Auths; use Koha::Import::Record::Matches; use base qw(Koha::Object); @@ -72,6 +75,20 @@ sub import_biblio { return Koha::Import::Record::Biblio->_new_from_dbic( $import_biblio_rs ); } +=head3 import_auth + +Returns the import auth object for this import record + + my $import_auth = $import_record->import_auth() + +=cut + +sub import_auth { + my ( $self ) = @_; + my $import_auth_rs = $self->_result->import_auth; + return Koha::Import::Record::Auth->_new_from_dbic( $import_auth_rs ); +} + =head3 get_import_record_matches Returns the Import::Record::Matches for the record @@ -93,6 +110,62 @@ sub get_import_record_matches { return $matches->search({},{ order_by => { -desc => ['score','candidate_match_id'] } }); } +=head3 replace + +Import the record to replace an existing record which is passed to this sub + + $import_record->replace({ biblio => $biblio_object }); + +=cut + +sub replace { + my ($self, $params) = @_; + my $biblio = $params->{biblio}; + my $authority = $params->{authority}; + + my $userenv = C4::Context->userenv; + my $logged_in_patron = Koha::Patrons->find( $userenv->{number} ); + + my $marc_record = $self->get_marc_record; + my $xmlrecord; + if( $biblio ){ + my $record = $biblio->metadata->record; + $xmlrecord = $record->as_xml; + my $context = { source => 'batchimport' }; + if ($logged_in_patron) { + $context->{categorycode} = $logged_in_patron->categorycode; + $context->{userid} = $logged_in_patron->userid; + } + ModBiblio( + $marc_record, + $biblio->id, + $biblio->frameworkcode, + { + overlay_context => $context, + skip_record_index => 1 + } + ); + $self->import_biblio->matched_biblionumber( $biblio->id )->store; + } elsif( $authority ) { + $xmlrecord = $authority->marcxml; + ModAuthority( + $authority->id, + $marc_record, + GuessAuthTypeCode($marc_record) + ); + $self->import_auth->matched_authid( $authority->id )->store; + } else { + # We could also throw an exception + return; + } + $self->marcxml_old( $xmlrecord ); + $self->status('imported'); + $self->overlay_status('match_applied'); + $self->store; + + return 1; +} + =head2 Internal methods =head3 _type diff --git a/t/db_dependent/Koha/Import/Records.t b/t/db_dependent/Koha/Import/Records.t index 687c324383..c6df426207 100755 --- a/t/db_dependent/Koha/Import/Records.t +++ b/t/db_dependent/Koha/Import/Records.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Koha::Import::Records; use Koha::Database; @@ -86,4 +86,58 @@ is_deeply( $matches->next->unblessed, $match_2->unblessed, "Match 2 is the chose $retrieved_record_1->delete; is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' ); +subtest 'replace' => sub { + plan tests => 4; + + + # Replace biblio + my $import_record = $builder->build_object({ class => 'Koha::Import::Records' }); + my $import_record_biblio = $builder->build_object({ + class => 'Koha::Import::Record::Biblios', + value => { + import_record_id => $import_record->id + } + }); + + my $koha_biblio = $builder->build_sample_biblio({ title => "The before" }); + my $koha_xml = $koha_biblio->metadata->record->as_xml; + my $import_biblio = $builder->build_sample_biblio({ title => "The after" }); + $import_record->marcxml( $import_biblio->metadata->record->as_xml )->store->discard_changes; + + $import_record->replace({ biblio => $koha_biblio }); + $koha_biblio->discard_changes; + $import_record->discard_changes; + is( $koha_biblio->title, "The after", "The Koha biblio is successfully updated" ); + is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import records is correctly updated" ); + + # Replace authority + my $auth_record = MARC::Record->new; + $auth_record->append_fields( + MARC::Field->new( '100', '', '', a => 'Author' ), + ); + my $auth_id = C4::AuthoritiesMarc::AddAuthority($auth_record, undef, 'PERSO_NAME'); + my $koha_auth = Koha::Authorities->find( $auth_id ); + $koha_xml = $koha_auth->marcxml; + $import_record = $builder->build_object({ class => 'Koha::Import::Records' }); + my $import_record_auth = $builder->build_object({ + class => 'Koha::Import::Record::Auths', + value => { + import_record_id => $import_record->id + } + }); + + + my $field = $auth_record->field('100'); + $field->update( 'a' => 'Other author' ); + $import_record->marcxml( $auth_record->as_xml )->store->discard_changes; + + $import_record->replace({ authority => $koha_auth }); + $koha_auth->discard_changes; + $import_record->discard_changes; + my $updated_record = MARC::Record->new_from_xml( $koha_auth->marcxml, 'UTF-8'); + is( $updated_record->field('100')->as_string, $auth_record->field('100')->as_string, "The Koha auhtority record is correctly updated" ); + is( $import_record->marcxml_old, $koha_xml, "The old marcxml in import record is correctly updated" ); + +}; + $schema->storage->txn_rollback; -- 2.39.5