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>.
20 use Test::More tests => 6;
24 use t::lib::TestBuilder;
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
37 my $library = $builder->build({ source => 'Branch' });
39 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
41 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
42 my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
44 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
45 my $item = $builder->build(
49 homebranch => $library->{branchcode},
50 holdingbranch => $library->{branchcode},
54 biblionumber => $biblioitem->{biblionumber},
59 subtest 'anonymous patron' => sub {
61 # The next call will raise an error, because data are not correctly set
62 t::lib::Mocks::mock_preference('AnonymousPatron', '');
63 my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
64 eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
65 like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'AnonymousPatron is not set - Fatal error on anonymization' );
66 Koha::Checkouts->find( $issue->issue_id )->delete;
68 my $anonymous_borrowernumber = Koha::Patron->new({categorycode => $patron_category->{categorycode}, branchcode => $library->{branchcode} })->store->borrowernumber;
69 t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_borrowernumber);
70 $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
71 eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
72 is ( $@, q||, 'AnonymousPatron is set correctly - no error expected');
75 my ( $issue_id, $issue );
76 # The next call will return undef for invalid item number
77 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, 'invalid_itemnumber', 'dropbox_branch', 'returndate', 0 ) };
78 is( $@, '', 'No die triggered by invalid itemnumber' );
79 is( $issue_id, undef, 'No issue_id returned' );
81 # In the next call we return the item and try it another time
82 $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
83 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
84 is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
85 eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
86 is( $@, '', 'No crash on returning item twice' );
87 is( $issue_id, undef, 'Cannot return an item twice' );
89 $schema->storage->txn_rollback;