From 1094577cff8a0f054b05717b3bc852fa3b0fbfaa Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 8 Mar 2022 11:27:59 +0000 Subject: [PATCH] Bug 24857: Add Koha Objects To test: 1 - prove t/db_dependent/Koha/Biblio/ItemGroups.t Signed-off-by: Tomas Cohen Arazi --- Koha/Biblio/ItemGroup.pm | 142 ++++++++++++++++++++++++ Koha/Biblio/ItemGroup/Item.pm | 46 ++++++++ Koha/Biblio/ItemGroup/Items.pm | 48 ++++++++ Koha/Biblio/ItemGroups.pm | 48 ++++++++ t/db_dependent/Koha/Biblio/ItemGroups.t | 70 ++++++++++++ 5 files changed, 354 insertions(+) create mode 100644 Koha/Biblio/ItemGroup.pm create mode 100644 Koha/Biblio/ItemGroup/Item.pm create mode 100644 Koha/Biblio/ItemGroup/Items.pm create mode 100644 Koha/Biblio/ItemGroups.pm create mode 100755 t/db_dependent/Koha/Biblio/ItemGroups.t diff --git a/Koha/Biblio/ItemGroup.pm b/Koha/Biblio/ItemGroup.pm new file mode 100644 index 0000000000..8c4ba66db1 --- /dev/null +++ b/Koha/Biblio/ItemGroup.pm @@ -0,0 +1,142 @@ +package Koha::Biblio::ItemGroup; + +# 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 base qw(Koha::Object); + +use Koha::Biblio::ItemGroup::Items; +use Koha::Exceptions::Object; +use Koha::Items; + +=head1 NAME + +Koha::Biblio::ItemGroup - Koha ItemGroup Object class + +=head1 API + +=head2 Class methods + +=head3 store + + $item_group->store; + +Overloaded I method that takes care of creation date handling. + +=cut + +sub store { + my ($self) = @_; + + unless ( $self->in_storage ) { + # new entry + $self->set( + { + created_on => \'NOW()' + } + ); + } + + return $self->SUPER::store(); +} + +=head3 items + + my $items = $item_group->items; + +Returns all the items linked to the item group. + +=cut + +sub items { + my ($self) = @_; + + my $items_rs = $self->_result->item_group_items; + my @item_ids = $items_rs->get_column('item_id')->all; + + return Koha::Items->new->empty unless @item_ids; + + return Koha::Items->search( + { + itemnumber => { + -in => \@item_ids + } + } + ); +} + +=head3 add_item + + $item_group->add_item({ item_id => $item_id }); + +=cut + +sub add_item { + my ($self, $params) = @_; + + my $item_id = $params->{item_id}; + + my $item = Koha::Items->find( $item_id ); + unless ( $item->biblionumber == $self->biblio_id ) { + Koha::Exceptions::Object::FKConstraint->throw( + broken_fk => 'biblio_id' + ); + } + + Koha::Biblio::ItemGroup::Item->new( + { + item_group_id => $self->id, + item_id => $item_id, + } + )->store; + + return $self; +} + +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Biblio::ItemGroup object +on the API. + +=cut + +sub to_api_mapping { + return { + created_on => 'creation_date', + updated_on => 'modification_date' + }; +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ItemGroup'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Biblio::ItemGroup'; +} + +1; diff --git a/Koha/Biblio/ItemGroup/Item.pm b/Koha/Biblio/ItemGroup/Item.pm new file mode 100644 index 0000000000..17a175f302 --- /dev/null +++ b/Koha/Biblio/ItemGroup/Item.pm @@ -0,0 +1,46 @@ +package Koha::Biblio::ItemGroup::Item; + +# 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 base qw(Koha::Object); + +=head1 NAME + +Koha::Biblio::ItemGroup::Item - Koha ItemGroup Item Object class + +=head1 API + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ItemGroupItem'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Biblio::ItemGroup::Item'; +} + +1; diff --git a/Koha/Biblio/ItemGroup/Items.pm b/Koha/Biblio/ItemGroup/Items.pm new file mode 100644 index 0000000000..9018c5e5fa --- /dev/null +++ b/Koha/Biblio/ItemGroup/Items.pm @@ -0,0 +1,48 @@ +package Koha::Biblio::ItemGroup::Items; + +# 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::Biblio::ItemGroup::Item; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Biblio::ItemGroup::Items - Koha ItemGroup Items Object set class + +=head1 API + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ItemGroupItem'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Biblio::ItemGroup::Item'; +} + +1; diff --git a/Koha/Biblio/ItemGroups.pm b/Koha/Biblio/ItemGroups.pm new file mode 100644 index 0000000000..78a6b337c6 --- /dev/null +++ b/Koha/Biblio/ItemGroups.pm @@ -0,0 +1,48 @@ +package Koha::Biblio::ItemGroups; + +# 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::Biblio::ItemGroup; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Biblio::ItemGroups - Koha ItemGroup Object set class + +=head1 API + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ItemGroup'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Biblio::ItemGroup'; +} + +1; diff --git a/t/db_dependent/Koha/Biblio/ItemGroups.t b/t/db_dependent/Koha/Biblio/ItemGroups.t new file mode 100755 index 0000000000..321e778842 --- /dev/null +++ b/t/db_dependent/Koha/Biblio/ItemGroups.t @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +# 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 Test::More tests => 3; + +use Koha::Database; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +BEGIN { + use_ok('Koha::Biblio::ItemGroup'); + use_ok('Koha::Biblio::ItemGroups'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_preference('EnableItemGroups', 1); + +subtest 'add_item() and items() tests' => sub { + + plan tests => 8; + + $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 }); + + my $item_group = Koha::Biblio::ItemGroup->new( { biblio_id => $biblio->id } )->store(); + + my $items = $item_group->items; + is( $items->count, 0, 'Item group has no items'); + + $item_group->add_item({ item_id => $item_1->id }); + my @items = $item_group->items->as_list(); + is( scalar(@items), 1, 'Item group has one item'); + is( $items[0]->id, $item_1->id, 'Item 1 is correct' ); + + $item_group->add_item({ item_id => $item_2->id }); + @items = $item_group->items->as_list(); + is( scalar(@items), 2, 'Item group has two items'); + is( $items[0]->id, $item_1->id, 'Item 1 is correct' ); + is( $items[1]->id, $item_2->id, 'Item 2 is correct' ); + + # Remove an item + $item_1->delete; + @items = $item_group->items->as_list(); + is( scalar(@items), 1, 'Item group now has only one item'); + is( $items[0]->id, $item_2->id, 'Item 2 is correct' ); + + $schema->storage->txn_rollback; +}; -- 2.39.5