Bug 27062: Fix pickup_location tests when existing libraries exist
[koha.git] / t / db_dependent / Koha / Item.t
blobf2713b4e26af055d790aa607f61800988c1ce361
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 7;
24 use C4::Biblio;
25 use C4::Circulation;
27 use Koha::Items;
28 use Koha::Database;
29 use Koha::Old::Items;
31 use List::MoreUtils qw(all);
33 use t::lib::TestBuilder;
34 use t::lib::Mocks;
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
39 subtest 'hidden_in_opac() tests' => sub {
41 plan tests => 4;
43 $schema->storage->txn_begin;
45 my $item = $builder->build_sample_item({ itemlost => 2 });
46 my $rules = {};
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 {
73 plan tests => 2;
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 });
108 $marc_field->tag,
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();
123 $marc_field->tag,
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(
136 frameworkcode => '',
137 tagfield => $mss->{'items.itemnumber'}[0]->{tagfield},
138 tagsubfield => 'X',
140 )->store;
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 {
159 plan tests => 66;
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 } } );
177 our @branchcodes = (
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,
186 copynumber => 1,
187 ccode => 'Gollum'
189 )->store;
191 my $item3 = $builder->build_sample_item(
193 homebranch => $library3->branchcode,
194 holdingbranch => $library4->branchcode,
195 copynumber => 3,
196 itype => $item1->itype,
198 )->store;
200 Koha::CirculationRules->set_rules(
202 categorycode => undef,
203 itemtype => $item1->itype,
204 branchcode => undef,
205 rules => {
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 $results = {
216 "1-1-1-any" => 3,
217 "1-1-1-holdgroup" => 2,
218 "1-1-1-patrongroup" => 2,
219 "1-1-1-homebranch" => 1,
220 "1-1-1-holdingbranch" => 1,
221 "1-1-2-any" => 3,
222 "1-1-2-holdgroup" => 2,
223 "1-1-2-patrongroup" => 2,
224 "1-1-2-homebranch" => 1,
225 "1-1-2-holdingbranch" => 1,
226 "1-1-3-any" => 3,
227 "1-1-3-holdgroup" => 2,
228 "1-1-3-patrongroup" => 2,
229 "1-1-3-homebranch" => 1,
230 "1-1-3-holdingbranch" => 1,
231 "1-4-1-any" => 0,
232 "1-4-1-holdgroup" => 0,
233 "1-4-1-patrongroup" => 0,
234 "1-4-1-homebranch" => 0,
235 "1-4-1-holdingbranch" => 0,
236 "1-4-2-any" => 3,
237 "1-4-2-holdgroup" => 2,
238 "1-4-2-patrongroup" => 1,
239 "1-4-2-homebranch" => 1,
240 "1-4-2-holdingbranch" => 1,
241 "1-4-3-any" => 0,
242 "1-4-3-holdgroup" => 0,
243 "1-4-3-patrongroup" => 0,
244 "1-4-3-homebranch" => 0,
245 "1-4-3-holdingbranch" => 0,
246 "3-1-1-any" => 0,
247 "3-1-1-holdgroup" => 0,
248 "3-1-1-patrongroup" => 0,
249 "3-1-1-homebranch" => 0,
250 "3-1-1-holdingbranch" => 0,
251 "3-1-2-any" => 3,
252 "3-1-2-holdgroup" => 1,
253 "3-1-2-patrongroup" => 2,
254 "3-1-2-homebranch" => 0,
255 "3-1-2-holdingbranch" => 1,
256 "3-1-3-any" => 0,
257 "3-1-3-holdgroup" => 0,
258 "3-1-3-patrongroup" => 0,
259 "3-1-3-homebranch" => 0,
260 "3-1-3-holdingbranch" => 0,
261 "3-4-1-any" => 0,
262 "3-4-1-holdgroup" => 0,
263 "3-4-1-patrongroup" => 0,
264 "3-4-1-homebranch" => 0,
265 "3-4-1-holdingbranch" => 0,
266 "3-4-2-any" => 3,
267 "3-4-2-holdgroup" => 1,
268 "3-4-2-patrongroup" => 1,
269 "3-4-2-homebranch" => 0,
270 "3-4-2-holdingbranch" => 1,
271 "3-4-3-any" => 3,
272 "3-4-3-holdgroup" => 1,
273 "3-4-3-patrongroup" => 1,
274 "3-4-3-homebranch" => 0,
275 "3-4-3-holdingbranch" => 1
278 sub _doTest {
279 my ( $item, $patron, $ha, $hfp, $results ) = @_;
281 Koha::CirculationRules->set_rules(
283 branchcode => undef,
284 itemtype => undef,
285 rules => {
286 holdallowed => $ha,
287 hold_fulfillment_policy => $hfp,
288 returnbranch => 'any'
292 my $ha_value=$ha==3?'holdgroup':($ha==2?'any':'homebranch');
294 my @pl = map {
295 my $pickup_location = $_;
296 grep { $pickup_location->branchcode eq $_ } @branchcodes
297 } $item->pickup_locations( { patron => $patron } )->as_list;
300 scalar(@pl) == $results->{
301 $item->copynumber . '-'
302 . $patron->firstname . '-'
303 . $ha . '-'
304 . $hfp
306 'item'
307 . $item->copynumber
308 . ', patron'
309 . $patron->firstname
310 . ', holdallowed: '
311 . $ha_value
312 . ', hold_fulfillment_policy: '
313 . $hfp
314 . ' should return '
315 . $results->{
316 $item->copynumber . '-'
317 . $patron->firstname . '-'
318 . $ha . '-'
319 . $hfp
321 . ' and returns '
322 . scalar(@pl)
328 foreach my $item ($item1, $item3) {
329 foreach my $patron ($patron1, $patron4) {
330 #holdallowed 1: homebranch, 2: any, 3: holdgroup
331 foreach my $ha (1, 2, 3) {
332 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
333 _doTest($item, $patron, $ha, $hfp, $results);
339 # Now test that branchtransferlimits will further filter the pickup locations
341 my $item_no_ccode = $builder->build_sample_item(
343 homebranch => $library1->branchcode,
344 holdingbranch => $library2->branchcode,
345 itype => $item1->itype,
347 )->store;
349 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
350 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
351 Koha::CirculationRules->set_rules(
353 branchcode => undef,
354 itemtype => $item1->itype,
355 rules => {
356 holdallowed => 1,
357 hold_fulfillment_policy => 1,
358 returnbranch => 'any'
362 $builder->build_object(
364 class => 'Koha::Item::Transfer::Limits',
365 value => {
366 toBranch => $library1->branchcode,
367 fromBranch => $library2->branchcode,
368 itemtype => $item1->itype,
369 ccode => undef,
374 my @pickup_locations = map {
375 my $pickup_location = $_;
376 grep { $pickup_location->branchcode eq $_ } @branchcodes
377 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
379 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
381 $builder->build_object(
383 class => 'Koha::Item::Transfer::Limits',
384 value => {
385 toBranch => $library4->branchcode,
386 fromBranch => $library2->branchcode,
387 itemtype => $item1->itype,
388 ccode => undef,
393 @pickup_locations = map {
394 my $pickup_location = $_;
395 grep { $pickup_location->branchcode eq $_ } @branchcodes
396 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
398 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
400 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
401 @pickup_locations = map {
402 my $pickup_location = $_;
403 grep { $pickup_location->branchcode eq $_ } @branchcodes
404 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
405 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode we get back the libraries that are pickup locations");
407 @pickup_locations = map {
408 my $pickup_location = $_;
409 grep { $pickup_location->branchcode eq $_ } @branchcodes
410 } $item_no_ccode->pickup_locations( { patron => $patron1 } )->as_list;
411 is( scalar @pickup_locations, 3, "With no transfer limits of type ccode and an item with no ccode we get back the libraries that are pickup locations");
413 $builder->build_object(
415 class => 'Koha::Item::Transfer::Limits',
416 value => {
417 toBranch => $library2->branchcode,
418 fromBranch => $library2->branchcode,
419 itemtype => undef,
420 ccode => $item1->ccode,
425 @pickup_locations = map {
426 my $pickup_location = $_;
427 grep { $pickup_location->branchcode eq $_ } @branchcodes
428 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
429 is( scalar @pickup_locations, 3 - 1, "With a transfer limits we get back the libraries that are pickup locations minus 1 limited library");
431 $builder->build_object(
433 class => 'Koha::Item::Transfer::Limits',
434 value => {
435 toBranch => $library4->branchcode,
436 fromBranch => $library2->branchcode,
437 itemtype => undef,
438 ccode => $item1->ccode,
443 @pickup_locations = map {
444 my $pickup_location = $_;
445 grep { $pickup_location->branchcode eq $_ } @branchcodes
446 } $item1->pickup_locations( { patron => $patron1 } )->as_list;
447 is( scalar @pickup_locations, 3 - 2, "With 2 transfer limits we get back the libraries that are pickup locations minus 2 limited libraries");
449 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
451 $schema->storage->txn_rollback;
454 subtest 'deletion' => sub {
455 plan tests => 12;
457 $schema->storage->txn_begin;
459 my $biblio = $builder->build_sample_biblio();
461 my $item = $builder->build_sample_item(
463 biblionumber => $biblio->biblionumber,
467 is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
468 ; # FIXME This should be Koha::Deleted::Item
469 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
470 $item = $builder->build_sample_item(
472 biblionumber => $biblio->biblionumber,
475 $item->delete;
476 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
479 my $library = $builder->build_object({ class => 'Koha::Libraries' });
480 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
481 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
483 my $patron = $builder->build_object({class => 'Koha::Patrons'});
484 $item = $builder->build_sample_item({ library => $library->branchcode });
486 # book_on_loan
487 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
490 $item->safe_to_delete,
491 'book_on_loan',
492 'Koha::Item->safe_to_delete reports item on loan',
496 $item->safe_delete,
497 'book_on_loan',
498 'item that is on loan cannot be deleted',
501 AddReturn( $item->barcode, $library->branchcode );
503 # book_reserved is tested in t/db_dependent/Reserves.t
505 # not_same_branch
506 t::lib::Mocks::mock_preference('IndependentBranches', 1);
507 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
510 $item_2->safe_to_delete,
511 'not_same_branch',
512 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
516 $item_2->safe_delete,
517 'not_same_branch',
518 'IndependentBranches prevents deletion at another branch',
521 # linked_analytics
523 { # codeblock to limit scope of $module->mock
525 my $module = Test::MockModule->new('C4::Items');
526 $module->mock( GetAnalyticsCount => sub { return 1 } );
528 $item->discard_changes;
530 $item->safe_to_delete,
531 'linked_analytics',
532 'Koha::Item->safe_to_delete reports linked analytics',
536 $item->safe_delete,
537 'linked_analytics',
538 'Linked analytics prevents deletion of item',
543 { # last_item_for_hold
544 C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
545 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' );
547 # With another item attached to the biblio, the item can be deleted
548 $builder->build_sample_item({ biblionumber => $item->biblionumber });
552 $item->safe_to_delete,
554 'Koha::Item->safe_to_delete shows item safe to delete'
557 $item->safe_delete,
559 my $test_item = Koha::Items->find( $item->itemnumber );
561 is( $test_item, undef,
562 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
565 $schema->storage->txn_rollback;
568 subtest 'renewal_branchcode' => sub {
569 plan tests => 13;
571 $schema->storage->txn_begin;
573 my $item = $builder->build_sample_item();
574 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
575 my $checkout = $builder->build_object({
576 class => 'Koha::Checkouts',
577 value => {
578 itemnumber => $item->itemnumber,
583 C4::Context->interface( 'intranet' );
584 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
586 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
587 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
588 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
589 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
591 C4::Context->interface( 'opac' );
593 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
594 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
595 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
597 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
598 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
599 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
601 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
602 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
603 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");
605 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
606 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
607 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");
609 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
610 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
611 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");
613 $schema->storage->txn_rollback;
616 subtest 'Tests for itemtype' => sub {
617 plan tests => 2;
618 $schema->storage->txn_begin;
620 my $biblio = $builder->build_sample_biblio;
621 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
622 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
624 t::lib::Mocks::mock_preference('item-level_itypes', 1);
625 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
626 t::lib::Mocks::mock_preference('item-level_itypes', 0);
627 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
629 $schema->storage->txn_rollback;