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 <tomascohen@theke.io>
This commit is contained in:
Nick Clemens 2022-03-08 12:00:23 +00:00 committed by Tomas Cohen Arazi
parent 95a6ee4d53
commit 35d5ec1a67
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
4 changed files with 96 additions and 6 deletions

View File

@ -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 );

View File

@ -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();

View File

@ -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(

View File

@ -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;
};