From 020b93bfc07764c11fe2948b42048828d499c14d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 5 Aug 2021 14:19:55 +0100 Subject: [PATCH] Bug 22690: (QA follow-up) Fix indexing for Items sets This patch adds tests and handling for calling move_to_biblio on a Koha::Items set that contains items from more than one source biblio. Test plan 1/ Inspect the changes to t/db_dependent/Koha/SearchEngine/Indexer.t 2/ Run t/db_dependent/Koha/SearchEngine/Indexer.t and confirm it passes Signed-off-by: Jonathan Druart --- Koha/Items.pm | 11 +++++++---- t/db_dependent/Koha/SearchEngine/Indexer.t | 22 ++++++++++++++-------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Koha/Items.pm b/Koha/Items.pm index 4d3de57056..193e6e58d2 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -121,12 +121,15 @@ Move items to a given biblio. sub move_to_biblio { my ( $self, $to_biblio ) = @_; - while (my $item = $self->next()) { - $item->move_to_biblio($to_biblio, { skip_record_index => 1 }); + my $biblionumbers = { $to_biblio->biblionumber => 1 }; + while ( my $item = $self->next() ) { + $biblionumbers->{ $item->biblionumber } = 1; + $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } ); } my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); - $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" ); - $indexer->index_records( $to_biblio->biblionumber, "specialUpdate", "biblioserver" ); + for my $biblionumber ( keys %{$biblionumbers} ) { + $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" ); + } } diff --git a/t/db_dependent/Koha/SearchEngine/Indexer.t b/t/db_dependent/Koha/SearchEngine/Indexer.t index 22ff468571..610a738060 100755 --- a/t/db_dependent/Koha/SearchEngine/Indexer.t +++ b/t/db_dependent/Koha/SearchEngine/Indexer.t @@ -61,7 +61,7 @@ subtest 'Test indexer object creation' => sub { }; subtest 'Test indexer calls' => sub { - plan tests => 46; + plan tests => 48; my @engines = ('Zebra'); eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; }; @@ -105,11 +105,12 @@ subtest 'Test indexer calls' => sub { my $biblio; my $biblio2; + my $biblio3; warnings_are{ $biblio = $builder->build_sample_biblio(); $biblio2 = $builder->build_sample_biblio(); - } [$engine,'C4::Biblio',$engine,'C4::Biblio'], "index_records is called for $engine when adding a biblio (ModBiblioMarc)"; - + $biblio3 = $builder->build_sample_biblio(); + } [$engine,'C4::Biblio',$engine,'C4::Biblio',$engine,'C4::Biblio'], "index_records is called for $engine when adding a biblio (ModBiblioMarc)"; my $item; my $item2; @@ -132,8 +133,8 @@ subtest 'Test indexer calls' => sub { }); $item3 = $builder->build_sample_item({biblionumber => $biblio->biblionumber}); $item4 = $builder->build_sample_item({biblionumber => $biblio->biblionumber}); - $item5 = $builder->build_sample_item({biblionumber => $biblio->biblionumber}); - $item6 = $builder->build_sample_item({biblionumber => $biblio->biblionumber}); + $item5 = $builder->build_sample_item({biblionumber => $biblio3->biblionumber}); + $item6 = $builder->build_sample_item({biblionumber => $biblio3->biblionumber}); } [$engine,"Koha::Item", $engine,"Koha::Item", $engine,"Koha::Item", @@ -185,7 +186,12 @@ subtest 'Test indexer calls' => sub { warnings_are{ $biblio->items->move_to_biblio($biblio2); - } [$engine,"Koha::Biblio",$engine,"Koha::Biblio"], "index_records is called for both biblios for $engine when adopting items (Biblio->items->move_to_biblio(Biblio)"; + } [$engine,"Koha::Items",$engine,"Koha::Items"], "index_records is called for from and to biblios for $engine when adopting items (Biblio->items->move_to_biblio(Biblio)"; + + my $items = Koha::Items->search({ itemnumber => [ $item2->itemnumber, $item5->itemnumber, $item6->itemnumber ] }); + warnings_are{ + $items->move_to_biblio($biblio); + } [$engine,"Koha::Items",$engine,"Koha::Items",$engine,"Koha::Items"], "index_records is called for all from and to biblios for $engine when adopting items (Items->move_to_biblio(Biblio)"; $builder->build({ source => 'Issue', @@ -231,10 +237,10 @@ subtest 'Test indexer calls' => sub { } undef, "index_records is not called for $engine when adding an item (Item->store) if skip_record_index passed"; warnings_are{ - DelBiblio( $biblio->biblionumber ); + DelBiblio( $biblio3->biblionumber ); } [$engine, "C4::Biblio"], "index_records is called for $engine when calling DelBiblio"; warnings_are{ - DelBiblio( $biblio->biblionumber, { skip_record_index =>1 }); + DelBiblio( $biblio3->biblionumber, { skip_record_index =>1 }); } undef, "index_records is not called for $engine when calling DelBiblio if skip_record_index passed"; } -- 2.39.5