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
26 use Test::More tests => 6;
32 my $dbh = C4::Context->dbh;
34 subtest 'General Add, Get and Del tests' => sub {
39 $dbh->{AutoCommit} = 0;
40 $dbh->{RaiseError} = 1;
42 # Create a biblio instance for testing
43 C4::Context->set_preference('marcflavour', 'MARC21');
44 my ($bibnum, $bibitemnum) = get_biblio();
47 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
48 cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
49 cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
52 my $getitem = GetItem($itemnumber);
53 cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
54 cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
56 # Modify item; setting barcode.
57 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
58 my $moditem = GetItem($itemnumber);
59 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
62 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
63 my $getdeleted = GetItem($itemnumber);
64 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
69 subtest 'GetHiddenItemnumbers tests' => sub {
73 # This sub is controlled by the OpacHiddenItems system preference.
76 $dbh->{AutoCommit} = 0;
77 $dbh->{RaiseError} = 1;
80 C4::Context->set_preference('marcflavour', 'MARC21');
81 my ($biblionumber, $biblioitemnumber) = get_biblio();
83 # Add branches if they don't exist
84 if (not defined GetBranchDetail('CPL')) {
85 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
87 if (not defined GetBranchDetail('MPL')) {
88 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
92 my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
93 { homebranch => 'CPL',
94 holdingbranch => 'CPL',
98 my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
99 { homebranch => 'MPL',
100 holdingbranch => 'MPL',
106 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
109 push @items, GetItem( $item1_itemnumber );
110 push @items, GetItem( $item2_itemnumber );
112 # Empty OpacHiddenItems
113 C4::Context->set_preference('OpacHiddenItems','');
114 ok( !defined( GetHiddenItemnumbers( @items ) ),
115 "Hidden items list undef if OpacHiddenItems empty");
118 C4::Context->set_preference('OpacHiddenItems',' ');
119 ok( scalar GetHiddenItemnumbers( @items ) == 0,
120 "Hidden items list empty if OpacHiddenItems only contains blanks");
122 # One variable / value
125 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
126 @hidden = GetHiddenItemnumbers( @items );
127 ok( scalar @hidden == 1, "Only one hidden item");
128 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
130 # One variable, two values
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 withdrawn=0 hidden");
138 # Two variables, a value each
143 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
144 @hidden = GetHiddenItemnumbers( @items );
145 ok( scalar @hidden == 2, "Two items hidden");
146 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
148 # Valid OpacHiddenItems, empty list
150 @hidden = GetHiddenItemnumbers( @items );
151 ok( scalar @hidden == 0, "Empty items list, no item hidden");
156 subtest 'GetItemsInfo tests' => sub {
161 $dbh->{AutoCommit} = 0;
162 $dbh->{RaiseError} = 1;
164 my $homebranch = 'CPL';
165 my $holdingbranch = 'MPL';
168 my $biblionumber = get_biblio();
170 my ($item_bibnum, $item_bibitemnum, $itemnumber)
172 homebranch => $homebranch,
173 holdingbranch => $holdingbranch
176 my $branch = GetBranchDetail( $homebranch );
177 $branch->{ opac_info } = "homebranch OPAC info";
180 $branch = GetBranchDetail( $holdingbranch );
181 $branch->{ opac_info } = "holdingbranch OPAC info";
184 my @results = GetItemsInfo( $biblionumber );
185 ok( @results, 'GetItemsInfo returns results');
186 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
187 'GetItemsInfo returns the correct home branch OPAC info notice' );
188 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
189 'GetItemsInfo returns the correct holding branch OPAC info notice' );
194 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
199 $dbh->{AutoCommit} = 0;
200 $dbh->{RaiseError} = 1;
202 my $schema = Koha::Database->new()->schema();
205 $schema->resultset('Biblio')->create(
207 title => "Test title",
210 itemtype => 'BIB_LEVEL',
211 items => [ { itype => "ITEM_LEVEL" } ]
217 my $biblioitem = $biblio->biblioitem();
218 my ( $item ) = $biblioitem->items();
220 C4::Context->set_preference( 'item-level_itypes', 0 );
221 ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
223 C4::Context->set_preference( 'item-level_itypes', 1 );
224 ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is disabled' );
229 subtest 'SearchItems test' => sub {
233 $dbh->{AutoCommit} = 0;
234 $dbh->{RaiseError} = 1;
236 C4::Context->set_preference('marcflavour', 'MARC21');
237 my ($biblionumber) = get_biblio();
239 # Add branches if they don't exist
240 if (not defined GetBranchDetail('CPL')) {
241 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
243 if (not defined GetBranchDetail('MPL')) {
244 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
247 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
250 my (undef, undef, $item1_itemnumber) = AddItem({
252 holdingbranch => 'CPL',
254 my (undef, undef, $item2_itemnumber) = AddItem({
256 holdingbranch => 'MPL',
259 my ($items, $total_results);
261 ($items, $total_results) = SearchItems();
262 is($total_results, $initial_items_count + 2, "Created 2 new items");
263 is(scalar @$items, $total_results, "SearchItems() returns all items");
265 ($items, $total_results) = SearchItems(undef, {rows => 1});
266 is($total_results, $initial_items_count + 2);
267 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
269 # Search all items where homebranch = 'CPL'
271 field => 'homebranch',
275 ($items, $total_results) = SearchItems($filter);
276 ok($total_results > 0, "There is at least one CPL item");
277 my $all_items_are_CPL = 1;
278 foreach my $item (@$items) {
279 if ($item->{homebranch} ne 'CPL') {
280 $all_items_are_CPL = 0;
284 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
286 # Search all items where homebranch != 'CPL'
288 field => 'homebranch',
292 ($items, $total_results) = SearchItems($filter);
293 ok($total_results > 0, "There is at least one non-CPL item");
294 my $all_items_are_not_CPL = 1;
295 foreach my $item (@$items) {
296 if ($item->{homebranch} eq 'CPL') {
297 $all_items_are_not_CPL = 0;
301 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
303 # Search all items where biblio title (245$a) is like 'Silence in the %'
305 field => 'marc:245$a',
306 query => 'Silence in the %',
309 ($items, $total_results) = SearchItems($filter);
310 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
312 # Search all items where biblio title is 'Silence in the library'
313 # and homebranch is 'CPL'
315 conjunction => 'AND',
318 field => 'marc:245$a',
319 query => 'Silence in the %',
323 field => 'homebranch',
329 ($items, $total_results) = SearchItems($filter);
331 foreach my $item (@$items) {
332 if ($item->{itemnumber} == $item1_itemnumber) {
337 ok($found, "item1 found");
342 # Helper method to set up a Biblio.
344 my $bib = MARC::Record->new();
346 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
347 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
349 my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
350 return ($bibnum, $bibitemnum);