e9152d3f9feebde6815f85d7bf0b8c2de3e62e20
[srvgit] / 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;
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;
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->unblessed, $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 => 2;
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->unblessed, $item->barcode );
102     eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
103     like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );
104     Koha::Checkouts->find( $issue->issue_id )->delete;
105
106     # Create a valid anonymous user
107     my $anonymous = $builder->build_object({
108         class => 'Koha::Patrons',
109         value => {
110             categorycode => $category->categorycode,
111             branchcode => $library->branchcode
112         }
113     });
114     t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
115     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
116
117     eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
118     is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
119
120     $schema->storage->txn_rollback;
121 };
122
123 subtest 'Manually pass a return date' => sub {
124
125     plan tests => 3;
126
127     $schema->storage->txn_begin;
128
129     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
130     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
131     my $patron  = $builder->build_object(
132         {   class => 'Koha::Patrons',
133             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
134         }
135     );
136     my $item = $builder->build_sample_item(
137         {
138             library => $library->branchcode,
139         }
140     );
141
142     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
143
144     my ( $issue, $issue_id );
145
146     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
147     $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
148
149     is( $issue_id, $issue->issue_id, "Item has been returned" );
150     my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
151     is( $old_checkout->returndate, '2018-12-25 00:00:00', 'Manually passed date stored correctly' );
152
153     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
154
155     {
156         # Hiding the expected warning displayed by DBI
157         # DBD::mysql::st execute failed: Incorrect datetime value: 'bad_date' for column 'returndate'
158         local *STDERR;
159         open STDERR, '>', '/dev/null';
160         throws_ok
161             { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, 'bad_date', 0 ); }
162             'Koha::Exceptions::Object::BadValue',
163             'An exception is thrown on bad date';
164         close STDERR;
165     }
166
167     $schema->storage->txn_rollback;
168 };
169
170 subtest 'AutoRemoveOverduesRestrictions' => sub {
171     plan tests => 2;
172
173     $schema->storage->txn_begin;
174
175     t::lib::Mocks::mock_preference('AutoRemoveOverduesRestrictions', 1);
176
177     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
178     t::lib::Mocks::mock_userenv( { branchcode => $patron->branchcode } );
179     my $item_1 = $builder->build_sample_item;
180     my $item_2 = $builder->build_sample_item;
181     my $item_3 = $builder->build_sample_item;
182     my $five_days_ago = dt_from_string->subtract( days => 5 );
183     my $checkout_1 = AddIssue( $patron->unblessed, $item_1->barcode, $five_days_ago ); # overdue
184     my $checkout_2 = AddIssue( $patron->unblessed, $item_2->barcode, $five_days_ago ); # overdue
185     my $checkout_3 = AddIssue( $patron->unblessed, $item_3->barcode ); # not overdue
186
187     Koha::Patron::Debarments::AddUniqueDebarment(
188         {
189             borrowernumber => $patron->borrowernumber,
190             type           => 'OVERDUES',
191             comment => "OVERDUES_PROCESS simulation",
192         }
193     );
194
195     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
196
197     my $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
198     is( $debarments->[0]->{type}, 'OVERDUES', 'OVERDUES debarment is not removed if patron still has overdues' );
199
200     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
201
202     $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
203     is( scalar @$debarments, 0, 'OVERDUES debarment is removed if patron does not have overdues' );
204
205     $schema->storage->txn_rollback;
206 };