From cb8f95bce0408927abce0ee0abbbe2c66dfc7fa8 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 8 Feb 2024 20:18:07 -0300 Subject: [PATCH] Bug 31791: Add 'source_record_id' param to {Add,Mod}Biblio(Marc) Sponsored-by: ByWater Solutions Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass Signed-off-by: Arthur Suzuki Signed-off-by: Katrin Fischer --- C4/Biblio.pm | 43 ++++++++++++++++++---- t/db_dependent/Biblio.t | 52 +++++++++++++++++++++++++-- t/db_dependent/Biblio/ModBiblioMarc.t | 40 ++++++++++++++++++++- 3 files changed, 125 insertions(+), 10 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index d9e43b7dba..8264e02755 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -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 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: 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 is enabl Used when the indexing schedulling will be handled by the caller +=item C + +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: used when the indexing scheduling will be handled by the caller +=item B: used when the indexing scheduling will be handled by the caller + +=item B: 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 }); diff --git a/t/db_dependent/Biblio.t b/t/db_dependent/Biblio.t index 6848f7094c..6cbf47ee0c 100755 --- a/t/db_dependent/Biblio.t +++ b/t/db_dependent/Biblio.t @@ -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-" ); diff --git a/t/db_dependent/Biblio/ModBiblioMarc.t b/t/db_dependent/Biblio/ModBiblioMarc.t index fc38684c39..a1fb6cf596 100755 --- a/t/db_dependent/Biblio/ModBiblioMarc.t +++ b/t/db_dependent/Biblio/ModBiblioMarc.t @@ -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; +}; -- 2.39.5