Bug 20469: Add Koha::Item->itemtype
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Mon, 27 Jul 2020 12:31:14 +0000 (12:31 +0000)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 18 Aug 2020 15:39:48 +0000 (17:39 +0200)
Also needed to add a missing rollback to preceding subtest.

Test plan:
Run t/db_dependent/Koha/Item.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Koha/Item.pm
t/db_dependent/Koha/Item.t

index 29406b5..2d66448 100644 (file)
@@ -38,6 +38,7 @@ use Koha::Checkouts;
 use Koha::CirculationRules;
 use Koha::Item::Transfer::Limits;
 use Koha::Item::Transfers;
+use Koha::ItemTypes;
 use Koha::Patrons;
 use Koha::Plugins;
 use Koha::Libraries;
@@ -819,6 +820,19 @@ sub to_api_mapping {
     };
 }
 
+=head3 itemtype
+
+    my $itemtype = $item->itemtype({ effective => 1 });
+
+    Returns Koha object for (effective) itemtype
+
+=cut
+
+sub itemtype {
+    my ( $self, $params ) = @_;
+    return Koha::ItemTypes->find( $params->{effective} ?  $self->effective_itemtype : $self->itype ); # no FK
+}
+
 =head2 Internal methods
 
 =head3 _after_item_action_hooks
index 20547d7..e8852ff 100644 (file)
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 6;
+use Test::More tests => 7;
 
 use C4::Biblio;
 use C4::Circulation;
@@ -500,4 +500,23 @@ subtest 'renewal_branchcode' => sub {
     is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
     is( $item->renewal_branchcode({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed");
 
+    $schema->storage->txn_rollback;
+};
+
+subtest 'Tests for itemtype' => sub {
+    plan tests => 4;
+    $schema->storage->txn_begin;
+
+    my $biblio = $builder->build_sample_biblio;
+    my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
+    my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
+
+    t::lib::Mocks::mock_preference('item-level_itypes', 1);
+    is( $item->itemtype({ effective => 1 })->itemtype, $item->itype, 'Pref enabled, effective parameter' );
+    is( $item->itemtype->itemtype, $item->itype, 'Pref enabled, no parameter' );
+    t::lib::Mocks::mock_preference('item-level_itypes', 0);
+    is( $item->itemtype({ effective => 1 })->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled, effective parameter' );
+    is( $item->itemtype->itemtype, $item->itype, 'Pref disabled, no parameter' );
+
+    $schema->storage->txn_rollback;
 };