Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Checkouts / ReturnClaim.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21 use Test::Exception;
22
23 use Koha::Database;
24 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Checkouts::ReturnClaims;
26
27 use t::lib::TestBuilder;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 subtest "store() tests" => sub {
33
34     plan tests => 9;
35
36     $schema->storage->txn_begin;
37
38     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
39     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
40     my $item      = $builder->build_sample_item;
41
42     my $checkout = $builder->build_object(
43         {
44             class => 'Koha::Checkouts',
45             value => {
46                 borrowernumber => $patron->borrowernumber,
47                 itemnumber     => $item->itemnumber,
48                 branchcode     => $patron->branchcode
49             }
50         }
51     );
52
53     throws_ok
54         { Koha::Checkouts::ReturnClaim->new(
55             {
56                 issue_id       => $checkout->id,
57                 itemnumber     => $checkout->itemnumber,
58                 borrowernumber => $checkout->borrowernumber,
59                 notes          => 'Some notes'
60             }
61           )->store }
62         'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy',
63         'Exception thrown if no created_by passed on creation';
64
65     my $old_checkout = $builder->build_object(
66         {
67             class => 'Koha::Old::Checkouts',
68             value => {
69                 borrowernumber => $patron->borrowernumber,
70                 itemnumber     => $item->itemnumber,
71                 branchcode     => $patron->branchcode
72             }
73         }
74     );
75
76     my $nullable_created_by = Koha::Checkouts::ReturnClaim->new(
77         {
78             issue_id       => $old_checkout->id,
79             itemnumber     => $old_checkout->itemnumber,
80             borrowernumber => $old_checkout->borrowernumber,
81             notes          => 'Some notes',
82             created_by     => $librarian->borrowernumber
83         }
84     )->store;
85     is( $nullable_created_by->created_by, $librarian->borrowernumber, 'Claim created with created_by set' );
86     ok( $nullable_created_by->in_storage, 'In storage' );
87
88     $nullable_created_by->created_by(undef)->store();
89     is( $nullable_created_by->created_by, undef, 'Deletion was deleted' );
90     ok( $nullable_created_by->in_storage, 'In storage' );
91     is(
92         ref($nullable_created_by->notes('Some other note')->store),
93         'Koha::Checkouts::ReturnClaim',
94         'Subsequent store succeeds after created_by has been unset'
95     );
96
97     is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
98
99     my $claim = Koha::Checkouts::ReturnClaim->new(
100         {
101             issue_id       => $checkout->id,
102             itemnumber     => $checkout->itemnumber,
103             borrowernumber => $checkout->borrowernumber,
104             notes          => 'Some notes',
105             created_by     => $librarian->borrowernumber
106         }
107     )->store;
108
109     is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
110     is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
111
112     $schema->storage->txn_rollback;
113 };
114
115 subtest "resolve() tests" => sub {
116
117     plan tests => 10;
118
119     $schema->storage->txn_begin;
120
121     my $itemlost  = 1;
122     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
123     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
124     my $item      = $builder->build_sample_item({ itemlost => $itemlost });
125
126     my $checkout = $builder->build_object(
127         {
128             class => 'Koha::Checkouts',
129             value => {
130                 borrowernumber => $patron->borrowernumber,
131                 itemnumber     => $item->itemnumber,
132                 branchcode     => $patron->branchcode
133             }
134         }
135     );
136
137     my $claim = Koha::Checkouts::ReturnClaim->new(
138         {
139             issue_id       => $checkout->id,
140             itemnumber     => $checkout->itemnumber,
141             borrowernumber => $checkout->borrowernumber,
142             notes          => 'Some notes',
143             created_by     => $librarian->borrowernumber
144         }
145     )->store;
146
147     throws_ok
148         { $claim->resolve({ resolution => 1 }); }
149         'Koha::Exceptions::MissingParameter',
150         "Not passing 'resolved_by' makes it throw an exception";
151
152     throws_ok
153         { $claim->resolve({ resolved_by => 1 }); }
154         'Koha::Exceptions::MissingParameter',
155         "Not passing 'resolution' makes it throw an exception";
156
157     my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
158     my $deleted_patron_id = $deleted_patron->id;
159     $deleted_patron->delete;
160
161     {   # hide useless warnings
162         local *STDERR;
163         open STDERR, '>', '/dev/null';
164
165         throws_ok
166             { $claim->resolve({ resolution => "X", resolved_by => $deleted_patron_id }) }
167             'Koha::Exceptions::Object::FKConstraint',
168             "Exception thrown on invalid resolver";
169
170         close STDERR;
171     }
172
173     my $today    = dt_from_string;
174     my $tomorrow = dt_from_string->add( days => 1 );
175
176     $claim->resolve(
177         {
178             resolution  => "X",
179             resolved_by => $librarian->id,
180             resolved_on => $tomorrow,
181         }
182     )->discard_changes;
183
184     is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
185     is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
186
187     # Make sure $item is refreshed
188     $item->discard_changes;
189     is( $item->itemlost, $itemlost, 'Item lost status remains unchanged' );
190
191     # New checkout and claim
192     $checkout->delete;
193     $checkout = $builder->build_object(
194         {
195             class => 'Koha::Checkouts',
196             value => {
197                 borrowernumber => $patron->borrowernumber,
198                 itemnumber     => $item->itemnumber,
199                 branchcode     => $patron->branchcode
200             }
201         }
202     );
203
204     $claim = Koha::Checkouts::ReturnClaim->new(
205         {
206             issue_id       => $checkout->id,
207             itemnumber     => $checkout->itemnumber,
208             borrowernumber => $checkout->borrowernumber,
209             notes          => 'Some notes',
210             created_by     => $librarian->borrowernumber
211         }
212     )->store;
213
214     my $new_lost_status = 2;
215
216     $claim->resolve(
217         {
218             resolution      => "X",
219             resolved_by     => $librarian->id,
220             resolved_on     => $tomorrow,
221             new_lost_status => $new_lost_status,
222         }
223     )->discard_changes;
224
225     is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
226     is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
227
228     # Make sure $item is refreshed
229     $item->discard_changes;
230     is( $item->itemlost, $new_lost_status, 'Item lost status is updated' );
231
232     # Resolve claim for checkout that has been cleaned from the database
233     $checkout->delete;
234     $checkout = $builder->build_object(
235         {
236             class => 'Koha::Checkouts',
237             value => {
238                 borrowernumber => $patron->borrowernumber,
239                 itemnumber     => $item->itemnumber,
240                 branchcode     => $patron->branchcode
241             }
242         }
243     );
244
245     $claim = Koha::Checkouts::ReturnClaim->new(
246         {
247             issue_id       => $checkout->id,
248             itemnumber     => $checkout->itemnumber,
249             borrowernumber => $checkout->borrowernumber,
250             notes          => 'Some notes',
251             created_by     => $librarian->borrowernumber
252         }
253     )->store;
254
255     $checkout->delete;
256
257     $claim->resolve(
258         {
259             resolution      => "X",
260             resolved_by     => $librarian->id,
261             resolved_on     => $tomorrow,
262             new_lost_status => $new_lost_status,
263         }
264     )->discard_changes;
265
266     is( $claim->issue_id, undef, "Resolved claim cleaned checkout is updated correctly" );
267
268     $schema->storage->txn_rollback;
269 };
270
271 subtest 'item() tests' => sub {
272
273     plan tests => 3;
274
275     $schema->storage->txn_begin;
276
277     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
278     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
279     my $item      = $builder->build_sample_item;
280     my $checkout = $builder->build_object(
281         {
282             class => 'Koha::Checkouts',
283             value => {
284                 borrowernumber => $patron->borrowernumber,
285                 itemnumber     => $item->itemnumber,
286                 branchcode     => $patron->branchcode
287             }
288         }
289     );
290
291     my $claim = Koha::Checkouts::ReturnClaim->new(
292         {
293             issue_id       => $checkout->id,
294             itemnumber     => $checkout->itemnumber,
295             borrowernumber => $checkout->borrowernumber,
296             notes          => 'Some notes',
297             created_by     => $librarian->borrowernumber
298         }
299     )->store;
300
301     my $return_claim_item = $claim->item;
302     is( ref( $return_claim_item ), 'Koha::Item', 'Koha::Checkouts::ReturnClaim->item should return a Koha::Item' );
303     is( $claim->itemnumber, $return_claim_item->itemnumber, 'Koha::Checkouts::ReturnClaim->item should return the correct item' );
304
305     my $itemnumber = $item->itemnumber;
306     $checkout->delete; # Required to allow deletion of item
307     $item->delete;
308
309     my $claims = Koha::Checkouts::ReturnClaims->search({ itemnumber => $itemnumber });
310     is( $claims->count, 0, 'Koha::Checkouts::ReturnClaim is deleted on item deletion' );
311
312     $schema->storage->txn_rollback;
313 };
314
315 subtest 'patron() tests' => sub {
316
317     plan tests => 3;
318
319     $schema->storage->txn_begin;
320
321     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
322     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
323     my $item      = $builder->build_sample_item;
324     my $checkout = $builder->build_object(
325         {
326             class => 'Koha::Checkouts',
327             value => {
328                 borrowernumber => $patron->borrowernumber,
329                 itemnumber     => $item->itemnumber,
330                 branchcode     => $patron->branchcode
331             }
332         }
333     );
334
335     my $claim = Koha::Checkouts::ReturnClaim->new(
336         {
337             issue_id       => $checkout->id,
338             itemnumber     => $checkout->itemnumber,
339             borrowernumber => $checkout->borrowernumber,
340             notes          => 'Some notes',
341             created_by     => $librarian->borrowernumber
342         }
343     )->store;
344
345     my $return_claim_patron = $claim->patron;
346     is( ref( $return_claim_patron ), 'Koha::Patron', 'Koha::Checkouts::ReturnClaim->patron should return a Koha::Patron' );
347     is( $claim->borrowernumber, $return_claim_patron->borrowernumber, 'Koha::Checkouts::ReturnClaim->patron should return the correct borrower' );
348
349     my $borrowernumber = $patron->borrowernumber;
350     $checkout->delete; # Required to allow deletion of patron
351     $patron->delete;
352
353     my $claims = Koha::Checkouts::ReturnClaims->search({ borrowernumber => $borrowernumber });
354     is( $claims->count, 0, 'Koha::Checkouts::ReturnClaim is deleted on borrower deletion' );
355
356     $schema->storage->txn_rollback;
357 };
358
359 subtest 'old_checkout() tests' => sub {
360
361     plan tests => 4;
362
363     $schema->storage->txn_begin;
364
365     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
366     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
367     my $item      = $builder->build_sample_item;
368     my $old_checkout = $builder->build_object(
369         {
370             class => 'Koha::Old::Checkouts',
371             value => {
372                 borrowernumber => $patron->borrowernumber,
373                 itemnumber     => $item->itemnumber,
374                 branchcode     => $patron->branchcode
375             }
376         }
377     );
378
379     my $claim = Koha::Checkouts::ReturnClaim->new(
380         {
381             issue_id       => $old_checkout->id,
382             itemnumber     => $old_checkout->itemnumber,
383             borrowernumber => $old_checkout->borrowernumber,
384             notes          => 'Some notes',
385             created_by     => $librarian->borrowernumber
386         }
387     )->store;
388
389     my $return_claim_old_checkout = $claim->old_checkout;
390     is( ref( $return_claim_old_checkout ), 'Koha::Old::Checkout', 'Koha::Checkouts::ReturnClaim->old_checkout should return a Koha::Old::Checkout' );
391     is( $claim->issue_id, $return_claim_old_checkout->issue_id, 'Koha::Checkouts::ReturnClaim->old_checkout should return the correct borrower' );
392
393     my $issue_id = $old_checkout->issue_id;
394     $old_checkout->delete;
395
396     my $claims = Koha::Checkouts::ReturnClaims->search({ issue_id => $issue_id });
397     is( $claims->count, 1, 'Koha::Checkouts::ReturnClaim remains on old_checkout deletion' );
398     # FIXME: Should we actually set null on OldCheckout deletion?
399
400     $claim->issue_id(undef)->store;
401     is( $claim->old_checkout, undef, 'Koha::Checkouts::ReturnClaim->old_checkout should return undef if no old_checkout linked' );
402
403     $schema->storage->txn_rollback;
404 };
405
406 subtest 'checkout() tests' => sub {
407
408     plan tests => 4;
409
410     $schema->storage->txn_begin;
411
412     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
413     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
414     my $item      = $builder->build_sample_item;
415     my $checkout = $builder->build_object(
416         {
417             class => 'Koha::Checkouts',
418             value => {
419                 borrowernumber => $patron->borrowernumber,
420                 itemnumber     => $item->itemnumber,
421                 branchcode     => $patron->branchcode
422             }
423         }
424     );
425
426     my $claim = Koha::Checkouts::ReturnClaim->new(
427         {
428             issue_id       => $checkout->id,
429             itemnumber     => $checkout->itemnumber,
430             borrowernumber => $checkout->borrowernumber,
431             notes          => 'Some notes',
432             created_by     => $librarian->borrowernumber
433         }
434     )->store;
435
436     my $return_claim_checkout = $claim->checkout;
437     is( ref( $return_claim_checkout ), 'Koha::Checkout', 'Koha::Checkouts::ReturnClaim->checkout should return a Koha::Checkout' );
438     is( $claim->issue_id, $return_claim_checkout->issue_id, 'Koha::Checkouts::ReturnClaim->checkout should return the correct borrower' );
439
440     my $issue_id = $checkout->issue_id;
441     $checkout->delete;
442
443     my $claims = Koha::Checkouts::ReturnClaims->search({ issue_id => $issue_id });
444     is( $claims->count, 1, 'Koha::Checkouts::ReturnClaim remains on checkout deletion' );
445
446     $claim->issue_id(undef)->store;
447     is( $claim->checkout, undef, 'Koha::Checkouts::ReturnClaim->checkout should return undef if no checkout linked' );
448
449     $schema->storage->txn_rollback;
450 };