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
=> 8;
29 use Koha
::Item
::Transfer
::Limits
;
36 use t
::lib
::TestBuilder
;
38 my $schema = Koha
::Database
->new->schema;
39 $schema->storage->txn_begin;
41 my $builder = t
::lib
::TestBuilder
->new;
42 my $nb_of_libraries = Koha
::Libraries
->search->count;
43 my $new_library_1 = Koha
::Library
->new({
44 branchcode
=> 'my_bc_1',
45 branchname
=> 'my_branchname_1',
46 branchnotes
=> 'my_branchnotes_1',
47 marcorgcode
=> 'US-MyLib',
49 my $new_library_2 = Koha
::Library
->new({
50 branchcode
=> 'my_bc_2',
51 branchname
=> 'my_branchname_2',
52 branchnotes
=> 'my_branchnotes_2',
55 is
( Koha
::Libraries
->search->count, $nb_of_libraries + 2, 'The 2 libraries should have been added' );
57 my $retrieved_library_1 = Koha
::Libraries
->find( $new_library_1->branchcode );
58 is
( $retrieved_library_1->branchname, $new_library_1->branchname, 'Find a library by branchcode should return the correct library' );
60 $retrieved_library_1->delete;
61 is
( Koha
::Libraries
->search->count, $nb_of_libraries + 1, 'Delete should have deleted the library' );
63 # Stockrotation relationship testing
65 my $new_library_sr = $builder->build({ source
=> 'Branch' });
68 source
=> 'Stockrotationstage',
69 value
=> { branchcode_id
=> $new_library_sr->{branchcode
} },
72 source
=> 'Stockrotationstage',
73 value
=> { branchcode_id
=> $new_library_sr->{branchcode
} },
76 source
=> 'Stockrotationstage',
77 value
=> { branchcode_id
=> $new_library_sr->{branchcode
} },
80 my $srstages = Koha
::Libraries
->find($new_library_sr->{branchcode
})
81 ->stockrotationstages;
82 is
( $srstages->count, 3, 'Correctly fetched stockrotationstages associated with this branch');
84 isa_ok
( $srstages->next, 'Koha::StockRotationStage', "Relationship correctly creates Koha::Objects." );
86 subtest
'pickup_locations' => sub {
89 my $from = Koha
::Library
->new({
90 branchcode
=> 'zzzfrom',
91 branchname
=> 'zzzfrom',
92 branchnotes
=> 'zzzfrom',
94 my $to = Koha
::Library
->new({
95 branchcode
=> 'zzzto',
96 branchname
=> 'zzzto',
97 branchnotes
=> 'zzzto',
101 my $biblio = $builder->build_sample_biblio({ itemtype
=> 'DUMMY' });
102 my $itemtype = $biblio->itemtype;
104 biblionumber
=> $biblio->biblionumber,
105 library
=> $from->branchcode,
108 my $item1 = $builder->build_sample_item({%$item_info});
109 my $item2 = $builder->build_sample_item({%$item_info});
110 my $item3 = $builder->build_sample_item({%$item_info});
112 subtest
'UseBranchTransferLimits = OFF' => sub {
115 t
::lib
::Mocks
::mock_preference
('UseBranchTransferLimits', 0);
116 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType', 'itemtype');
117 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
118 Koha
::Item
::Transfer
::Limits
->delete;
119 Koha
::Item
::Transfer
::Limit
->new({
120 fromBranch
=> $from->branchcode,
121 toBranch
=> $to->branchcode,
122 itemtype
=> $biblio->itemtype,
124 my $total_pickup = Koha
::Libraries
->search({
127 my $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
128 is
(C4
::Context
->preference('UseBranchTransferLimits'), 0, 'Given system '
129 .'preference UseBranchTransferLimits is switched OFF,');
130 is
(@
{$pickup}, $total_pickup, 'Then the total number of pickup locations '
131 .'equal number of libraries with pickup_location => 1');
133 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType', 'itemtype');
134 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
135 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
136 is
(@
{$pickup}, $total_pickup, '...when '
137 .'BranchTransferLimitsType = itemtype and item-level_itypes = 1');
138 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0);
139 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
140 is
(@
{$pickup}, $total_pickup, '...as well as when '
141 .'BranchTransferLimitsType = itemtype and item-level_itypes = 0');
142 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType', 'ccode');
143 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
144 is
(@
{$pickup}, $total_pickup, '...as well as when '
145 .'BranchTransferLimitsType = ccode');
146 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
149 subtest
'UseBranchTransferLimits = ON' => sub {
151 t
::lib
::Mocks
::mock_preference
('UseBranchTransferLimits', 1);
153 is
(C4
::Context
->preference('UseBranchTransferLimits'), 1, 'Given system '
154 .'preference UseBranchTransferLimits is switched ON,');
156 subtest
'Given BranchTransferLimitsType = itemtype and '
157 .'item-level_itypes = ON' => sub {
160 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType','itemtype');
161 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 1);
162 Koha
::Item
::Transfer
::Limits
->delete;
163 my $limit = Koha
::Item
::Transfer
::Limit
->new({
164 fromBranch
=> $from->branchcode,
165 toBranch
=> $to->branchcode,
166 itemtype
=> $item1->effective_itemtype,
168 ok
($item1->effective_itemtype eq $item2->effective_itemtype
169 && $item1->effective_itemtype eq $item3->effective_itemtype,
170 'Given all items of a biblio have same the itemtype,');
171 is
($limit->itemtype, $item1->effective_itemtype, 'and given there '
172 .'is an existing transfer limit for that itemtype,');
173 my $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber});
175 foreach my $lib (@
{$pickup}) {
176 if ($lib->{'branchcode'} eq $limit->toBranch) {
180 is
($found, 0, 'Then the to-library of which the limit applies for, '
181 .'is not included in the list of pickup libraries.');
182 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1 });
184 foreach my $lib (@
{$pickup}) {
185 if ($lib->{'branchcode'} eq $limit->toBranch) {
189 is
($found, 0, 'The same applies when asking pickup locations of '
191 my $others = Koha
::Libraries
->search({
192 pickup_location
=> 1,
193 branchcode
=> { 'not in' => [$limit->toBranch] }})->count;
194 is
(@
{$pickup}, $others, 'However, the number of other pickup '
195 .'libraries is correct.');
196 $item2->itype('BK')->store;
197 ok
($item1->effective_itemtype ne $item2->effective_itemtype,
198 'Given one of the item in this biblio has a different itemtype,');
199 is
(Koha
::Item
::Transfer
::Limits
->search({
200 itemtype
=> $item2->effective_itemtype })->count, 0, 'and it is'
201 .' not restricted by transfer limits,');
202 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
204 foreach my $lib (@
{$pickup}) {
205 if ($lib->{'branchcode'} eq $limit->toBranch) {
209 is
($found, 1, 'Then the to-library of which the limit applies for, '
210 .'is included in the list of pickup libraries.');
211 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item2 });
213 foreach my $lib (@
{$pickup}) {
214 if ($lib->{'branchcode'} eq $limit->toBranch) {
218 is
($found, 1, 'The same applies when asking pickup locations of '
219 .'a that particular item.');
220 Koha
::Item
::Transfer
::Limits
->delete;
221 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
223 foreach my $lib (@
{$pickup}) {
224 if ($lib->{'branchcode'} eq $limit->toBranch) {
228 is
($found, 1, 'Given we deleted transfer limit, the previously '
229 .'transfer-limited library is included in the list.');
230 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1 });
232 foreach my $lib (@
{$pickup}) {
233 if ($lib->{'branchcode'} eq $limit->toBranch) {
237 is
($found, 1, 'The same applies when asking pickup locations of '
241 subtest
'Given BranchTransferLimitsType = itemtype and '
242 .'item-level_itypes = OFF' => sub {
245 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType','itemtype');
246 t
::lib
::Mocks
::mock_preference
('item-level_itypes', 0);
247 $biblio->biblioitem->itemtype('BK')->store;
248 Koha
::Item
::Transfer
::Limits
->delete;
249 my $limit = Koha
::Item
::Transfer
::Limit
->new({
250 fromBranch
=> $from->branchcode,
251 toBranch
=> $to->branchcode,
252 itemtype
=> $item1->effective_itemtype,
255 ok
($item1->effective_itemtype eq 'BK',
256 'Given items use biblio-level itemtype,');
257 is
($limit->itemtype, $item1->effective_itemtype, 'and given there '
258 .'is an existing transfer limit for that itemtype,');
259 my $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
261 foreach my $lib (@
{$pickup}) {
262 if ($lib->{'branchcode'} eq $limit->toBranch) {
266 is
($found, 0, 'Then the to-library of which the limit applies for, '
267 .'is not included in the list of pickup libraries.');
268 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1 });
270 foreach my $lib (@
{$pickup}) {
271 if ($lib->{'branchcode'} eq $limit->toBranch) {
275 is
($found, 0, 'The same applies when asking pickup locations of '
277 my $others = Koha
::Libraries
->search({
278 pickup_location
=> 1,
279 branchcode
=> { 'not in' => [$limit->toBranch] }})->count;
280 is
(@
{$pickup}, $others, 'However, the number of other pickup '
281 .'libraries is correct.');
282 Koha
::Item
::Transfer
::Limits
->delete;
283 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
285 foreach my $lib (@
{$pickup}) {
286 if ($lib->{'branchcode'} eq $limit->toBranch) {
290 is
($found, 1, 'Given we deleted transfer limit, the previously '
291 .'transfer-limited library is included in the list.');
292 $limit = Koha
::Item
::Transfer
::Limit
->new({
293 fromBranch
=> $from->branchcode,
294 toBranch
=> $to->branchcode,
295 itemtype
=> $item1->itype,
297 ok
($item1->itype ne $item1->effective_itemtype
298 && $limit->itemtype eq $item1->itype, 'Given we have added a limit'
299 .' matching ITEM-level itemtype,');
300 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
302 foreach my $lib (@
{$pickup}) {
303 if ($lib->{'branchcode'} eq $limit->toBranch) {
307 is
($found, 1, 'Then the limited branch is still included as a pickup'
309 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1 });
311 foreach my $lib (@
{$pickup}) {
312 if ($lib->{'branchcode'} eq $limit->toBranch) {
316 is
($found, 1, 'The same applies when asking pickup locations of '
320 subtest
'Given BranchTransferLimitsType = ccode' => sub {
323 t
::lib
::Mocks
::mock_preference
('BranchTransferLimitsType', 'ccode');
324 $item1->ccode('hi')->store;
325 $item2->ccode('hi')->store;
326 $item3->ccode('hi')->store;
327 Koha
::Item
::Transfer
::Limits
->delete;
328 my $limit = Koha
::Item
::Transfer
::Limit
->new({
329 fromBranch
=> $from->branchcode,
330 toBranch
=> $to->branchcode,
331 ccode
=> $item1->ccode,
334 is
($limit->ccode, $item1->ccode, 'Given there '
335 .'is an existing transfer limit for that ccode,');
336 my $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
338 foreach my $lib (@
{$pickup}) {
339 if ($lib->{'branchcode'} eq $limit->toBranch) {
343 is
($found, 0, 'Then the to-library of which the limit applies for, '
344 .'is not included in the list of pickup libraries.');
345 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1 });
347 foreach my $lib (@
{$pickup}) {
348 if ($lib->{'branchcode'} eq $limit->toBranch) {
352 is
($found, 0, 'The same applies when asking pickup locations of '
354 my $others = Koha
::Libraries
->search({
355 pickup_location
=> 1,
356 branchcode
=> { 'not in' => [$limit->toBranch] }})->count;
357 is
(@
{$pickup}, $others, 'However, the number of other pickup '
358 .'libraries is correct.');
359 $item3->ccode('yo')->store;
360 ok
($item1->ccode ne $item3->ccode,
361 'Given one of the item in this biblio has a different ccode,');
362 is
(Koha
::Item
::Transfer
::Limits
->search({
363 ccode
=> $item3->ccode })->count, 0, 'and it is'
364 .' not restricted by transfer limits,');
365 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
367 foreach my $lib (@
{$pickup}) {
368 if ($lib->{'branchcode'} eq $limit->toBranch) {
372 is
($found, 1, 'Then the to-library of which the limit applies for, '
373 .'is included in the list of pickup libraries.');
374 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item3 });
376 foreach my $lib (@
{$pickup}) {
377 if ($lib->{'branchcode'} eq $limit->toBranch) {
381 is
($found, 1, 'The same applies when asking pickup locations of '
382 .'a that particular item.');
383 Koha
::Item
::Transfer
::Limits
->delete;
384 $pickup = Koha
::Libraries
->pickup_locations({ biblio
=> $biblio->biblionumber });
386 foreach my $lib (@
{$pickup}) {
387 if ($lib->{'branchcode'} eq $limit->toBranch) {
391 is
($found, 1, 'Given we deleted transfer limit, the previously '
392 .'transfer-limited library is included in the list.');
393 $pickup = Koha
::Libraries
->pickup_locations({ item
=> $item1 });
395 foreach my $lib (@
{$pickup}) {
396 if ($lib->{'branchcode'} eq $limit->toBranch) {
400 is
($found, 1, 'The same applies when asking pickup locations of '
406 $schema->storage->txn_rollback;
408 subtest
'->get_effective_marcorgcode' => sub {
412 $schema->storage->txn_begin;
414 my $library_1 = $builder->build_object({ class => 'Koha::Libraries',
415 value
=> { marcorgcode
=> 'US-MyLib' } });
416 my $library_2 = $builder->build_object({ class => 'Koha::Libraries',
417 value
=> { marcorgcode
=> undef } });
419 t
::lib
::Mocks
::mock_preference
('MARCOrgCode', 'US-Default');
421 is
( $library_1->get_effective_marcorgcode, 'US-MyLib',
422 'If defined, use library\'s own marc org code');
423 is
( $library_2->get_effective_marcorgcode, 'US-Default',
424 'If not defined library\' marc org code, use the one from system preferences');
426 t
::lib
::Mocks
::mock_preference
('MARCOrgCode', 'Blah');
427 is
( $library_2->get_effective_marcorgcode, 'Blah',
428 'Fallback is always MARCOrgCode syspref');
430 $library_2->marcorgcode('ThisIsACode')->store();
431 is
( $library_2->get_effective_marcorgcode, 'ThisIsACode',
432 'Pick library_2 code');
434 $schema->storage->txn_rollback;
437 subtest
'cash_registers' => sub {
440 $schema->storage->txn_begin;
442 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
443 my $register1 = $builder->build_object(
445 class => 'Koha::Cash::Registers',
446 value
=> { branch
=> $library->branchcode },
449 my $register2 = $builder->build_object(
451 class => 'Koha::Cash::Registers',
452 value
=> { branch
=> $library->branchcode },
456 my $registers = $library->cash_registers;
457 is
( ref($registers), 'Koha::Cash::Registers',
458 'Koha::Library->cash_registers should return a set of Koha::Cash::Registers'
460 is
( $registers->count, 2,
461 'Koha::Library->cash_registers should return the correct cash registers'
465 is
( $library->cash_registers->next->id, $register2->id,
466 'Koha::Library->cash_registers should return the correct cash registers'
469 $schema->storage->txn_rollback;