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 => 17;
22 use C4::Biblio qw( AddBiblio );
23 use C4::Circulation qw( AddIssue AddReturn );
25 use C4::Items qw( AddItem );
26 use C4::Members qw( AddMember GetMember );
28 use t::lib::TestBuilder;
29 use Koha::Borrower::Discharge;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
37 my $dbh = C4::Context->dbh;
38 $dbh->do(q|DELETE FROM discharges|);
40 C4::Context->_new_userenv('xxx');
41 C4::Context->set_userenv(0, 0, 0, 'firstname', 'surname', 'CPL', 'CPL', '', '', '', '', '');
42 my $branchcode = 'CPL';
43 my $another_branchcode = 'MPL';
44 my $borrower = $builder->build({
47 branchcode => $branchcode,
50 my $borrower2 = $builder->build({
53 branchcode => $branchcode,
56 my $borrower3 = $builder->build({
59 branchcode => $another_branchcode,
63 # Discharge not possible with issues
64 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
65 my $barcode = 'BARCODE42';
66 my ( undef, undef, $itemnumber ) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL', barcode => $barcode }, $biblionumber);
67 AddIssue( $borrower, $barcode );
68 is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrower->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' );
70 is( Koha::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} }), undef, 'No request done if patron has issues' );
71 is( Koha::Borrower::Discharge::discharge({ borrowernumber => $borrower->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
72 is_deeply( Koha::Borrower::Discharge::get_pendings(), [], 'There is no pending discharge request' );
73 is_deeply( Koha::Borrower::Discharge::get_validated(), [], 'There is no validated discharge' );
75 AddReturn( $barcode );
77 # Discharge possible without issue
78 is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrower->{borrowernumber} }), 1, 'A patron without issues can be discharged' );
80 is(Koha::Borrower::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
82 # Verify that the user is not discharged anymore if the restriction has been lifted
83 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower->{borrowernumber} } );
84 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower2->{borrowernumber} } );
85 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower3->{borrowernumber} } );
86 is( Koha::Borrower::Discharge::is_discharged( { borrowernumber => $borrower->{borrowernumber} } ), 1, 'The patron has been discharged' );
87 is( Koha::Borrower::Debarments::IsDebarred( $borrower->{borrowernumber} ), '9999-12-31', 'The patron has been debarred after discharge' );
88 is( scalar( @{ Koha::Borrower::Discharge::get_validated() } ), 3, 'There are 3 validated discharges' );
89 is( scalar( @{ Koha::Borrower::Discharge::get_validated( { borrowernumber => $borrower->{borrowernumber} } ) } ), 1, 'There is 1 validated discharge for a given patron' );
90 is( scalar( @{ Koha::Borrower::Discharge::get_validated( { branchcode => 'CPL' } ) } ), 2, 'There is 2 validated discharges for a given branchcode' ); # This is not used in the code yet
91 Koha::Borrower::Debarments::DelUniqueDebarment( { 'borrowernumber' => $borrower->{borrowernumber}, 'type' => 'DISCHARGE' } );
92 ok( !Koha::Borrower::Debarments::IsDebarred( $borrower->{borrowernumber} ), 'The debarment has been lifted' );
93 ok( !Koha::Borrower::Discharge::is_discharged( { borrowernumber => $borrower->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
95 # Verify that the discharge works multiple times
96 Koha::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} });
97 is(scalar( @{ Koha::Borrower::Discharge::get_pendings() }), 1, 'There is a pending discharge request (second time)');
98 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower->{borrowernumber} } );
99 is_deeply( Koha::Borrower::Discharge::get_pendings(), [], 'There is no pending discharge request (second time)');
101 # Check if PDF::FromHTML is installed.
102 my $check = eval { require PDF::FromHTML; };
104 # Tests for if PDF::FromHTML is installed
106 isnt( Koha::Borrower::Discharge::generate_as_pdf({ borrowernumber => $borrower->{borrowernumber} }), undef, "Temporary PDF generated." );
108 # Tests for if PDF::FromHTML is not installed
110 warning_like { Koha::Borrower::Discharge::generate_as_pdf({ borrowernumber => $borrower->{borrowernumber}, testing => 1 }) }
111 [ qr/Can't locate PDF\/FromHTML.pm in \@INC/ ],
112 "Expected failure because of missing PDF::FromHTML.";
116 # At this point, there is a problem with the AutoCommit off
117 # The transaction is bloked into DBIx::Class::Storage::DBI::_dbh_execute
118 # line my $rv = $sth->execute();
119 # We are using 2 connections and the one used by Koha::Schema has the AutoCommit set to 1
120 # Even if we switch off this flag, the connection will be blocked.
122 # DBIx::Class::ResultSet::create(): DBI Exception: DBD::mysql::st execute failed: Lock wait timeout exceeded; try restarting transaction [for Statement "INSERT INTO discharges ( borrower, needed, validated) VALUES ( ?, ?, ? )" with ParamValues: 0='121', 1='2014-01-08T16:38:29', 2=undef] at /home/koha/src/Koha/DataObject/Discharge.pm line 33
123 #is( Koha::Service::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} }), 1, 'Discharge request sent' );
125 $schema->storage->txn_rollback;