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>.
19 use Test::More tests => 23;
24 use IPC::Cmd qw(can_run);
27 use C4::Circulation qw( AddIssue AddReturn );
28 use C4::Biblio qw( AddBiblio );
32 use Koha::Patron::Discharge;
35 use t::lib::TestBuilder;
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new;
43 my $dbh = C4::Context->dbh;
44 $dbh->do(q|DELETE FROM discharges|);
46 my $library = $builder->build({ source => 'Branch' });
47 my $another_library = $builder->build({ source => 'Branch' });
48 my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
50 C4::Context->_new_userenv('xxx');
51 my $patron = $builder->build({
54 branchcode => $library->{branchcode},
55 flags => 1, # superlibrarian
58 my $p = Koha::Patrons->find( $patron->{borrowernumber} );
59 t::lib::Mocks::mock_userenv({ patron => $p });
61 my $patron2 = $builder->build({
64 branchcode => $library->{branchcode},
67 my $patron3 = $builder->build({
70 branchcode => $another_library->{branchcode},
74 my $p3 = Koha::Patrons->find( $patron3->{borrowernumber} );
76 # Discharge not possible with issues
77 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
78 my $barcode = 'BARCODE42';
79 $builder->build_sample_item(
81 biblionumber => $biblionumber,
82 library => $library->{branchcode},
88 AddIssue( $patron, $barcode );
89 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' );
91 is( Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} }), undef, 'No request done if patron has issues' );
92 is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->{borrowernumber} }), undef, 'No discharge done if patron has issues' );
93 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
94 is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
96 AddReturn( $barcode );
98 # Discharge possible without issue
99 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 1, 'A patron without issues can be discharged' );
101 is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
103 # Verify that the user is not discharged anymore if the restriction has been lifted
104 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
105 Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->{borrowernumber} } );
106 Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->{borrowernumber} } );
107 is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 1, 'The patron has been discharged' );
108 is( Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
109 is( scalar( Koha::Patron::Discharge::get_validated ), 3, 'There are 3 validated discharges' );
110 is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->{borrowernumber} } ) ), 1, 'There is 1 validated discharge for a given patron' );
111 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
112 Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->{borrowernumber}, 'type' => 'DISCHARGE' } );
113 ok( !Koha::Patrons->find( $patron->{borrowernumber} )->is_debarred, 'The debarment has been lifted' );
114 ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->{borrowernumber} } ), 'The patron is not discharged after the restriction has been lifted' );
116 # Verify that the discharge works multiple times
117 Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
118 is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
119 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->{borrowernumber} } );
120 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
123 skip "Skipping because weasyprint is not installed",
124 5 unless can_run('weasyprint');
127 Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ),
129 "Temporary PDF generated."
132 my $mocked_ipc = Test::MockModule->new('IPC::Cmd');
134 $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
138 { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ); }
140 'Failed call to run() prints the generated error';
142 is( $result, undef, 'undef returned if failed run' );
144 $mocked_ipc->mock( 'can_run', undef );
147 { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ); }
148 'weasyprint not found!',
149 'Expected failure because of missing weasyprint';
151 is( $result, undef, 'undef returned if missing weasyprint' );
154 # FIXME Should be a Koha::Object object
155 is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
157 subtest 'search_limited' => sub {
159 $dbh->do(q|DELETE FROM discharges|);
160 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
161 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
162 # $patron and $patron2 are from the same library, $patron3 from another one
163 # Logged in user is $patron, superlibrarian
164 t::lib::Mocks::mock_userenv({ patron => $p });
165 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->{branchcode} })->store();
166 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->{branchcode} })->store();
167 Koha::Patron::Discharge::request({ borrowernumber => $patron->{borrowernumber} });
168 Koha::Patron::Discharge::request({ borrowernumber => $patron2->{borrowernumber} });
169 Koha::Patron::Discharge::request({ borrowernumber => $patron3->{borrowernumber} });
170 is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
171 is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
173 # With patron 3 logged in, only discharges from their group are visible
174 t::lib::Mocks::mock_userenv({ patron => $p3 });
175 is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
176 is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
179 $schema->storage->txn_rollback;