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