Bug 19487: Fix MarkIssueReturned.t
[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 => 2;
21 use Test::Warn;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use C4::Circulation;
27 use C4::Members;
28 use Koha::Library;
29
30 my $schema = Koha::Database->schema;
31 my $dbh = C4::Context->dbh;
32
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new;
36
37 t::lib::Mocks::mock_preference('AnonymousPatron', '');
38
39 my $library = $builder->build({ source => 'Branch' });
40 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
41
42 C4::Context->_new_userenv('xxx');
43 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
44
45 my %item_branch_infos = (
46     homebranch => $library->{branchcode},
47     holdingbranch => $library->{branchcode},
48 );
49
50 my $borrowernumber = AddMember( categorycode => $categorycode, branchcode => $library->{branchcode} );
51 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
52     my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
53
54 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
55 my $item = $builder->build(
56     {
57         source => 'Item',
58         value  => {
59             homebranch    => $library->{branchcode},
60             holdingbranch => $library->{branchcode},
61             notforloan    => 0,
62             itemlost      => 0,
63             withdrawn     => 0,
64             biblionumber  => $biblioitem->{biblionumber},
65         }
66     }
67 );
68 C4::Circulation::AddIssue( $patron, $item->{barcode} );
69
70 eval { C4::Circulation::MarkIssueReturned( $borrowernumber, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
71 like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, );
72
73 my $anonymous_borrowernumber = AddMember( categorycode => $categorycode, branchcode => $library->{branchcode} );
74 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_borrowernumber);
75 # The next call will raise an error, because data are not correctly set
76 $dbh->{PrintError} = 0;
77 eval { C4::Circulation::MarkIssueReturned( $borrowernumber, 'itemnumber', 'dropbox_branch', 'returndate', 2 ) };
78 unlike ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, );
79
80 $schema->storage->txn_rollback;
81