From b78dd5aeaa31092747f9e7142cf61e096b37609d Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 16 Sep 2013 15:29:04 -0300 Subject: [PATCH] Bug 10872: UT for GetHiddenItemnumbers and POD fix This patch adds several unit tests for GetHiddenItemnumbers and fixes the POD for it. It also wraps the tests for rollback, modernizes and adds a license text to it. Sponsored-by: Universidad Nacional de Cordoba Signed-off-by: Galen Charlton (cherry picked from commit 5d8ad739293ef8cf22a808404e52a9c74275aaf0) Signed-off-by: Tomas Cohen Arazi "Fixed" 'wthdrawn' column name for 3.12.x --- C4/Items.pm | 8 +-- t/db_dependent/Items.t | 158 +++++++++++++++++++++++++++++++++-------- 2 files changed, 133 insertions(+), 33 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 5c3f23066e..e89bc574a5 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -1617,11 +1617,11 @@ sub GetBarcodeFromItemnumber { =head2 GetHiddenItemnumbers -=over 4 - -$result = GetHiddenItemnumbers(@items); + my @itemnumbers_to_hide = GetHiddenItemnumbers(@items); -=back +Given a list of items it checks which should be hidden from the OPAC given +the current configuration. Returns a list of itemnumbers corresponding to +those that should be hidden. =cut diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index eff524972e..8d8777ac02 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -1,47 +1,146 @@ #!/usr/bin/perl # -# This Koha test module is a stub! -# Add more tests here!!! +# 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 2 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +# + +use Modern::Perl; -use strict; -use warnings; use MARC::Record; use C4::Biblio; -use Test::More tests => 7; +use Test::More tests => 3; BEGIN { use_ok('C4::Items'); } -# Helper biblio. -diag("Creating biblio instance for testing."); -my ($bibnum, $bibitemnum) = get_biblio(); +my $dbh = C4::Context->dbh; -# Add an item. -my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum); -cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber."); -cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber."); +subtest 'General Add, Get and Del tests' => sub { -# Get item. -my $getitem = GetItem($itemnumber); -cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber."); -cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber."); + plan tests => 6; -# Modify item; setting barcode. -ModItem({ barcode => '987654321' }, $bibnum, $itemnumber); -my $moditem = GetItem($itemnumber); -cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.'); + # Start transaction + $dbh->{AutoCommit} = 0; + $dbh->{RaiseError} = 1; -# Delete item. -my $dbh = C4::Context->dbh; -DelItem($dbh, $bibnum, $itemnumber); -my $getdeleted = GetItem($itemnumber); -is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected."); + # Helper biblio. + diag("Creating biblio instance for testing."); + my ($bibnum, $bibitemnum) = get_biblio(); + + # Add an item. + my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum); + cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber."); + cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber."); + + # Get item. + my $getitem = GetItem($itemnumber); + cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber."); + cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber."); + + # Modify item; setting barcode. + ModItem({ barcode => '987654321' }, $bibnum, $itemnumber); + my $moditem = GetItem($itemnumber); + cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.'); + + # Delete item. + DelItem($dbh, $bibnum, $itemnumber); + my $getdeleted = GetItem($itemnumber); + is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected."); + + $dbh->rollback; +}; + +subtest 'GetHiddenItemnumbers tests' => sub { + + plan tests => 9; + + # This sub is controlled by the OpacHiddenItems system preference. + + # Start transaction + $dbh->{AutoCommit} = 0; + $dbh->{RaiseError} = 1; + + # Create a new biblio + my ($biblionumber, $biblioitemnumber) = get_biblio(); + + # Add two items + my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem( + { homebranch => 'CPL', + holdingbranch => 'CPL', + wthdrawn => 1 }, + $biblionumber + ); + my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem( + { homebranch => 'MPL', + holdingbranch => 'MPL', + wthdrawn => 0 }, + $biblionumber + ); + + my $opachiddenitems; + my @itemnumbers = ($item1_itemnumber,$item2_itemnumber); + my @hidden; + my @items; + push @items, GetItem( $item1_itemnumber ); + push @items, GetItem( $item2_itemnumber ); + + # Empty OpacHiddenItems + C4::Context->set_preference('OpacHiddenItems',''); + ok( !defined( GetHiddenItemnumbers( @items ) ), + "Hidden items list undef if OpacHiddenItems empty"); + + # Blank spaces + C4::Context->set_preference('OpacHiddenItems',' '); + ok( scalar GetHiddenItemnumbers( @items ) == 0, + "Hidden items list empty if OpacHiddenItems only contains blanks"); + + # One variable / value + $opachiddenitems = " + wthdrawn: [1]"; + C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); + @hidden = GetHiddenItemnumbers( @items ); + ok( scalar @hidden == 1, "Only one hidden item"); + is( $hidden[0], $item1_itemnumber, "wthdrawn=1 is hidden"); + + # One variable, two values + $opachiddenitems = " + wthdrawn: [1,0]"; + C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); + @hidden = GetHiddenItemnumbers( @items ); + ok( scalar @hidden == 2, "Two items hidden"); + is_deeply( \@hidden, \@itemnumbers, "wthdrawn=1 and wthdrawn=0 hidden"); + + # Two variables, a value each + $opachiddenitems = " + wthdrawn: [1] + homebranch: [MPL] + "; + C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems ); + @hidden = GetHiddenItemnumbers( @items ); + ok( scalar @hidden == 2, "Two items hidden"); + is_deeply( \@hidden, \@itemnumbers, "wthdrawn=1 and homebranch=MPL hidden"); + + # Valid OpacHiddenItems, empty list + @items = (); + @hidden = GetHiddenItemnumbers( @items ); + ok( scalar @hidden == 0, "Empty items list, no item hidden"); -# Delete helper Biblio. -diag("Deleting biblio testing instance."); -DelBiblio($bibnum); + $dbh->rollback; +}; # Helper method to set up a Biblio. sub get_biblio { @@ -50,5 +149,6 @@ sub get_biblio { MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), ); - return ($bibnum, $bibitemnum) = AddBiblio($bib, ''); + my ($bibnum, $bibitemnum) = AddBiblio($bib, ''); + return ($bibnum, $bibitemnum); } -- 2.39.5