Bug 29869: (QA follow-up) import t::lib::Mocks for Hold.t for mock_preference
[koha.git] / t / db_dependent / Koha / Hold.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 Koha Development team
4 #
5 # This file is part of Koha
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Test::More tests => 4;
23
24 use Test::Exception;
25 use Test::MockModule;
26
27 use t::lib::TestBuilder;
28 use t::lib::Mocks;
29
30 use Koha::ActionLogs;
31 use Koha::Holds;
32 use Koha::Libraries;
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'fill() tests' => sub {
38
39     plan tests => 11;
40
41     $schema->storage->txn_begin;
42
43     my $fee = 15;
44
45     my $category = $builder->build_object(
46         {
47             class => 'Koha::Patron::Categories',
48             value => { reservefee => $fee }
49         }
50     );
51     my $patron = $builder->build_object(
52         {
53             class => 'Koha::Patrons',
54             value => { categorycode => $category->id }
55         }
56     );
57     my $manager = $builder->build_object( { class => 'Koha::Patrons' } );
58
59     my $title  = 'Do what you want';
60     my $biblio = $builder->build_sample_biblio( { title => $title } );
61     my $item   = $builder->build_sample_item( { biblionumber => $biblio->id } );
62     my $hold   = $builder->build_object(
63         {
64             class => 'Koha::Holds',
65             value => {
66                 biblionumber   => $biblio->id,
67                 borrowernumber => $patron->id,
68                 itemnumber     => $item->id,
69                 priority       => 10,
70             }
71         }
72     );
73
74     t::lib::Mocks::mock_preference( 'HoldFeeMode', 'any_time_is_collected' );
75     t::lib::Mocks::mock_preference( 'HoldsLog',    1 );
76     t::lib::Mocks::mock_userenv(
77         { patron => $manager, branchcode => $manager->branchcode } );
78
79     my $interface = 'api';
80     C4::Context->interface($interface);
81
82     my $ret = $hold->fill;
83
84     is( ref($ret), 'Koha::Hold', '->fill returns the object type' );
85     is( $ret->id, $hold->id, '->fill returns the object' );
86
87     is( Koha::Holds->find($hold->id), undef, 'Hold no longer current' );
88     my $old_hold = Koha::Old::Holds->find( $hold->id );
89
90     is( $old_hold->id, $hold->id, 'reserve_id retained' );
91     is( $old_hold->priority, 0, 'priority set to 0' );
92     is( $old_hold->found, 'F', 'found set to F' );
93
94     subtest 'fee applied tests' => sub {
95
96         plan tests => 9;
97
98         my $account = $patron->account;
99         is( $account->balance, $fee, 'Charge applied correctly' );
100
101         my $debits = $account->outstanding_debits;
102         is( $debits->count, 1, 'Only one fee charged' );
103
104         my $fee_debit = $debits->next;
105         is( $fee_debit->amount * 1, $fee, 'Fee amount stored correctly' );
106         is( $fee_debit->description, $title,
107             'Fee description stored correctly' );
108         is( $fee_debit->manager_id, $manager->id,
109             'Fee manager_id stored correctly' );
110         is( $fee_debit->branchcode, $manager->branchcode,
111             'Fee branchcode stored correctly' );
112         is( $fee_debit->interface, $interface,
113             'Fee interface stored correctly' );
114         is( $fee_debit->debit_type_code,
115             'RESERVE', 'Fee debit_type_code stored correctly' );
116         is( $fee_debit->itemnumber, $item->id,
117             'Fee itemnumber stored correctly' );
118     };
119
120     my $logs = Koha::ActionLogs->search(
121         {
122             action => 'FILL',
123             module => 'HOLDS',
124             object => $hold->id
125         }
126     );
127
128     is( $logs->count, 1, '1 log line added' );
129
130     # Set HoldFeeMode to something other than any_time_is_collected
131     t::lib::Mocks::mock_preference( 'HoldFeeMode', 'not_always' );
132     # Disable logging
133     t::lib::Mocks::mock_preference( 'HoldsLog',    0 );
134
135     $hold = $builder->build_object(
136         {
137             class => 'Koha::Holds',
138             value => {
139                 biblionumber   => $biblio->id,
140                 borrowernumber => $patron->id,
141                 itemnumber     => $item->id,
142                 priority       => 10,
143             }
144         }
145     );
146
147     $hold->fill;
148
149     my $account = $patron->account;
150     is( $account->balance, $fee, 'No new charge applied' );
151
152     my $debits = $account->outstanding_debits;
153     is( $debits->count, 1, 'Only one fee charged, because of HoldFeeMode' );
154
155     $logs = Koha::ActionLogs->search(
156         {
157             action => 'FILL',
158             module => 'HOLDS',
159             object => $hold->id
160         }
161     );
162
163     is( $logs->count, 0, 'HoldsLog disabled, no logs added' );
164
165     $schema->storage->txn_rollback;
166 };
167
168 subtest 'patron() tests' => sub {
169
170     plan tests => 2;
171
172     $schema->storage->txn_begin;
173
174     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
175     my $hold   = $builder->build_object(
176         {
177             class => 'Koha::Holds',
178             value => {
179                 borrowernumber => $patron->borrowernumber
180             }
181         }
182     );
183
184     my $hold_patron = $hold->patron;
185     is( ref($hold_patron), 'Koha::Patron', 'Right type' );
186     is( $hold_patron->id, $patron->id, 'Right object' );
187
188     $schema->storage->txn_rollback;
189 };
190
191 subtest 'set_pickup_location() tests' => sub {
192
193     plan tests => 11;
194
195     $schema->storage->txn_begin;
196
197     my $mock_biblio = Test::MockModule->new('Koha::Biblio');
198     my $mock_item   = Test::MockModule->new('Koha::Item');
199
200     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
201     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
202     my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
203
204     # let's control what Koha::Biblio->pickup_locations returns, for testing
205     $mock_biblio->mock( 'pickup_locations', sub {
206         return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
207     });
208     # let's mock what Koha::Item->pickup_locations returns, for testing
209     $mock_item->mock( 'pickup_locations', sub {
210         return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
211     });
212
213     my $biblio = $builder->build_sample_biblio;
214     my $item   = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
215
216     # Test biblio-level holds
217     my $biblio_hold = $builder->build_object(
218         {
219             class => "Koha::Holds",
220             value => {
221                 biblionumber => $biblio->biblionumber,
222                 branchcode   => $library_3->branchcode,
223                 itemnumber   => undef,
224             }
225         }
226     );
227
228     throws_ok
229         { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
230         'Koha::Exceptions::Hold::InvalidPickupLocation',
231         'Exception thrown on invalid pickup location';
232
233     $biblio_hold->discard_changes;
234     is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
235
236     my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id });
237     is( ref($ret), 'Koha::Hold', 'self is returned' );
238
239     $biblio_hold->discard_changes;
240     is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
241
242     # Test item-level holds
243     my $item_hold = $builder->build_object(
244         {
245             class => "Koha::Holds",
246             value => {
247                 biblionumber => $biblio->biblionumber,
248                 branchcode   => $library_3->branchcode,
249                 itemnumber   => $item->itemnumber,
250             }
251         }
252     );
253
254     throws_ok
255         { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); }
256         'Koha::Exceptions::Hold::InvalidPickupLocation',
257         'Exception thrown on invalid pickup location';
258
259     $item_hold->discard_changes;
260     is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' );
261
262     $item_hold->set_pickup_location({ library_id => $library_1->branchcode, force => 1 });
263     $item_hold->discard_changes;
264     is( $item_hold->branchcode, $library_1->branchcode, 'branchcode changed because of \'force\'' );
265
266     $ret = $item_hold->set_pickup_location({ library_id => $library_2->id });
267     is( ref($ret), 'Koha::Hold', 'self is returned' );
268
269     $item_hold->discard_changes;
270     is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' );
271
272     throws_ok
273         { $item_hold->set_pickup_location({ library_id => undef }); }
274         'Koha::Exceptions::MissingParameter',
275         'Exception thrown if missing parameter';
276
277     is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' );
278
279     $schema->storage->txn_rollback;
280 };
281
282 subtest 'is_pickup_location_valid() tests' => sub {
283
284     plan tests => 5;
285
286     $schema->storage->txn_begin;
287
288     my $mock_biblio = Test::MockModule->new('Koha::Biblio');
289     my $mock_item   = Test::MockModule->new('Koha::Item');
290
291     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
292     my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
293     my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
294
295     # let's control what Koha::Biblio->pickup_locations returns, for testing
296     $mock_biblio->mock( 'pickup_locations', sub {
297         return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
298     });
299     # let's mock what Koha::Item->pickup_locations returns, for testing
300     $mock_item->mock( 'pickup_locations', sub {
301         return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } );
302     });
303
304     my $biblio = $builder->build_sample_biblio;
305     my $item   = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
306
307     # Test biblio-level holds
308     my $biblio_hold = $builder->build_object(
309         {
310             class => "Koha::Holds",
311             value => {
312                 biblionumber => $biblio->biblionumber,
313                 branchcode   => $library_3->branchcode,
314                 itemnumber   => undef,
315             }
316         }
317     );
318
319     ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
320     ok( $biblio_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid');
321
322     # Test item-level holds
323     my $item_hold = $builder->build_object(
324         {
325             class => "Koha::Holds",
326             value => {
327                 biblionumber => $biblio->biblionumber,
328                 branchcode   => $library_3->branchcode,
329                 itemnumber   => $item->itemnumber,
330             }
331         }
332     );
333
334     ok( !$item_hold->is_pickup_location_valid({ library_id => $library_1->branchcode }), 'Pickup location invalid');
335     ok( $item_hold->is_pickup_location_valid({ library_id => $library_2->id }), 'Pickup location valid' );
336
337     subtest 'pickup_locations() returning ->empty' => sub {
338
339         plan tests => 2;
340
341         $schema->storage->txn_begin;
342
343         my $library = $builder->build_object({ class => 'Koha::Libraries' });
344
345         my $mock_item = Test::MockModule->new('Koha::Item');
346         $mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
347
348         my $mock_biblio = Test::MockModule->new('Koha::Biblio');
349         $mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->new->empty; } );
350
351         my $item   = $builder->build_sample_item();
352         my $biblio = $item->biblio;
353
354         # Test biblio-level holds
355         my $biblio_hold = $builder->build_object(
356             {
357                 class => "Koha::Holds",
358                 value => {
359                     biblionumber => $biblio->biblionumber,
360                     itemnumber   => undef,
361                 }
362             }
363         );
364
365         ok( !$biblio_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
366
367         # Test item-level holds
368         my $item_hold = $builder->build_object(
369             {
370                 class => "Koha::Holds",
371                 value => {
372                     biblionumber => $biblio->biblionumber,
373                     itemnumber   => $item->itemnumber,
374                 }
375             }
376         );
377
378         ok( !$item_hold->is_pickup_location_valid({ library_id => $library->branchcode }), 'Pickup location invalid');
379
380         $schema->storage->txn_rollback;
381     };
382
383     $schema->storage->txn_rollback;
384 };