From ccb614c2a1f97645e21eac5a36e6ac57d125361a Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Tue, 19 Jan 2016 13:47:41 +0000
Subject: [PATCH] Bug 15599 - Unit tests output warnings

The unit tests t/db_dependent/Circulation/Returns.t and
t/db_dependent/Items.t print out eh warning "item-level_itypes set but
no itemtype set for item". This should not happen.

Test Plan:
1) prove t/db_dependent/Circulation/Returns.t
2) Note the warning
3) prove t/db_dependent/Items.t
4) Note the warning
5) Apply this patch
6) prove t/db_dependent/Circulation/Returns.t
7) Note there is no warning
8) prove t/db_dependent/Items.t
9) Note there is no warning
---
 Koha/Schema/Result/Item.pm           |  3 ++-
 t/db_dependent/Circulation/Returns.t | 12 +++++++++---
 t/db_dependent/Items.t               | 10 +++++++---
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/Koha/Schema/Result/Item.pm b/Koha/Schema/Result/Item.pm
index c370d86..a390967 100644
--- a/Koha/Schema/Result/Item.pm
+++ b/Koha/Schema/Result/Item.pm
@@ -650,7 +650,8 @@ sub effective_itemtype {
     if ( $pref->value() && $self->itype() ) {
         return $self->itype();
     } else {
-        warn "item-level_itypes set but no itemtype set for item ($self->itemnumber)"
+        my $itemnumber = $self->id;
+        warn "item-level_itypes set but no itemtype set for item ($itemnumber)"
           if $pref->value();
         return $self->biblioitemnumber()->itemtype();
     }
diff --git a/t/db_dependent/Circulation/Returns.t b/t/db_dependent/Circulation/Returns.t
index 040779e..f11d90c 100644
--- a/t/db_dependent/Circulation/Returns.t
+++ b/t/db_dependent/Circulation/Returns.t
@@ -19,6 +19,7 @@ use Modern::Perl;
 
 use Test::More tests => 3;
 use Test::MockModule;
+use Test::Warn;
 use t::lib::TestBuilder;
 
 use C4::Biblio;
@@ -89,7 +90,7 @@ subtest "InProcessingToShelvingCart tests" => sub {
 
 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
 
-    plan tests => 2;
+    plan tests => 4;
 
     # Set item-level item types
     C4::Context->set_preference( "item-level_itypes", 1 );
@@ -161,8 +162,13 @@ subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
     is( $stat->itemtype, $ilevel_itemtype,
         "item-level itype recorded on statistics for return");
 
-    AddIssue( $borrower, $item_without_itemtype->{ barcode } );
-    AddReturn( $item_without_itemtype->{ barcode }, $branch );
+    my $itemnumber = $item_without_itemtype->{itemnumber};
+    warning_is { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
+      "item-level_itypes set but no itemtype set for item ($itemnumber)",
+      '->effective_itemtype() raises a warning when falling back to bib-level';
+    warning_is { AddReturn( $item_without_itemtype->{barcode}, $branch ) }
+      "item-level_itypes set but no itemtype set for item ($itemnumber)",
+      '->effective_itemtype() raises a warning when falling back to bib-level';
     # Test biblio-level itemtype was recorded on the 'statistics' table
     $stat = $schema->resultset('Statistic')->search({
         branch     => $branch,
diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t
index 3cc03a3..63ea70e 100755
--- a/t/db_dependent/Items.t
+++ b/t/db_dependent/Items.t
@@ -280,8 +280,9 @@ subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
     $item->itype( undef );
     $item->update();
     my $effective_itemtype;
+    my $itemnumber = $item->id;
     warning_is { $effective_itemtype = $item->effective_itemtype() }
-                "item-level_itypes set but no itemtype set for item ($item->itemnumber)",
+                "item-level_itypes set but no itemtype set for item ($itemnumber)",
                 '->effective_itemtype() raises a warning when falling back to bib-level';
 
     ok( defined $effective_itemtype &&
@@ -292,7 +293,7 @@ subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
 };
 
 subtest 'SearchItems test' => sub {
-    plan tests => 14;
+    plan tests => 15;
 
     $schema->storage->txn_begin;
     my $dbh = C4::Context->dbh;
@@ -445,7 +446,10 @@ subtest 'SearchItems test' => sub {
     ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
 
     # Make sure the link is used
-    my $item3 = GetItem($item3_itemnumber);
+    my $item3;
+    warning_is { $item3 = GetItem($item3_itemnumber) }
+                "item-level_itypes set but no itemtype set for item ($item3_itemnumber)",
+                '->effective_itemtype() raises a warning when falling back to bib-level';
     ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
 
     # Do the same search again.
-- 
2.1.4