Bug 24413: Add tests
[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;
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 $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
52     my $item       = $builder->build_object(
53         {   class => 'Koha::Items',
54             value  => {
55                 homebranch    => $library->branchcode,
56                 holdingbranch => $library->branchcode,
57                 notforloan    => 0,
58                 itemlost      => 0,
59                 withdrawn     => 0,
60                 biblionumber  => $biblioitem->biblionumber,
61             }
62         }
63     );
64
65     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
66
67     my ( $issue_id, $issue );
68     # The next call will return undef for invalid item number
69     eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, 'invalid_itemnumber', undef, 0 ) };
70     is( $@, '', 'No die triggered by invalid itemnumber' );
71     is( $issue_id, undef, 'No issue_id returned' );
72
73     # In the next call we return the item and try it another time
74     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
75     eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) };
76     is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
77     eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) };
78     is( $@, '', 'No crash on returning item twice' );
79     is( $issue_id, undef, 'Cannot return an item twice' );
80
81
82     $schema->storage->txn_rollback;
83 };
84
85 subtest 'Anonymous patron tests' => sub {
86
87     plan tests => 2;
88
89     $schema->storage->txn_begin;
90
91     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
92     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
93     my $patron   = $builder->build_object(
94         {   class => 'Koha::Patrons',
95             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
96         }
97     );
98     my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
99     my $item       = $builder->build_object(
100         {   class => 'Koha::Items',
101             value  => {
102                 homebranch    => $library->branchcode,
103                 holdingbranch => $library->branchcode,
104                 notforloan    => 0,
105                 itemlost      => 0,
106                 withdrawn     => 0,
107                 biblionumber  => $biblioitem->biblionumber,
108             }
109         }
110     );
111
112     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
113
114     # Anonymous patron not set
115     t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
116
117     my $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
118     eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
119     like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );
120     Koha::Checkouts->find( $issue->issue_id )->delete;
121
122     # Create a valid anonymous user
123     my $anonymous = $builder->build_object({
124         class => 'Koha::Patrons',
125         value => {
126             categorycode => $category->categorycode,
127             branchcode => $library->branchcode
128         }
129     });
130     t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
131     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
132
133     eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
134     is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
135
136     $schema->storage->txn_rollback;
137 };
138
139 subtest 'Manually pass a return date' => sub {
140
141     plan tests => 3;
142
143     $schema->storage->txn_begin;
144
145     my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
146     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
147     my $patron  = $builder->build_object(
148         {   class => 'Koha::Patrons',
149             value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
150         }
151     );
152     my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
153     my $item       = $builder->build_object(
154         {   class => 'Koha::Items',
155             value  => {
156                 homebranch    => $library->branchcode,
157                 holdingbranch => $library->branchcode,
158                 notforloan    => 0,
159                 itemlost      => 0,
160                 withdrawn     => 0,
161                 biblionumber  => $biblioitem->biblionumber,
162             }
163         }
164     );
165
166     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
167
168     my ( $issue, $issue_id );
169
170     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
171     $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
172
173     is( $issue_id, $issue->issue_id, "Item has been returned" );
174     my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
175     is( $old_checkout->returndate, '2018-12-25 00:00:00', 'Manually passed date stored correctly' );
176
177     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
178
179     {
180         # Hiding the expected warning displayed by DBI
181         # DBD::mysql::st execute failed: Incorrect datetime value: 'bad_date' for column 'returndate'
182         local *STDERR;
183         open STDERR, '>', '/dev/null';
184         throws_ok
185             { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, 'bad_date', 0 ); }
186             'Koha::Exceptions::Object::BadValue',
187             'An exception is thrown on bad date';
188         close STDERR;
189     }
190
191     $schema->storage->txn_rollback;
192 };
193
194 subtest 'AutoRemoveOverduesRestrictions' => sub {
195     plan tests => 2;
196
197     t::lib::Mocks::mock_preference('AutoRemoveOverduesRestrictions', 1);
198
199     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
200     t::lib::Mocks::mock_userenv( { branchcode => $patron->branchcode } );
201     my $item_1 = $builder->build_sample_item;
202     my $item_2 = $builder->build_sample_item;
203     my $item_3 = $builder->build_sample_item;
204     my $five_days_ago = dt_from_string->subtract( days => 5 );
205     my $checkout_1 = AddIssue( $patron->unblessed, $item_1->barcode, $five_days_ago ); # overdue
206     my $checkout_2 = AddIssue( $patron->unblessed, $item_2->barcode, $five_days_ago ); # overdue
207     my $checkout_3 = AddIssue( $patron->unblessed, $item_3->barcode ); # not overdue
208
209     Koha::Patron::Debarments::AddUniqueDebarment(
210         {
211             borrowernumber => $patron->borrowernumber,
212             type           => 'OVERDUES',
213             comment => "OVERDUES_PROCESS simulation",
214         }
215     );
216
217     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
218
219     my $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
220     is( $debarments->[0]->{type}, 'OVERDUES', 'OVERDUES debarment is not removed if patron still has overdues' );
221
222     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
223
224     $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
225     is( scalar @$debarments, 0, 'OVERDUES debarment is removed if patron does not have overdues' );
226 };