Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Patron / Borrower_Discharge.t
blob8c7421953774325333fbfb4222c7eb0fcb27e7d3
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 => 19;
19 use Test::Warn;
20 use MARC::Record;
22 use C4::Circulation qw( AddIssue AddReturn );
23 use C4::Biblio qw( AddBiblio );
24 use C4::Context;
26 use Koha::Patrons;
27 use Koha::Patron::Discharge;
28 use Koha::Database;
30 use t::lib::TestBuilder;
31 use t::lib::Mocks;
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({ source => 'Branch' });
42 my $another_library = $builder->build({ source => 'Branch' });
43 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
45 C4::Context->_new_userenv('xxx');
46 my $patron = $builder->build({
47 source => 'Borrower',
48 value => {
49 branchcode => $library->{branchcode},
50 flags => 1, # superlibrarian
52 });
53 my $p = Koha::Patrons->find( $patron->{borrowernumber} );
54 t::lib::Mocks::mock_userenv({ patron => $p });
56 my $patron2 = $builder->build({
57 source => 'Borrower',
58 value => {
59 branchcode => $library->{branchcode},
61 });
62 my $patron3 = $builder->build({
63 source => 'Borrower',
64 value => {
65 branchcode => $another_library->{branchcode},
66 flags => undef,
68 });
69 my $p3 = Koha::Patrons->find( $patron3->{borrowernumber} );
71 # Discharge not possible with issues
72 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
73 my $barcode = 'BARCODE42';
74 $builder->build_sample_item(
76 biblionumber => $biblionumber,
77 library => $library->{branchcode},
78 barcode => $barcode,
79 itype => $itemtype
83 AddIssue( $patron, $barcode );
84 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' );
86 is( Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} }), undef, 'No request done if patron has issues' );
87 is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
88 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
89 is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
91 AddReturn( $barcode );
93 # Discharge possible without issue
94 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 1, 'A patron without issues can be discharged' );
96 is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
98 # Verify that the user is not discharged anymore if the restriction has been lifted
99 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
100 Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->{borrowernumber} } );
101 Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->{borrowernumber} } );
102 is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 1, 'The patron has been discharged' );
103 is( Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
104 is( scalar( Koha::Patron::Discharge::get_validated ), 3, 'There are 3 validated discharges' );
105 is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->{borrowernumber} } ) ), 1, 'There is 1 validated discharge for a given patron' );
106 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
107 Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->{borrowernumber}, 'type' => 'DISCHARGE' } );
108 ok( !Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, 'The debarment has been lifted' );
109 ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
111 # Verify that the discharge works multiple times
112 Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
113 is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
114 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
115 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
117 # Check if PDF::FromHTML is installed.
118 my $check = eval { require PDF::FromHTML; };
120 # Tests for if PDF::FromHTML is installed
121 if ($check) {
122 isnt( Koha::Patron::Discharge::generate_as_pdf({ borrowernumber => $patron->{borrowernumber} }), undef, "Temporary PDF generated." );
124 # Tests for if PDF::FromHTML is not installed
125 else {
126 warning_like { Koha::Patron::Discharge::generate_as_pdf({ borrowernumber => $patron->{borrowernumber}, testing => 1 }) }
127 [ qr/Can't locate PDF\/FromHTML.pm in \@INC/ ],
128 "Expected failure because of missing PDF::FromHTML.";
131 # FIXME Should be a Koha::Object object
132 is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
134 subtest 'search_limited' => sub {
135 plan tests => 4;
136 $dbh->do(q|DELETE FROM discharges|);
137 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
138 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
139 # $patron and $patron2 are from the same library, $patron3 from another one
140 # Logged in user is $patron, superlibrarian
141 t::lib::Mocks::mock_userenv({ patron => $p });
142 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->{branchcode} })->store();
143 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->{branchcode} })->store();
144 Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
145 Koha::Patron::Discharge::request({ borrowernumber => $patron2->{borrowernumber} });
146 Koha::Patron::Discharge::request({ borrowernumber => $patron3->{borrowernumber} });
147 is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
148 is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
150 # With patron 3 logged in, only discharges from their group are visible
151 t::lib::Mocks::mock_userenv({ patron => $p3 });
152 is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
153 is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
156 $schema->storage->txn_rollback;