Bug 22132: (QA follow-up) Tests - use Mojo builtin for auth
[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::Checkouts;
29 use Koha::Database;
30 use Koha::Patrons;
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 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
40
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} } } );
43
44 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
45 my $item = $builder->build(
46     {
47         source => 'Item',
48         value  => {
49             homebranch    => $library->{branchcode},
50             holdingbranch => $library->{branchcode},
51             notforloan    => 0,
52             itemlost      => 0,
53             withdrawn     => 0,
54             biblionumber  => $biblioitem->{biblionumber},
55         }
56     }
57 );
58
59 subtest 'anonymous patron' => sub {
60     plan tests => 2;
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;
67
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');
73 };
74
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' );
80
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' );
88
89 $schema->storage->txn_rollback;