Bug 19513: More changes to MarkIssueReturned.t (after bug 19487)
[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 => 6;
21 use Test::Warn;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use C4::Circulation;
27 use C4::Context;
28 use Koha::Database;
29
30 my $schema = Koha::Database->schema;
31 $schema->storage->txn_begin;
32
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference('AnonymousPatron', '');
36
37 my $library = $builder->build({ source => 'Branch' });
38
39 C4::Context->_new_userenv('xxx');
40 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
41
42 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
43 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
44
45 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
46 my $item = $builder->build(
47     {
48         source => 'Item',
49         value  => {
50             homebranch    => $library->{branchcode},
51             holdingbranch => $library->{branchcode},
52             notforloan    => 0,
53             itemlost      => 0,
54             withdrawn     => 0,
55             biblionumber  => $biblioitem->{biblionumber},
56         }
57     }
58 );
59 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
60
61 eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
62 like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'Fatal error on anonymization' );
63
64 # The next call will return undef for invalid item number
65 my $issue_id;
66 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, 'invalid_itemnumber', 'dropbox_branch', 'returndate', 0 ) };
67 is( $@, '', 'No die triggered by invalid itemnumber' );
68 is( $issue_id, undef, 'No issue_id returned' );
69
70 # In the next call we return the item and try it another time
71 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
72 is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
73 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
74 is( $@, '', 'No crash on returning item twice' );
75 is( $issue_id, undef, 'Cannot return an item twice' );
76
77 $schema->storage->txn_rollback;