Bug 23038: Remove unused stderr vars
[srvgit] / 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 => 3;
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::Old::Checkouts;
31 use Koha::Patrons;
32
33 my $schema = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'Failure tests' => sub {
37
38     plan tests => 5;
39
40     $schema->storage->txn_begin;
41
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 }
47         }
48     );
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,
59             }
60         }
61     );
62
63     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
64
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' );
70
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' );
78
79
80     $schema->storage->txn_rollback;
81 };
82
83 subtest 'Anonymous patron tests' => sub {
84
85     plan tests => 2;
86
87     $schema->storage->txn_begin;
88
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 }
94         }
95     );
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,
106             }
107         }
108     );
109
110     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
111
112     # Anonymous patron not set
113     t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
114
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;
119
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
126         }
127     });
128     t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous->borrowernumber);
129     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
130
131     eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) };
132     is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
133
134     $schema->storage->txn_rollback;
135 };
136
137 subtest 'Manually pass a return date' => sub {
138
139     plan tests => 3;
140
141     $schema->storage->txn_begin;
142
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 }
148         }
149     );
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,
160             }
161         }
162     );
163
164     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode });
165
166     my ( $issue, $issue_id );
167
168     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
169     $issue_id = C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, '2018-12-25', 0 );
170
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' );
174
175     $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
176
177     {
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;
187     }
188
189     $schema->storage->txn_rollback;
190 };