Bug 23051: (QA follow-up) Missing curly and tabs and fix test
[koha.git] / t / db_dependent / Koha / Libraries.t
blob7c3cd78efffba0f4c051972132dbeed0b4f1ad60
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use Test::More tests => 9;
24 use C4::Biblio;
25 use C4::Context;
26 use C4::Items;
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;
36 use t::lib::Mocks;
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',
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;
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' });
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 });
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 subtest 'pickup_locations' => sub {
92 plan tests => 2;
94 Koha::CirculationRules->set_rules(
96 branchcode => undef,
97 itemtype => undef,
98 rules => {
99 holdallowed => 2,
100 hold_fulfillment_policy => 'any',
101 returnbranch => 'any'
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;
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
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 } } );
130 subtest 'UseBranchTransferLimits = OFF' => sub {
131 plan tests => 5;
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');
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);
167 subtest 'UseBranchTransferLimits = ON' => sub {
168 plan tests => 4;
169 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
171 is(C4::Context->preference('UseBranchTransferLimits'), 1, 'Given system '
172 .'preference UseBranchTransferLimits is switched ON,');
174 subtest 'Given BranchTransferLimitsType = itemtype and '
175 .'item-level_itypes = ON' => sub {
176 plan tests => 11;
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;
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;
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;
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;
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;
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;
255 is($found, 1, 'The same applies when asking pickup locations of '
256 .'a single item.');
259 subtest 'Given BranchTransferLimitsType = itemtype and '
260 .'item-level_itypes = OFF' => sub {
261 plan tests => 9;
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;
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;
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;
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;
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;
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;
334 is($found, 1, 'The same applies when asking pickup locations of '
335 .'a single item.');
338 subtest 'Given BranchTransferLimitsType = ccode' => sub {
339 plan tests => 10;
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;
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;
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;
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;
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;
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;
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;
418 is($found, 1, 'The same applies when asking pickup locations of '
419 .'a single item.');
424 $schema->storage->txn_rollback;
426 subtest '->get_effective_marcorgcode' => sub {
428 plan tests => 4;
430 $schema->storage->txn_begin;
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 } });
437 t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
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');
444 t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
445 is( $library_2->get_effective_marcorgcode, 'Blah',
446 'Fallback is always MARCOrgCode syspref');
448 $library_2->marcorgcode('ThisIsACode')->store();
449 is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
450 'Pick library_2 code');
452 $schema->storage->txn_rollback;
455 subtest 'cash_registers' => sub {
456 plan tests => 3;
458 $schema->storage->txn_begin;
460 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
461 my $register1 = $builder->build_object(
463 class => 'Koha::Cash::Registers',
464 value => { branch => $library->branchcode },
467 my $register2 = $builder->build_object(
469 class => 'Koha::Cash::Registers',
470 value => { branch => $library->branchcode },
474 my $registers = $library->cash_registers;
475 is( ref($registers), 'Koha::Cash::Registers',
476 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
478 is( $registers->count, 2,
479 'Koha::Library->cash_registers should return the correct cash registers'
482 $register1->delete;
483 is( $library->cash_registers->next->id, $register2->id,
484 'Koha::Library->cash_registers should return the correct cash registers'
487 $schema->storage->txn_rollback;
490 subtest 'get_hold_libraries and validate_hold_sibling' => sub {
492 plan tests => 5;
494 $schema->storage->txn_begin;
496 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
497 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
498 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
500 my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
501 my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
502 my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
504 my @hold_libraries = ($library1, $library2);
506 my @result = $library1->get_hold_libraries();
508 ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
510 my %map = map {$_->branchcode, 1} @result;
512 foreach my $hold_library ( @hold_libraries ) {
513 ok(exists $map{$hold_library->branchcode}, 'library in hold group');
516 ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
517 ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
519 $schema->storage->txn_rollback;