Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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( GetDebarments 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->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 => 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->unblessed, $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->unblessed, $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->unblessed, $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->unblessed, $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 => 2;
177
178     $schema->storage->txn_begin;
179
180     t::lib::Mocks::mock_preference('AutoRemoveOverduesRestrictions', 1);
181
182     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
183     t::lib::Mocks::mock_userenv( { branchcode => $patron->branchcode } );
184     my $item_1 = $builder->build_sample_item;
185     my $item_2 = $builder->build_sample_item;
186     my $item_3 = $builder->build_sample_item;
187     my $five_days_ago = dt_from_string->subtract( days => 5 );
188     my $checkout_1 = AddIssue( $patron->unblessed, $item_1->barcode, $five_days_ago ); # overdue
189     my $checkout_2 = AddIssue( $patron->unblessed, $item_2->barcode, $five_days_ago ); # overdue
190     my $checkout_3 = AddIssue( $patron->unblessed, $item_3->barcode ); # not overdue
191
192     Koha::Patron::Debarments::AddUniqueDebarment(
193         {
194             borrowernumber => $patron->borrowernumber,
195             type           => 'OVERDUES',
196             comment => "OVERDUES_PROCESS simulation",
197         }
198     );
199
200     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
201
202     my $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
203     is( $debarments->[0]->{type}, 'OVERDUES', 'OVERDUES debarment is not removed if patron still has overdues' );
204
205     C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
206
207     $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
208     is( scalar @$debarments, 0, 'OVERDUES debarment is removed if patron does not have overdues' );
209
210     $schema->storage->txn_rollback;
211 };