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 => 8;
30 use_ok('Koha::Items');
33 my $dbh = C4::Context->dbh;
34 my $branches = GetBranches;
35 my ($branch1, $branch2) = keys %$branches;
37 subtest 'General Add, Get and Del tests' => sub {
42 $dbh->{AutoCommit} = 0;
43 $dbh->{RaiseError} = 1;
45 # Create a biblio instance for testing
46 C4::Context->set_preference('marcflavour', 'MARC21');
47 my ($bibnum, $bibitemnum) = get_biblio();
50 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch1 } , $bibnum);
51 cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
52 cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
55 my $getitem = GetItem($itemnumber);
56 cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
57 cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
59 # Modify item; setting barcode.
60 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
61 my $moditem = GetItem($itemnumber);
62 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
65 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
66 my $getdeleted = GetItem($itemnumber);
67 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
72 subtest 'GetHiddenItemnumbers tests' => sub {
76 # This sub is controlled by the OpacHiddenItems system preference.
79 $dbh->{AutoCommit} = 0;
80 $dbh->{RaiseError} = 1;
83 C4::Context->set_preference('marcflavour', 'MARC21');
84 my ($biblionumber, $biblioitemnumber) = get_biblio();
86 # Add branches if they don't exist
87 if (not defined GetBranchDetail('CPL')) {
88 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
90 if (not defined GetBranchDetail('MPL')) {
91 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
95 my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
96 { homebranch => $branch1,
97 holdingbranch => $branch1,
101 my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
102 { homebranch => $branch2,
103 holdingbranch => $branch2,
109 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
112 push @items, GetItem( $item1_itemnumber );
113 push @items, GetItem( $item2_itemnumber );
115 # Empty OpacHiddenItems
116 C4::Context->set_preference('OpacHiddenItems','');
117 ok( !defined( GetHiddenItemnumbers( @items ) ),
118 "Hidden items list undef if OpacHiddenItems empty");
121 C4::Context->set_preference('OpacHiddenItems',' ');
122 ok( scalar GetHiddenItemnumbers( @items ) == 0,
123 "Hidden items list empty if OpacHiddenItems only contains blanks");
125 # One variable / value
128 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
129 @hidden = GetHiddenItemnumbers( @items );
130 ok( scalar @hidden == 1, "Only one hidden item");
131 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
133 # One variable, two values
136 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
137 @hidden = GetHiddenItemnumbers( @items );
138 ok( scalar @hidden == 2, "Two items hidden");
139 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
141 # Two variables, a value each
144 homebranch: [$branch2]
146 C4::Context->set_preference( 'OpacHiddenItems', $opachiddenitems );
147 @hidden = GetHiddenItemnumbers( @items );
148 ok( scalar @hidden == 2, "Two items hidden");
149 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch=MPL hidden");
151 # Valid OpacHiddenItems, empty list
153 @hidden = GetHiddenItemnumbers( @items );
154 ok( scalar @hidden == 0, "Empty items list, no item hidden");
159 subtest 'GetItemsInfo tests' => sub {
164 $dbh->{AutoCommit} = 0;
165 $dbh->{RaiseError} = 1;
168 my ($biblionumber, $biblioitemnumber) = get_biblio();
170 my ($item_bibnum, $item_bibitemnum, $itemnumber)
172 homebranch => $branch1,
173 holdingbranch => $branch2
176 my $branch = GetBranchDetail( $branch1 );
177 $branch->{ opac_info } = "homebranch OPAC info";
180 $branch = GetBranchDetail( $branch2 );
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' );
190 is( exists( $results[0]->{ onsite_checkout } ), 1,
191 'GetItemsInfo returns a onsite_checkout key' );
196 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
201 $dbh->{AutoCommit} = 0;
202 $dbh->{RaiseError} = 1;
204 my $schema = Koha::Database->new()->schema();
207 $schema->resultset('Biblio')->create(
209 title => "Test title",
212 itemtype => 'BIB_LEVEL',
213 items => [ { itype => "ITEM_LEVEL" } ]
219 my @bi = $biblio->biblioitems();
220 my ( $item ) = $bi[0]->items();
222 C4::Context->set_preference( 'item-level_itypes', 0 );
223 ok( $item->effective_itemtype() eq 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
225 C4::Context->set_preference( 'item-level_itypes', 1 );
226 ok( $item->effective_itemtype() eq 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
231 subtest 'SearchItems test' => sub {
235 $dbh->{AutoCommit} = 0;
236 $dbh->{RaiseError} = 1;
238 C4::Context->set_preference('marcflavour', 'MARC21');
239 my ($biblionumber) = get_biblio();
241 # Add branches if they don't exist
242 if (not defined GetBranchDetail('CPL')) {
243 ModBranch({add => 1, branchcode => 'CPL', branchname => 'Centerville'});
245 if (not defined GetBranchDetail('MPL')) {
246 ModBranch({add => 1, branchcode => 'MPL', branchname => 'Midway'});
249 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
252 my (undef, undef, $item1_itemnumber) = AddItem({
254 holdingbranch => 'CPL',
256 my (undef, undef, $item2_itemnumber) = AddItem({
258 holdingbranch => 'MPL',
261 my ($items, $total_results);
263 ($items, $total_results) = SearchItems();
264 is($total_results, $initial_items_count + 2, "Created 2 new items");
265 is(scalar @$items, $total_results, "SearchItems() returns all items");
267 ($items, $total_results) = SearchItems(undef, {rows => 1});
268 is($total_results, $initial_items_count + 2);
269 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
271 # Search all items where homebranch = 'CPL'
273 field => 'homebranch',
277 ($items, $total_results) = SearchItems($filter);
278 ok($total_results > 0, "There is at least one CPL item");
279 my $all_items_are_CPL = 1;
280 foreach my $item (@$items) {
281 if ($item->{homebranch} ne 'CPL') {
282 $all_items_are_CPL = 0;
286 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
288 # Search all items where homebranch != 'CPL'
290 field => 'homebranch',
294 ($items, $total_results) = SearchItems($filter);
295 ok($total_results > 0, "There is at least one non-CPL item");
296 my $all_items_are_not_CPL = 1;
297 foreach my $item (@$items) {
298 if ($item->{homebranch} eq 'CPL') {
299 $all_items_are_not_CPL = 0;
303 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
305 # Search all items where biblio title (245$a) is like 'Silence in the %'
307 field => 'marc:245$a',
308 query => 'Silence in the %',
311 ($items, $total_results) = SearchItems($filter);
312 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
314 # Search all items where biblio title is 'Silence in the library'
315 # and homebranch is 'CPL'
317 conjunction => 'AND',
320 field => 'marc:245$a',
321 query => 'Silence in the %',
325 field => 'homebranch',
331 ($items, $total_results) = SearchItems($filter);
333 foreach my $item (@$items) {
334 if ($item->{itemnumber} == $item1_itemnumber) {
339 ok($found, "item1 found");
341 my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
343 # Create item subfield 'z' without link
344 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
345 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
348 $C4::Context::context->{marcfromkohafield} = undef;
349 $C4::Biblio::inverted_field_map = undef;
351 my $item3_record = new MARC::Record;
352 $item3_record->append_fields(
353 new MARC::Field($itemfield, '', '', 'z' => 'foobar')
355 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
358 # Search item where item subfield z is "foobar"
360 field => 'marc:' . $itemfield . '$z',
364 ($items, $total_results) = SearchItems($filter);
365 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
367 # Link $z to items.itemnotes (and make sure there is no other subfields
369 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
370 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
373 $C4::Context::context->{marcfromkohafield} = undef;
374 $C4::Biblio::inverted_field_map = undef;
376 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
378 # Make sure the link is used
379 my $item3 = GetItem($item3_itemnumber);
380 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
382 # Do the same search again.
383 # This time it will search in items.itemnotes
384 ($items, $total_results) = SearchItems($filter);
385 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
387 my $cpl_items = SearchItemsByField( 'homebranch', 'CPL');
388 is( ( $cpl_items and scalar( @$cpl_items ) ), 1, 'SearchItemsByField should return something' );
393 subtest 'Koha::Item(s) tests' => sub {
398 my $schema = Koha::Database->new()->schema();
399 $schema->storage->txn_begin();
400 $dbh->{RaiseError} = 1;
402 # Create a biblio and item for testing
403 C4::Context->set_preference('marcflavour', 'MARC21');
404 my ($bibnum, $bibitemnum) = get_biblio();
405 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch1, holdingbranch => $branch2 } , $bibnum);
408 my $item = Koha::Items->find( $itemnumber );
409 is( ref($item), 'Koha::Item', "Got Koha::Item" );
411 my $homebranch = $item->home_branch();
412 is( ref($homebranch), 'Koha::Branch', "Got Koha::Branch from home_branch method" );
413 is( $homebranch->branchcode(), $branch1, "Home branch code matches homebranch" );
415 my $holdingbranch = $item->holding_branch();
416 is( ref($holdingbranch), 'Koha::Branch', "Got Koha::Branch from holding_branch method" );
417 is( $holdingbranch->branchcode(), $branch2, "Home branch code matches holdingbranch" );
420 # Helper method to set up a Biblio.
422 my $bib = MARC::Record->new();
424 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
425 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
427 my ($bibnum, $bibitemnum) = AddBiblio($bib, '');
428 return ($bibnum, $bibitemnum);