Bug 28588: Unit tests
[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 => 2;
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 => 11;
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 correctly';
64
65     my $nullified_created_by = $builder->build_object(
66         {
67             class => 'Koha::Checkouts::ReturnClaims',
68             value => {
69                 created_by => undef
70             }
71         }
72     );
73
74     is( $nullified_created_by->created_by, undef, 'Is undef' );
75     ok( $nullified_created_by->in_storage, 'In storage' );
76     is(
77         ref($nullified_created_by->notes('Some other note')->store),
78         'Koha::Checkouts::ReturnClaim',
79         'No exception, store success'
80     );
81
82     is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
83
84     my $claim = Koha::Checkouts::ReturnClaim->new(
85         {
86             issue_id       => $checkout->id,
87             itemnumber     => $checkout->itemnumber,
88             borrowernumber => $checkout->borrowernumber,
89             notes          => 'Some notes',
90             created_by     => $librarian->borrowernumber
91         }
92     )->store;
93
94     is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
95     is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
96
97     {   # hide useless warnings
98         local *STDERR;
99         open STDERR, '>', '/dev/null';
100         throws_ok {
101             Koha::Checkouts::ReturnClaim->new(
102                 {
103                     issue_id       => $checkout->id,
104                     itemnumber     => $checkout->itemnumber,
105                     borrowernumber => $checkout->borrowernumber,
106                     notes          => 'Some notes',
107                     created_by     => $librarian->borrowernumber
108                 }
109             )->store;
110         }
111         'Koha::Exceptions::Object::DuplicateID',
112             'An exception is thrown on duplicate issue_id';
113         close STDERR;
114
115         like(
116             $@->duplicate_id,
117             qr/(return_claims\.)?issue_id/,
118             'Exception field is correct'
119         );
120     }
121
122     {    # hide useless warnings
123         local *STDERR;
124         open STDERR, '>', '/dev/null';
125
126         my $another_checkout = $builder->build_object({ class => 'Koha::Checkouts' });
127         my $checkout_id = $another_checkout->id;
128         $another_checkout->delete;
129
130         my $THE_claim;
131
132         throws_ok {
133             $THE_claim = Koha::Checkouts::ReturnClaim->new(
134                 {
135                     issue_id       => $checkout_id,
136                     itemnumber     => $checkout->itemnumber,
137                     borrowernumber => $checkout->borrowernumber,
138                     notes          => 'Some notes',
139                     created_by     => $librarian->borrowernumber
140                 }
141             )->store;
142         }
143         'Koha::Exceptions::Object::FKConstraint',
144           'An exception is thrown on invalid issue_id';
145         close STDERR;
146
147         is( $@->broken_fk, 'issue_id', 'Exception field is correct' );
148     }
149
150     $schema->storage->txn_rollback;
151 };
152
153 subtest "resolve() tests" => sub {
154
155     plan tests => 9;
156
157     $schema->storage->txn_begin;
158
159     my $itemlost  = 1;
160     my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
161     my $patron    = $builder->build_object({ class => 'Koha::Patrons' });
162     my $item      = $builder->build_sample_item({ itemlost => $itemlost });
163
164     my $checkout = $builder->build_object(
165         {
166             class => 'Koha::Checkouts',
167             value => {
168                 borrowernumber => $patron->borrowernumber,
169                 itemnumber     => $item->itemnumber,
170                 branchcode     => $patron->branchcode
171             }
172         }
173     );
174
175     my $claim = Koha::Checkouts::ReturnClaim->new(
176         {
177             issue_id       => $checkout->id,
178             itemnumber     => $checkout->itemnumber,
179             borrowernumber => $checkout->borrowernumber,
180             notes          => 'Some notes',
181             created_by     => $librarian->borrowernumber
182         }
183     )->store;
184
185     throws_ok
186         { $claim->resolve({ resolution => 1 }); }
187         'Koha::Exceptions::MissingParameter',
188         "Not passing 'resolved_by' makes it throw an exception";
189
190     throws_ok
191         { $claim->resolve({ resolved_by => 1 }); }
192         'Koha::Exceptions::MissingParameter',
193         "Not passing 'resolution' makes it throw an exception";
194
195     my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
196     my $deleted_patron_id = $deleted_patron->id;
197     $deleted_patron->delete;
198
199     {   # hide useless warnings
200         local *STDERR;
201         open STDERR, '>', '/dev/null';
202
203         throws_ok
204             { $claim->resolve({ resolution => "X", resolved_by => $deleted_patron_id }) }
205             'Koha::Exceptions::Object::FKConstraint',
206             "Exception thrown on invalid resolver";
207
208         close STDERR;
209     }
210
211     my $today    = dt_from_string;
212     my $tomorrow = dt_from_string->add( days => 1 );
213
214     $claim->resolve(
215         {
216             resolution  => "X",
217             resolved_by => $librarian->id,
218             resolved_on => $tomorrow,
219         }
220     )->discard_changes;
221
222     is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
223     is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
224
225     # Make sure $item is refreshed
226     $item->discard_changes;
227     is( $item->itemlost, $itemlost, 'Item lost status remains unchanged' );
228
229     # New checkout and claim
230     $checkout->delete;
231     $checkout = $builder->build_object(
232         {
233             class => 'Koha::Checkouts',
234             value => {
235                 borrowernumber => $patron->borrowernumber,
236                 itemnumber     => $item->itemnumber,
237                 branchcode     => $patron->branchcode
238             }
239         }
240     );
241
242     $claim = Koha::Checkouts::ReturnClaim->new(
243         {
244             issue_id       => $checkout->id,
245             itemnumber     => $checkout->itemnumber,
246             borrowernumber => $checkout->borrowernumber,
247             notes          => 'Some notes',
248             created_by     => $librarian->borrowernumber
249         }
250     )->store;
251
252     my $new_lost_status = 2;
253
254     $claim->resolve(
255         {
256             resolution      => "X",
257             resolved_by     => $librarian->id,
258             resolved_on     => $tomorrow,
259             new_lost_status => $new_lost_status,
260         }
261     )->discard_changes;
262
263     is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
264     is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
265
266     # Make sure $item is refreshed
267     $item->discard_changes;
268     is( $item->itemlost, $new_lost_status, 'Item lost status is updated' );
269
270     $schema->storage->txn_rollback;
271 };