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