From c958dec78527415385dc51b4b43c5774d5b6ed2a Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 22 Jul 2021 11:40:20 +0100 Subject: [PATCH] Bug 22690: (QA follow-up) Add TrackedLink classes and use them This patch adds Koha::TrackedLink(s) classes based on Koha::Object(s) and then adds the relationship accessor to Koha::Item and uses it within the move_to_biblio method. Tests for new relationship also added to t/db_dependent/Koha/Item.t Signed-off-by: Jonathan Druart --- Koha/Item.pm | 22 ++++++++++++++--- Koha/TrackedLink.pm | 43 +++++++++++++++++++++++++++++++++ Koha/TrackedLinks.pm | 49 ++++++++++++++++++++++++++++++++++++++ t/db_dependent/Koha/Item.t | 22 ++++++++++++++--- 4 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 Koha/TrackedLink.pm create mode 100644 Koha/TrackedLinks.pm diff --git a/Koha/Item.pm b/Koha/Item.pm index c8b0d1cc91..3419305019 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -44,6 +44,7 @@ use Koha::Plugins; use Koha::Libraries; use Koha::StockRotationItem; use Koha::StockRotationRotas; +use Koha::TrackedLinks; use base qw(Koha::Object); @@ -1188,6 +1189,21 @@ sub orders { return Koha::Acquisition::Orders->_new_from_dbic($orders); } +=head3 tracked_links + + my $tracked_links = $item->tracked_links(); + +Returns a Koha::TrackedLinks object + +=cut + +sub tracked_links { + my ( $self ) = @_; + + my $tracked_links = $self->_result->linktrackers; + return Koha::TrackedLinks->_new_from_dbic($tracked_links); +} + =head3 move_to_biblio $item->move_to_biblio($to_biblio[, $params]); @@ -1254,9 +1270,9 @@ sub move_to_biblio { } ); - # linktrackers (there's no Koha object set available yet) - my $linktrackers = $self->_result->linktrackers; - $linktrackers->update_all({ biblionumber => $to_biblionumber }); + # tracked_links + my $tracked_links = $self->tracked_links; + $tracked_links->update({ biblionumber => $to_biblionumber }); return $to_biblionumber; } diff --git a/Koha/TrackedLink.pm b/Koha/TrackedLink.pm new file mode 100644 index 0000000000..7c7eeb871e --- /dev/null +++ b/Koha/TrackedLink.pm @@ -0,0 +1,43 @@ +package Koha::TrackedLink; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::TrackedLink - Koha TrackedLink Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'Linktracker'; +} + +1; diff --git a/Koha/TrackedLinks.pm b/Koha/TrackedLinks.pm new file mode 100644 index 0000000000..20ea4a90b2 --- /dev/null +++ b/Koha/TrackedLinks.pm @@ -0,0 +1,49 @@ +package Koha::TrackedLinks; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + + +use Koha::Database; + +use Koha::TrackedLink; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::TrackedLinks - Koha TrackedLink Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'Linktracker'; +} + +sub object_class { + return 'Koha::TrackedLink'; +} + +1; diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index d03a60a30a..9260ca5282 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 12; use Test::Exception; use C4::Biblio qw( GetMarcSubfieldStructure ); @@ -38,6 +38,22 @@ use t::lib::Mocks; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; +subtest 'tracked_links relationship' => sub { + plan tests => 3; + + my $biblio = $builder->build_sample_biblio(); + my $item = $builder->build_sample_item({ + biblionumber => $biblio->biblionumber, + }); + my $tracked_links = $item->tracked_links; + is( ref($tracked_links), 'Koha::TrackedLinks', 'tracked_links returns a Koha::TrackedLinks object set' ); + is($item->tracked_links->count, 0, "Empty Koha::TrackedLinks set returned if no tracked_links"); + my $link1 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }}); + my $link2 = $builder->build({ source => 'Linktracker', value => { itemnumber => $item->itemnumber }}); + + is($item->tracked_links()->count,2,"Two tracked links found"); +}; + subtest 'hidden_in_opac() tests' => sub { plan tests => 4; @@ -1034,8 +1050,8 @@ subtest 'move_to_biblio() tests' => sub { my $get_linktracker1 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker1->{itemnumber} })->single; my $get_linktracker2 = $schema->resultset('Linktracker')->search({ itemnumber => $linktracker2->{itemnumber} })->single; - is($get_linktracker1->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1'); - is($get_linktracker2->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2'); + is($get_linktracker1->biblionumber->biblionumber, $target_biblionumber, 'move_to_biblio moves linktracker for item 1'); + is($get_linktracker2->biblionumber->biblionumber, $source_biblionumber, 'move_to_biblio does not move linktracker for item 2'); $schema->storage->txn_rollback; }; -- 2.39.5