Bug 20434: Update UNIMARC framework - authorised values
[koha.git] / t / db_dependent / Circulation / MarkIssueReturned.t
blob26c8a0629168840666802de8f442244da0ee90b0
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 => 3;
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::Old::Checkouts;
31 use Koha::Patrons;
33 my $schema = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'Failure tests' => sub {
38 plan tests => 5;
40 $schema->storage->txn_begin;
42 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
43 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
44 my $patron = $builder->build_object(
45 { class => 'Koha::Patrons',
46 value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
49 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
50 my $item = $builder->build_object(
51 { class => 'Koha::Items',
52 value => {
53 homebranch => $library->branchcode,
54 holdingbranch => $library->branchcode,
55 notforloan => 0,
56 itemlost => 0,
57 withdrawn => 0,
58 biblionumber => $biblioitem->biblionumber,
63 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
65 my ( $issue_id, $issue );
66 # The next call will return undef for invalid item number
67 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, 'invalid_itemnumber', undef, 0 ) };
68 is( $@, '', 'No die triggered by invalid itemnumber' );
69 is( $issue_id, undef, 'No issue_id returned' );
71 # In the next call we return the item and try it another time
72 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
73 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) };
74 is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
75 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 0 ) };
76 is( $@, '', 'No crash on returning item twice' );
77 is( $issue_id, undef, 'Cannot return an item twice' );
80 $schema->storage->txn_rollback;
83 subtest 'Anonymous patron tests' => sub {
85 plan tests => 2;
87 $schema->storage->txn_begin;
89 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
90 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
91 my $patron = $builder->build_object(
92 { class => 'Koha::Patrons',
93 value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
96 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
97 my $item = $builder->build_object(
98 { class => 'Koha::Items',
99 value => {
100 homebranch => $library->branchcode,
101 holdingbranch => $library->branchcode,
102 notforloan => 0,
103 itemlost => 0,
104 withdrawn => 0,
105 biblionumber => $biblioitem->biblionumber,
110 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
112 # Anonymous patron not set
113 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
115 my $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
116 eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
117 like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );
118 Koha::Checkouts->find( $issue->issue_id )->delete;
120 # Create a valid anonymous user
121 my $anonymous = $builder->build_object({
122 class => 'Koha::Patrons',
123 value => {
124 categorycode => $category->categorycode,
125 branchcode => $library->branchcode
128 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
129 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
131 eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
132 is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
134 $schema->storage->txn_rollback;
137 subtest 'Manually pass a return date' => sub {
139 plan tests => 3;
141 $schema->storage->txn_begin;
143 my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => { category_type => 'P', enrolmentfee => 0 } } );
144 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
145 my $patron = $builder->build_object(
146 { class => 'Koha::Patrons',
147 value => { branchcode => $library->branchcode, categorycode => $category->categorycode }
150 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
151 my $item = $builder->build_object(
152 { class => 'Koha::Items',
153 value => {
154 homebranch => $library->branchcode,
155 holdingbranch => $library->branchcode,
156 notforloan => 0,
157 itemlost => 0,
158 withdrawn => 0,
159 biblionumber => $biblioitem->biblionumber,
164 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
166 my ( $issue, $issue_id );
168 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
169 $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
171 is( $issue_id, $issue->issue_id, "Item has been returned" );
172 my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
173 is( $old_checkout->returndate, '2018-12-25 00:00:00', 'Manually passed date stored correctly' );
175 $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
178 # Hiding the expected warning displayed by DBI
179 # DBD::mysql::st execute failed: Incorrect datetime value: 'bad_date' for column 'returndate'
180 local *STDERR;
181 open STDERR, '>', '/dev/null';
182 throws_ok
183 { $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, 'bad_date', 0 ); }
184 'Koha::Exceptions::Object::BadValue',
185 'An exception is thrown on bad date';
186 close STDERR;
189 $schema->storage->txn_rollback;