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( '', { unsafe => 1 } );
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( '', { unsafe => 0 } );
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;
166 Koha::Holds->search->delete;
167 $dbh->do('DELETE FROM issues');
168 Koha::Patrons->search->delete;
169 Koha::Items->search->delete;
170 Koha::Libraries->search->delete;
171 Koha::CirculationRules->search->delete;
172 Koha::CirculationRules->set_rules(
174 categorycode => undef,
178 reservesallowed => 25,
183 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
184 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
185 my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
186 my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
187 my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
188 my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
189 my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
190 my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
192 my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
193 my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
195 my $item1 = $builder->build_sample_item(
197 homebranch => $library1->branchcode,
198 holdingbranch => $library2->branchcode,
204 my $item3 = $builder->build_sample_item(
206 homebranch => $library3->branchcode,
207 holdingbranch => $library4->branchcode,
213 my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
214 my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
218 "1-1-1-holdgroup" => 2,
219 "1-1-1-patrongroup" => 2,
220 "1-1-1-homebranch" => 1,
221 "1-1-1-holdingbranch" => 1,
223 "1-1-2-holdgroup" => 2,
224 "1-1-2-patrongroup" => 2,
225 "1-1-2-homebranch" => 1,
226 "1-1-2-holdingbranch" => 1,
228 "1-1-3-holdgroup" => 2,
229 "1-1-3-patrongroup" => 2,
230 "1-1-3-homebranch" => 1,
231 "1-1-3-holdingbranch" => 1,
233 "1-4-1-holdgroup" => 0,
234 "1-4-1-patrongroup" => 0,
235 "1-4-1-homebranch" => 0,
236 "1-4-1-holdingbranch" => 0,
238 "1-4-2-holdgroup" => 2,
239 "1-4-2-patrongroup" => 1,
240 "1-4-2-homebranch" => 1,
241 "1-4-2-holdingbranch" => 1,
243 "1-4-3-holdgroup" => 0,
244 "1-4-3-patrongroup" => 0,
245 "1-4-3-homebranch" => 0,
246 "1-4-3-holdingbranch" => 0,
248 "3-1-1-holdgroup" => 0,
249 "3-1-1-patrongroup" => 0,
250 "3-1-1-homebranch" => 0,
251 "3-1-1-holdingbranch" => 0,
253 "3-1-2-holdgroup" => 1,
254 "3-1-2-patrongroup" => 2,
255 "3-1-2-homebranch" => 0,
256 "3-1-2-holdingbranch" => 1,
258 "3-1-3-holdgroup" => 0,
259 "3-1-3-patrongroup" => 0,
260 "3-1-3-homebranch" => 0,
261 "3-1-3-holdingbranch" => 0,
263 "3-4-1-holdgroup" => 0,
264 "3-4-1-patrongroup" => 0,
265 "3-4-1-homebranch" => 0,
266 "3-4-1-holdingbranch" => 0,
268 "3-4-2-holdgroup" => 1,
269 "3-4-2-patrongroup" => 1,
270 "3-4-2-homebranch" => 0,
271 "3-4-2-holdingbranch" => 1,
273 "3-4-3-holdgroup" => 1,
274 "3-4-3-patrongroup" => 1,
275 "3-4-3-homebranch" => 0,
276 "3-4-3-holdingbranch" => 1
280 my ( $item, $patron, $ha, $hfp, $results ) = @_;
282 Koha::CirculationRules->set_rules(
288 hold_fulfillment_policy => $hfp,
289 returnbranch => 'any'
293 my @pl = @{ $item->pickup_locations( { patron => $patron} ) };
294 my $ha_value=$ha==3?'holdgroup':($ha==2?'any':'homebranch');
296 foreach my $pickup_location (@pl) {
297 is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
300 scalar(@pl) == $results->{
302 . $patron->firstname . '-'
312 . ', hold_fulfillment_policy: '
317 . $patron->firstname . '-'
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 $schema->storage->txn_rollback;
342 subtest 'deletion' => sub {
345 $schema->storage->txn_begin;
347 my $biblio = $builder->build_sample_biblio();
349 my $item = $builder->build_sample_item(
351 biblionumber => $biblio->biblionumber,
355 is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
356 ; # FIXME This should be Koha::Deleted::Item
357 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
358 $item = $builder->build_sample_item(
360 biblionumber => $biblio->biblionumber,
364 is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
367 my $library = $builder->build_object({ class => 'Koha::Libraries' });
368 my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
369 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
371 my $patron = $builder->build_object({class => 'Koha::Patrons'});
372 $item = $builder->build_sample_item({ library => $library->branchcode });
375 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
378 $item->safe_to_delete,
380 'Koha::Item->safe_to_delete reports item on loan',
386 'item that is on loan cannot be deleted',
389 AddReturn( $item->barcode, $library->branchcode );
391 # book_reserved is tested in t/db_dependent/Reserves.t
394 t::lib::Mocks::mock_preference('IndependentBranches', 1);
395 my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
398 $item_2->safe_to_delete,
400 'Koha::Item->safe_to_delete reports IndependentBranches restriction',
404 $item_2->safe_delete,
406 'IndependentBranches prevents deletion at another branch',
411 { # codeblock to limit scope of $module->mock
413 my $module = Test::MockModule->new('C4::Items');
414 $module->mock( GetAnalyticsCount => sub { return 1 } );
416 $item->discard_changes;
418 $item->safe_to_delete,
420 'Koha::Item->safe_to_delete reports linked analytics',
426 'Linked analytics prevents deletion of item',
431 { # last_item_for_hold
432 C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber });
433 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' );
435 # With another item attached to the biblio, the item can be deleted
436 $builder->build_sample_item({ biblionumber => $item->biblionumber });
440 $item->safe_to_delete,
442 'Koha::Item->safe_to_delete shows item safe to delete'
447 my $test_item = Koha::Items->find( $item->itemnumber );
449 is( $test_item, undef,
450 "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
453 $schema->storage->txn_rollback;
456 subtest 'renewal_branchcode' => sub {
459 $schema->storage->txn_begin;
461 my $item = $builder->build_sample_item();
462 my $branch = $builder->build_object({ class => 'Koha::Libraries' });
463 my $checkout = $builder->build_object({
464 class => 'Koha::Checkouts',
466 itemnumber => $item->itemnumber,
471 C4::Context->interface( 'intranet' );
472 t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
474 is( $item->renewal_branchcode, $branch->branchcode, "If interface not opac, we get the branch from context");
475 is( $item->renewal_branchcode({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
476 C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
477 is( $item->renewal_branchcode({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
479 C4::Context->interface( 'opac' );
481 t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
482 is( $item->renewal_branchcode, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
483 is( $item->renewal_branchcode({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
485 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
486 is( $item->renewal_branchcode, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
487 is( $item->renewal_branchcode({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
489 t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
490 is( $item->renewal_branchcode, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
491 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");
493 t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
494 is( $item->renewal_branchcode, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
495 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");
497 t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
498 is( $item->renewal_branchcode, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
499 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");
501 $schema->storage->txn_rollback;
504 subtest 'Tests for itemtype' => sub {
506 $schema->storage->txn_begin;
508 my $biblio = $builder->build_sample_biblio;
509 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
510 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, itype => $itemtype->itemtype });
512 t::lib::Mocks::mock_preference('item-level_itypes', 1);
513 is( $item->itemtype->itemtype, $item->itype, 'Pref enabled' );
514 t::lib::Mocks::mock_preference('item-level_itypes', 0);
515 is( $item->itemtype->itemtype, $biblio->biblioitem->itemtype, 'Pref disabled' );
517 $schema->storage->txn_rollback;