3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Test::More tests => 19;
22 use C4::Circulation qw( AddIssue AddReturn );
23 use C4::Items qw( AddItem );
24 use C4::Biblio qw( AddBiblio );
28 use Koha::Patron::Discharge;
31 use t::lib::TestBuilder;
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
39 my $dbh = C4::Context->dbh;
40 $dbh->do(q|DELETE FROM discharges|);
42 my $library = $builder->build({ source => 'Branch' });
43 my $another_library = $builder->build({ source => 'Branch' });
44 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
46 C4::Context->_new_userenv('xxx');
47 my $patron = $builder->build({
50 branchcode => $library->{branchcode},
51 flags => 1, # superlibrarian
54 my $p = Koha::Patrons->find( $patron->{borrowernumber} );
55 t::lib::Mocks::mock_userenv({ patron => $p });
57 my $patron2 = $builder->build({
60 branchcode => $library->{branchcode},
63 my $patron3 = $builder->build({
66 branchcode => $another_library->{branchcode},
70 my $p3 = Koha::Patrons->find( $patron3->{borrowernumber} );
72 # Discharge not possible with issues
73 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
74 my $barcode = 'BARCODE42';
75 my ( undef, undef, $itemnumber ) = AddItem(
76 { homebranch => $library->{branchcode},
77 holdingbranch => $library->{branchcode},
84 AddIssue( $patron, $barcode );
85 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' );
87 is( Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} }), undef, 'No request done if patron has issues' );
88 is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
89 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
90 is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
92 AddReturn( $barcode );
94 # Discharge possible without issue
95 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 1, 'A patron without issues can be discharged' );
97 is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
99 # Verify that the user is not discharged anymore if the restriction has been lifted
100 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
101 Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->{borrowernumber} } );
102 Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->{borrowernumber} } );
103 is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 1, 'The patron has been discharged' );
104 is( Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
105 is( scalar( Koha::Patron::Discharge::get_validated ), 3, 'There are 3 validated discharges' );
106 is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->{borrowernumber} } ) ), 1, 'There is 1 validated discharge for a given patron' );
107 is( scalar( Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) ), 2, 'There is 2 validated discharges for a given branchcode' ); # This is not used in the code yet
108 Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->{borrowernumber}, 'type' => 'DISCHARGE' } );
109 ok( !Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, 'The debarment has been lifted' );
110 ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
112 # Verify that the discharge works multiple times
113 Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
114 is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
115 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
116 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
118 # Check if PDF::FromHTML is installed.
119 my $check = eval { require PDF::FromHTML; };
121 # Tests for if PDF::FromHTML is installed
123 isnt( Koha::Patron::Discharge::generate_as_pdf({ borrowernumber => $patron->{borrowernumber} }), undef, "Temporary PDF generated." );
125 # Tests for if PDF::FromHTML is not installed
127 warning_like { Koha::Patron::Discharge::generate_as_pdf({ borrowernumber => $patron->{borrowernumber}, testing => 1 }) }
128 [ qr/Can't locate PDF\/FromHTML.pm in \@INC/ ],
129 "Expected failure because of missing PDF::FromHTML.";
132 # FIXME Should be a Koha::Object object
133 is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
135 subtest 'search_limited' => sub {
137 $dbh->do(q|DELETE FROM discharges|);
138 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
139 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
140 # $patron and $patron2 are from the same library, $patron3 from another one
141 # Logged in user is $patron, superlibrarian
142 t::lib::Mocks::mock_userenv({ patron => $p });
143 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->{branchcode} })->store();
144 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->{branchcode} })->store();
145 Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
146 Koha::Patron::Discharge::request({ borrowernumber => $patron2->{borrowernumber} });
147 Koha::Patron::Discharge::request({ borrowernumber => $patron3->{borrowernumber} });
148 is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
149 is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
151 # With patron 3 logged in, only discharges from their group are visible
152 t::lib::Mocks::mock_userenv({ patron => $p3 });
153 is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
154 is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
157 $schema->storage->txn_rollback;