From ec9efcc9dd94b5bc30d9bea82b74b7d1451f6304 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Wed, 19 Apr 2017 11:54:22 +0200
Subject: [PATCH] Bug 18433: QA fixes

 - Use snake_case for Koha::AuthorisedValueCategories method name
 - Replace a loop of Koha::Items->find by a single call to Koha::Items->search
 - Add unit tests for Koha::AuthorisedValueCategories::find_by_koha_field
---
 Koha/AuthorisedValueCategories.pm               |  6 +--
 Koha/Template/Plugin/AuthorisedValues.pm        |  2 +-
 catalogue/item-export.pl                        |  8 +---
 catalogue/itemsearch.pl                         |  2 +-
 t/db_dependent/Koha/AuthorisedValueCategories.t | 59 +++++++++++++++++++++++++
 5 files changed, 65 insertions(+), 12 deletions(-)
 create mode 100755 t/db_dependent/Koha/AuthorisedValueCategories.t

diff --git a/Koha/AuthorisedValueCategories.pm b/Koha/AuthorisedValueCategories.pm
index 5702893aea..767cb92408 100644
--- a/Koha/AuthorisedValueCategories.pm
+++ b/Koha/AuthorisedValueCategories.pm
@@ -36,15 +36,15 @@ Koha::AuthorisedValueCategories - Koha AuthorisedValueCategory Object set class
 
 =cut
 
-=head3 findByKohaField
+=head3 find_by_koha_field
 
-    my $category = Koha::AuthorisedValueCategories->findByKohaField($kohafield, [$frameworkcode]);
+    my $category = Koha::AuthorisedValueCategories->find_by_koha_field($kohafield, [$frameworkcode]);
 
 Returns the authorised value category linked to the given koha field
 
 =cut
 
-sub findByKohaField {
+sub find_by_koha_field {
     my ($class, $kohafield, $frameworkcode) = @_;
 
     $frameworkcode //= '';
diff --git a/Koha/Template/Plugin/AuthorisedValues.pm b/Koha/Template/Plugin/AuthorisedValues.pm
index 280b4eb21e..603f5a90f0 100644
--- a/Koha/Template/Plugin/AuthorisedValues.pm
+++ b/Koha/Template/Plugin/AuthorisedValues.pm
@@ -72,7 +72,7 @@ sub GetCategories {
 sub GetCategoryByKohaField {
     my ($self, $kohafield, $frameworkcode) = @_;
 
-    my $category = Koha::AuthorisedValueCategories->findByKohaField($kohafield, $frameworkcode);
+    my $category = Koha::AuthorisedValueCategories->find_by_koha_field($kohafield, $frameworkcode);
 
     return $category ? $category->category_name : undef;
 }
diff --git a/catalogue/item-export.pl b/catalogue/item-export.pl
index 564cd0be93..ef6aaa4b9f 100755
--- a/catalogue/item-export.pl
+++ b/catalogue/item-export.pl
@@ -37,13 +37,7 @@ my ($template, $borrowernumber, $cookie) = get_template_and_user({
 my @itemnumbers = $cgi->multi_param('itemnumber');
 my $format = $cgi->param('format') // 'csv';
 
-my @items;
-foreach my $itemnumber (@itemnumbers) {
-    my $item = Koha::Items->find($itemnumber);
-    if ($item) {
-        push @items, $item;
-    }
-}
+my @items = Koha::Items->search({ itemnumber => { -in => \@itemnumbers } });
 
 if ($format eq 'barcodes') {
     print $cgi->header({
diff --git a/catalogue/itemsearch.pl b/catalogue/itemsearch.pl
index 7ca7013a60..bec0ff6b20 100755
--- a/catalogue/itemsearch.pl
+++ b/catalogue/itemsearch.pl
@@ -247,7 +247,7 @@ foreach my $itemtype ( Koha::ItemTypes->search ) {
     };
 }
 
-my $ccode_avcategory = Koha::AuthorisedValueCategories->findByKohaField('items.ccode');
+my $ccode_avcategory = Koha::AuthorisedValueCategories->find_by_koha_field('items.ccode');
 my $ccode_avcode = $ccode_avcategory ? $ccode_avcategory->category_name : 'CCODE';
 my $ccodes = GetAuthorisedValues($ccode_avcode);
 my @ccodes;
diff --git a/t/db_dependent/Koha/AuthorisedValueCategories.t b/t/db_dependent/Koha/AuthorisedValueCategories.t
new file mode 100755
index 0000000000..99735ac24c
--- /dev/null
+++ b/t/db_dependent/Koha/AuthorisedValueCategories.t
@@ -0,0 +1,59 @@
+#!/usr/bin/perl
+
+# Copyright 2017 BibLibre
+#
+# 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::MarcSubfieldStructures;
+use Koha::Database;
+
+use t::lib::TestBuilder;
+
+BEGIN {
+    use_ok('Koha::AuthorisedValueCategories');
+}
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+
+my $category = Koha::AuthorisedValueCategories->find_or_create({
+    category_name => 'TEST',
+});
+
+Koha::MarcSubfieldStructures->search({
+    frameworkcode => '',
+    kohafield => 'items.notforloan',
+})->delete();
+
+my $subfield = Koha::MarcSubfieldStructures->find_or_create({
+    frameworkcode => '',
+    tagfield => 999,
+    tagsubfield => 9,
+});
+$subfield->authorised_value($category->category_name)
+    ->kohafield('items.notforloan')
+    ->store();
+
+my $result = Koha::AuthorisedValueCategories->find_by_koha_field('items.notforloan');
+
+isa_ok($result, 'Koha::AuthorisedValueCategory', 'Result found');
+is($result->category_name, $category->category_name, 'Result is correct');
+
+$schema->storage->txn_rollback;
-- 
2.11.0