From 2ab8f50835acb06a115f00119b88e6f5053472c1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 22 Jul 2021 10:13:42 +0100 Subject: [PATCH] Bug 22690: (QA follow-up) Move adopt_items_from_biblios to Koha::Items This patch moves the Koha::Biblio->adopt_items_from_biblio method to the Koha::Items set class and updates all calls from Biblio2->adopt_items_from_biblio(Biblio1) to Biblio->items->move_to_biblio(Biblio2) Signed-off-by: Jonathan Druart --- Koha/Biblio.pm | 24 ---------------------- Koha/Items.pm | 22 ++++++++++++++++++++ cataloguing/merge.pl | 4 ++-- t/db_dependent/Koha/Biblio.t | 24 +--------------------- t/db_dependent/Koha/Items.t | 24 +++++++++++++++++++++- t/db_dependent/Koha/SearchEngine/Indexer.t | 4 ++-- 6 files changed, 50 insertions(+), 52 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 4f1fabc98a..26ee7abd96 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -39,7 +39,6 @@ use Koha::CirculationRules; use Koha::Item::Transfer::Limits; use Koha::Items; use Koha::Libraries; -use Koha::SearchEngine::Indexer; use Koha::Suggestions; use Koha::Subscriptions; @@ -953,29 +952,6 @@ sub get_marc_host { } } -=head3 adopt_items_from_biblio - -$biblio->adopt_items_from_biblio($from_biblio); - -Move items from the given biblio to this one. - -=cut - -sub adopt_items_from_biblio { - my ( $self, $from_biblio ) = @_; - - my $items = $from_biblio->items; - if ($items) { - while (my $item = $items->next()) { - $item->move_to_biblio($self, { 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( $from_biblio->biblionumber, "specialUpdate", "biblioserver" ); - } -} - - =head2 Internal methods =head3 type diff --git a/Koha/Items.pm b/Koha/Items.pm index 2643223d3b..834ac18230 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -26,6 +26,8 @@ use Koha::Item; use base qw(Koha::Objects); +use Koha::SearchEngine::Indexer; + =head1 NAME Koha::Items - Koha Item object set class @@ -108,6 +110,26 @@ sub filter_out_lost { return $self->search( $params ); } +=head3 move_to_biblio + + $items->move_to_biblio($to_biblio); + +Move items to a given biblio. + +=cut + +sub move_to_biblio { + my ( $self, $to_biblio ) = @_; + + while (my $item = $self->next()) { + $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( $from_biblio->biblionumber, "specialUpdate", "biblioserver" ); +} + + =head2 Internal methods =head3 _type diff --git a/cataloguing/merge.pl b/cataloguing/merge.pl index 47227178b6..fca94ca4d5 100755 --- a/cataloguing/merge.pl +++ b/cataloguing/merge.pl @@ -96,7 +96,7 @@ if ($merge) { my $biblio = Koha::Biblios->find($ref_biblionumber); foreach my $biblionumber (@biblionumbers) { my $from_biblio = Koha::Biblios->find($biblionumber); - $biblio->adopt_items_from_biblio($from_biblio); + $from_biblio->items->move_to_biblio($biblio); $from_biblio->article_requests->update({ biblionumber => $ref_biblionumber }, { no_triggers => 1 }); } @@ -161,7 +161,7 @@ if ($merge) { # Moving suggestions $sth_suggestions->execute($ref_biblionumber, $biblionumber); - # Moving orders (orders linked to items of frombiblio have already been moved by adopt_items_from_biblio) + # Moving orders (orders linked to items of frombiblio have already been moved by move_to_biblio) my @allorders = GetOrdersByBiblionumber($biblionumber); foreach my $myorder (@allorders) { $myorder->{'biblionumber'} = $ref_biblionumber; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index c978672aca..83de0634c6 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 15; +use Test::More tests => 14; use C4::Biblio qw( AddBiblio ModBiblio ); use Koha::Database; @@ -637,25 +637,3 @@ subtest 'get_marc_notes() UNIMARC tests' => sub { $schema->storage->txn_rollback; }; - -subtest 'adopt_items_from_biblio() tests' => sub { - - plan tests => 2; - - $schema->storage->txn_begin; - - my $biblio1 = $builder->build_sample_biblio; - my $biblio2 = $builder->build_sample_biblio; - my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber }); - my $item2 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber }); - - $biblio2->adopt_items_from_biblio($biblio1); - - $item1->discard_changes; - $item2->discard_changes; - - is($item1->biblionumber, $biblio2->biblionumber, "Item 1 moved"); - is($item2->biblionumber, $biblio2->biblionumber, "Item 2 moved"); - - $schema->storage->txn_rollback; -}; diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index bad4256d6a..d743402b1a 100755 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 14; +use Test::More tests => 15; use Test::MockModule; use Test::Exception; @@ -1725,3 +1725,25 @@ subtest 'filter_out_lost() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'move_to_biblio() tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + my $biblio1 = $builder->build_sample_biblio; + my $biblio2 = $builder->build_sample_biblio; + my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber }); + my $item2 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber }); + + $biblio1->items->move_to_biblio($biblio2); + + $item1->discard_changes; + $item2->discard_changes; + + is($item1->biblionumber, $biblio2->biblionumber, "Item 1 moved"); + is($item2->biblionumber, $biblio2->biblionumber, "Item 2 moved"); + + $schema->storage->txn_rollback; + +}; diff --git a/t/db_dependent/Koha/SearchEngine/Indexer.t b/t/db_dependent/Koha/SearchEngine/Indexer.t index 9ee997c263..22ff468571 100755 --- a/t/db_dependent/Koha/SearchEngine/Indexer.t +++ b/t/db_dependent/Koha/SearchEngine/Indexer.t @@ -184,8 +184,8 @@ subtest 'Test indexer calls' => sub { } undef, "index_records is not called for $engine when moving an item to another biblio (Item->move_to_biblio) if skip_record_index passed"; warnings_are{ - $biblio2->adopt_items_from_biblio($biblio); - } [$engine,"Koha::Biblio",$engine,"Koha::Biblio"], "index_records is called for both biblios for $engine when adopting items (Biblio->adopt_items_from_biblio)"; + $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)"; $builder->build({ source => 'Issue', -- 2.39.5