Bug 31791: Add 'source_record_id' param to {Add,Mod}Biblio(Marc)

Sponsored-by: ByWater Solutions
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Arthur Suzuki <arthur.suzuki@biblibre.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Tomas Cohen Arazi 2024-02-08 20:18:07 -03:00 committed by Katrin Fischer
parent 03384fdcc7
commit cb8f95bce0
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
3 changed files with 125 additions and 10 deletions

View File

@ -178,7 +178,7 @@ The MARC record (in biblio_metadata.metadata) contains the complete marc record,
=head2 AddBiblio
($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, $frameworkcode, $options );
Exported function (core API) for adding a new biblio to koha.
@ -199,6 +199,8 @@ Unless C<disable_autolink> is passed AddBiblio will link record headings
to authorities based on settings in the system preferences. This flag allows
us to not link records when the authority linker is saving modifications.
=item B<record_source_id>: set as the record source when saving the record
=back
=cut
@ -293,7 +295,13 @@ sub AddBiblio {
}
# now add the record, don't index while we are in the transaction though
ModBiblioMarc( $record, $biblionumber, { skip_record_index => 1 } );
ModBiblioMarc(
$record, $biblionumber,
{
skip_record_index => 1,
record_source_id => $options->{record_source_id},
}
);
# update OAI-PMH sets
if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
@ -360,6 +368,10 @@ task to rebuild the holds queue for the biblio if I<RealTimeHoldsQueue> is enabl
Used when the indexing schedulling will be handled by the caller
=item C<record_source_id>
Set as the record source when saving the record.
=back
Returns 1 on success 0 on failure
@ -370,7 +382,14 @@ sub ModBiblio {
my ( $record, $biblionumber, $frameworkcode, $options ) = @_;
$options //= {};
my $mod_biblio_marc_options = { skip_record_index => $options->{'skip_record_index'} // 0 };
my $mod_biblio_marc_options = {
skip_record_index => $options->{'skip_record_index'} // 0,
(
( exists $options->{record_source_id} )
? ( record_source_id => $options->{record_source_id} )
: ()
)
};
if (!$record) {
carp 'No record passed to ModBiblio';
@ -2795,7 +2814,7 @@ sub _koha_delete_biblio_metadata {
=head2 ModBiblioMarc
ModBiblioMarc($newrec, $biblionumber, $options);
ModBiblioMarc( $newrec, $biblionumber, $options );
Add MARC XML data for a biblio to koha
@ -2805,7 +2824,9 @@ The C<$options> argument is a hashref with additional parameters:
=over 4
=item C<skip_record_index>: used when the indexing scheduling will be handled by the caller
=item B<skip_record_index>: used when the indexing scheduling will be handled by the caller
=item B<record_source_id>: set as the record source when saving the record
=back
@ -2890,8 +2911,16 @@ sub ModBiblioMarc {
{ action => 'save', payload => { biblio_id => $biblionumber, record => $record } }
);
$m_rs->metadata( $record->as_xml_record($encoding) );
$m_rs->store;
$m_rs->set(
{
metadata => $record->as_xml_record($encoding),
(
exists $options->{record_source_id}
? ( record_source_id => $options->{record_source_id} )
: ()
)
}
)->store;
unless ( $skip_record_index ) {
my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });

View File

@ -17,7 +17,7 @@
use Modern::Perl;
use Test::More tests => 21;
use Test::More tests => 22;
use Test::MockModule;
use Test::Warn;
use List::MoreUtils qw( uniq );
@ -45,7 +45,7 @@ Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
my $builder = t::lib::TestBuilder->new;
subtest 'AddBiblio' => sub {
plan tests => 9;
plan tests => 10;
my $marcflavour = 'MARC21';
t::lib::Mocks::mock_preference( 'marcflavour', $marcflavour );
@ -99,6 +99,25 @@ subtest 'AddBiblio' => sub {
warning_like { $builder->build_sample_biblio(); }
qr/My biblionumber is \d+ and my frameworkcode is /, "The biblionumber is correctly passed to BiblioAutoLink";
subtest 'record_source_id param tests' => sub {
plan tests => 2;
my $source = $builder->build_object( { class => 'Koha::RecordSources' } );
my $record = MARC::Record->new;
$record->append_fields( MARC::Field->new( '245', ' ', ' ', a => 'Some title' ) );
my ($biblio_id) = C4::Biblio::AddBiblio( $record, '', { record_source_id => undef } );
my $metadata = Koha::Biblios->find($biblio_id)->metadata;
is( $metadata->record_source_id, undef, 'Record source is not defined' );
($biblio_id) = C4::Biblio::AddBiblio( $record, '', { record_source_id => $source->id } );
$metadata = Koha::Biblios->find($biblio_id)->metadata;
is( $metadata->record_source_id, $source->id, 'Record source is stored as expected' );
};
};
subtest 'GetMarcSubfieldStructureFromKohaField' => sub {
@ -1057,6 +1076,35 @@ subtest 'UpdateTotalIssues on Invalid record' => sub {
};
subtest 'ModBiblio - record_source_id param tests' => sub {
plan tests => 4;
my $source = $builder->build_object( { class => 'Koha::RecordSources' } );
my $record = MARC::Record->new;
$record->append_fields( MARC::Field->new( '245', ' ', ' ', a => 'Some title' ) );
my ($biblio_id) = C4::Biblio::AddBiblio( $record, '', { record_source_id => undef } );
my $metadata = Koha::Biblios->find($biblio_id)->metadata;
is( $metadata->record_source_id, undef, 'Record source is not defined' );
C4::Biblio::ModBiblio( $record, $biblio_id, '', { record_source_id => $source->id } );
$metadata->discard_changes;
is( $metadata->record_source_id, $source->id, 'Record source is stored as expected' );
C4::Biblio::ModBiblio( $record, $biblio_id, '' );
$metadata->discard_changes;
is( $metadata->record_source_id, $source->id, 'Record source not passed, remains untouched' );
C4::Biblio::ModBiblio( $record, $biblio_id, '', { record_source_id => undef } );
$metadata->discard_changes;
is( $metadata->record_source_id, undef, 'Record source is not defined' );
};
# Cleanup
Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );

View File

@ -17,7 +17,7 @@
use Modern::Perl;
use Test::More tests => 2;
use Test::More tests => 3;
use t::lib::Mocks;
use t::lib::TestBuilder;
use MARC::Record;
@ -27,6 +27,8 @@ use Koha::Database;
use Koha::Biblios;
my $schema = Koha::Database->new->schema;
my $builder = t::lib::TestBuilder->new;
$schema->storage->txn_begin;
subtest "Check MARC field length calculation" => sub {
@ -82,3 +84,39 @@ subtest "StripWhitespaceChars tests" => sub {
};
$schema->storage->txn_rollback;
subtest "record_source_id parameter tests" => sub {
plan tests => 5;
$schema->storage->txn_begin;
my $biblio = $builder->build_sample_biblio;
my $source = $builder->build_object( { class => 'Koha::RecordSources' } );
my $metadata = $biblio->metadata;
is( $metadata->record_source_id, undef, 'Record source not set for biblio' );
C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => undef } );
$metadata->discard_changes;
is( $metadata->record_source_id, undef, 'Record source not set for biblio' );
C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => $source->id } );
$metadata->discard_changes;
is( $metadata->record_source_id, $source->id, 'Record source set for biblio' );
C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id );
$metadata->discard_changes;
is( $metadata->record_source_id, $source->id, 'Record source param not passed, no change' );
C4::Biblio::ModBiblioMarc( $metadata->record, $biblio->id, { record_source_id => undef } );
$metadata->discard_changes;
is( $metadata->record_source_id, undef, 'Record source passed but undef, unset for biblio' );
$schema->storage->txn_rollback;
};