Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Circulation / MarkIssueReturned.t
blobe9152d3f9feebde6815f85d7bf0b8c2de3e62e20
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 4;
21 use Test::Exception;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
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;
35 my $schema = Koha::Database->schema;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'Failure tests' => sub {
40 plan tests => 5;
42 $schema->storage->txn_begin;
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 }
51 my $item = $builder->build_sample_item(
53 library => $library->branchcode,
57 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
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' );
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' );
74 $schema->storage->txn_rollback;
77 subtest 'Anonymous patron tests' => sub {
79 plan tests => 2;
81 $schema->storage->txn_begin;
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 }
90 my $item = $builder->build_sample_item(
92 library => $library->branchcode,
96 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
98 # Anonymous patron not set
99 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
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;
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
114 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
115 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
117 eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
118 is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
120 $schema->storage->txn_rollback;
123 subtest 'Manually pass a return date' => sub {
125 plan tests => 3;
127 $schema->storage->txn_begin;
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 }
136 my $item = $builder->build_sample_item(
138 library => $library->branchcode,
142 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
144 my ( $issue, $issue_id );
146 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
147 $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
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' );
153 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
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;
167 $schema->storage->txn_rollback;
170 subtest 'AutoRemoveOverduesRestrictions' => sub {
171 plan tests => 2;
173 $schema->storage->txn_begin;
175 t::lib::Mocks::mock_preference('AutoRemoveOverduesRestrictions', 1);
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
187 Koha::Patron::Debarments::AddUniqueDebarment(
189 borrowernumber => $patron->borrowernumber,
190 type => 'OVERDUES',
191 comment => "OVERDUES_PROCESS simulation",
195 C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
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' );
200 C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
202 $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
203 is( scalar @$debarments, 0, 'OVERDUES debarment is removed if patron does not have overdues' );
205 $schema->storage->txn_rollback;