Bug 24759: CleanUp OpacRenewalBranch values
[koha.git] / t / db_dependent / Koha / Item.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 6;
23
24 use C4::Biblio;
25 use C4::Circulation;
26
27 use Koha::Items;
28 use Koha::Database;
29 use Koha::Old::Items;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'hidden_in_opac() tests' => sub {
38
39     plan tests => 4;
40
41     $schema->storage->txn_begin;
42
43     my $item  = $builder->build_sample_item({ itemlost => 2 });
44     my $rules = {};
45
46     # disable hidelostitems as it interteres with OpachiddenItems for the calculation
47     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
48
49     ok( !$item->hidden_in_opac, 'No rules passed, shouldn\'t hide' );
50     ok( !$item->hidden_in_opac({ rules => $rules }), 'Empty rules passed, shouldn\'t hide' );
51
52     # enable hidelostitems to verify correct behaviour
53     t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
54     ok( $item->hidden_in_opac, 'Even with no rules, item should hide because of hidelostitems syspref' );
55
56     # disable hidelostitems
57     t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
58     my $withdrawn = $item->withdrawn + 1; # make sure this attribute doesn't match
59
60     $rules = { withdrawn => [$withdrawn], itype => [ $item->itype ] };
61
62     ok( $item->hidden_in_opac({ rules => $rules }), 'Rule matching itype passed, should hide' );
63
64
65
66     $schema->storage->txn_rollback;
67 };
68
69 subtest 'has_pending_hold() tests' => sub {
70
71     plan tests => 2;
72
73     $schema->storage->txn_begin;
74
75     my $dbh = C4::Context->dbh;
76     my $item  = $builder->build_sample_item({ itemlost => 0 });
77     my $itemnumber = $item->itemnumber;
78
79     $dbh->do("INSERT INTO tmp_holdsqueue (surname,borrowernumber,itemnumber) VALUES ('Clamp',42,$itemnumber)");
80     ok( $item->has_pending_hold, "Yes, we have a pending hold");
81     $dbh->do("DELETE FROM tmp_holdsqueue WHERE itemnumber=$itemnumber");
82     ok( !$item->has_pending_hold, "We don't have a pending hold if nothing in the tmp_holdsqueue");
83
84     $schema->storage->txn_rollback;
85 };
86
87 subtest "as_marc_field() tests" => sub {
88
89     my $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 1 } );
90
91     my @schema_columns = $schema->resultset('Item')->result_source->columns;
92     my @mapped_columns = grep { exists $mss->{'items.'.$_} } @schema_columns;
93
94     plan tests => 2 * (scalar @mapped_columns + 1) + 1;
95
96     $schema->storage->txn_begin;
97
98     my $item = $builder->build_sample_item;
99
100     # Tests with the mss parameter
101     my $marc_field = $item->as_marc_field({ mss => $mss });
102
103     is(
104         $marc_field->tag,
105         $mss->{'items.itemnumber'}[0]->{tagfield},
106         'Generated field set the right tag number'
107     );
108
109     foreach my $column ( @mapped_columns ) {
110         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
111         is( $marc_field->subfield($tagsubfield),
112             $item->$column, "Value is mapped correctly for column $column" );
113     }
114
115     # Tests without the mss parameter
116     $marc_field = $item->as_marc_field();
117
118     is(
119         $marc_field->tag,
120         $mss->{'items.itemnumber'}[0]->{tagfield},
121         'Generated field set the right tag number'
122     );
123
124     foreach my $column (@mapped_columns) {
125         my $tagsubfield = $mss->{ 'items.' . $column }[0]->{tagsubfield};
126         is( $marc_field->subfield($tagsubfield),
127             $item->$column, "Value is mapped correctly for column $column" );
128     }
129
130     my $unmapped_subfield = Koha::MarcSubfieldStructure->new(
131         {
132             frameworkcode => '',
133             tagfield      => $mss->{'items.itemnumber'}[0]->{tagfield},
134             tagsubfield   => 'X',
135         }
136     )->store;
137
138     $mss = C4::Biblio::GetMarcSubfieldStructure( '', { unsafe => 0 } );
139     my @unlinked_subfields;
140     push @unlinked_subfields, X => 'Something weird';
141     $item->more_subfields_xml( C4::Items::_get_unlinked_subfields_xml( \@unlinked_subfields ) )->store;
142
143     $marc_field = $item->as_marc_field;
144     is( scalar $marc_field->subfield('X'), 'Something weird', 'more_subfield_xml is considered' );
145
146     $schema->storage->txn_rollback;
147 };
148
149 subtest 'pickup_locations' => sub {
150     plan tests => 114;
151
152     $schema->storage->txn_begin;
153
154     my $dbh = C4::Context->dbh;
155
156     # Cleanup database
157     Koha::Holds->search->delete;
158     Koha::Patrons->search->delete;
159     Koha::Items->search->delete;
160     Koha::Libraries->search->delete;
161     $dbh->do('DELETE FROM issues');
162     Koha::CirculationRules->search->delete;
163     Koha::CirculationRules->set_rules(
164         {
165             categorycode => undef,
166             itemtype     => undef,
167             branchcode   => undef,
168             rules        => {
169                 reservesallowed => 25,
170             }
171         }
172     );
173
174     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
175     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1, branchcode => undef } } );
176     my $library1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
177     my $library2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
178     my $library3 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0, } } );
179     my $library4 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1, } } );
180     my $group1_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
181     my $group1_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
182
183     my $group2_1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
184     my $group2_2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
185
186     my $biblioitem  = $builder->build( { source => 'Biblioitem' } );
187
188     my $item1  = Koha::Item->new({
189         biblionumber     => $biblioitem->{biblionumber},
190         biblioitemnumber => $biblioitem->{biblioitemnumber},
191         homebranch       => $library1->branchcode,
192         holdingbranch    => $library2->branchcode,
193         barcode          => '1',
194         itype            => 'test',
195     })->store;
196
197     my $item3  = Koha::Item->new({
198         biblionumber     => $biblioitem->{biblionumber},
199         biblioitemnumber => $biblioitem->{biblioitemnumber},
200         homebranch       => $library3->branchcode,
201         holdingbranch    => $library4->branchcode,
202         barcode          => '3',
203         itype            => 'test',
204     })->store;
205
206     my $patron1 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library1->branchcode, firstname => '1' } } );
207     my $patron4 = $builder->build_object( { class => 'Koha::Patrons', value => { branchcode => $library4->branchcode, firstname => '4' } } );
208
209     my $results = {
210         "1-1-1-any" => 3,
211         "1-1-1-holdgroup" => 2,
212         "1-1-1-patrongroup" => 2,
213         "1-1-1-homebranch" => 1,
214         "1-1-1-holdingbranch" => 1,
215         "1-1-2-any" => 3,
216         "1-1-2-holdgroup" => 2,
217         "1-1-2-patrongroup" => 2,
218         "1-1-2-homebranch" => 1,
219         "1-1-2-holdingbranch" => 1,
220         "1-1-3-any" => 3,
221         "1-1-3-holdgroup" => 2,
222         "1-1-3-patrongroup" => 2,
223         "1-1-3-homebranch" => 1,
224         "1-1-3-holdingbranch" => 1,
225         "1-4-1-any" => 0,
226         "1-4-1-holdgroup" => 0,
227         "1-4-1-patrongroup" => 0,
228         "1-4-1-homebranch" => 0,
229         "1-4-1-holdingbranch" => 0,
230         "1-4-2-any" => 3,
231         "1-4-2-holdgroup" => 2,
232         "1-4-2-patrongroup" => 1,
233         "1-4-2-homebranch" => 1,
234         "1-4-2-holdingbranch" => 1,
235         "1-4-3-any" => 0,
236         "1-4-3-holdgroup" => 0,
237         "1-4-3-patrongroup" => 0,
238         "1-4-3-homebranch" => 0,
239         "1-4-3-holdingbranch" => 0,
240         "3-1-1-any" => 0,
241         "3-1-1-holdgroup" => 0,
242         "3-1-1-patrongroup" => 0,
243         "3-1-1-homebranch" => 0,
244         "3-1-1-holdingbranch" => 0,
245         "3-1-2-any" => 3,
246         "3-1-2-holdgroup" => 1,
247         "3-1-2-patrongroup" => 2,
248         "3-1-2-homebranch" => 0,
249         "3-1-2-holdingbranch" => 1,
250         "3-1-3-any" => 0,
251         "3-1-3-holdgroup" => 0,
252         "3-1-3-patrongroup" => 0,
253         "3-1-3-homebranch" => 0,
254         "3-1-3-holdingbranch" => 0,
255         "3-4-1-any" => 0,
256         "3-4-1-holdgroup" => 0,
257         "3-4-1-patrongroup" => 0,
258         "3-4-1-homebranch" => 0,
259         "3-4-1-holdingbranch" => 0,
260         "3-4-2-any" => 3,
261         "3-4-2-holdgroup" => 1,
262         "3-4-2-patrongroup" => 1,
263         "3-4-2-homebranch" => 0,
264         "3-4-2-holdingbranch" => 1,
265         "3-4-3-any" => 3,
266         "3-4-3-holdgroup" => 1,
267         "3-4-3-patrongroup" => 1,
268         "3-4-3-homebranch" => 0,
269         "3-4-3-holdingbranch" => 1
270     };
271
272     sub _doTest {
273         my ( $item, $patron, $ha, $hfp, $results ) = @_;
274
275         Koha::CirculationRules->set_rules(
276             {
277                 branchcode => undef,
278                 itemtype   => undef,
279                 rules => {
280                     holdallowed => $ha,
281                     hold_fulfillment_policy => $hfp,
282                     returnbranch => 'any'
283                 }
284             }
285         );
286         my @pl = $item->pickup_locations( { patron => $patron} );
287         my $ha_value=$ha==3?'holdgroup':($ha==2?'any':'homebranch');
288
289         foreach my $pickup_location (@pl) {
290             is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
291         }
292         ok(
293             scalar(@pl) == $results->{
294                     $item->barcode . '-'
295                   . $patron->firstname . '-'
296                   . $ha . '-'
297                   . $hfp
298             },
299             'item'
300               . $item->barcode
301               . ', patron'
302               . $patron->firstname
303               . ', holdallowed: '
304               . $ha_value
305               . ', hold_fulfillment_policy: '
306               . $hfp
307               . ' should return '
308               . $results->{
309                     $item->barcode . '-'
310                   . $patron->firstname . '-'
311                   . $ha . '-'
312                   . $hfp
313               }
314               . ' but returns '
315               . scalar(@pl)
316         );
317
318     }
319
320
321     foreach my $item ($item1, $item3) {
322         foreach my $patron ($patron1, $patron4) {
323             #holdallowed 1: homebranch, 2: any, 3: holdgroup
324             foreach my $ha (1, 2, 3) {
325                 foreach my $hfp ('any', 'holdgroup', 'patrongroup', 'homebranch', 'holdingbranch') {
326                     _doTest($item, $patron, $ha, $hfp, $results);
327                 }
328             }
329         }
330     }
331
332     $schema->storage->txn_rollback;
333 };
334
335 subtest 'deletion' => sub {
336     plan tests => 11;
337
338     $schema->storage->txn_begin;
339
340     my $biblio = $builder->build_sample_biblio();
341
342     my $item = $builder->build_sample_item(
343         {
344             biblionumber => $biblio->biblionumber,
345         }
346     );
347
348     is( ref( $item->move_to_deleted ), 'Koha::Schema::Result::Deleteditem', 'Koha::Item->move_to_deleted should return the Deleted item' )
349       ;    # FIXME This should be Koha::Deleted::Item
350     is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 1, '->move_to_deleted must have moved the item to deleteditem' );
351     $item = $builder->build_sample_item(
352         {
353             biblionumber => $biblio->biblionumber,
354         }
355     );
356     $item->delete;
357     is( Koha::Old::Items->search({itemnumber => $item->itemnumber})->count, 0, '->move_to_deleted must not have moved the item to deleteditem' );
358
359
360     my $library   = $builder->build_object({ class => 'Koha::Libraries' });
361     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
362     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
363
364     my $patron = $builder->build_object({class => 'Koha::Patrons'});
365     $item = $builder->build_sample_item({ library => $library->branchcode });
366
367     # book_on_loan
368     C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
369
370     is(
371         $item->safe_to_delete,
372         'book_on_loan',
373         'Koha::Item->safe_to_delete reports item on loan',
374     );
375
376     is(
377         $item->safe_delete,
378         'book_on_loan',
379         'item that is on loan cannot be deleted',
380     );
381
382     AddReturn( $item->barcode, $library->branchcode );
383
384     # book_reserved is tested in t/db_dependent/Reserves.t
385
386     # not_same_branch
387     t::lib::Mocks::mock_preference('IndependentBranches', 1);
388     my $item_2 = $builder->build_sample_item({ library => $library_2->branchcode });
389
390     is(
391         $item_2->safe_to_delete,
392         'not_same_branch',
393         'Koha::Item->safe_to_delete reports IndependentBranches restriction',
394     );
395
396     is(
397         $item_2->safe_delete,
398         'not_same_branch',
399         'IndependentBranches prevents deletion at another branch',
400     );
401
402     # linked_analytics
403
404     { # codeblock to limit scope of $module->mock
405
406         my $module = Test::MockModule->new('C4::Items');
407         $module->mock( GetAnalyticsCount => sub { return 1 } );
408
409         $item->discard_changes;
410         is(
411             $item->safe_to_delete,
412             'linked_analytics',
413             'Koha::Item->safe_to_delete reports linked analytics',
414         );
415
416         is(
417             $item->safe_delete,
418             'linked_analytics',
419             'Linked analytics prevents deletion of item',
420         );
421
422     }
423
424     is(
425         $item->safe_to_delete,
426         1,
427         'Koha::Item->safe_to_delete shows item safe to delete'
428     );
429
430     $item->safe_delete,
431
432     my $test_item = Koha::Items->find( $item->itemnumber );
433
434     is( $test_item, undef,
435         "Koha::Item->safe_delete should delete item if safe_to_delete returns true"
436     );
437
438     $schema->storage->txn_rollback;
439 };
440
441 subtest 'renewalbranch' => sub {
442     plan tests => 15;
443
444     $schema->storage->txn_begin;
445
446     my $item = $builder->build_sample_item();
447     my $branch = $builder->build_object({ class => 'Koha::Libraries' });
448     my $checkout = $builder->build_object({
449         class => 'Koha::Checkouts',
450         value => {
451             itemnumber => $item->itemnumber,
452         }
453     });
454
455
456     C4::Context->interface( 'intranet' );
457     t::lib::Mocks::mock_userenv({ branchcode => $branch->branchcode });
458
459     is( $item->renewalbranch, $branch->branchcode, "If interface not opac, we get the branch from context");
460     is( $item->renewalbranch({ branch => "PANDA"}), $branch->branchcode, "If interface not opac, we get the branch from context even if we pass one in");
461     C4::Context->set_userenv(51, 'userid4tests', undef, 'firstname', 'surname', undef, undef, 0, undef, undef, undef ); #mock userenv doesn't let us set null branch
462     is( $item->renewalbranch({ branch => "PANDA"}), "PANDA", "If interface not opac, we get the branch we pass one in if context not set");
463
464     C4::Context->interface( 'opac' );
465
466     t::lib::Mocks::mock_preference('OpacRenewalBranch', undef);
467     is( $item->renewalbranch, 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew");
468     is( $item->renewalbranch({branch=>'COW'}), 'OPACRenew', "If interface opac and OpacRenewalBranch undef, we get OPACRenew even if branch passed");
469
470     t::lib::Mocks::mock_preference('OpacRenewalBranch', 'none');
471     is( $item->renewalbranch, '', "If interface opac and OpacRenewalBranch is none, we get blank string");
472     is( $item->renewalbranch({branch=>'COW'}), '', "If interface opac and OpacRenewalBranch is none, we get blank string even if branch passed");
473
474     t::lib::Mocks::mock_preference('OpacRenewalBranch', 'checkoutbranch');
475     is( $item->renewalbranch, $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout");
476     is( $item->renewalbranch({branch=>'MONKEY'}), $checkout->branchcode, "If interface opac and OpacRenewalBranch set to checkoutbranch, we get branch of checkout even if branch passed");
477
478     t::lib::Mocks::mock_preference('OpacRenewalBranch','patronhomebranch');
479     is( $item->renewalbranch, $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron");
480     is( $item->renewalbranch({branch=>'TURKEY'}), $checkout->patron->branchcode, "If interface opac and OpacRenewalBranch set to patronbranch, we get branch of patron even if branch passed");
481
482     t::lib::Mocks::mock_preference('OpacRenewalBranch','itemhomebranch');
483     is( $item->renewalbranch, $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item");
484     is( $item->renewalbranch({branch=>'MANATEE'}), $item->homebranch, "If interface opac and OpacRenewalBranch set to itemhomebranch, we get homebranch of item even if branch passed");
485
486 };