Bug 32496: Reduce unnecessary unblessings of objects in Circulation.pm
[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_object({
52     class => 'Koha::Patrons',
53     value => {
54         branchcode => $library->{branchcode},
55         flags      => 1, # superlibrarian
56     }
57 });
58 t::lib::Mocks::mock_userenv({ patron => $patron });
59
60 my $patron2 = $builder->build_object({
61     class => 'Koha::Patrons',
62     value => {
63         branchcode => $library->{branchcode},
64     }
65 });
66 my $patron3 = $builder->build_object({
67     class => 'Koha::Patrons',
68     value => {
69         branchcode => $another_library->{branchcode},
70         flags => undef,
71     }
72 });
73
74 # Discharge not possible with issues
75 my ( $biblionumber ) = AddBiblio( MARC::Record->new, '');
76 my $barcode = 'BARCODE42';
77 $builder->build_sample_item(
78     {
79         biblionumber => $biblionumber,
80         library      => $library->{branchcode},
81         barcode      => $barcode,
82         itype        => $itemtype
83     }
84 );
85
86 AddIssue( $patron, $barcode );
87 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }), 0, 'A patron with issues cannot be discharged' );
88
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' );
93
94 AddReturn( $barcode );
95
96 # Discharge possible without issue
97 is( Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }), 1, 'A patron without issues can be discharged' );
98
99 is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number");
100
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' );
113
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)');
119
120 SKIP: {
121     skip "Skipping because weasyprint is not installed",
122         5 unless can_run('weasyprint');
123
124     isnt(
125         Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ),
126         undef,
127         "Temporary PDF generated."
128     );
129
130     my $mocked_ipc = Test::MockModule->new('IPC::Cmd');
131
132     $mocked_ipc->mock( 'run', sub { return 0, 'Some error' } );
133
134     my $result;
135     warning_is
136         { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
137         'Some error',
138         'Failed call to run() prints the generated error';
139
140     is( $result, undef, 'undef returned if failed run' );
141
142     $mocked_ipc->mock( 'can_run', undef );
143
144     warning_is
145         { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); }
146         'weasyprint not found!',
147         'Expected failure because of missing weasyprint';
148
149     is( $result, undef, 'undef returned if missing weasyprint' );
150 }
151
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' );
154
155 subtest 'search_limited' => sub {
156     plan tests => 4;
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' );
170
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' );
175 };
176
177 $schema->storage->txn_rollback;