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;
33 my $branches = GetBranches;
34 my ($branch1, $branch2) = keys %$branches;
36 subtest 'General Add, Get and Del tests' => sub {
41 $dbh->{AutoCommit} = 0;
42 $dbh->{RaiseError} = 1;
44 # Create a biblio instance for testing
45 C4::Context->set_preference('marcflavour', 'MARC21');
46 my ($bibnum, $bibitemnum) = get_biblio();
49 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch1 } , $bibnum);
50 cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
51 cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
54 my $getitem = GetItem($itemnumber);
55 cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
56 cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
58 # Modify item; setting barcode.
59 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
60 my $moditem = GetItem($itemnumber);
61 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
64 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
65 my $getdeleted = GetItem($itemnumber);
66 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
71 subtest 'GetHiddenItemnumbers tests' => sub {
75 # This sub is controlled by the OpacHiddenItems system preference.
78 $dbh->{AutoCommit} = 0;
79 $dbh->{RaiseError} = 1;
82 C4::Context->set_preference('marcflavour', 'MARC21');
83 my ($biblionumber, $biblioitemnumber) = get_biblio();
85 # Add branches if they don't exist
86 if (not defined GetBranchDetail('CPL')) {
87 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
89 if (not defined GetBranchDetail('MPL')) {
90 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
94 my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
95 { homebranch => $branch1,
96 holdingbranch => $branch1,
100 my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
101 { homebranch => $branch2,
102 holdingbranch => $branch2,
108 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
111 push @items, GetItem( $item1_itemnumber );
112 push @items, GetItem( $item2_itemnumber );
114 # Empty OpacHiddenItems
115 C4::Context->set_preference('OpacHiddenItems','');
116 ok( !defined( GetHiddenItemnumbers( @items ) ),
117 "Hidden items list undef if OpacHiddenItems empty");
120 C4::Context->set_preference('OpacHiddenItems',' ');
121 ok( scalar GetHiddenItemnumbers( @items ) == 0,
122 "Hidden items list empty if OpacHiddenItems only contains blanks");
124 # One variable / value
127 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
128 @hidden = GetHiddenItemnumbers( @items );
129 ok( scalar @hidden == 1, "Only one hidden item");
130 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
132 # One variable, two values
135 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
136 @hidden = GetHiddenItemnumbers( @items );
137 ok( scalar @hidden == 2, "Two items hidden");
138 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
140 # Two variables, a value each
143 homebranch: [$branch2]
145 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
146 @hidden = GetHiddenItemnumbers( @items );
147 ok( scalar @hidden == 2, "Two items hidden");
148 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
150 # Valid OpacHiddenItems, empty list
152 @hidden = GetHiddenItemnumbers( @items );
153 ok( scalar @hidden == 0, "Empty items list, no item hidden");
158 subtest 'GetItemsInfo tests' => sub {
163 $dbh->{AutoCommit} = 0;
164 $dbh->{RaiseError} = 1;
167 my ($biblionumber, $biblioitemnumber) = get_biblio();
169 my ($item_bibnum, $item_bibitemnum, $itemnumber)
171 homebranch => $branch1,
172 holdingbranch => $branch2
175 my $branch = GetBranchDetail( $branch1 );
176 $branch->{ opac_info } = "homebranch OPAC info";
179 $branch = GetBranchDetail( $branch2 );
180 $branch->{ opac_info } = "holdingbranch OPAC info";
183 my @results = GetItemsInfo( $biblionumber );
184 ok( @results, 'GetItemsInfo returns results');
185 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
186 'GetItemsInfo returns the correct home branch OPAC info notice' );
187 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
188 'GetItemsInfo returns the correct holding branch OPAC info notice' );
193 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
198 $dbh->{AutoCommit} = 0;
199 $dbh->{RaiseError} = 1;
201 my $schema = Koha::Database->new()->schema();
204 $schema->resultset('Biblio')->create(
206 title => "Test title",
209 itemtype => 'BIB_LEVEL',
210 items => [ { itype => "ITEM_LEVEL" } ]
216 my $biblioitem = $biblio->biblioitem();
217 my ( $item ) = $biblioitem->items();
219 C4::Context->set_preference( 'item-level_itypes', 0 );
220 ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
222 C4::Context->set_preference( 'item-level_itypes', 1 );
223 ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
228 subtest 'SearchItems test' => sub {
232 $dbh->{AutoCommit} = 0;
233 $dbh->{RaiseError} = 1;
235 C4::Context->set_preference('marcflavour', 'MARC21');
236 my ($biblionumber) = get_biblio();
238 # Add branches if they don't exist
239 if (not defined GetBranchDetail('CPL')) {
240 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
242 if (not defined GetBranchDetail('MPL')) {
243 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
246 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
249 my (undef, undef, $item1_itemnumber) = AddItem({
251 holdingbranch => 'CPL',
253 my (undef, undef, $item2_itemnumber) = AddItem({
255 holdingbranch => 'MPL',
258 my ($items, $total_results);
260 ($items, $total_results) = SearchItems();
261 is($total_results, $initial_items_count + 2, "Created 2 new items");
262 is(scalar @$items, $total_results, "SearchItems() returns all items");
264 ($items, $total_results) = SearchItems(undef, {rows => 1});
265 is($total_results, $initial_items_count + 2);
266 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
268 # Search all items where homebranch = 'CPL'
270 field => 'homebranch',
274 ($items, $total_results) = SearchItems($filter);
275 ok($total_results > 0, "There is at least one CPL item");
276 my $all_items_are_CPL = 1;
277 foreach my $item (@$items) {
278 if ($item->{homebranch} ne 'CPL') {
279 $all_items_are_CPL = 0;
283 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
285 # Search all items where homebranch != 'CPL'
287 field => 'homebranch',
291 ($items, $total_results) = SearchItems($filter);
292 ok($total_results > 0, "There is at least one non-CPL item");
293 my $all_items_are_not_CPL = 1;
294 foreach my $item (@$items) {
295 if ($item->{homebranch} eq 'CPL') {
296 $all_items_are_not_CPL = 0;
300 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
302 # Search all items where biblio title (245$a) is like 'Silence in the %'
304 field => 'marc:245$a',
305 query => 'Silence in the %',
308 ($items, $total_results) = SearchItems($filter);
309 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
311 # Search all items where biblio title is 'Silence in the library'
312 # and homebranch is 'CPL'
314 conjunction => 'AND',
317 field => 'marc:245$a',
318 query => 'Silence in the %',
322 field => 'homebranch',
328 ($items, $total_results) = SearchItems($filter);
330 foreach my $item (@$items) {
331 if ($item->{itemnumber} == $item1_itemnumber) {
336 ok($found, "item1 found");
341 # Helper method to set up a Biblio.
343 my $bib = MARC::Record->new();
345 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
346 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
348 my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
349 return ($bibnum, $bibitemnum);