3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 use Test::More tests => 4;
31 my $dbh = C4::Context->dbh;
33 subtest 'General Add, Get and Del tests' => sub {
38 $dbh->{AutoCommit} = 0;
39 $dbh->{RaiseError} = 1;
42 diag("Creating biblio instance for testing.");
43 my ($bibnum, $bibitemnum) = get_biblio();
46 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
47 cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
48 cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
51 my $getitem = GetItem($itemnumber);
52 cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
53 cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
55 # Modify item; setting barcode.
56 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
57 my $moditem = GetItem($itemnumber);
58 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
61 DelItem($dbh, $bibnum, $itemnumber);
62 my $getdeleted = GetItem($itemnumber);
63 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
68 subtest 'GetHiddenItemnumbers tests' => sub {
72 # This sub is controlled by the OpacHiddenItems system preference.
75 $dbh->{AutoCommit} = 0;
76 $dbh->{RaiseError} = 1;
79 my ($biblionumber, $biblioitemnumber) = get_biblio();
82 my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
83 { homebranch => 'CPL',
84 holdingbranch => 'CPL',
88 my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
89 { homebranch => 'MPL',
90 holdingbranch => 'MPL',
96 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
99 push @items, GetItem( $item1_itemnumber );
100 push @items, GetItem( $item2_itemnumber );
102 # Empty OpacHiddenItems
103 C4::Context->set_preference('OpacHiddenItems','');
104 ok( !defined( GetHiddenItemnumbers( @items ) ),
105 "Hidden items list undef if OpacHiddenItems empty");
108 C4::Context->set_preference('OpacHiddenItems',' ');
109 ok( scalar GetHiddenItemnumbers( @items ) == 0,
110 "Hidden items list empty if OpacHiddenItems only contains blanks");
112 # One variable / value
115 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
116 @hidden = GetHiddenItemnumbers( @items );
117 ok( scalar @hidden == 1, "Only one hidden item");
118 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
120 # One variable, two values
123 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
124 @hidden = GetHiddenItemnumbers( @items );
125 ok( scalar @hidden == 2, "Two items hidden");
126 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
128 # Two variables, a value each
133 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
134 @hidden = GetHiddenItemnumbers( @items );
135 ok( scalar @hidden == 2, "Two items hidden");
136 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
138 # Valid OpacHiddenItems, empty list
140 @hidden = GetHiddenItemnumbers( @items );
141 ok( scalar @hidden == 0, "Empty items list, no item hidden");
146 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
151 $dbh->{AutoCommit} = 0;
152 $dbh->{RaiseError} = 1;
154 my $schema = Koha::Database->new()->schema();
157 $schema->resultset('Biblio')->create(
159 title => "Test title",
162 itemtype => 'BIB_LEVEL',
163 items => [ { itype => "ITEM_LEVEL" } ]
169 my $biblioitem = $biblio->biblioitem();
170 my ( $item ) = $biblioitem->items();
172 $schema->resultset('Systempreference')->update_or_create({ variable => 'item-level_itypes', value => 0 });
173 ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
175 $schema->resultset('Systempreference')->update_or_create({ variable => 'item-level_itypes', value => 1 });
176 ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
182 # Helper method to set up a Biblio.
184 my $bib = MARC::Record->new();
186 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
187 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
189 my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
190 return ($bibnum, $bibitemnum);