Bug 17627: Move C4::Koha::GetItemTypesByCategory to Koha::ItemTypes
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 15 Nov 2016 10:17:22 +0000 (10:17 +0000)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 17 Feb 2017 15:31:32 +0000 (15:31 +0000)
C4::Koha::GetItemTypesByCategory can be easily replaced with
Koha::ItemTypes->search({ searchcategory => ? });

So let's replace it where it is used.

Test plan:
Make sure this patch does not break the test plan of bug 10937

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
C4/Koha.pm
opac/opac-search.pl
t/db_dependent/Koha.t

index b6e494a..2884267 100644 (file)
@@ -42,7 +42,7 @@ BEGIN {
        @EXPORT = qw(
         &GetPrinters &GetPrinter
         &GetItemTypes &getitemtypeinfo
-                &GetItemTypesCategorized &GetItemTypesByCategory
+        &GetItemTypesCategorized
         &getallthemes
         &getFacets
         &getnbpages
@@ -207,24 +207,6 @@ sub GetItemTypesCategorized {
 return ($dbh->selectall_hashref($query,'itemtype'));
 }
 
-=head2 GetItemTypesByCategory
-
-    @results = GetItemTypesByCategory( $searchcategory );
-
-Returns the itemtype code of all itemtypes included in a searchcategory.
-
-=cut
-
-sub GetItemTypesByCategory {
-    my ($category) = @_;
-    my $count = 0;
-    my @results;
-    my $dbh = C4::Context->dbh;
-    my $query = qq|SELECT itemtype FROM itemtypes WHERE searchcategory=?|;
-    my $tmp=$dbh->selectcol_arrayref($query,undef,$category);
-    return @$tmp;
-}
-
 =head2 getitemtypeinfo
 
   $itemtype = &getitemtypeinfo($itemtype, [$interface]);
index 15e24b3..a84e9d3 100755 (executable)
@@ -479,7 +479,8 @@ my %is_nolimit = map { $_ => 1 } @nolimits;
 if (@searchCategories > 0) {
     my @tabcat;
     foreach my $typecategory (@searchCategories) {
-        push (@tabcat, GetItemTypesByCategory($typecategory));
+        my @itemtypes = Koha::ItemTypes->search({ searchcategory => $typecategory });
+        push @tabcat, $_->itemtype for @itemtypes;
     }
 
     foreach my $itemtypeInCategory (@tabcat) {
index 3d5e203..feb4a1c 100644 (file)
@@ -13,7 +13,7 @@ use Test::More tests => 8;
 use DateTime::Format::MySQL;
 
 BEGIN {
-    use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesByCategory GetItemTypesCategorized));
+    use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesCategorized));
     use_ok('C4::Members');
 }
 
@@ -239,7 +239,7 @@ subtest 'ISBN tests' => sub {
 
 };
 
-subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
+subtest 'GetItemTypesCategorized test' => sub{
     plan tests => 7;
 
     my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
@@ -259,12 +259,13 @@ subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
     $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
 
     # Azertyware should not exist.
-    my @results = GetItemTypesByCategory('Azertyware');
-    is(scalar @results, 0, 'GetItemTypesByCategory: Invalid category returns nothing');
+    my @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Azertyware' });
+    is( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
 
-    @results = GetItemTypesByCategory('Qwertyware');
+    @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Qwertyware' });
+    my @got = map { $_->itemtype } @itemtypes;
     my @expected = ( 'BKghjklo2', 'BKghjklo3' );
-    is_deeply(\@results,\@expected,'GetItemTypesByCategory: valid category returns itemtypes');
+    is_deeply(\@got,\@expected,'Search item types by searchcategory: valid category returns itemtypes');
 
     # add more data since GetItemTypesCategorized's search is more subtle
     $insertGroup = Koha::AuthorisedValue->new(
@@ -285,7 +286,7 @@ subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
     ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
 
     my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
-    @results = ();
+    my @results = ();
     foreach my $key (@only) {
         push @results, $key if exists $hrCat->{$key};
     }