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
27 use t::lib::TestBuilder;
29 use Test::More tests => 10;
35 use_ok('Koha::Items');
38 my $schema = Koha::Database->new->schema;
39 my $location = 'My Location';
41 subtest 'General Add, Get and Del tests' => sub {
45 $schema->storage->txn_begin;
47 my $builder = t::lib::TestBuilder->new;
48 my $library = $builder->build({
52 # Create a biblio instance for testing
53 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
54 my ($bibnum, $bibitemnum) = get_biblio();
57 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location } , $bibnum);
58 cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
59 cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
62 my $getitem = GetItem($itemnumber);
63 cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
64 cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibitemnum, "Retrieved item has correct biblioitemnumber.");
65 is( $getitem->{location}, $location, "The location should not have been modified" );
66 is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to the location value" );
68 # Modify item; setting barcode.
69 ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
70 my $moditem = GetItem($itemnumber);
71 cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
74 DelItem({ biblionumber => $bibnum, itemnumber => $itemnumber });
75 my $getdeleted = GetItem($itemnumber);
76 is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
78 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, location => $location, permanent_location => 'my permanent location' } , $bibnum);
79 $getitem = GetItem($itemnumber);
80 is( $getitem->{location}, $location, "The location should not have been modified" );
81 is( $getitem->{permanent_location}, 'my permanent location', "The permanent_location should not have modified" );
83 ModItem({ location => $location }, $bibnum, $itemnumber);
84 $getitem = GetItem($itemnumber);
85 is( $getitem->{location}, $location, "The location should have been set to correct location" );
86 is( $getitem->{permanent_location}, $location, "The permanent_location should have been set to location" );
88 ModItem({ location => 'CART' }, $bibnum, $itemnumber);
89 $getitem = GetItem($itemnumber);
90 is( $getitem->{location}, 'CART', "The location should have been set to CART" );
91 is( $getitem->{permanent_location}, $location, "The permanent_location should not have been set to CART" );
93 $schema->storage->txn_rollback;
96 subtest 'GetHiddenItemnumbers tests' => sub {
100 # This sub is controlled by the OpacHiddenItems system preference.
102 $schema->storage->txn_begin;
104 my $builder = t::lib::TestBuilder->new;
105 my $library1 = $builder->build({
109 my $library2 = $builder->build({
113 # Create a new biblio
114 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
115 my ($biblionumber, $biblioitemnumber) = get_biblio();
118 my ($item1_bibnum, $item1_bibitemnum, $item1_itemnumber) = AddItem(
119 { homebranch => $library1->{branchcode},
120 holdingbranch => $library1->{branchcode},
124 my ($item2_bibnum, $item2_bibitemnum, $item2_itemnumber) = AddItem(
125 { homebranch => $library2->{branchcode},
126 holdingbranch => $library2->{branchcode},
132 my @itemnumbers = ($item1_itemnumber,$item2_itemnumber);
135 push @items, GetItem( $item1_itemnumber );
136 push @items, GetItem( $item2_itemnumber );
138 # Empty OpacHiddenItems
139 t::lib::Mocks::mock_preference('OpacHiddenItems','');
140 ok( !defined( GetHiddenItemnumbers( @items ) ),
141 "Hidden items list undef if OpacHiddenItems empty");
144 t::lib::Mocks::mock_preference('OpacHiddenItems',' ');
145 ok( scalar GetHiddenItemnumbers( @items ) == 0,
146 "Hidden items list empty if OpacHiddenItems only contains blanks");
148 # One variable / value
151 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
152 @hidden = GetHiddenItemnumbers( @items );
153 ok( scalar @hidden == 1, "Only one hidden item");
154 is( $hidden[0], $item1_itemnumber, "withdrawn=1 is hidden");
156 # One variable, two values
159 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
160 @hidden = GetHiddenItemnumbers( @items );
161 ok( scalar @hidden == 2, "Two items hidden");
162 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and withdrawn=0 hidden");
164 # Two variables, a value each
167 homebranch: [$library2->{branchcode}]
169 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
170 @hidden = GetHiddenItemnumbers( @items );
171 ok( scalar @hidden == 2, "Two items hidden");
172 is_deeply( \@hidden, \@itemnumbers, "withdrawn=1 and homebranch library2 hidden");
174 # Valid OpacHiddenItems, empty list
176 @hidden = GetHiddenItemnumbers( @items );
177 ok( scalar @hidden == 0, "Empty items list, no item hidden");
179 $schema->storage->txn_rollback;
182 subtest 'GetItemsInfo tests' => sub {
186 $schema->storage->txn_begin;
188 my $builder = t::lib::TestBuilder->new;
189 my $library1 = $builder->build({
192 my $library2 = $builder->build({
197 my ($biblionumber, $biblioitemnumber) = get_biblio();
199 my ($item_bibnum, $item_bibitemnum, $itemnumber)
201 homebranch => $library1->{branchcode},
202 holdingbranch => $library2->{branchcode},
205 my $library = Koha::Libraries->find( $library1->{branchcode} );
206 $library->opac_info("homebranch OPAC info");
209 $library = Koha::Libraries->find( $library2->{branchcode} );
210 $library->opac_info("holdingbranch OPAC info");
213 my @results = GetItemsInfo( $biblionumber );
214 ok( @results, 'GetItemsInfo returns results');
215 is( $results[0]->{ home_branch_opac_info }, "homebranch OPAC info",
216 'GetItemsInfo returns the correct home branch OPAC info notice' );
217 is( $results[0]->{ holding_branch_opac_info }, "holdingbranch OPAC info",
218 'GetItemsInfo returns the correct holding branch OPAC info notice' );
219 is( exists( $results[0]->{ onsite_checkout } ), 1,
220 'GetItemsInfo returns a onsite_checkout key' );
222 $schema->storage->txn_rollback;
225 subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
229 $schema->storage->txn_begin;
231 my $biblio = $schema->resultset('Biblio')->create({
232 title => "Test title",
233 biblioitems => [ { itemtype => 'BIB_LEVEL' } ],
235 my $biblioitem = $biblio->biblioitems->first;
236 my $item = $schema->resultset('Item')->create({
237 biblioitemnumber => $biblioitem->biblioitemnumber,
238 biblionumber => $biblio->biblionumber,
239 itype => "ITEM_LEVEL",
242 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
243 is( $item->effective_itemtype(), 'BIB_LEVEL', '$item->itemtype() returns biblioitem.itemtype when item-level_itypes is disabled' );
245 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
246 is( $item->effective_itemtype(), 'ITEM_LEVEL', '$item->itemtype() returns items.itype when item-level_itypes is enabled' );
248 # If itemtype is not defined and item-level_level item types are set
249 # fallback to biblio-level itemtype (Bug 14651) and warn
250 $item->itype( undef );
252 my $effective_itemtype;
253 warning_is { $effective_itemtype = $item->effective_itemtype() }
254 "item-level_itypes set but no itemtype set for item ($item->itemnumber)",
255 '->effective_itemtype() raises a warning when falling back to bib-level';
257 ok( defined $effective_itemtype &&
258 $effective_itemtype eq 'BIB_LEVEL',
259 '$item->effective_itemtype() falls back to biblioitems.itemtype when item-level_itypes is enabled but undef' );
261 $schema->storage->txn_rollback;
264 subtest 'SearchItems test' => sub {
267 $schema->storage->txn_begin;
268 my $dbh = C4::Context->dbh;
269 my $builder = t::lib::TestBuilder->new;
271 my $library1 = $builder->build({
274 my $library2 = $builder->build({
278 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
279 my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
281 my ($biblionumber) = get_biblio();
283 my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
286 my (undef, undef, $item1_itemnumber) = AddItem({
287 homebranch => $library1->{branchcode},
288 holdingbranch => $library1->{branchcode},
290 my (undef, undef, $item2_itemnumber) = AddItem({
291 homebranch => $library2->{branchcode},
292 holdingbranch => $library2->{branchcode},
295 my ($items, $total_results);
297 ($items, $total_results) = SearchItems();
298 is($total_results, $initial_items_count + 2, "Created 2 new items");
299 is(scalar @$items, $total_results, "SearchItems() returns all items");
301 ($items, $total_results) = SearchItems(undef, {rows => 1});
302 is($total_results, $initial_items_count + 2);
303 is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
305 # Search all items where homebranch = 'CPL'
307 field => 'homebranch',
308 query => $library1->{branchcode},
311 ($items, $total_results) = SearchItems($filter);
312 ok($total_results > 0, "There is at least one CPL item");
313 my $all_items_are_CPL = 1;
314 foreach my $item (@$items) {
315 if ($item->{homebranch} ne $library1->{branchcode}) {
316 $all_items_are_CPL = 0;
320 ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
322 # Search all items where homebranch != 'CPL'
324 field => 'homebranch',
325 query => $library1->{branchcode},
328 ($items, $total_results) = SearchItems($filter);
329 ok($total_results > 0, "There is at least one non-CPL item");
330 my $all_items_are_not_CPL = 1;
331 foreach my $item (@$items) {
332 if ($item->{homebranch} eq $library1->{branchcode}) {
333 $all_items_are_not_CPL = 0;
337 ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
339 # Search all items where biblio title (245$a) is like 'Silence in the %'
341 field => 'marc:245$a',
342 query => 'Silence in the %',
345 ($items, $total_results) = SearchItems($filter);
346 ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
348 # Search all items where biblio title is 'Silence in the library'
349 # and homebranch is 'CPL'
351 conjunction => 'AND',
354 field => 'marc:245$a',
355 query => 'Silence in the %',
359 field => 'homebranch',
360 query => $library1->{branchcode},
365 ($items, $total_results) = SearchItems($filter);
367 foreach my $item (@$items) {
368 if ($item->{itemnumber} == $item1_itemnumber) {
373 ok($found, "item1 found");
375 my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
377 # Create item subfield 'z' without link
378 $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
379 $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
382 $C4::Context::context->{marcfromkohafield} = undef;
383 $C4::Biblio::inverted_field_map = undef;
385 my $item3_record = new MARC::Record;
386 $item3_record->append_fields(
387 new MARC::Field($itemfield, '', '', 'z' => 'foobar')
389 my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
392 # Search item where item subfield z is "foobar"
394 field => 'marc:' . $itemfield . '$z',
398 ($items, $total_results) = SearchItems($filter);
399 ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
401 # Link $z to items.itemnotes (and make sure there is no other subfields
403 $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
404 $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
407 $C4::Context::context->{marcfromkohafield} = undef;
408 $C4::Biblio::inverted_field_map = undef;
410 ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
412 # Make sure the link is used
413 my $item3 = GetItem($item3_itemnumber);
414 ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
416 # Do the same search again.
417 # This time it will search in items.itemnotes
418 ($items, $total_results) = SearchItems($filter);
419 ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
421 my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
422 is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
424 $schema->storage->txn_rollback;
427 subtest 'Koha::Item(s) tests' => sub {
431 $schema->storage->txn_begin();
433 my $builder = t::lib::TestBuilder->new;
434 my $library1 = $builder->build({
437 my $library2 = $builder->build({
441 # Create a biblio and item for testing
442 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
443 my ($bibnum, $bibitemnum) = get_biblio();
444 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} } , $bibnum);
447 my $item = Koha::Items->find( $itemnumber );
448 is( ref($item), 'Koha::Item', "Got Koha::Item" );
450 my $homebranch = $item->home_branch();
451 is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
452 is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
454 my $holdingbranch = $item->holding_branch();
455 is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
456 is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
458 $schema->storage->txn_rollback;
461 subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
464 $schema->storage->txn_begin();
466 my $builder = t::lib::TestBuilder->new;
467 my $library1 = $builder->build({
470 my $library2 = $builder->build({
474 my ( $biblionumber, $biblioitemnumber ) = get_biblio();
476 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
477 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
478 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
479 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
480 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
481 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
482 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
483 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
485 my $number_of_items = scalar @$item_infos;
486 my $number_of_items_with_homebranch_is_CPL =
487 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
490 for my $item_info (@$item_infos) {
491 my ( undef, undef, $itemnumber ) = AddItem(
493 homebranch => $item_info->{homebranch},
494 holdingbranch => $item_info->{holdingbanch}
498 push @itemnumbers, $itemnumber;
501 # Emptied the OpacHiddenItems pref
502 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
505 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
506 my $record = C4::Biblio::GetMarcBiblio($biblionumber);
507 warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
508 { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
509 'Should crap is no record passed.';
511 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
512 my @items = $record->field($itemfield);
513 is( scalar @items, $number_of_items, 'Should return all items' );
515 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber,
516 [ $itemnumbers[1], $itemnumbers[3] ] );
517 @items = $record->field($itemfield);
518 is( scalar @items, 2, 'Should return all items present in the list' );
520 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
521 @items = $record->field($itemfield);
522 is( scalar @items, $number_of_items, 'Should return all items for opac' );
524 my $opachiddenitems = "
525 homebranch: ['$library1->{branchcode}']";
526 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
528 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber );
529 @items = $record->field($itemfield);
532 'Even with OpacHiddenItems set, all items should have been embedded' );
534 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
535 @items = $record->field($itemfield);
538 $number_of_items - $number_of_items_with_homebranch_is_CPL,
539 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
543 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
544 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
545 C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 );
546 @items = $record->field($itemfield);
550 'For OPAC, If all items are hidden, no item should have been embedded'
553 $schema->storage->txn_rollback;
557 subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
560 $schema->storage->txn_begin();
563 $C4::Context::context->{marcfromkohafield} = undef;
564 $C4::Biblio::inverted_field_map = undef;
566 my $builder = t::lib::TestBuilder->new;
567 my $framework = $builder->build({
568 source => 'BiblioFramework',
570 # Link biblio.biblionumber and biblioitems.biblioitemnumber to avoid _koha_marc_update_bib_ids to fail with 'no biblio[item]number tag for framework"
572 source => 'MarcSubfieldStructure',
574 frameworkcode => $framework->{frameworkcode},
575 kohafield => 'biblio.biblionumber',
581 source => 'MarcSubfieldStructure',
583 frameworkcode => $framework->{frameworkcode},
584 kohafield => 'biblioitems.biblioitemnumber',
589 my $mss_itemnumber = $builder->build({
590 source => 'MarcSubfieldStructure',
592 frameworkcode => $framework->{frameworkcode},
593 kohafield => 'items.itemnumber',
599 my $mss_barcode = $builder->build({
600 source => 'MarcSubfieldStructure',
602 frameworkcode => $framework->{frameworkcode},
603 kohafield => 'items.barcode',
609 # Create a record with a barcode
610 my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
611 my $item_record = new MARC::Record;
612 my $a_barcode = 'a barcode';
613 my $barcode_field = MARC::Field->new(
617 $item_record->append_fields( $barcode_field );
618 my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
620 # Make sure everything has been set up
621 my $item = GetItem($item_itemnumber);
622 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
624 # Delete the barcode field and save the record
625 $item_record->delete_fields( $barcode_field );
626 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
627 $item = GetItem($item_itemnumber);
628 is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
630 # Re-add the barcode field and save the record
631 $item_record->append_fields( $barcode_field );
632 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
633 $item = GetItem($item_itemnumber);
634 is( $item->{barcode}, $a_barcode, 'Everything has been set up correctly, the barcode is defined as expected' );
637 my $dbh = C4::Context->dbh;
639 DELETE FROM marc_subfield_structure
640 WHERE kohafield = 'items.barcode'
641 AND frameworkcode = ?
642 |, undef, $framework->{frameworkcode} );
644 # And make sure the caches are cleared
645 $C4::Context::context->{marcfromkohafield} = undef;
646 $C4::Biblio::inverted_field_map = undef;
647 my $cache = Koha::Cache->get_instance();
648 $cache->clear_from_cache("default_value_for_mod_marc-" . $framework->{frameworkcode} );
650 # Update the MARC field with another value
651 $item_record->delete_fields( $barcode_field );
652 my $another_barcode = 'another_barcode';
653 my $another_barcode_field = MARC::Field->new(
655 p => $another_barcode,
657 $item_record->append_fields( $another_barcode_field );
658 # The DB value should not have been updated
659 ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
660 $item = GetItem($item_itemnumber);
661 is ( $item->{barcode}, $a_barcode, 'items.barcode is not mapped anymore, so the DB column has not been updated' );
663 $schema->storage->txn_rollback;
666 # Helper method to set up a Biblio.
668 my ( $frameworkcode ) = @_;
669 $frameworkcode //= '';
670 my $bib = MARC::Record->new();
672 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
673 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
675 my ($bibnum, $bibitemnum) = AddBiblio($bib, $frameworkcode);
676 return ($bibnum, $bibitemnum);