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;
36 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
41 # Cleanup default_branch_item_rules
42 my $dbh = C4::Context->dbh;
43 $dbh->do('DELETE FROM circulation_rules');
45 my $builder = t::lib::TestBuilder->new;
46 my $nb_of_libraries = Koha::Libraries->search->count;
47 my $new_library_1 = Koha::Library->new({
48 branchcode => 'my_bc_1',
49 branchname => 'my_branchname_1',
50 branchnotes => 'my_branchnotes_1',
51 marcorgcode => 'US-MyLib',
53 my $new_library_2 = Koha::Library->new({
54 branchcode => 'my_bc_2',
55 branchname => 'my_branchname_2',
56 branchnotes => 'my_branchnotes_2',
59 is( Koha::Libraries->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
61 my $retrieved_library_1 = Koha::Libraries->find( $new_library_1->branchcode );
62 is( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
64 $retrieved_library_1->delete;
65 is( Koha::Libraries->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
67 # Stockrotation relationship testing
69 my $new_library_sr = $builder->build({ source => 'Branch' });
72 source => 'Stockrotationstage',
73 value => { branchcode_id => $new_library_sr->{branchcode} },
76 source => 'Stockrotationstage',
77 value => { branchcode_id => $new_library_sr->{branchcode} },
80 source => 'Stockrotationstage',
81 value => { branchcode_id => $new_library_sr->{branchcode} },
84 my $srstages = Koha::Libraries->find($new_library_sr->{branchcode})
85 ->stockrotationstages;
86 is( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
88 isa_ok( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
90 subtest 'pickup_locations' => sub {
93 my $from = Koha::Library->new({
94 branchcode => 'zzzfrom',
95 branchname => 'zzzfrom',
96 branchnotes => 'zzzfrom',
98 my $to = Koha::Library->new({
99 branchcode => 'zzzto',
100 branchname => 'zzzto',
101 branchnotes => 'zzzto',
105 my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' });
106 my $itemtype = $biblio->itemtype;
108 biblionumber => $biblio->biblionumber,
109 library => $from->branchcode,
112 my $item1 = $builder->build_sample_item({%$item_info});
113 my $item2 = $builder->build_sample_item({%$item_info});
114 my $item3 = $builder->build_sample_item({%$item_info});
116 subtest 'UseBranchTransferLimits = OFF' => sub {
119 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 0);
120 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
121 t::lib::Mocks::mock_preference('item-level_itypes', 1);
122 Koha::Item::Transfer::Limits->delete;
123 Koha::Item::Transfer::Limit->new({
124 fromBranch => $from->branchcode,
125 toBranch => $to->branchcode,
126 itemtype => $biblio->itemtype,
128 my $total_pickup = Koha::Libraries->search({
131 my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
132 is(C4::Context->preference('UseBranchTransferLimits'), 0, 'Given system '
133 .'preference UseBranchTransferLimits is switched OFF,');
134 is(@{$pickup}, $total_pickup, 'Then the total number of pickup locations '
135 .'equal number of libraries with pickup_location => 1');
137 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
138 t::lib::Mocks::mock_preference('item-level_itypes', 1);
139 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
140 is(@{$pickup}, $total_pickup, '...when '
141 .'BranchTransferLimitsType = itemtype and item-level_itypes = 1');
142 t::lib::Mocks::mock_preference('item-level_itypes', 0);
143 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
144 is(@{$pickup}, $total_pickup, '...as well as when '
145 .'BranchTransferLimitsType = itemtype and item-level_itypes = 0');
146 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
147 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
148 is(@{$pickup}, $total_pickup, '...as well as when '
149 .'BranchTransferLimitsType = ccode');
150 t::lib::Mocks::mock_preference('item-level_itypes', 1);
153 subtest 'UseBranchTransferLimits = ON' => sub {
155 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
157 is(C4::Context->preference('UseBranchTransferLimits'), 1, 'Given system '
158 .'preference UseBranchTransferLimits is switched ON,');
160 subtest 'Given BranchTransferLimitsType = itemtype and '
161 .'item-level_itypes = ON' => sub {
164 t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
165 t::lib::Mocks::mock_preference('item-level_itypes', 1);
166 Koha::Item::Transfer::Limits->delete;
167 my $limit = Koha::Item::Transfer::Limit->new({
168 fromBranch => $from->branchcode,
169 toBranch => $to->branchcode,
170 itemtype => $item1->effective_itemtype,
172 ok($item1->effective_itemtype eq $item2->effective_itemtype
173 && $item1->effective_itemtype eq $item3->effective_itemtype,
174 'Given all items of a biblio have same the itemtype,');
175 is($limit->itemtype, $item1->effective_itemtype, 'and given there '
176 .'is an existing transfer limit for that itemtype,');
177 my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber});
179 foreach my $lib (@{$pickup}) {
180 if ($lib->{'branchcode'} eq $limit->toBranch) {
184 is($found, 0, 'Then the to-library of which the limit applies for, '
185 .'is not included in the list of pickup libraries.');
186 $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
188 foreach my $lib (@{$pickup}) {
189 if ($lib->{'branchcode'} eq $limit->toBranch) {
193 is($found, 0, 'The same applies when asking pickup locations of '
195 my $others = Koha::Libraries->search({
196 pickup_location => 1,
197 branchcode => { 'not in' => [$limit->toBranch] }})->count;
198 is(@{$pickup}, $others, 'However, the number of other pickup '
199 .'libraries is correct.');
200 $item2->itype('BK')->store;
201 ok($item1->effective_itemtype ne $item2->effective_itemtype,
202 'Given one of the item in this biblio has a different itemtype,');
203 is(Koha::Item::Transfer::Limits->search({
204 itemtype => $item2->effective_itemtype })->count, 0, 'and it is'
205 .' not restricted by transfer limits,');
206 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
208 foreach my $lib (@{$pickup}) {
209 if ($lib->{'branchcode'} eq $limit->toBranch) {
213 is($found, 1, 'Then the to-library of which the limit applies for, '
214 .'is included in the list of pickup libraries.');
215 $pickup = Koha::Libraries->pickup_locations({ item => $item2 });
217 foreach my $lib (@{$pickup}) {
218 if ($lib->{'branchcode'} eq $limit->toBranch) {
222 is($found, 1, 'The same applies when asking pickup locations of '
223 .'a that particular item.');
224 Koha::Item::Transfer::Limits->delete;
225 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
227 foreach my $lib (@{$pickup}) {
228 if ($lib->{'branchcode'} eq $limit->toBranch) {
232 is($found, 1, 'Given we deleted transfer limit, the previously '
233 .'transfer-limited library is included in the list.');
234 $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
236 foreach my $lib (@{$pickup}) {
237 if ($lib->{'branchcode'} eq $limit->toBranch) {
241 is($found, 1, 'The same applies when asking pickup locations of '
245 subtest 'Given BranchTransferLimitsType = itemtype and '
246 .'item-level_itypes = OFF' => sub {
249 t::lib::Mocks::mock_preference('BranchTransferLimitsType','itemtype');
250 t::lib::Mocks::mock_preference('item-level_itypes', 0);
251 $biblio->biblioitem->itemtype('BK')->store;
252 Koha::Item::Transfer::Limits->delete;
253 my $limit = Koha::Item::Transfer::Limit->new({
254 fromBranch => $from->branchcode,
255 toBranch => $to->branchcode,
256 itemtype => $item1->effective_itemtype,
259 ok($item1->effective_itemtype eq 'BK',
260 'Given items use biblio-level itemtype,');
261 is($limit->itemtype, $item1->effective_itemtype, 'and given there '
262 .'is an existing transfer limit for that itemtype,');
263 my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
265 foreach my $lib (@{$pickup}) {
266 if ($lib->{'branchcode'} eq $limit->toBranch) {
270 is($found, 0, 'Then the to-library of which the limit applies for, '
271 .'is not included in the list of pickup libraries.');
272 $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
274 foreach my $lib (@{$pickup}) {
275 if ($lib->{'branchcode'} eq $limit->toBranch) {
279 is($found, 0, 'The same applies when asking pickup locations of '
281 my $others = Koha::Libraries->search({
282 pickup_location => 1,
283 branchcode => { 'not in' => [$limit->toBranch] }})->count;
284 is(@{$pickup}, $others, 'However, the number of other pickup '
285 .'libraries is correct.');
286 Koha::Item::Transfer::Limits->delete;
287 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
289 foreach my $lib (@{$pickup}) {
290 if ($lib->{'branchcode'} eq $limit->toBranch) {
294 is($found, 1, 'Given we deleted transfer limit, the previously '
295 .'transfer-limited library is included in the list.');
296 $limit = Koha::Item::Transfer::Limit->new({
297 fromBranch => $from->branchcode,
298 toBranch => $to->branchcode,
299 itemtype => $item1->itype,
301 ok($item1->itype ne $item1->effective_itemtype
302 && $limit->itemtype eq $item1->itype, 'Given we have added a limit'
303 .' matching ITEM-level itemtype,');
304 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
306 foreach my $lib (@{$pickup}) {
307 if ($lib->{'branchcode'} eq $limit->toBranch) {
311 is($found, 1, 'Then the limited branch is still included as a pickup'
313 $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
315 foreach my $lib (@{$pickup}) {
316 if ($lib->{'branchcode'} eq $limit->toBranch) {
320 is($found, 1, 'The same applies when asking pickup locations of '
324 subtest 'Given BranchTransferLimitsType = ccode' => sub {
327 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'ccode');
328 $item1->ccode('hi')->store;
329 $item2->ccode('hi')->store;
330 $item3->ccode('hi')->store;
331 Koha::Item::Transfer::Limits->delete;
332 my $limit = Koha::Item::Transfer::Limit->new({
333 fromBranch => $from->branchcode,
334 toBranch => $to->branchcode,
335 ccode => $item1->ccode,
338 is($limit->ccode, $item1->ccode, 'Given there '
339 .'is an existing transfer limit for that ccode,');
340 my $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
342 foreach my $lib (@{$pickup}) {
343 if ($lib->{'branchcode'} eq $limit->toBranch) {
347 is($found, 0, 'Then the to-library of which the limit applies for, '
348 .'is not included in the list of pickup libraries.');
349 $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
351 foreach my $lib (@{$pickup}) {
352 if ($lib->{'branchcode'} eq $limit->toBranch) {
356 is($found, 0, 'The same applies when asking pickup locations of '
358 my $others = Koha::Libraries->search({
359 pickup_location => 1,
360 branchcode => { 'not in' => [$limit->toBranch] }})->count;
361 is(@{$pickup}, $others, 'However, the number of other pickup '
362 .'libraries is correct.');
363 $item3->ccode('yo')->store;
364 ok($item1->ccode ne $item3->ccode,
365 'Given one of the item in this biblio has a different ccode,');
366 is(Koha::Item::Transfer::Limits->search({
367 ccode => $item3->ccode })->count, 0, 'and it is'
368 .' not restricted by transfer limits,');
369 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
371 foreach my $lib (@{$pickup}) {
372 if ($lib->{'branchcode'} eq $limit->toBranch) {
376 is($found, 1, 'Then the to-library of which the limit applies for, '
377 .'is included in the list of pickup libraries.');
378 $pickup = Koha::Libraries->pickup_locations({ item => $item3 });
380 foreach my $lib (@{$pickup}) {
381 if ($lib->{'branchcode'} eq $limit->toBranch) {
385 is($found, 1, 'The same applies when asking pickup locations of '
386 .'a that particular item.');
387 Koha::Item::Transfer::Limits->delete;
388 $pickup = Koha::Libraries->pickup_locations({ biblio => $biblio->biblionumber });
390 foreach my $lib (@{$pickup}) {
391 if ($lib->{'branchcode'} eq $limit->toBranch) {
395 is($found, 1, 'Given we deleted transfer limit, the previously '
396 .'transfer-limited library is included in the list.');
397 $pickup = Koha::Libraries->pickup_locations({ item => $item1 });
399 foreach my $lib (@{$pickup}) {
400 if ($lib->{'branchcode'} eq $limit->toBranch) {
404 is($found, 1, 'The same applies when asking pickup locations of '
410 $schema->storage->txn_rollback;
412 subtest '->get_effective_marcorgcode' => sub {
416 $schema->storage->txn_begin;
418 my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
419 value => { marcorgcode => 'US-MyLib' } });
420 my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
421 value => { marcorgcode => undef } });
423 t::lib::Mocks::mock_preference('MARCOrgCode', 'US-Default');
425 is( $library_1->get_effective_marcorgcode, 'US-MyLib',
426 'If defined, use library\'s own marc org code');
427 is( $library_2->get_effective_marcorgcode, 'US-Default',
428 'If not defined library\' marc org code, use the one from system preferences');
430 t::lib::Mocks::mock_preference('MARCOrgCode', 'Blah');
431 is( $library_2->get_effective_marcorgcode, 'Blah',
432 'Fallback is always MARCOrgCode syspref');
434 $library_2->marcorgcode('ThisIsACode')->store();
435 is( $library_2->get_effective_marcorgcode, 'ThisIsACode',
436 'Pick library_2 code');
438 $schema->storage->txn_rollback;
441 subtest 'cash_registers' => sub {
444 $schema->storage->txn_begin;
446 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
447 my $register1 = $builder->build_object(
449 class => 'Koha::Cash::Registers',
450 value => { branch => $library->branchcode },
453 my $register2 = $builder->build_object(
455 class => 'Koha::Cash::Registers',
456 value => { branch => $library->branchcode },
460 my $registers = $library->cash_registers;
461 is( ref($registers), 'Koha::Cash::Registers',
462 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
464 is( $registers->count, 2,
465 'Koha::Library->cash_registers should return the correct cash registers'
469 is( $library->cash_registers->next->id, $register2->id,
470 'Koha::Library->cash_registers should return the correct cash registers'
473 $schema->storage->txn_rollback;
476 subtest 'get_hold_libraries and validate_hold_sibling' => sub {
480 $schema->storage->txn_begin;
482 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
483 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
484 my $library3 = $builder->build_object( { class => 'Koha::Libraries' } );
486 my $root = $builder->build_object( { class => 'Koha::Library::Groups', value => { ft_local_hold_group => 1 } } );
487 my $g1 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library1->branchcode } } );
488 my $g2 = $builder->build_object( { class => 'Koha::Library::Groups', value => { parent_id => $root->id, branchcode => $library2->branchcode } } );
490 my @hold_libraries = ($library1, $library2);
492 my @result = $library1->get_hold_libraries();
494 ok(scalar(@result) == 2, 'get_hold_libraries returns 2 libraries');
496 my %map = map {$_->branchcode, 1} @result;
498 foreach my $hold_library ( @hold_libraries ) {
499 ok(exists $map{$hold_library->branchcode}, 'library in hold group');
502 ok($library1->validate_hold_sibling( { branchcode => $library2->branchcode } ), 'Library 2 is a valid hold sibling');
503 ok(!$library1->validate_hold_sibling( { branchcode => $library3->branchcode } ), 'Library 3 is not a valid hold sibling');
505 $schema->storage->txn_rollback;