From e4ddd3045430b4adb16f71472c3805326152f8a1 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 31 Oct 2022 17:56:23 +0000 Subject: [PATCH] Bug 32054: Add get_import_record_matches object method and use it Thispatch adds the new method and alters addorderiso2907.pl to use this rather than GetRecordImportMatches To test: 1 - Import the attached record several times 2 - Set up a matching rule: TitleAuthor threshold: 100 Matchpoint: search index: title, score: 100, tag: 245$a search index: author, score: 100, tag:100$a 3 - Edit one of the imported records to have a different author 4 - Stage the file again. and match using the matchpoint above 5 - Note that matches are found and listed on batch management, with the lowest scored match last 6 - Choose that match 7 - In acquisitions, add to a basket from the staged file 8 - Check the box for the record 9 - Note the match lists the biblionumber for the highest scoring match, not the chosen one 10 - Add an order and note it is for the wrong biblio 11 - Appy patch 12 - Restart_all 13 - Stage the file again and choose a lower scoring match 14 - Confirm when adding to basket this match is preserved 15 - Complete order and verify correct biblio ordered 16 - Stage again, select no match 17 - Confirm no match listed when adding to basket, and choose 'Do not look for matching records' while adding 18 - Confirm order is created on a new biblio --- Koha/Import/Record.pm | 22 ++++++++++++++ Koha/Import/Record/Matches.pm | 18 +++++++++++ acqui/addorderiso2709.pl | 17 ++++++----- t/db_dependent/Koha/Import/Records.t | 45 +++++++++++++++++++++++++++- 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/Koha/Import/Record.pm b/Koha/Import/Record.pm index a1fedab5fe..1e209df428 100644 --- a/Koha/Import/Record.pm +++ b/Koha/Import/Record.pm @@ -23,6 +23,7 @@ use MARC::Record; use C4::Context; use Koha::Database; use Koha::Import::Record::Biblios; +use Koha::Import::Record::Matches; use base qw(Koha::Object); @@ -71,6 +72,27 @@ sub import_biblio { return Koha::Import::Record::Biblio->_new_from_dbic( $import_biblio_rs ); } +=head3 get_import_record_matches + +Returns the Import::Record::Matches for the record +optionally specify a 'chosen' param to get only the chosen match + + my $matches = $import_record->get_import_record_matches([{ chosen => 1 }]) + +=cut + +sub get_import_record_matches { + my ($self, $params) = @_; + my $chosen = $params->{chosen}; + + my $matches = $self->_result->import_record_matches; + $matches = Koha::Import::Record::Matches->_new_from_dbic( $matches ); + + return $matches->filter_by_chosen() if $chosen; + + return $matches->search({},{ order_by => { -desc => ['score','candidate_match_id'] } }); +} + =head2 Internal methods =head3 _type diff --git a/Koha/Import/Record/Matches.pm b/Koha/Import/Record/Matches.pm index 5e6e5aa147..bd2a5bbd06 100644 --- a/Koha/Import/Record/Matches.pm +++ b/Koha/Import/Record/Matches.pm @@ -35,6 +35,24 @@ Koha::Import::Record::Matches - Koha Import Record Matches Object set class =cut +=head3 filter_by_chosen + + Koha::Import::Record::Matches->filter_by_chosen(); + + Returns chosen import record matches + +=cut + +sub filter_by_chosen { + + my ( $self ) = @_; + + my $chosen = $self->search({ chosen => 1 }); + + return $chosen; + +} + =head3 _type =cut diff --git a/acqui/addorderiso2709.pl b/acqui/addorderiso2709.pl index cdbb4541dd..199d65e3d5 100755 --- a/acqui/addorderiso2709.pl +++ b/acqui/addorderiso2709.pl @@ -30,7 +30,7 @@ use Encode; use C4::Context; use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); -use C4::ImportBatch qw( GetImportRecordMatches SetImportBatchStatus GetImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction ); +use C4::ImportBatch qw( SetImportBatchStatus GetImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction ); use C4::Matcher; use C4::Search qw( FindDuplicate ); use C4::Biblio qw( @@ -160,8 +160,9 @@ if ($op eq ""){ # Check if this import_record_id was selected next if not grep { $_ eq $import_record->import_record_id } @import_record_id_selected; my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information"; - my $match = GetImportRecordMatches( $import_record->import_record_id, 1 ); - my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0; + my $matches = $import_record->get_import_record_matches({ chosen => 1 }); + my $match = $matches->count ? $matches->next : undef; + my $biblionumber = $match ? $match->candidate_match_id : 0; my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1; my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id; my $c_discount = shift ( @discount); @@ -478,7 +479,9 @@ sub import_biblios_list { while ( my $import_record = $import_records->next ) { my $item_id = 1; $biblio_count++; - my $match = GetImportRecordMatches($import_record->import_record_id, 1); + my $matches = $import_record->get_import_record_matches({ chosen => 1 }); + my $match = $matches->count ? $matches->next : undef; + my $match_biblio = $match ? Koha::Biblios->find({ biblionumber => $match->candidate_match_id }) : undef; my %cellrecord = ( import_record_id => $import_record->import_record_id, import_biblio => $import_record->import_biblio, @@ -486,9 +489,9 @@ sub import_biblios_list { status => $import_record->status, record_sequence => $import_record->record_sequence, overlay_status => $import_record->overlay_status, - match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0, - match_citation => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '', - match_score => $#$match > -1 ? $match->[0]->{'score'} : 0, + match_biblionumber => $match ? $match->candidate_match_id : 0, + match_citation => $match_biblio ? ($match_biblio->title || '') . ' ' .( $match_biblio->author || ''): '', + match_score => $match ? $match->score : 0, ); my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information"; diff --git a/t/db_dependent/Koha/Import/Records.t b/t/db_dependent/Koha/Import/Records.t index 0c9974ebc8..687c324383 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 => 3; +use Test::More tests => 10; use Koha::Import::Records; use Koha::Database; @@ -40,6 +40,49 @@ is( Koha::Import::Records->search->count, $nb_of_records + 2, 'The 2 records sho my $retrieved_record_1 = Koha::Import::Records->search({ import_record_id => $record_1->{import_record_id}})->next; is_deeply( $retrieved_record_1->unblessed, $record_1, 'Find a record by import record id should return the correct record' ); +my $matches = $retrieved_record_1->get_import_record_matches(); +is( $matches->count, 0, "No matches returned if none set"); + +my $biblio = $builder->build_sample_biblio; +my $biblio_1 = $builder->build_sample_biblio; +my $biblio_2 = $builder->build_sample_biblio; +my $match_1 = $builder->build_object({ class => 'Koha::Import::Record::Matches', + value => { + score => 100, + chosen => 0, + candidate_match_id => $biblio->biblionumber, + import_record_id => $retrieved_record_1->import_record_id + } + }); +my $match_2 = $builder->build_object({ class => 'Koha::Import::Record::Matches', + value => { + score => 50, + chosen => 1, + candidate_match_id => $biblio_1->biblionumber, + import_record_id => $retrieved_record_1->import_record_id + } + }); +my $match_3 = $builder->build_object({ class => 'Koha::Import::Record::Matches', + value => { + score => 100, + chosen => 0, + candidate_match_id => $biblio_2->biblionumber, + import_record_id => $retrieved_record_1->import_record_id + } + }); + +$matches = $retrieved_record_1->get_import_record_matches(); +is( $matches->count, 3, 'We get three matches'); + +is_deeply( $matches->next->unblessed, $match_3->unblessed, "Match order is score desc, biblionumber desc, so 3 is first"); + +is_deeply( $matches->next->unblessed, $match_1->unblessed, "Match order is score desc, biblionumber desc, so 1 is second"); +is_deeply( $matches->next->unblessed, $match_2->unblessed, "Match order is score desc, biblionumber desc, so 2 is third"); + +$matches = $retrieved_record_1->get_import_record_matches({ chosen => 1 }); +is( $matches->count, 1, 'We get only the chosen match when requesting chosen'); +is_deeply( $matches->next->unblessed, $match_2->unblessed, "Match 2 is the chosen match"); + $retrieved_record_1->delete; is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' ); -- 2.30.2