3 # Copyright 2015 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 13;
29 use Koha::Item::Transfer::Limits;
34 use Koha::CirculationRules;
37 use t::lib::TestBuilder;
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
42 # Cleanup default_branch_item_rules
43 my $dbh = C4::Context->dbh;
44 $dbh->do('DELETE FROM circulation_rules');
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',
54 my $new_library_2 = Koha::Library->new({
55 branchcode => 'my_bc_2',
56 branchname => 'my_branchname_2',
57 branchnotes => 'my_branchnotes_2',
60 is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
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' );
65 $retrieved_library_1->delete;
66 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
68 # Stockrotation relationship testing
70 my $new_library_sr = $builder->build({ source => 'Branch' });
73 source => 'Stockrotationstage',
74 value => { branchcode_id => $new_library_sr->{branchcode} },
77 source => 'Stockrotationstage',
78 value => { branchcode_id => $new_library_sr->{branchcode} },
81 source => 'Stockrotationstage',
82 value => { branchcode_id => $new_library_sr->{branchcode} },
85 my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
86 ->stockrotationstages;
87 is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
89 isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
91 $schema->storage->txn_rollback;
93 subtest '->get_effective_marcorgcode' => sub {
97 $schema->storage->txn_begin;
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 } });
104 t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
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');
111 t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
112 is( $library_2->get_effective_marcorgcode, 'Blah',
113 'Fallback is always MARCOrgCode syspref');
115 $library_2->marcorgcode('ThisIsACode')->store();
116 is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
117 'Pick library_2 code');
119 $schema->storage->txn_rollback;
122 subtest '->from_email_address' => sub {
126 $schema->storage->txn_begin;
128 my $library_1 = $builder->build_object(
130 class => 'Koha::Libraries',
132 branchemail => 'from@mybranc.com',
137 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'admin@mylibrary.com' );
139 is( $library_1->from_email_address, $library_1->branchemail,
140 'If defined, use branches branchemail address');
142 $library_1->branchemail(undef)->store();
143 is( $library_1->from_email_address, 'admin@mylibrary.com',
144 'Fallback to KohaAdminEmailAddress email address when branchemail is undefined');
146 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', '' );
147 is( $library_1->from_email_address, undef,
148 'Return undef when branchemail and KohaAdminEmailAddress are both undefined');
149 $schema->storage->txn_rollback;
152 subtest '->inbound_email_address' => sub {
156 $schema->storage->txn_begin;
158 my $library_1 = $builder->build_object(
160 class => 'Koha::Libraries',
162 branchemail => 'from@mybranc.com',
163 branchreplyto => 'reply@mybranch.com'
168 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'admin@mylibrary.com' );
169 t::lib::Mocks::mock_preference( 'ReplytoDefault', 'reply@mylibrary.com' );
171 is( $library_1->inbound_email_address, $library_1->branchreplyto,
172 'If defined, use branches replyto address');
174 $library_1->branchreplyto(undef)->store();
175 is( $library_1->inbound_email_address, $library_1->branchemail,
176 'Fallback to branches email address when branchreplyto is undefined');
178 $library_1->branchemail(undef)->store();
179 is( $library_1->inbound_email_address, 'reply@mylibrary.com',
180 'Fallback to ReplytoDefault email address when branchreplyto and branchemail are undefined');
182 t::lib::Mocks::mock_preference( 'ReplytoDefault', '' );
183 is( $library_1->inbound_email_address, 'admin@mylibrary.com',
184 'Fallback to KohaAdminEmailAddress email address when branchreplyto, branchemail and ReplytoDefault are undefined');
186 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', '' );
187 is( $library_1->inbound_email_address, undef,
188 'Return undef when email address when branchreplyto, branchemail, ReplytoDefault and KohaAdminEmailAddress are undefined');
189 $schema->storage->txn_rollback;
192 subtest '->inbound_ill_address' => sub {
196 $schema->storage->txn_begin;
198 my $library_1 = $builder->build_object(
200 class => 'Koha::Libraries',
202 branchemail => 'from@mylibrary.com',
203 branchreplyto => 'reply@mylibrary.com',
204 branchillemail => 'ill@mylibrary.com'
209 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'admin@mylibrary.com' );
210 t::lib::Mocks::mock_preference( 'ReplytoDefault', 'reply@mylibrary.com' );
211 t::lib::Mocks::mock_preference( 'ILLDefaultStaffEmail', 'illdefault@mylibrary.com' );
213 is( $library_1->inbound_ill_address, $library_1->branchillemail,
214 'If defined, use library branchillemail address');
216 $library_1->branchillemail(undef)->store();
217 is( $library_1->inbound_ill_address, 'illdefault@mylibrary.com',
218 'Fallback to ILLDefaultStaffEmail preference when branchillemail is undefined');
220 t::lib::Mocks::mock_preference( 'ILLDefaultStaffEmail', undef );
221 is( $library_1->inbound_ill_address, $library_1->branchreplyto,
222 'Fallback to library replyto address when ILLDefaultStaffEmail is undefined');
224 $library_1->branchreplyto(undef)->store();
225 is( $library_1->inbound_ill_address, $library_1->branchemail,
226 'Fallback to branches email address when branchreplyto is undefined');
228 $library_1->branchemail(undef)->store();
229 is( $library_1->inbound_ill_address, 'reply@mylibrary.com',
230 'Fallback to ReplytoDefault email address when branchreplyto and branchemail are undefined');
232 t::lib::Mocks::mock_preference( 'ReplytoDefault', '' );
233 is( $library_1->inbound_ill_address, 'admin@mylibrary.com',
234 'Fallback to KohaAdminEmailAddress email address when branchreplyto, branchemail and ReplytoDefault are undefined');
236 t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', '' );
237 is( $library_1->inbound_ill_address, undef,
238 'Return undef when email address when branchreplyto, branchemail, ReplytoDefault and KohaAdminEmailAddress are undefined');
240 $schema->storage->txn_rollback;
243 subtest 'cash_registers' => sub {
246 $schema->storage->txn_begin;
248 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
249 my $register1 = $builder->build_object(
251 class => 'Koha::Cash::Registers',
252 value => { branch => $library->branchcode },
255 my $register2 = $builder->build_object(
257 class => 'Koha::Cash::Registers',
258 value => { branch => $library->branchcode },
262 my $registers = $library->cash_registers;
263 is( ref($registers), 'Koha::Cash::Registers',
264 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
266 is( $registers->count, 2,
267 'Koha::Library->cash_registers should return the correct cash registers'
271 is( $library->cash_registers->next->id, $register2->id,
272 'Koha::Library->cash_registers should return the correct cash registers'
275 $schema->storage->txn_rollback;
278 subtest 'get_hold_libraries and validate_hold_sibling' => sub {
282 $schema->storage->txn_begin;
284 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
285 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
286 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
287 my $library4 = $builder->build_object( { class => 'Koha::Libraries' } );
288 my $library5 = $builder->build_object( { class => 'Koha::Libraries' } );
290 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
291 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
293 $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } } );
294 $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } } );
296 $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } } );
297 $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } } );
298 $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library5->branchcode } } );
300 my @hold_libraries_1 = ($library1, $library2);
301 my @hold_libraries_2 = ($library3, $library4, $library5);
303 my @result = $library1->get_hold_libraries()->as_list;
304 # library1 and library2 are siblings
305 is(scalar(@result), 2, 'get_hold_libraries returns 2 libraries');
307 my %map = map {$_->branchcode, 1} @result;
309 foreach my $hold_library ( @hold_libraries_1 ) {
310 ok(exists $map{$hold_library->branchcode}, 'library in hold group');
313 @result = $library3->get_hold_libraries()->as_list;
314 # library3, library4 and library5 are siblings
315 is(scalar(@result), 3, 'get_hold_libraries returns 3 libraries');
317 %map = map {$_->branchcode, 1} @result;
319 foreach my $hold_library ( @hold_libraries_2 ) {
320 ok(exists $map{$hold_library->branchcode}, 'library in hold group');
323 ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
324 ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
326 ok($library3->validate_hold_sibling( { branchcode => $library4->branchcode } ), 'Library 4 is a valid hold sibling');
327 ok($library3->validate_hold_sibling( { branchcode => $library5->branchcode } ), 'Library 5 is a valid hold sibling');
328 ok(!$library3->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is not a valid hold sibling');
330 $schema->storage->txn_rollback;
334 subtest 'get_float_libraries and validate_float_sibling' => sub {
338 $schema->storage->txn_begin;
340 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
341 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
342 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
343 my $library4 = $builder->build_object( { class => 'Koha::Libraries' } );
345 my $root1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_float_group => 1 } } );
346 my $root2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_float_group => 1 } } );
349 $builder->build_object(
350 { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library1->branchcode } }
352 $builder->build_object(
353 { class => 'Koha::Library::Groups', value => { parent_id => $root1->id, branchcode => $library2->branchcode } }
357 $builder->build_object(
358 { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library3->branchcode } }
360 $builder->build_object(
361 { class => 'Koha::Library::Groups', value => { parent_id => $root2->id, branchcode => $library4->branchcode } }
364 my @libraries1 = $library1->get_float_libraries()->as_list;
365 is( scalar @libraries1, '2', '1st float group contains 2 libraries' );
367 my @libraries2 = $library3->get_float_libraries()->as_list;
368 is( scalar @libraries2, '2', '2nd float group also contains 2 libraries' );
371 $library1->validate_float_sibling( { branchcode => $library2->branchcode } ),
372 "Library1 and library2 belong in to the same float group."
375 $library3->validate_float_sibling( { branchcode => $library4->branchcode } ),
376 "Library3 and library5 belong in to the same float group."
379 $schema->storage->txn_rollback;
382 subtest 'outgoing_transfers' => sub {
385 $schema->storage->txn_begin;
387 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
388 my $transfer1 = $builder->build_object(
390 class => 'Koha::Item::Transfers',
391 value => { frombranch => $library->branchcode },
394 my $transfer2 = $builder->build_object(
396 class => 'Koha::Item::Transfers',
397 value => { frombranch => $library->branchcode },
401 my $outgoing_transfers = $library->outgoing_transfers;
402 is( ref($outgoing_transfers), 'Koha::Item::Transfers',
403 'Koha::Library->outgoing_transfers should return a set of Koha::Item::Transfers'
405 is( $outgoing_transfers->count, 2,
406 'Koha::Library->outgoing_transfers should return the correct number of transfers'
410 is( $library->outgoing_transfers->next->id, $transfer2->id,
411 'Koha::Library->outgoing_transfers should return the correct transfers'
414 $schema->storage->txn_rollback;