Bug 24857: Add Koha Objects

To test:
1 - prove t/db_dependent/Koha/Biblio/ItemGroups.t

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Nick Clemens 2022-03-08 11:27:59 +00:00 committed by Tomas Cohen Arazi
parent 9ab9601ee9
commit 1094577cff
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
5 changed files with 354 additions and 0 deletions

142
Koha/Biblio/ItemGroup.pm Normal file
View File

@ -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 <http://www.gnu.org/licenses>.
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<store> 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;

View File

@ -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 <http://www.gnu.org/licenses>.
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;

View File

@ -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 <http://www.gnu.org/licenses>.
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;

48
Koha/Biblio/ItemGroups.pm Normal file
View File

@ -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 <http://www.gnu.org/licenses>.
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;

View File

@ -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 <http://www.gnu.org/licenses>.
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;
};