Bug 22284: (follow-up) Squash multiple follow-ups
[koha.git] / t / db_dependent / Koha / Libraries.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 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 => 9;
23
24 use C4::Biblio;
25 use C4::Context;
26 use C4::Items;
27
28 use Koha::Biblios;
29 use Koha::Item::Transfer::Limits;
30 use Koha::Items;
31 use Koha::Library;
32 use Koha::Libraries;
33 use Koha::Database;
34
35 use t::lib::Mocks;
36 use t::lib::TestBuilder;
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40
41 # Cleanup default_branch_item_rules
42 my $dbh     = C4::Context->dbh;
43 $dbh->do('DELETE FROM circulation_rules');
44
45 my $builder = t::lib::TestBuilder->new;
46 my $nb_of_libraries = Koha::Libraries->search->count;
47 my $new_library_1 = Koha::Library->new({
48     branchcode => 'my_bc_1',
49     branchname => 'my_branchname_1',
50     branchnotes => 'my_branchnotes_1',
51     marcorgcode => 'US-MyLib',
52 })->store;
53 my $new_library_2 = Koha::Library->new({
54     branchcode => 'my_bc_2',
55     branchname => 'my_branchname_2',
56     branchnotes => 'my_branchnotes_2',
57 })->store;
58
59 is( Koha::Libraries->search->count,         $nb_of_libraries + 2,  'The 2 libraries should have been added' );
60
61 my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
62 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
63
64 $retrieved_library_1->delete;
65 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
66
67 # Stockrotation relationship testing
68
69 my $new_library_sr = $builder->build({ source => 'Branch' });
70
71 $builder->build({
72     source => 'Stockrotationstage',
73     value  => { branchcode_id => $new_library_sr->{branchcode} },
74 });
75 $builder->build({
76     source => 'Stockrotationstage',
77     value  => { branchcode_id => $new_library_sr->{branchcode} },
78 });
79 $builder->build({
80     source => 'Stockrotationstage',
81     value  => { branchcode_id => $new_library_sr->{branchcode} },
82 });
83
84 my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
85     ->stockrotationstages;
86 is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
87
88 isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
89
90 subtest 'pickup_locations' => sub {
91     plan tests => 2;
92
93     my $from = Koha::Library->new({
94         branchcode => 'zzzfrom',
95         branchname => 'zzzfrom',
96         branchnotes => 'zzzfrom',
97     })->store;
98     my $to = Koha::Library->new({
99         branchcode => 'zzzto',
100         branchname => 'zzzto',
101         branchnotes => 'zzzto',
102     })->store;
103
104
105     my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
106     my $itemtype = $biblio->itemtype;
107     my $item_info = {
108         biblionumber => $biblio->biblionumber,
109         library      => $from->branchcode,
110         itype        => $itemtype
111     };
112     my $item1 = $builder->build_sample_item({%$item_info});
113     my $item2 = $builder->build_sample_item({%$item_info});
114     my $item3 = $builder->build_sample_item({%$item_info});
115
116     subtest 'UseBranchTransferLimits = OFF' => sub {
117         plan tests => 5;
118
119         t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
120         t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
121         t::lib::Mocks::mock_preference('item-level_itypes', 1);
122         Koha::Item::Transfer::Limits->delete;
123         Koha::Item::Transfer::Limit->new({
124             fromBranch => $from->branchcode,
125             toBranch => $to->branchcode,
126             itemtype => $biblio->itemtype,
127         })->store;
128         my $total_pickup = Koha::Libraries->search({
129             pickup_location => 1
130         })->count;
131         my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
132         is(C4::Context->preference('UseBranchTransferLimits'), 0, 'Given system '
133            .'preference UseBranchTransferLimits is switched OFF,');
134         is(@{$pickup}, $total_pickup, 'Then the total number of pickup locations '
135            .'equal number of libraries with pickup_location => 1');
136
137         t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
138         t::lib::Mocks::mock_preference('item-level_itypes', 1);
139         $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
140         is(@{$pickup}, $total_pickup, '...when '
141            .'BranchTransferLimitsType = itemtype and item-level_itypes = 1');
142         t::lib::Mocks::mock_preference('item-level_itypes', 0);
143         $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
144         is(@{$pickup}, $total_pickup, '...as well as when '
145            .'BranchTransferLimitsType = itemtype and item-level_itypes = 0');
146         t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
147         $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
148         is(@{$pickup}, $total_pickup, '...as well as when '
149            .'BranchTransferLimitsType = ccode');
150         t::lib::Mocks::mock_preference('item-level_itypes', 1);
151     };
152
153     subtest 'UseBranchTransferLimits = ON' => sub {
154         plan tests => 4;
155         t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
156
157         is(C4::Context->preference('UseBranchTransferLimits'), 1, 'Given system '
158            .'preference UseBranchTransferLimits is switched ON,');
159
160         subtest 'Given BranchTransferLimitsType = itemtype and '
161                .'item-level_itypes = ON' => sub {
162             plan tests => 11;
163
164             t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
165             t::lib::Mocks::mock_preference('item-level_itypes', 1);
166             Koha::Item::Transfer::Limits->delete;
167             my $limit = Koha::Item::Transfer::Limit->new({
168                 fromBranch => $from->branchcode,
169                 toBranch => $to->branchcode,
170                 itemtype => $item1->effective_itemtype,
171             })->store;
172             ok($item1->effective_itemtype eq $item2->effective_itemtype
173                && $item1->effective_itemtype eq $item3->effective_itemtype,
174                'Given all items of a biblio have same the itemtype,');
175             is($limit->itemtype, $item1->effective_itemtype, 'and given there '
176                .'is an existing transfer limit for that itemtype,');
177             my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber});
178             my $found = 0;
179             foreach my $lib (@{$pickup}) {
180                 if ($lib->{'branchcode'} eq $limit->toBranch) {
181                     $found = 1;
182                 }
183             }
184             is($found, 0, 'Then the to-library of which the limit applies for, '
185                .'is not included in the list of pickup libraries.');
186             $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
187             $found = 0;
188             foreach my $lib (@{$pickup}) {
189                 if ($lib->{'branchcode'} eq $limit->toBranch) {
190                     $found = 1;
191                 }
192             }
193             is($found, 0, 'The same applies when asking pickup locations of '
194                .'a single item.');
195             my $others = Koha::Libraries->search({
196                 pickup_location => 1,
197                 branchcode => { 'not in' => [$limit->toBranch] }})->count;
198             is(@{$pickup}, $others, 'However, the number of other pickup '
199                .'libraries is correct.');
200             $item2->itype('BK')->store;
201             ok($item1->effective_itemtype ne $item2->effective_itemtype,
202                'Given one of the item in this biblio has a different itemtype,');
203             is(Koha::Item::Transfer::Limits->search({
204                 itemtype => $item2->effective_itemtype })->count, 0, 'and it is'
205                .' not restricted by transfer limits,');
206             $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
207             $found = 0;
208             foreach my $lib (@{$pickup}) {
209                 if ($lib->{'branchcode'} eq $limit->toBranch) {
210                     $found = 1;
211                 }
212             }
213             is($found, 1, 'Then the to-library of which the limit applies for, '
214                .'is included in the list of pickup libraries.');
215             $pickup = Koha::Libraries->pickup_locations({ item => $item2 });
216             $found = 0;
217             foreach my $lib (@{$pickup}) {
218                 if ($lib->{'branchcode'} eq $limit->toBranch) {
219                     $found = 1;
220                 }
221             }
222             is($found, 1, 'The same applies when asking pickup locations of '
223                .'a that particular item.');
224             Koha::Item::Transfer::Limits->delete;
225             $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
226             $found = 0;
227             foreach my $lib (@{$pickup}) {
228                 if ($lib->{'branchcode'} eq $limit->toBranch) {
229                     $found = 1;
230                 }
231             }
232             is($found, 1, 'Given we deleted transfer limit, the previously '
233                .'transfer-limited library is included in the list.');
234             $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
235             $found = 0;
236             foreach my $lib (@{$pickup}) {
237                 if ($lib->{'branchcode'} eq $limit->toBranch) {
238                     $found = 1;
239                 }
240             }
241             is($found, 1, 'The same applies when asking pickup locations of '
242                .'a single item.');
243         };
244
245         subtest 'Given BranchTransferLimitsType = itemtype and '
246                .'item-level_itypes = OFF' => sub {
247             plan tests => 9;
248
249             t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
250             t::lib::Mocks::mock_preference('item-level_itypes', 0);
251             $biblio->biblioitem->itemtype('BK')->store;
252             Koha::Item::Transfer::Limits->delete;
253             my $limit = Koha::Item::Transfer::Limit->new({
254                 fromBranch => $from->branchcode,
255                 toBranch => $to->branchcode,
256                 itemtype => $item1->effective_itemtype,
257             })->store;
258
259             ok($item1->effective_itemtype eq 'BK',
260                'Given items use biblio-level itemtype,');
261             is($limit->itemtype, $item1->effective_itemtype, 'and given there '
262                .'is an existing transfer limit for that itemtype,');
263             my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
264             my $found = 0;
265             foreach my $lib (@{$pickup}) {
266                 if ($lib->{'branchcode'} eq $limit->toBranch) {
267                     $found = 1;
268                 }
269             }
270             is($found, 0, 'Then the to-library of which the limit applies for, '
271                .'is not included in the list of pickup libraries.');
272             $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
273             $found = 0;
274             foreach my $lib (@{$pickup}) {
275                 if ($lib->{'branchcode'} eq $limit->toBranch) {
276                     $found = 1;
277                 }
278             }
279             is($found, 0, 'The same applies when asking pickup locations of '
280                .'a single item.');
281             my $others = Koha::Libraries->search({
282                 pickup_location => 1,
283                 branchcode => { 'not in' => [$limit->toBranch] }})->count;
284             is(@{$pickup}, $others, 'However, the number of other pickup '
285                .'libraries is correct.');
286             Koha::Item::Transfer::Limits->delete;
287             $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
288             $found = 0;
289             foreach my $lib (@{$pickup}) {
290                 if ($lib->{'branchcode'} eq $limit->toBranch) {
291                     $found = 1;
292                 }
293             }
294             is($found, 1, 'Given we deleted transfer limit, the previously '
295                .'transfer-limited library is included in the list.');
296             $limit = Koha::Item::Transfer::Limit->new({
297                 fromBranch => $from->branchcode,
298                 toBranch => $to->branchcode,
299                 itemtype => $item1->itype,
300             })->store;
301             ok($item1->itype ne $item1->effective_itemtype
302                && $limit->itemtype eq $item1->itype, 'Given we have added a limit'
303                .' matching ITEM-level itemtype,');
304             $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
305             $found = 0;
306             foreach my $lib (@{$pickup}) {
307                 if ($lib->{'branchcode'} eq $limit->toBranch) {
308                     $found = 1;
309                 }
310             }
311             is($found, 1, 'Then the limited branch is still included as a pickup'
312                .' library.');
313             $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
314             $found = 0;
315             foreach my $lib (@{$pickup}) {
316                 if ($lib->{'branchcode'} eq $limit->toBranch) {
317                     $found = 1;
318                 }
319             }
320             is($found, 1, 'The same applies when asking pickup locations of '
321                .'a single item.');
322         };
323
324         subtest 'Given BranchTransferLimitsType = ccode' => sub {
325             plan tests => 10;
326
327             t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
328             $item1->ccode('hi')->store;
329             $item2->ccode('hi')->store;
330             $item3->ccode('hi')->store;
331             Koha::Item::Transfer::Limits->delete;
332             my $limit = Koha::Item::Transfer::Limit->new({
333                 fromBranch => $from->branchcode,
334                 toBranch => $to->branchcode,
335                 ccode => $item1->ccode,
336             })->store;
337
338             is($limit->ccode, $item1->ccode, 'Given there '
339                .'is an existing transfer limit for that ccode,');
340             my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
341             my $found = 0;
342             foreach my $lib (@{$pickup}) {
343                 if ($lib->{'branchcode'} eq $limit->toBranch) {
344                     $found = 1;
345                 }
346             }
347             is($found, 0, 'Then the to-library of which the limit applies for, '
348                .'is not included in the list of pickup libraries.');
349             $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
350             $found = 0;
351             foreach my $lib (@{$pickup}) {
352                 if ($lib->{'branchcode'} eq $limit->toBranch) {
353                     $found = 1;
354                 }
355             }
356             is($found, 0, 'The same applies when asking pickup locations of '
357                .'a single item.');
358             my $others = Koha::Libraries->search({
359                 pickup_location => 1,
360                 branchcode => { 'not in' => [$limit->toBranch] }})->count;
361             is(@{$pickup}, $others, 'However, the number of other pickup '
362                .'libraries is correct.');
363             $item3->ccode('yo')->store;
364             ok($item1->ccode ne $item3->ccode,
365                'Given one of the item in this biblio has a different ccode,');
366             is(Koha::Item::Transfer::Limits->search({
367                 ccode => $item3->ccode })->count, 0, 'and it is'
368                .' not restricted by transfer limits,');
369             $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
370             $found = 0;
371             foreach my $lib (@{$pickup}) {
372                 if ($lib->{'branchcode'} eq $limit->toBranch) {
373                     $found = 1;
374                 }
375             }
376             is($found, 1, 'Then the to-library of which the limit applies for, '
377                .'is included in the list of pickup libraries.');
378             $pickup = Koha::Libraries->pickup_locations({ item => $item3 });
379             $found = 0;
380             foreach my $lib (@{$pickup}) {
381                 if ($lib->{'branchcode'} eq $limit->toBranch) {
382                     $found = 1;
383                 }
384             }
385             is($found, 1, 'The same applies when asking pickup locations of '
386                .'a that particular item.');
387             Koha::Item::Transfer::Limits->delete;
388             $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
389             $found = 0;
390             foreach my $lib (@{$pickup}) {
391                 if ($lib->{'branchcode'} eq $limit->toBranch) {
392                     $found = 1;
393                 }
394             }
395             is($found, 1, 'Given we deleted transfer limit, the previously '
396                .'transfer-limited library is included in the list.');
397             $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
398             $found = 0;
399             foreach my $lib (@{$pickup}) {
400                 if ($lib->{'branchcode'} eq $limit->toBranch) {
401                     $found = 1;
402                 }
403             }
404             is($found, 1, 'The same applies when asking pickup locations of '
405                .'a single item.');
406         };
407     };
408 };
409
410 $schema->storage->txn_rollback;
411
412 subtest '->get_effective_marcorgcode' => sub {
413
414     plan tests => 4;
415
416     $schema->storage->txn_begin;
417
418     my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
419                                              value => { marcorgcode => 'US-MyLib' } });
420     my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
421                                              value => { marcorgcode => undef } });
422
423     t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
424
425     is( $library_1->get_effective_marcorgcode, 'US-MyLib',
426        'If defined, use library\'s own marc org code');
427     is( $library_2->get_effective_marcorgcode, 'US-Default',
428        'If not defined library\' marc org code, use the one from system preferences');
429
430     t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
431     is( $library_2->get_effective_marcorgcode, 'Blah',
432        'Fallback is always MARCOrgCode syspref');
433
434     $library_2->marcorgcode('ThisIsACode')->store();
435     is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
436        'Pick library_2 code');
437
438     $schema->storage->txn_rollback;
439 };
440
441 subtest 'cash_registers' => sub {
442     plan tests => 3;
443
444     $schema->storage->txn_begin;
445
446     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
447     my $register1 = $builder->build_object(
448         {
449             class => 'Koha::Cash::Registers',
450             value  => { branch => $library->branchcode },
451         }
452     );
453     my $register2 = $builder->build_object(
454         {
455             class => 'Koha::Cash::Registers',
456             value  => { branch => $library->branchcode },
457         }
458     );
459
460     my $registers = $library->cash_registers;
461     is( ref($registers), 'Koha::Cash::Registers',
462 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
463     );
464     is( $registers->count, 2,
465         'Koha::Library->cash_registers should return the correct cash registers'
466     );
467
468     $register1->delete;
469     is( $library->cash_registers->next->id, $register2->id,
470         'Koha::Library->cash_registers should return the correct cash registers'
471     );
472
473     $schema->storage->txn_rollback;
474 };
475
476 subtest 'get_hold_libraries and validate_hold_sibling' => sub {
477
478     plan tests => 5;
479
480     $schema->storage->txn_begin;
481
482     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
483     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
484     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
485
486     my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
487     my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
488     my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
489
490     my @hold_libraries = ($library1, $library2);
491
492     my @result = $library1->get_hold_libraries();
493
494     ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
495
496     my %map = map {$_->branchcode, 1} @result;
497
498     foreach my $hold_library ( @hold_libraries ) {
499         ok(exists $map{$hold_library->branchcode}, 'library in hold group');
500     }
501
502     ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
503     ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
504
505     $schema->storage->txn_rollback;
506
507 };