Bug 29145: Perltidy files and added code
[koha.git] / t / db_dependent / Circulation / MarkIssueReturned.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 => 4;
21 use Test::Exception;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use C4::Circulation qw( MarkIssueReturned AddIssue );
27 use C4::Context;
28 use Koha::Checkouts;
29 use Koha::Database;
30 use Koha::DateUtils qw(dt_from_string);
31 use Koha::Old::Checkouts;
32 use Koha::Patrons;
33 use Koha::Patron::Debarments qw( AddUniqueDebarment );
34
35 my $schema = Koha::Database->schema;
36 my $builder = t::lib::TestBuilder->new;
37
38 subtest 'Failure tests' => sub {
39
40     plan tests => 5;
41
42     $schema->storage->txn_begin;
43
44     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
45     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
46     my $patron   = $builder->build_object(
47         {   class => 'Koha::Patrons',
48             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
49         }
50     );
51     my $item = $builder->build_sample_item(
52         {
53             library => $library->branchcode,
54         }
55     );
56
57     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
58
59     my ( $issue_id, $issue );
60     # The next call will return undef for invalid item number
61     eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, 'invalid_itemnumber', undef, 0 ) };
62     is( $@, '', 'No die triggered by invalid itemnumber' );
63     is( $issue_id, undef, 'No issue_id returned' );
64
65     # In the next call we return the item and try it another time
66     $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
67     eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) };
68     is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
69     eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) };
70     is( $@, '', 'No crash on returning item twice' );
71     is( $issue_id, undef, 'Cannot return an item twice' );
72
73
74     $schema->storage->txn_rollback;
75 };
76
77 subtest 'Anonymous patron tests' => sub {
78
79     plan tests => 3;
80
81     $schema->storage->txn_begin;
82
83     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
84     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
85     my $patron   = $builder->build_object(
86         {   class => 'Koha::Patrons',
87             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
88         }
89     );
90     my $item = $builder->build_sample_item(
91         {
92             library => $library->branchcode,
93         }
94     );
95
96     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
97
98     # Anonymous patron not set
99     t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
100
101     my $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
102
103     throws_ok
104         { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ); }
105         'Koha::Exceptions::SysPref::NotSet',
106         'AnonymousPatron not set causes an exception';
107
108     is( $@->syspref, 'AnonymousPatron', 'AnonymousPatron is not set - Fatal error on anonymization' );
109     Koha::Checkouts->find( $issue->issue_id )->delete;
110
111     # Create a valid anonymous user
112     my $anonymous = $builder->build_object({
113         class => 'Koha::Patrons',
114         value => {
115             categorycode => $category->categorycode,
116             branchcode => $library->branchcode
117         }
118     });
119     t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
120     $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
121
122     eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
123     is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
124
125     $schema->storage->txn_rollback;
126 };
127
128 subtest 'Manually pass a return date' => sub {
129
130     plan tests => 3;
131
132     $schema->storage->txn_begin;
133
134     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
135     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
136     my $patron  = $builder->build_object(
137         {   class => 'Koha::Patrons',
138             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
139         }
140     );
141     my $item = $builder->build_sample_item(
142         {
143             library => $library->branchcode,
144         }
145     );
146
147     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
148
149     my ( $issue, $issue_id );
150
151     $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
152     $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
153
154     is( $issue_id, $issue->issue_id, "Item has been returned" );
155     my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
156     is( $old_checkout->returndate, '2018-12-25 00:00:00', 'Manually passed date stored correctly' );
157
158     $issue = C4::Circulation::AddIssue( $patron, $item->barcode );
159
160     {
161         # Hiding the expected warning displayed by DBI
162         # DBD::mysql::st execute failed: Incorrect datetime value: 'bad_date' for column 'returndate'
163         local *STDERR;
164         open STDERR, '>', '/dev/null';
165         throws_ok
166             { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, 'bad_date', 0 ); }
167             'Koha::Exceptions::Object::BadValue',
168             'An exception is thrown on bad date';
169         close STDERR;
170     }
171
172     $schema->storage->txn_rollback;
173 };
174
175 subtest 'AutoRemoveOverduesRestrictions' => sub {
176     plan tests => 5;
177
178     $schema->storage->txn_begin;
179
180     my $dbh          = C4::Context->dbh;
181     my $patron       = $builder->build_object( { class => 'Koha::Patrons' } );
182     my $categorycode = $patron->categorycode;
183     my $branchcode   = $patron->branchcode;
184
185     $dbh->do(
186         qq{
187         INSERT INTO `overduerules` (
188             `categorycode`,
189             `delay1`,
190             `letter1`,
191             `debarred1`,
192             `delay2`,
193             `letter2`,
194             `debarred2`
195         )
196         VALUES ('$categorycode', 6, 'ODUE', 0, 10, 'ODUE2', 1)
197     }
198     );
199
200     t::lib::Mocks::mock_preference( 'AutoRemoveOverduesRestrictions', 'when_no_overdue' );
201
202     t::lib::Mocks::mock_userenv( { branchcode => $branchcode } );
203     my $item_1        = $builder->build_sample_item;
204     my $item_2        = $builder->build_sample_item;
205     my $item_3        = $builder->build_sample_item;
206     my $nine_days_ago = dt_from_string->subtract( days => 9 );
207     my $checkout_1 =
208         AddIssue( $patron, $item_1->barcode, $nine_days_ago );    # overdue, but would not trigger debarment
209     my $checkout_2 =
210         AddIssue( $patron, $item_2->barcode, $nine_days_ago );    # overdue, but would not trigger debarment
211     my $checkout_3 = AddIssue( $patron, $item_3->barcode );       # not overdue
212
213
214     Koha::Patron::Debarments::AddUniqueDebarment(
215         {
216             borrowernumber => $patron->borrowernumber,
217             type           => 'OVERDUES',
218             comment        => "OVERDUES_PROCESS simulation",
219         }
220     );
221
222     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
223
224     my $restrictions    = $patron->restrictions;
225     my $THE_restriction = $restrictions->next;
226     is( $THE_restriction->type->code, 'OVERDUES', 'OVERDUES debarment is not removed if patron still has overdues' );
227
228     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
229
230     $restrictions = $patron->restrictions;
231     is( $restrictions->count, 0, 'OVERDUES debarment is removed if patron does not have overdues' );
232
233     t::lib::Mocks::mock_preference( 'AutoRemoveOverduesRestrictions', 'when_no_overdue_causing_debarment' );
234
235     my $ten_days_ago = dt_from_string->subtract( days => 10 );
236
237     $checkout_1 = AddIssue( $patron->unblessed, $item_1->barcode, $ten_days_ago ); # overdue and would trigger debarment
238     $checkout_2 =
239         AddIssue( $patron->unblessed, $item_2->barcode, $nine_days_ago );    # overdue, but would not trigger debarment
240
241     Koha::Patron::Debarments::AddUniqueDebarment(
242         {
243             borrowernumber => $patron->borrowernumber,
244             type           => 'OVERDUES',
245             comment        => "OVERDUES_PROCESS simulation",
246         }
247     );
248
249     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
250
251     $restrictions = $patron->restrictions;
252     is(
253         $restrictions->count, 0,
254         'OVERDUES debarment is removed if remaining items would not result in patron debarment'
255     );
256
257     $checkout_1 = AddIssue( $patron->unblessed, $item_1->barcode, $ten_days_ago ); # overdue and would trigger debarment
258
259     Koha::Patron::Debarments::AddUniqueDebarment(
260         {
261             borrowernumber => $patron->borrowernumber,
262             type           => 'OVERDUES',
263             comment        => "OVERDUES_PROCESS simulation",
264         }
265     );
266
267     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
268
269     $restrictions = $patron->restrictions->search( { type => 'OVERDUES' } );
270     is(
271         $restrictions->count, 1,
272         'OVERDUES debarment is not removed if patron still has overdues that would trigger debarment'
273     );
274
275     my $eleven_days_ago = dt_from_string->subtract( days => 11 );
276
277     # overdue and would trigger debarment
278     $checkout_2 = AddIssue( $patron->unblessed, $item_2->barcode, $eleven_days_ago );
279
280     # $checkout_1 should now not trigger debarment with this new rule for specific branchcode
281     $dbh->do(
282         qq{
283         INSERT INTO `overduerules` (
284             `branchcode`,
285             `categorycode`,
286             `delay1`,
287             `letter1`,
288             `debarred1`,
289             `delay2`,
290             `letter2`,
291             `debarred2`
292         )
293         VALUES ('$branchcode', '$categorycode', 6, 'ODUE', 0, 11, 'ODUE2', 1)
294     }
295     );
296
297     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
298
299     $restrictions = $patron->restrictions;
300     is(
301         $restrictions->count, 0,
302         'OVERDUES debarment is removed if remaining items would not result in patron debarment'
303     );
304
305     $schema->storage->txn_rollback;
306 };