Bug 19513: Re-add AnonymousPatron-related tests
[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 => 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 C4::Members;
29 use Koha::Checkouts;
30 use Koha::Database;
31
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new;
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
60 subtest 'anonymous patron' => sub {
61     plan tests => 2;
62     # The next call will raise an error, because data are not correctly set
63     t::lib::Mocks::mock_preference('AnonymousPatron', '');
64     my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
65     eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
66     like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );
67     Koha::Checkouts->find( $issue->issue_id )->delete;
68
69     my $anonymous_borrowernumber = C4::Members::AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library->{branchcode} );
70     t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_borrowernumber);
71     $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
72     eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
73     is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
74 };
75
76 my ( $issue_id, $issue );
77 # The next call will return undef for invalid item number
78 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, 'invalid_itemnumber', 'dropbox_branch', 'returndate', 0 ) };
79 is( $@, '', 'No die triggered by invalid itemnumber' );
80 is( $issue_id, undef, 'No issue_id returned' );
81
82 # In the next call we return the item and try it another time
83 $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
84 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
85 is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
86 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
87 is( $@, '', 'No crash on returning item twice' );
88 is( $issue_id, undef, 'Cannot return an item twice' );
89
90 $schema->storage->txn_rollback;