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_object({
52 class => 'Koha::Patrons',
54 branchcode => $library->{branchcode},
55 flags => 1, # superlibrarian
58 t::lib::Mocks::mock_userenv({ patron => $patron });
60 my $patron2 = $builder->build_object({
61 class => 'Koha::Patrons',
63 branchcode => $library->{branchcode},
66 my $patron3 = $builder->build_object({
67 class => 'Koha::Patrons',
69 branchcode => $another_library->{branchcode},
74 # Discharge not possible with issues
75 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
76 my $barcode = 'BARCODE42';
77 $builder->build_sample_item(
79 biblionumber => $biblionumber,
80 library => $library->{branchcode},
86 AddIssue( $patron, $barcode );
87 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }), 0, 'A patron with issues cannot be discharged' );
89 is( Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }), undef, 'No request done if patron has issues' );
90 is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->borrowernumber }), undef, 'No discharge done if patron has issues' );
91 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' );
92 is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' );
94 AddReturn( $barcode );
96 # Discharge possible without issue
97 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }), 1, 'A patron without issues can be discharged' );
99 is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
101 # Verify that the user is not discharged anymore if the restriction has been lifted
102 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
103 Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } );
104 Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } );
105 is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1, 'The patron has been discharged' );
106 is( Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' );
107 is( scalar( Koha::Patron::Discharge::get_validated ), 3, 'There are 3 validated discharges' );
108 is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1, 'There is 1 validated discharge for a given patron' );
109 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
110 Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } );
111 ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' );
112 ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 'The patron is not discharged after the restriction has been lifted' );
114 # Verify that the discharge works multiple times
115 Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
116 is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)');
117 Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } );
118 is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)');
121 skip "Skipping because weasyprint is not installed",
122 5 unless can_run('weasyprint');
125 Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ),
127 "Temporary PDF generated."
130 my $mocked_ipc = Test::MockModule->new('IPC::Cmd');
132 $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
136 { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
138 'Failed call to run() prints the generated error';
140 is( $result, undef, 'undef returned if failed run' );
142 $mocked_ipc->mock( 'can_run', undef );
145 { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
146 'weasyprint not found!',
147 'Expected failure because of missing weasyprint';
149 is( $result, undef, 'undef returned if missing weasyprint' );
152 # FIXME Should be a Koha::Object object
153 is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' );
155 subtest 'search_limited' => sub {
157 $dbh->do(q|DELETE FROM discharges|);
158 my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store;
159 my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store;
160 # $patron and $patron2 are from the same library, $patron3 from another one
161 # Logged in user is $patron, superlibrarian
162 t::lib::Mocks::mock_userenv({ patron => $patron });
163 Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store();
164 Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->branchcode })->store();
165 Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber });
166 Koha::Patron::Discharge::request({ borrowernumber => $patron2->borrowernumber });
167 Koha::Patron::Discharge::request({ borrowernumber => $patron3->borrowernumber });
168 is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' );
169 is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' );
171 # With patron 3 logged in, only discharges from their group are visible
172 t::lib::Mocks::mock_userenv({ patron => $patron3 });
173 is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' );
174 is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' );
177 $schema->storage->txn_rollback;