From 06263876c8bf66cf2b1c90225fe3e39ee03a4692 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 14 Dec 2023 10:55:42 +0100 Subject: [PATCH] Bug 33036: Use Koha::Objects Better to use Koha::Objects everywhere and have the same pattern for the different related objects. Signed-off-by: Jonathan Druart --- Koha/Biblio.pm | 69 ++++++++++++++++++++++++++----- t/db_dependent/Koha/Biblio.t | 78 +++++++++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 10 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 42a835b0981..0b4cd67898c 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -995,6 +995,34 @@ sub subscriptions { return Koha::Subscriptions->_new_from_dbic($rs); } +=head3 serials + +my $serials = $self->serials + +Returns the related Koha::Serials object for this Biblio object + +=cut + +sub serials { + my ($self) = @_; + my $rs = $self->_result->serials; + return Koha::Serials->_new_from_dbic($rs); +} + +=head3 subscription_histories + +my $subscription_histories = $self->subscription_histories + +Returns the related Koha::Subscription::Histories object for this Biblio object + +=cut + +sub subscription_histories { + my ($self) = @_; + my $rs = $self->_result->subscriptionhistories; + return Koha::Subscription::Histories->_new_from_dbic($rs); +} + =head3 has_items_waiting_or_intransit my $itemsWaitingOrInTransit = $biblio->has_items_waiting_or_intransit @@ -1953,27 +1981,50 @@ sub merge_with { foreach my $bn_merge (@biblio_ids_to_merge) { my $from_biblio = Koha::Biblios->find($bn_merge); $from_biblio->items->move_to_biblio($self); + + # Move article requests $from_biblio->article_requests->update( { biblionumber => $ref_biblionumber }, { no_triggers => 1 } ); - for my $resultset_name (qw(Subscription Subscriptionhistory Serial Suggestion)) { - $schema->resultset($resultset_name)->search( { biblionumber => $bn_merge } ) - ->update( { biblionumber => $ref_biblionumber } ); - } + # Move subscriptions + $from_biblio->subscriptions->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); - # TODO should this be ported to more modern DB usage (i.e. DBIx::Class)? - my @allorders = GetOrdersByBiblionumber($bn_merge); - foreach my $myorder (@allorders) { - $myorder->{'biblionumber'} = $ref_biblionumber; - ModOrder($myorder); + # Move subscription histories + $from_biblio->subscription_histories->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); + + # Move serials + $from_biblio->serials->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); + + # Move suggestions + $from_biblio->suggestions->update( + { biblionumber => $ref_biblionumber }, + { no_triggers => 1 } + ); + + my $orders = $from_biblio->orders->unblessed; + for my $order (@$orders) { + $order->{'biblionumber'} = $ref_biblionumber; + + # TODO Do we really need to call ModOrder here? + ModOrder($order); # TODO : add error control (in ModOrder?) } # Move holds MergeHolds( $schema->storage->dbh, $ref_biblionumber, $bn_merge ); + my $error = DelBiblio($bn_merge); #DelBiblio return undef unless an error occurs if ($error) { die $error; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 1ce1e25ae36..b68fa35afef 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 => 31; +use Test::More tests => 33; use Test::Exception; use Test::Warn; @@ -912,6 +912,41 @@ subtest 'tickets() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'serials() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + + my $serials = $biblio->serials; + is( + ref($serials), 'Koha::Serials', + 'Koha::Biblio->serials should return a Koha::Serials object' + ); + is( $serials->count, 0, 'Koha::Biblio->serials should return the correct number of serials' ); + + # Add two serials + foreach ( 1 .. 2 ) { + $builder->build_object( + { + class => 'Koha::Serials', + value => { biblionumber => $biblio->biblionumber } + } + ); + } + + $serials = $biblio->serials; + is( + ref($serials), 'Koha::Serials', + 'Koha::Biblio->serials should return a Koha::Serials object' + ); + is( $serials->count, 2, 'Koha::Biblio->serials should return the correct number of serials' ); + + $schema->storage->txn_rollback; +}; + subtest 'subscriptions() tests' => sub { plan tests => 4; @@ -945,6 +980,47 @@ subtest 'subscriptions() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'subscription_histories() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio; + + my $sub_histories = $biblio->subscription_histories; + is( + ref($sub_histories), 'Koha::Subscription::Histories', + 'Koha::Biblio->subscription_histories should return a Koha::Subscription::Histories object' + ); + is( + $sub_histories->count, 0, + 'Koha::Biblio->subscription_histories should return the correct number of subscription histories' + ); + + # Add two subscription histories + foreach ( 1 .. 2 ) { + $builder->build_object( + { + class => 'Koha::Subscription::Histories', + value => { biblionumber => $biblio->biblionumber } + } + ); + } + + $sub_histories = $biblio->subscription_histories; + is( + ref($sub_histories), 'Koha::Subscription::Histories', + 'Koha::Biblio->subscription_histories should return a Koha::Subscription::Histories object' + ); + is( + $sub_histories->count, 2, + 'Koha::Biblio->subscription_histories should return the correct number of subscription histories' + ); + + $schema->storage->txn_rollback; +}; + subtest 'get_marc_notes() MARC21 tests' => sub { plan tests => 14; -- 2.34.1