Bug 34223: (QA follow-up) Fix file permissions
[koha.git] / t / db_dependent / Patron / Borrower_Discharge.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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>.
16
17 use Modern::Perl;
18
19 use Test::More tests => 23;
20
21 use Test::MockModule;
22 use Test::Warn;
23
24 use IPC::Cmd qw(can_run);
25 use MARC::Record;
26
27 use C4::Circulation qw( AddIssue AddReturn );
28 use C4::Biblio qw( AddBiblio );
29 use C4::Context;
30
31 use Koha::Patrons;
32 use Koha::Patron::Discharge;
33 use Koha::Database;
34
35 use t::lib::TestBuilder;
36 use t::lib::Mocks;
37
38 my $schema  = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40
41 my $builder = t::lib::TestBuilder->new;
42
43 my $dbh = C4::Context->dbh;
44 $dbh->do(q|DELETE FROM discharges|);
45
46 my $library         = $builder->build({ source => 'Branch' });
47 my $another_library = $builder->build({ source => 'Branch' });
48 my $itemtype        = $builder->build({ source => 'Itemtype' })->{itemtype};
49
50 C4::Context->_new_userenv('xxx');
51 my $patron = $builder->build({
52     source => 'Borrower',
53     value => {
54         branchcode => $library->{branchcode},
55         flags      => 1, # superlibrarian
56     }
57 });
58 my $p = Koha::Patrons->find( $patron->{borrowernumber} );
59 t::lib::Mocks::mock_userenv({ patron => $p });
60
61 my $patron2 = $builder->build({
62     source => 'Borrower',
63     value => {
64         branchcode => $library->{branchcode},
65     }
66 });
67 my $patron3 = $builder->build({
68     source => 'Borrower',
69     value => {
70         branchcode => $another_library->{branchcode},
71         flags => undef,
72     }
73 });
74 my $p3 = Koha::Patrons->find( $patron3->{borrowernumber} );
75
76 # Discharge not possible with issues
77 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
78 my $barcode = 'BARCODE42';
79 $builder->build_sample_item(
80     {
81         biblionumber => $biblionumber,
82         library      => $library->{branchcode},
83         barcode      => $barcode,
84         itype        => $itemtype
85     }
86 );
87
88 AddIssue( $patron, $barcode );
89 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 0, 'A patron with issues cannot be discharged' );
90
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' );
95
96 AddReturn( $barcode );
97
98 # Discharge possible without issue
99 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->{borrowernumber} }), 1, 'A patron without issues can be discharged' );
100
101 is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
102
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' );
115
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)');
121
122 SKIP: {
123     skip "Skipping because weasyprint is not installed",
124         5 unless can_run('weasyprint');
125
126     isnt(
127         Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ),
128         undef,
129         "Temporary PDF generated."
130     );
131
132     my $mocked_ipc = Test::MockModule->new('IPC::Cmd');
133
134     $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
135
136     my $result;
137     warning_is
138         { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ); }
139         'Some error',
140         'Failed call to run() prints the generated error';
141
142     is( $result, undef, 'undef returned if failed run' );
143
144     $mocked_ipc->mock( 'can_run', undef );
145
146     warning_is
147         { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->{borrowernumber} } ); }
148         'weasyprint not found!',
149         'Expected failure because of missing weasyprint';
150
151     is( $result, undef, 'undef returned if missing weasyprint' );
152 }
153
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' );
156
157 subtest 'search_limited' => sub {
158     plan tests => 4;
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' );
172
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' );
177 };
178
179 $schema->storage->txn_rollback;