Bug 15244: t/db_dependent/Reserves.t depends on external data/configuration
[koha.git] / t / db_dependent / Borrower_Discharge.t
blob99cb58a5ebd5793af0fad66a2662059e0ca0a62c
1 #!/usr/bin/perl
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
8 # version.
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>.
17 use Modern::Perl;
18 use Test::More tests => 17;
19 use Test::Warn;
20 use MARC::Record;
22 use C4::Biblio qw( AddBiblio );
23 use C4::Circulation qw( AddIssue AddReturn );
24 use C4::Context;
25 use C4::Items qw( AddItem );
26 use C4::Members qw( AddMember GetMember );
28 use Koha::Borrower::Discharge;
29 use Koha::Database;
31 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 my $dbh = C4::Context->dbh;
39 $dbh->do(q|DELETE FROM discharges|);
41 my $library = $builder->build({
42 source => 'Branch',
43 });
44 my $another_library = $builder->build({
45 source => 'Branch',
46 });
48 C4::Context->_new_userenv('xxx');
49 C4::Context->set_userenv(0, 0, 0, 'firstname', 'surname', $library->{branchcode}, $library->{branchcode}, '', '', '', '', '');
50 my $borrower = $builder->build({
51 source => 'Borrower',
52 value => {
53 branchcode => $library->{branchcode},
55 });
56 my $borrower2 = $builder->build({
57 source => 'Borrower',
58 value => {
59 branchcode => $library->{branchcode},
61 });
62 my $borrower3 = $builder->build({
63 source => 'Borrower',
64 value => {
65 branchcode => $another_library->{branchcode},
67 });
69 # Discharge not possible with issues
70 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
71 my $barcode = 'BARCODE42';
72 my ( undef, undef, $itemnumber ) = AddItem({ homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $barcode }, $biblionumber);
73 AddIssue( $borrower, $barcode );
74 is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrower->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' );
76 is( Koha::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} }), undef, 'No request done if patron has issues' );
77 is( Koha::Borrower::Discharge::discharge({ borrowernumber => $borrower->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
78 is_deeply( Koha::Borrower::Discharge::get_pendings(), [], 'There is no pending discharge request' );
79 is_deeply( Koha::Borrower::Discharge::get_validated(), [], 'There is no validated discharge' );
81 AddReturn( $barcode );
83 # Discharge possible without issue
84 is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrower->{borrowernumber} }), 1, 'A patron without issues can be discharged' );
86 is(Koha::Borrower::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
88 # Verify that the user is not discharged anymore if the restriction has been lifted
89 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower->{borrowernumber} } );
90 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower2->{borrowernumber} } );
91 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower3->{borrowernumber} } );
92 is( Koha::Borrower::Discharge::is_discharged( { borrowernumber => $borrower->{borrowernumber} } ), 1, 'The patron has been discharged' );
93 is( Koha::Borrower::Debarments::IsDebarred( $borrower->{borrowernumber} ), '9999-12-31', 'The patron has been debarred after discharge' );
94 is( scalar( @{ Koha::Borrower::Discharge::get_validated() } ), 3, 'There are 3 validated discharges' );
95 is( scalar( @{ Koha::Borrower::Discharge::get_validated( { borrowernumber => $borrower->{borrowernumber} } ) } ), 1, 'There is 1 validated discharge for a given patron' );
96 is( scalar( @{ Koha::Borrower::Discharge::get_validated( { branchcode => $library->{branchcode} } ) } ), 2, 'There is 2 validated discharges for a given branchcode' ); # This is not used in the code yet
97 Koha::Borrower::Debarments::DelUniqueDebarment( { 'borrowernumber' => $borrower->{borrowernumber}, 'type' => 'DISCHARGE' } );
98 ok( !Koha::Borrower::Debarments::IsDebarred( $borrower->{borrowernumber} ), 'The debarment has been lifted' );
99 ok( !Koha::Borrower::Discharge::is_discharged( { borrowernumber => $borrower->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
101 # Verify that the discharge works multiple times
102 Koha::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} });
103 is(scalar( @{ Koha::Borrower::Discharge::get_pendings() }), 1, 'There is a pending discharge request (second time)');
104 Koha::Borrower::Discharge::discharge( { borrowernumber => $borrower->{borrowernumber} } );
105 is_deeply( Koha::Borrower::Discharge::get_pendings(), [], 'There is no pending discharge request (second time)');
107 # Check if PDF::FromHTML is installed.
108 my $check = eval { require PDF::FromHTML; };
110 # Tests for if PDF::FromHTML is installed
111 if ($check) {
112 isnt( Koha::Borrower::Discharge::generate_as_pdf({ borrowernumber => $borrower->{borrowernumber} }), undef, "Temporary PDF generated." );
114 # Tests for if PDF::FromHTML is not installed
115 else {
116 warning_like { Koha::Borrower::Discharge::generate_as_pdf({ borrowernumber => $borrower->{borrowernumber}, testing => 1 }) }
117 [ qr/Can't locate PDF\/FromHTML.pm in \@INC/ ],
118 "Expected failure because of missing PDF::FromHTML.";
121 # FIXME
122 # At this point, there is a problem with the AutoCommit off
123 # The transaction is bloked into DBIx::Class::Storage::DBI::_dbh_execute
124 # line my $rv = $sth->execute();
125 # We are using 2 connections and the one used by Koha::Schema has the AutoCommit set to 1
126 # Even if we switch off this flag, the connection will be blocked.
127 # The error is:
128 # 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
129 #is( Koha::Service::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} }), 1, 'Discharge request sent' );
131 $schema->storage->txn_rollback;