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
=> 9;
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 subtest
'pickup_locations' => sub {
94 Koha
::CirculationRules
->set_rules(
100 hold_fulfillment_policy
=> 'any',
101 returnbranch
=> 'any'
106 my $from = Koha
::Library
->new({
107 branchcode
=> 'zzzfrom',
108 branchname
=> 'zzzfrom',
109 branchnotes
=> 'zzzfrom',
111 my $to = Koha
::Library
->new({
112 branchcode
=> 'zzzto',
113 branchname
=> 'zzzto',
114 branchnotes
=> 'zzzto',
118 my $biblio = $builder->build_sample_biblio({ itemtype
=> 'DUMMY' });
119 my $itemtype = $biblio->itemtype;
121 biblionumber
=> $biblio->biblionumber,
122 library
=> $from->branchcode,
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 {
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,
142 my $total_pickup = Koha
::Libraries
->search({
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 {
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 {
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,
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 });
193 foreach my $lib (@
{$pickup}) {
194 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
202 foreach my $lib (@
{$pickup}) {
203 if ($lib->{branchcode
} eq $limit->toBranch) {
207 is
($found, 0, 'The same applies when asking pickup locations of '
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 });
222 foreach my $lib (@
{$pickup}) {
223 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
231 foreach my $lib (@
{$pickup}) {
232 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
241 foreach my $lib (@
{$pickup}) {
242 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
250 foreach my $lib (@
{$pickup}) {
251 if ($lib->{branchcode
} eq $limit->toBranch) {
255 is
($found, 1, 'The same applies when asking pickup locations of '
259 subtest
'Given BranchTransferLimitsType = itemtype and '
260 .'item-level_itypes = OFF' => sub {
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,
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 });
279 foreach my $lib (@
{$pickup}) {
280 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
288 foreach my $lib (@
{$pickup}) {
289 if ($lib->{branchcode
} eq $limit->toBranch) {
293 is
($found, 0, 'The same applies when asking pickup locations of '
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 });
303 foreach my $lib (@
{$pickup}) {
304 if ($lib->{branchcode
} eq $limit->toBranch) {
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,
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 });
320 foreach my $lib (@
{$pickup}) {
321 if ($lib->{branchcode
} eq $limit->toBranch) {
325 is
($found, 1, 'Then the limited branch is still included as a pickup'
327 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1, patron
=> $patron1 });
329 foreach my $lib (@
{$pickup}) {
330 if ($lib->{branchcode
} eq $limit->toBranch) {
334 is
($found, 1, 'The same applies when asking pickup locations of '
338 subtest
'Given BranchTransferLimitsType = ccode' => sub {
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,
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 });
356 foreach my $lib (@
{$pickup}) {
357 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
365 foreach my $lib (@
{$pickup}) {
366 if ($lib->{branchcode
} eq $limit->toBranch) {
370 is
($found, 0, 'The same applies when asking pickup locations of '
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 });
385 foreach my $lib (@
{$pickup}) {
386 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
394 foreach my $lib (@
{$pickup}) {
395 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
404 foreach my $lib (@
{$pickup}) {
405 if ($lib->{branchcode
} eq $limit->toBranch) {
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 });
413 foreach my $lib (@
{$pickup}) {
414 if ($lib->{branchcode
} eq $limit->toBranch) {
418 is
($found, 1, 'The same applies when asking pickup locations of '
424 $schema->storage->txn_rollback;
426 subtest
'->get_effective_marcorgcode' => sub {
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 {
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'
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 {
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;