From 35d5ec1a67e44b9044737e2744c2eabc37550955 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 8 Mar 2022 12:00:23 +0000 Subject: [PATCH] Bug 24857: Add Object Methods To test: 1 - prove t/db_dependent/Koha/Biblio.t 2 - prove t/db_dependent/Koha/Item.t Signed-off-by: Tomas Cohen Arazi --- Koha/Biblio.pm | 16 ++++++++++++++++ Koha/Item.pm | 30 ++++++++++++++++++++++++++---- t/db_dependent/Koha/Biblio.t | 29 ++++++++++++++++++++++++++++- t/db_dependent/Koha/Item.t | 27 ++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 6 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 1fc0a57c26..0af49b939e 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -33,6 +33,7 @@ use base qw(Koha::Object); use Koha::Acquisition::Orders; use Koha::ArticleRequests; use Koha::Biblio::Metadatas; +use Koha::Biblio::ItemGroups; use Koha::Biblioitems; use Koha::Checkouts; use Koha::CirculationRules; @@ -118,6 +119,21 @@ sub active_orders { return $self->orders->search({ datecancellationprinted => undef }); } +=head3 item_groups + +my $item_groups = $biblio->item_groups(); + +Returns a Koha::Biblio::ItemGroups object + +=cut + +sub item_groups { + my ( $self ) = @_; + + my $item_groups = $self->_result->item_groups; + return Koha::Biblio::ItemGroups->_new_from_dbic($item_groups); +} + =head3 can_article_request my $bool = $biblio->can_article_request( $borrower ); diff --git a/Koha/Item.pm b/Koha/Item.pm index 6e96df5999..cfb81bca37 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -31,22 +31,23 @@ use C4::ClassSource qw( GetClassSort ); use C4::Log qw( logaction ); use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue; +use Koha::Biblio::ItemGroups; use Koha::Checkouts; use Koha::CirculationRules; use Koha::CoverImages; -use Koha::SearchEngine::Indexer; use Koha::Exceptions::Item::Transfer; +use Koha::Item::Attributes; use Koha::Item::Transfer::Limits; use Koha::Item::Transfers; -use Koha::Item::Attributes; use Koha::ItemTypes; +use Koha::Libraries; use Koha::Patrons; use Koha::Plugins; -use Koha::Libraries; +use Koha::Result::Boolean; +use Koha::SearchEngine::Indexer; use Koha::StockRotationItem; use Koha::StockRotationRotas; use Koha::TrackedLinks; -use Koha::Result::Boolean; use base qw(Koha::Object); @@ -412,6 +413,27 @@ sub checkout { return Koha::Checkout->_new_from_dbic( $checkout_rs ); } +=head3 item_group + +my $item_group = $item->item_group; + +Return the item group for this item + +=cut + +sub item_group { + my ( $self ) = @_; + + my $item_group_item = $self->_result->item_group_item; + return unless $item_group_item; + + my $item_group_rs = $item_group_item->item_group; + return unless $item_group_rs; + + my $item_group = Koha::Biblio::ItemGroup->_new_from_dbic( $item_group_rs ); + return $item_group; +} + =head3 holds my $holds = $item->holds(); diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 14fab65a60..df9dffe0bd 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 => 21; # +1 +use Test::More tests => 22; # +1 use Test::Warn; use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc ); @@ -1026,6 +1026,33 @@ subtest 'Recalls tests' => sub { $schema->storage->txn_rollback; }; +subtest 'item_groups() tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + + my @item_groups = $biblio->item_groups->as_list; + is( scalar(@item_groups), 0, 'Got zero item groups'); + + my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + @item_groups = $biblio->item_groups->as_list; + is( scalar(@item_groups), 1, 'Got one item group'); + is( $item_groups[0]->id, $item_group_1->id, 'Got correct item group'); + + my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + @item_groups = $biblio->item_groups->as_list; + is( scalar(@item_groups), 2, 'Got two item groups'); + is( $item_groups[0]->id, $item_group_1->id, 'Got correct item group 1'); + is( $item_groups[1]->id, $item_group_2->id, 'Got correct item group 2'); + + $schema->storage->txn_rollback; +}; + sub component_record1 { my $marc = MARC::Record->new; $marc->append_fields( diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 9c3ef01d1c..a61be9baa0 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -20,7 +20,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 16; +use Test::More tests => 17; use Test::Exception; use Test::MockModule; @@ -1496,3 +1496,28 @@ subtest 'Notforloan tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'item_group() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + + is( $item_1->item_group, undef, 'Item 1 has no item group'); + is( $item_2->item_group, undef, 'Item 2 has no item group'); + + my $item_group_1 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + my $item_group_2 = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + $item_group_1->add_item({ item_id => $item_1->id }); + $item_group_2->add_item({ item_id => $item_2->id }); + + is( $item_1->item_group->id, $item_group_1->id, 'Got item group 1 correctly' ); + is( $item_2->item_group->id, $item_group_2->id, 'Got item group 2 correctly' ); + + $schema->storage->txn_rollback; +}; -- 2.39.2