Bug 22569: Add Unit Tests
[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 => 11;
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 $schema->storage->txn_rollback;
92
93 subtest '->get_effective_marcorgcode' => sub {
94
95     plan tests => 4;
96
97     $schema->storage->txn_begin;
98
99     my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
100                                              value => { marcorgcode => 'US-MyLib' } });
101     my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
102                                              value => { marcorgcode => undef } });
103
104     t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
105
106     is( $library_1->get_effective_marcorgcode, 'US-MyLib',
107        'If defined, use library\'s own marc org code');
108     is( $library_2->get_effective_marcorgcode, 'US-Default',
109        'If not defined library\' marc org code, use the one from system preferences');
110
111     t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
112     is( $library_2->get_effective_marcorgcode, 'Blah',
113        'Fallback is always MARCOrgCode syspref');
114
115     $library_2->marcorgcode('ThisIsACode')->store();
116     is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
117        'Pick library_2 code');
118
119     $schema->storage->txn_rollback;
120 };
121
122 subtest '->inbound_email_address' => sub {
123
124     plan tests => 5;
125
126     $schema->storage->txn_begin;
127
128     my $library_1 = $builder->build_object(
129         {
130             class => 'Koha::Libraries',
131             value => {
132                 branchemail   => 'from@mybranc.com',
133                 branchreplyto => 'reply@mybranch.com'
134             }
135         }
136     );
137
138     t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'admin@mylibrary.com' );
139     t::lib::Mocks::mock_preference( 'ReplytoDefault', 'reply@mylibrary.com' );
140
141     is( $library_1->inbound_email_address, $library_1->branchreplyto,
142        'If defined, use branches replyto address');
143
144     $library_1->branchreplyto(undef)->store();
145     is( $library_1->inbound_email_address, $library_1->branchemail,
146        'Fallback to branches email address when branchreplyto is undefined');
147
148     $library_1->branchemail(undef)->store();
149     is( $library_1->inbound_email_address, 'reply@mylibrary.com',
150        'Fallback to ReplytoDefault email address when branchreplyto and branchemail are undefined');
151
152     t::lib::Mocks::mock_preference( 'ReplytoDefault', '' );
153     is( $library_1->inbound_email_address, 'admin@mylibrary.com',
154        'Fallback to KohaAdminEmailAddress email address when branchreplyto, branchemail and ReplytoDefault are undefined');
155
156     t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', '' );
157     is( $library_1->inbound_email_address, undef,
158        'Return undef when  email address when branchreplyto, branchemail, ReplytoDefault and KohaAdminEmailAddress are undefined');
159     $schema->storage->txn_rollback;
160 };
161
162 subtest '->inbound_ill_address' => sub {
163
164     plan tests => 7;
165
166     $schema->storage->txn_begin;
167
168     my $library_1 = $builder->build_object(
169         {
170             class => 'Koha::Libraries',
171             value => {
172                 branchemail   => 'from@mylibrary.com',
173                 branchreplyto => 'reply@mylibrary.com',
174                 branchillemail => 'ill@mylibrary.com'
175             }
176         }
177     );
178
179     t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'admin@mylibrary.com' );
180     t::lib::Mocks::mock_preference( 'ReplytoDefault', 'reply@mylibrary.com' );
181     t::lib::Mocks::mock_preference( 'ILLDefaultStaffEmail', 'illdefault@mylibrary.com' );
182
183     is( $library_1->inbound_ill_address, $library_1->branchillemail,
184        'If defined, use library branchillemail address');
185
186     $library_1->branchillemail(undef)->store();
187     is( $library_1->inbound_ill_address, 'illdefault@mylibrary.com',
188        'Fallback to ILLDefaultStaffEmail preference when branchillemail is undefined');
189
190     t::lib::Mocks::mock_preference( 'ILLDefaultStaffEmail', undef );
191     is( $library_1->inbound_ill_address, $library_1->branchreplyto,
192        'Fallback to library replyto address when ILLDefaultStaffEmail is undefined');
193
194     $library_1->branchreplyto(undef)->store();
195     is( $library_1->inbound_ill_address, $library_1->branchemail,
196        'Fallback to branches email address when branchreplyto is undefined');
197
198     $library_1->branchemail(undef)->store();
199     is( $library_1->inbound_ill_address, 'reply@mylibrary.com',
200        'Fallback to ReplytoDefault email address when branchreplyto and branchemail are undefined');
201
202     t::lib::Mocks::mock_preference( 'ReplytoDefault', '' );
203     is( $library_1->inbound_ill_address, 'admin@mylibrary.com',
204        'Fallback to KohaAdminEmailAddress email address when branchreplyto, branchemail and ReplytoDefault are undefined');
205
206     t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', '' );
207     is( $library_1->inbound_ill_address, undef,
208        'Return undef when  email address when branchreplyto, branchemail, ReplytoDefault and KohaAdminEmailAddress are undefined');
209
210     $schema->storage->txn_rollback;
211 };
212
213 subtest 'cash_registers' => sub {
214     plan tests => 3;
215
216     $schema->storage->txn_begin;
217
218     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
219     my $register1 = $builder->build_object(
220         {
221             class => 'Koha::Cash::Registers',
222             value  => { branch => $library->branchcode },
223         }
224     );
225     my $register2 = $builder->build_object(
226         {
227             class => 'Koha::Cash::Registers',
228             value  => { branch => $library->branchcode },
229         }
230     );
231
232     my $registers = $library->cash_registers;
233     is( ref($registers), 'Koha::Cash::Registers',
234 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
235     );
236     is( $registers->count, 2,
237         'Koha::Library->cash_registers should return the correct cash registers'
238     );
239
240     $register1->delete;
241     is( $library->cash_registers->next->id, $register2->id,
242         'Koha::Library->cash_registers should return the correct cash registers'
243     );
244
245     $schema->storage->txn_rollback;
246 };
247
248 subtest 'get_hold_libraries and validate_hold_sibling' => sub {
249
250     plan tests => 12;
251
252     $schema->storage->txn_begin;
253
254     my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
255     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
256     my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
257     my $library4 = $builder->build_object( { class => 'Koha::Libraries' } );
258     my $library5 = $builder->build_object( { class => 'Koha::Libraries' } );
259
260     my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
261     my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
262     # G1
263     $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
264     $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
265     # G2
266     $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
267     $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
268     $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library5->branchcode } } );
269
270     my @hold_libraries_1 = ($library1, $library2);
271     my @hold_libraries_2 = ($library3, $library4, $library5);
272
273     my @result = $library1->get_hold_libraries();
274     # library1 and library2 are siblings
275     is(scalar(@result), 2, 'get_hold_libraries returns 2 libraries');
276
277     my %map = map {$_->branchcode, 1} @result;
278
279     foreach my $hold_library ( @hold_libraries_1 ) {
280         ok(exists $map{$hold_library->branchcode}, 'library in hold group');
281     }
282
283     @result = $library3->get_hold_libraries();
284     # library3, library4 and library5 are siblings
285     is(scalar(@result), 3, 'get_hold_libraries returns 3 libraries');
286
287     %map = map {$_->branchcode, 1} @result;
288
289     foreach my $hold_library ( @hold_libraries_2 ) {
290         ok(exists $map{$hold_library->branchcode}, 'library in hold group');
291     }
292
293     ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
294     ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
295
296     ok($library3->validate_hold_sibling( { branchcode => $library4->branchcode } ), 'Library 4 is a valid hold sibling');
297     ok($library3->validate_hold_sibling( { branchcode => $library5->branchcode } ), 'Library 5 is a valid hold sibling');
298     ok(!$library3->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is not a valid hold sibling');
299
300     $schema->storage->txn_rollback;
301
302 };
303
304 subtest 'outgoing_transfers' => sub {
305     plan tests => 3;
306
307     $schema->storage->txn_begin;
308
309     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
310     my $transfer1 = $builder->build_object(
311         {
312             class => 'Koha::Item::Transfers',
313             value  => { frombranch => $library->branchcode },
314         }
315     );
316     my $transfer2 = $builder->build_object(
317         {
318             class => 'Koha::Item::Transfers',
319             value  => { frombranch => $library->branchcode },
320         }
321     );
322
323     my $outgoing_transfers = $library->outgoing_transfers;
324     is( ref($outgoing_transfers), 'Koha::Item::Transfers',
325 'Koha::Library->outgoing_transfers should return a set of Koha::Item::Transfers'
326     );
327     is( $outgoing_transfers->count, 2,
328         'Koha::Library->outgoing_transfers should return the correct number of transfers'
329     );
330
331     $transfer1->delete;
332     is( $library->outgoing_transfers->next->id, $transfer2->id,
333         'Koha::Library->outgoing_transfers should return the correct transfers'
334     );
335
336     $schema->storage->txn_rollback;
337 };