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 <andrewfh@dubcolib.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Nick Clemens 2022-12-12 18:13:25 +00:00 committed by Tomas Cohen Arazi
parent f41f272ff0
commit ecc92f92ca
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
2 changed files with 128 additions and 1 deletions

View File

@ -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

View File

@ -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;