3 # Copyright 2019 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 7;
31 use List::MoreUtils qw(all);
33 use t::lib::TestBuilder;
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'hidden_in_opac() tests' => sub {
43 $schema->storage->txn_begin;
45 my $item = $builder->build_sample_item({ itemlost => 2 });
48 # disable hidelostitems as it interteres with OpachiddenItems for the calculation
49 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
51 ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
52 ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
54 # enable hidelostitems to verify correct behaviour
55 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
56 ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
58 # disable hidelostitems
59 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
60 my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
62 $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
64 ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
68 $schema->storage->txn_rollback;
71 subtest 'has_pending_hold() tests' => sub {
75 $schema->storage->txn_begin;
77 my $dbh = C4::Context->dbh;
78 my $item = $builder->build_sample_item({ itemlost => 0 });
79 my $itemnumber = $item->itemnumber;
81 $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
82 ok( $item->has_pending_hold, "Yes, we have a pending hold");
83 $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
84 ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
86 $schema->storage->txn_rollback;
89 subtest "as_marc_field() tests" => sub {
91 my $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
93 my @schema_columns = $schema->resultset('Item')->result_source->columns;
94 my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
96 plan tests => 2 * (scalar @mapped_columns + 1) + 2;
98 $schema->storage->txn_begin;
100 my $item = $builder->build_sample_item;
101 # Make sure it has at least one undefined attribute
102 $item->set({ replacementprice => undef })->store->discard_changes;
104 # Tests with the mss parameter
105 my $marc_field = $item->as_marc_field({ mss => $mss });
109 $mss->{'items.itemnumber'}[0]->{tagfield},
110 'Generated field set the right tag number'
113 foreach my $column ( @mapped_columns ) {
114 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
115 is( $marc_field->subfield($tagsubfield),
116 $item->$column, "Value is mapped correctly for column $column" );
119 # Tests without the mss parameter
120 $marc_field = $item->as_marc_field();
124 $mss->{'items.itemnumber'}[0]->{tagfield},
125 'Generated field set the right tag number'
128 foreach my $column (@mapped_columns) {
129 my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
130 is( $marc_field->subfield($tagsubfield),
131 $item->$column, "Value is mapped correctly for column $column" );
134 my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
137 tagfield => $mss->{'items.itemnumber'}[0]->{tagfield},
142 $mss = C4::Biblio::GetMarcSubfieldStructure( '' );
143 my @unlinked_subfields;
144 push @unlinked_subfields, X => 'Something weird';
145 $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
147 $marc_field = $item->as_marc_field;
149 my @subfields = $marc_field->subfields;
150 my $result = all { defined $_->[1] } @subfields;
151 ok( $result, 'There are no undef subfields' );
153 is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
155 $schema->storage->txn_rollback;
158 subtest 'pickup_locations' => sub {
161 $schema->storage->txn_begin;
163 my $dbh = C4::Context->dbh;
165 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
166 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
167 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
168 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
169 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
170 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
171 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
172 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
174 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
175 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
178 $library1->branchcode, $library2->branchcode,
179 $library3->branchcode, $library4->branchcode
182 my $item1 = $builder->build_sample_item(
184 homebranch => $library1->branchcode,
185 holdingbranch => $library2->branchcode,
191 my $item3 = $builder->build_sample_item(
193 homebranch => $library3->branchcode,
194 holdingbranch => $library4->branchcode,
196 itype => $item1->itype,
200 Koha::CirculationRules->set_rules(
202 categorycode => undef,
203 itemtype => $item1->itype,
206 reservesallowed => 25,
212 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
213 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
215 my $all_count = Koha::Libraries->search({ pickup_location => 1})->count();
218 "1-1-1-any" => $all_count,
219 "1-1-1-holdgroup" => 2,
220 "1-1-1-patrongroup" => 2,
221 "1-1-1-homebranch" => 1,
222 "1-1-1-holdingbranch" => 1,
223 "1-1-2-any" => $all_count,
224 "1-1-2-holdgroup" => 2,
225 "1-1-2-patrongroup" => 2,
226 "1-1-2-homebranch" => 1,
227 "1-1-2-holdingbranch" => 1,
228 "1-1-3-any" => $all_count,
229 "1-1-3-holdgroup" => 2,
230 "1-1-3-patrongroup" => 2,
231 "1-1-3-homebranch" => 1,
232 "1-1-3-holdingbranch" => 1,
234 "1-4-1-holdgroup" => 0,
235 "1-4-1-patrongroup" => 0,
236 "1-4-1-homebranch" => 0,
237 "1-4-1-holdingbranch" => 0,
238 "1-4-2-any" => $all_count,
239 "1-4-2-holdgroup" => 2,
240 "1-4-2-patrongroup" => 1,
241 "1-4-2-homebranch" => 1,
242 "1-4-2-holdingbranch" => 1,
244 "1-4-3-holdgroup" => 0,
245 "1-4-3-patrongroup" => 0,
246 "1-4-3-homebranch" => 0,
247 "1-4-3-holdingbranch" => 0,
249 "3-1-1-holdgroup" => 0,
250 "3-1-1-patrongroup" => 0,
251 "3-1-1-homebranch" => 0,
252 "3-1-1-holdingbranch" => 0,
253 "3-1-2-any" => $all_count,
254 "3-1-2-holdgroup" => 1,
255 "3-1-2-patrongroup" => 2,
256 "3-1-2-homebranch" => 0,
257 "3-1-2-holdingbranch" => 1,
259 "3-1-3-holdgroup" => 0,
260 "3-1-3-patrongroup" => 0,
261 "3-1-3-homebranch" => 0,
262 "3-1-3-holdingbranch" => 0,
264 "3-4-1-holdgroup" => 0,
265 "3-4-1-patrongroup" => 0,
266 "3-4-1-homebranch" => 0,
267 "3-4-1-holdingbranch" => 0,
268 "3-4-2-any" => $all_count,
269 "3-4-2-holdgroup" => 1,
270 "3-4-2-patrongroup" => 1,
271 "3-4-2-homebranch" => 0,
272 "3-4-2-holdingbranch" => 1,
273 "3-4-3-any" => $all_count,
274 "3-4-3-holdgroup" => 1,
275 "3-4-3-patrongroup" => 1,
276 "3-4-3-homebranch" => 0,
277 "3-4-3-holdingbranch" => 1
281 my ( $item, $patron, $ha, $hfp, $results ) = @_;
283 Koha::CirculationRules->set_rules(
289 hold_fulfillment_policy => $hfp,
290 returnbranch => 'any'
294 my @pl = $item->pickup_locations( { patron => $patron} )->as_list;
295 my $ha_value=$ha==3?'holdgroup':($ha==2?'any':'homebranch');
297 foreach my $pickup_location (@pl) {
299 unless grep { $pickup_location eq $_ } @branchcodes;
300 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
303 scalar(@pl) == $results->{
304 $item->copynumber . '-'
305 . $patron->firstname . '-'
315 . ', hold_fulfillment_policy: '
319 $item->copynumber . '-'
320 . $patron->firstname . '-'
331 foreach my $item ($item1, $item3) {
332 foreach my $patron ($patron1, $patron4) {
333 #holdallowed 1: homebranch, 2: any, 3: holdgroup
334 foreach my $ha (1, 2, 3) {
335 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
336 _doTest($item, $patron, $ha, $hfp, $results);
342 # Now test that branchtransferlimits will further filter the pickup locations
344 my $item_no_ccode = $builder->build_sample_item(
346 homebranch => $library1->branchcode,
347 holdingbranch => $library2->branchcode,
348 itype => $item1->itype,
352 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
353 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
354 Koha::CirculationRules->set_rules(
357 itemtype => $item1->itype,
360 hold_fulfillment_policy => 1,
361 returnbranch => 'any'
365 $builder->build_object(
367 class => 'Koha::Item::Transfer::Limits',
369 toBranch => $library1->branchcode,
370 fromBranch => $library2->branchcode,
371 itemtype => $item1->itype,
377 my $pickup_locations = $item1->pickup_locations( { patron => $patron1 } )->as_list;
378 is( scalar @$pickup_locations, $all_count - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
380 $builder->build_object(
382 class => 'Koha::Item::Transfer::Limits',
384 toBranch => $library4->branchcode,
385 fromBranch => $library2->branchcode,
386 itemtype => $item1->itype,
392 $pickup_locations = $item1->pickup_locations( { patron => $patron1 } )->as_list;
393 is( scalar @$pickup_locations, $all_count - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
395 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
396 $pickup_locations = $item1->pickup_locations( { patron => $patron1 } )->as_list;
397 is( scalar @$pickup_locations, $all_count, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
399 $pickup_locations = $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
400 is( scalar @$pickup_locations, $all_count, "With no transfer limits of type ccode and an item with no ccode we get back the libraries that are pickup locations");
402 $builder->build_object(
404 class => 'Koha::Item::Transfer::Limits',
406 toBranch => $library2->branchcode,
407 fromBranch => $library2->branchcode,
409 ccode => $item1->ccode,
414 $pickup_locations = $item1->pickup_locations( { patron => $patron1 } )->as_list;
415 is( scalar @$pickup_locations, $all_count - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
417 $builder->build_object(
419 class => 'Koha::Item::Transfer::Limits',
421 toBranch => $library4->branchcode,
422 fromBranch => $library2->branchcode,
424 ccode => $item1->ccode,
429 $pickup_locations = $item1->pickup_locations( { patron => $patron1 } )->as_list;
430 is( scalar @$pickup_locations, $all_count - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
432 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
434 $schema->storage->txn_rollback;
437 subtest 'deletion' => sub {
440 $schema->storage->txn_begin;
442 my $biblio = $builder->build_sample_biblio();
444 my $item = $builder->build_sample_item(
446 biblionumber => $biblio->biblionumber,
450 is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
451 ; # FIXME This should be Koha::Deleted::Item
452 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
453 $item = $builder->build_sample_item(
455 biblionumber => $biblio->biblionumber,
459 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
462 my $library = $builder->build_object({ class => 'Koha::Libraries' });
463 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
464 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
466 my $patron = $builder->build_object({class => 'Koha::Patrons'});
467 $item = $builder->build_sample_item({ library => $library->branchcode });
470 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
473 $item->safe_to_delete,
475 'Koha::Item->safe_to_delete reports item on loan',
481 'item that is on loan cannot be deleted',
484 AddReturn( $item->barcode, $library->branchcode );
486 # book_reserved is tested in t/db_dependent/Reserves.t
489 t::lib::Mocks::mock_preference('IndependentBranches', 1);
490 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
493 $item_2->safe_to_delete,
495 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
499 $item_2->safe_delete,
501 'IndependentBranches prevents deletion at another branch',
506 { # codeblock to limit scope of $module->mock
508 my $module = Test::MockModule->new('C4::Items');
509 $module->mock( GetAnalyticsCount => sub { return 1 } );
511 $item->discard_changes;
513 $item->safe_to_delete,
515 'Koha::Item->safe_to_delete reports linked analytics',
521 'Linked analytics prevents deletion of item',
526 { # last_item_for_hold
527 C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
528 is( $item->safe_to_delete, 'last_item_for_hold', 'Item cannot be deleted if a biblio-level is placed on the biblio and there is only 1 item attached to the biblio' );
530 # With another item attached to the biblio, the item can be deleted
531 $builder->build_sample_item({ biblionumber => $item->biblionumber });
535 $item->safe_to_delete,
537 'Koha::Item->safe_to_delete shows item safe to delete'
542 my $test_item = Koha::Items->find( $item->itemnumber );
544 is( $test_item, undef,
545 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
548 $schema->storage->txn_rollback;
551 subtest 'renewal_branchcode' => sub {
554 $schema->storage->txn_begin;
556 my $item = $builder->build_sample_item();
557 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
558 my $checkout = $builder->build_object({
559 class => 'Koha::Checkouts',
561 itemnumber => $item->itemnumber,
566 C4::Context->interface( 'intranet' );
567 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
569 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
570 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
571 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
572 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
574 C4::Context->interface( 'opac' );
576 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
577 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
578 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
580 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
581 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
582 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
584 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
585 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
586 is( $item->renewal_branchcode({branch=>'MONKEY'}), $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed");
588 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
589 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
590 is( $item->renewal_branchcode({branch=>'TURKEY'}), $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron even if branch passed");
592 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
593 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
594 is( $item->renewal_branchcode({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed");
596 $schema->storage->txn_rollback;
599 subtest 'Tests for itemtype' => sub {
601 $schema->storage->txn_begin;
603 my $biblio = $builder->build_sample_biblio;
604 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
605 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
607 t::lib::Mocks::mock_preference('item-level_itypes', 1);
608 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
609 t::lib::Mocks::mock_preference('item-level_itypes', 0);
610 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
612 $schema->storage->txn_rollback;