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>.
20 use Test::More tests => 4;
24 use t::lib::TestBuilder;
30 use Koha::DateUtils qw(dt_from_string);
31 use Koha::Old::Checkouts;
33 use Koha::Patron::Debarments;
35 my $schema = Koha::Database->schema;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'Failure tests' => sub {
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 $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
52 my $item = $builder->build_object(
53 { class => 'Koha::Items',
55 homebranch => $library->branchcode,
56 holdingbranch => $library->branchcode,
60 biblionumber => $biblioitem->biblionumber,
65 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
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' );
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' );
82 $schema->storage->txn_rollback;
85 subtest 'Anonymous patron tests' => sub {
89 $schema->storage->txn_begin;
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 }
98 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
99 my $item = $builder->build_object(
100 { class => 'Koha::Items',
102 homebranch => $library->branchcode,
103 holdingbranch => $library->branchcode,
107 biblionumber => $biblioitem->biblionumber,
112 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
114 # Anonymous patron not set
115 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
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;
122 # Create a valid anonymous user
123 my $anonymous = $builder->build_object({
124 class => 'Koha::Patrons',
126 categorycode => $category->categorycode,
127 branchcode => $library->branchcode
130 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
131 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
133 eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
134 is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
136 $schema->storage->txn_rollback;
139 subtest 'Manually pass a return date' => sub {
143 $schema->storage->txn_begin;
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 }
152 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
153 my $item = $builder->build_object(
154 { class => 'Koha::Items',
156 homebranch => $library->branchcode,
157 holdingbranch => $library->branchcode,
161 biblionumber => $biblioitem->biblionumber,
166 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
168 my ( $issue, $issue_id );
170 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
171 $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
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' );
177 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
180 # Hiding the expected warning displayed by DBI
181 # DBD::mysql::st execute failed: Incorrect datetime value: 'bad_date' for column 'returndate'
183 open STDERR, '>', '/dev/null';
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';
191 $schema->storage->txn_rollback;
194 subtest 'AutoRemoveOverduesRestrictions' => sub {
197 t::lib::Mocks::mock_preference('AutoRemoveOverduesRestrictions', 1);
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
209 Koha::Patron::Debarments::AddUniqueDebarment(
211 borrowernumber => $patron->borrowernumber,
213 comment => "OVERDUES_PROCESS simulation",
217 C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_1->itemnumber );
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' );
222 C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item_2->itemnumber );
224 $debarments = Koha::Patron::Debarments::GetDebarments({ borrowernumber => $patron->borrowernumber });
225 is( scalar @$debarments, 0, 'OVERDUES debarment is removed if patron does not have overdues' );