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' );
189 is
( exists( $results[0]->{ onsite_checkout
} ), 1,
190 'GetItemsInfo returns a onsite_checkout key' );
195 subtest
q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
200 $dbh->{AutoCommit
} = 0;
201 $dbh->{RaiseError
} = 1;
203 my $schema = Koha
::Database
->new()->schema();
206 $schema->resultset('Biblio')->create(
208 title
=> "Test title",
211 itemtype
=> 'BIB_LEVEL',
212 items
=> [ { itype
=> "ITEM_LEVEL" } ]
218 my $biblioitem = $biblio->biblioitem();
219 my ( $item ) = $biblioitem->items();
221 C4
::Context
->set_preference( 'item-level_itypes', 0 );
222 ok
( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
224 C4
::Context
->set_preference( 'item-level_itypes', 1 );
225 ok
( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
230 subtest
'SearchItems test' => sub {
234 $dbh->{AutoCommit
} = 0;
235 $dbh->{RaiseError
} = 1;
237 C4
::Context
->set_preference('marcflavour', 'MARC21');
238 my ($biblionumber) = get_biblio
();
240 # Add branches if they don't exist
241 if (not defined GetBranchDetail
('CPL')) {
242 ModBranch
({add
=> 1, branchcode
=> 'CPL', branchname
=> 'Centerville'});
244 if (not defined GetBranchDetail
('MPL')) {
245 ModBranch
({add
=> 1, branchcode
=> 'MPL', branchname
=> 'Midway'});
248 my (undef, $initial_items_count) = SearchItems
(undef, {rows
=> 1});
251 my (undef, undef, $item1_itemnumber) = AddItem
({
253 holdingbranch
=> 'CPL',
255 my (undef, undef, $item2_itemnumber) = AddItem
({
257 holdingbranch
=> 'MPL',
260 my ($items, $total_results);
262 ($items, $total_results) = SearchItems
();
263 is
($total_results, $initial_items_count + 2, "Created 2 new items");
264 is
(scalar @
$items, $total_results, "SearchItems() returns all items");
266 ($items, $total_results) = SearchItems
(undef, {rows
=> 1});
267 is
($total_results, $initial_items_count + 2);
268 is
(scalar @
$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
270 # Search all items where homebranch = 'CPL'
272 field
=> 'homebranch',
276 ($items, $total_results) = SearchItems
($filter);
277 ok
($total_results > 0, "There is at least one CPL item");
278 my $all_items_are_CPL = 1;
279 foreach my $item (@
$items) {
280 if ($item->{homebranch
} ne 'CPL') {
281 $all_items_are_CPL = 0;
285 ok
($all_items_are_CPL, "All items returned by SearchItems are from CPL");
287 # Search all items where homebranch != 'CPL'
289 field
=> 'homebranch',
293 ($items, $total_results) = SearchItems
($filter);
294 ok
($total_results > 0, "There is at least one non-CPL item");
295 my $all_items_are_not_CPL = 1;
296 foreach my $item (@
$items) {
297 if ($item->{homebranch
} eq 'CPL') {
298 $all_items_are_not_CPL = 0;
302 ok
($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
304 # Search all items where biblio title (245$a) is like 'Silence in the %'
306 field
=> 'marc:245$a',
307 query
=> 'Silence in the %',
310 ($items, $total_results) = SearchItems
($filter);
311 ok
($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
313 # Search all items where biblio title is 'Silence in the library'
314 # and homebranch is 'CPL'
316 conjunction
=> 'AND',
319 field
=> 'marc:245$a',
320 query
=> 'Silence in the %',
324 field
=> 'homebranch',
330 ($items, $total_results) = SearchItems
($filter);
332 foreach my $item (@
$items) {
333 if ($item->{itemnumber
} == $item1_itemnumber) {
338 ok
($found, "item1 found");
340 my ($itemfield) = GetMarcFromKohaField
('items.itemnumber', '');
342 # Create item subfield 'z' without link
343 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
344 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
347 $C4::Context
::context
->{marcfromkohafield
} = undef;
348 $C4::Biblio
::inverted_field_map
= undef;
350 my $item3_record = new MARC
::Record
;
351 $item3_record->append_fields(
352 new MARC
::Field
($itemfield, '', '', 'z' => 'foobar')
354 my (undef, undef, $item3_itemnumber) = AddItemFromMarc
($item3_record,
357 # Search item where item subfield z is "foobar"
359 field
=> 'marc:' . $itemfield . '$z',
363 ($items, $total_results) = SearchItems
($filter);
364 ok
(scalar @
$items == 1, 'found 1 item with $z = "foobar"');
366 # Link $z to items.itemnotes (and make sure there is no other subfields
368 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
369 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
372 $C4::Context
::context
->{marcfromkohafield
} = undef;
373 $C4::Biblio
::inverted_field_map
= undef;
375 ModItemFromMarc
($item3_record, $biblionumber, $item3_itemnumber);
377 # Make sure the link is used
378 my $item3 = GetItem
($item3_itemnumber);
379 ok
($item3->{itemnotes
} eq 'foobar', 'itemnotes eq "foobar"');
381 # Do the same search again.
382 # This time it will search in items.itemnotes
383 ($items, $total_results) = SearchItems
($filter);
384 ok
(scalar @
$items == 1, 'found 1 item with itemnotes = "foobar"');
389 # Helper method to set up a Biblio.
391 my $bib = MARC
::Record
->new();
393 MARC
::Field
->new('100', ' ', ' ', a
=> 'Moffat, Steven'),
394 MARC
::Field
->new('245', ' ', ' ', a
=> 'Silence in the library'),
396 my ($bibnum, $bibitemnum) = AddBiblio
($bib, '');
397 return ($bibnum, $bibitemnum);