Lines 25-31
use IPC::Cmd qw(can_run);
Link Here
|
25 |
use MARC::Record; |
25 |
use MARC::Record; |
26 |
|
26 |
|
27 |
use C4::Circulation qw( AddIssue AddReturn ); |
27 |
use C4::Circulation qw( AddIssue AddReturn ); |
28 |
use C4::Biblio qw( AddBiblio ); |
28 |
use C4::Biblio qw( AddBiblio ); |
29 |
use C4::Context; |
29 |
use C4::Context; |
30 |
|
30 |
|
31 |
use Koha::Patrons; |
31 |
use Koha::Patrons; |
Lines 35-41
use Koha::Database;
Link Here
|
35 |
use t::lib::TestBuilder; |
35 |
use t::lib::TestBuilder; |
36 |
use t::lib::Mocks; |
36 |
use t::lib::Mocks; |
37 |
|
37 |
|
38 |
my $schema = Koha::Database->new->schema; |
38 |
my $schema = Koha::Database->new->schema; |
39 |
$schema->storage->txn_begin; |
39 |
$schema->storage->txn_begin; |
40 |
|
40 |
|
41 |
my $builder = t::lib::TestBuilder->new; |
41 |
my $builder = t::lib::TestBuilder->new; |
Lines 43-51
my $builder = t::lib::TestBuilder->new;
Link Here
|
43 |
my $dbh = C4::Context->dbh; |
43 |
my $dbh = C4::Context->dbh; |
44 |
$dbh->do(q|DELETE FROM discharges|); |
44 |
$dbh->do(q|DELETE FROM discharges|); |
45 |
|
45 |
|
46 |
my $library = $builder->build({ source => 'Branch' }); |
46 |
my $library = $builder->build( { source => 'Branch' } ); |
47 |
my $another_library = $builder->build({ source => 'Branch' }); |
47 |
my $another_library = $builder->build( { source => 'Branch' } ); |
48 |
my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype}; |
48 |
my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype}; |
49 |
|
49 |
|
50 |
my $patron = $builder->build_object({ |
50 |
my $patron = $builder->build_object({ |
51 |
class => 'Koha::Patrons', |
51 |
class => 'Koha::Patrons', |
Lines 54-77
my $patron = $builder->build_object({
Link Here
|
54 |
flags => 1, # superlibrarian |
54 |
flags => 1, # superlibrarian |
55 |
} |
55 |
} |
56 |
}); |
56 |
}); |
57 |
t::lib::Mocks::mock_userenv({ patron => $patron }); |
57 |
t::lib::Mocks::mock_userenv( { patron => $patron } ); |
58 |
|
58 |
|
59 |
my $patron2 = $builder->build_object({ |
59 |
my $patron2 = $builder->build_object( |
60 |
class => 'Koha::Patrons', |
60 |
{ |
61 |
value => { |
61 |
class => 'Koha::Patrons', |
62 |
branchcode => $library->{branchcode}, |
62 |
value => { |
|
|
63 |
branchcode => $library->{branchcode}, |
64 |
} |
63 |
} |
65 |
} |
64 |
}); |
66 |
); |
65 |
my $patron3 = $builder->build_object({ |
67 |
my $patron3 = $builder->build_object( |
66 |
class => 'Koha::Patrons', |
68 |
{ |
67 |
value => { |
69 |
class => 'Koha::Patrons', |
68 |
branchcode => $another_library->{branchcode}, |
70 |
value => { |
69 |
flags => undef, |
71 |
branchcode => $another_library->{branchcode}, |
|
|
72 |
flags => undef, |
73 |
} |
70 |
} |
74 |
} |
71 |
}); |
75 |
); |
72 |
|
76 |
|
73 |
# Discharge not possible with issues |
77 |
# Discharge not possible with issues |
74 |
my ( $biblionumber ) = AddBiblio( MARC::Record->new, ''); |
78 |
my ($biblionumber) = AddBiblio( MARC::Record->new, '' ); |
75 |
my $barcode = 'BARCODE42'; |
79 |
my $barcode = 'BARCODE42'; |
76 |
$builder->build_sample_item( |
80 |
$builder->build_sample_item( |
77 |
{ |
81 |
{ |
Lines 83-97
$builder->build_sample_item(
Link Here
|
83 |
); |
87 |
); |
84 |
|
88 |
|
85 |
AddIssue( $patron, $barcode ); |
89 |
AddIssue( $patron, $barcode ); |
86 |
my ( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }); |
90 |
my ( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } ); |
87 |
is( $can, 0, 'A patron with issues cannot be discharged' ); |
91 |
is( $can, 0, 'A patron with issues cannot be discharged' ); |
88 |
is( $problems->{checkouts}, 1, "Patron has checkouts" ); |
92 |
is( $problems->{checkouts}, 1, "Patron has checkouts" ); |
89 |
is( $problems->{debt}, undef, "Patron has no debt" ); |
93 |
is( $problems->{debt}, undef, "Patron has no debt" ); |
90 |
|
94 |
|
91 |
is( Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }), undef, 'No request done if patron has issues' ); |
95 |
is( |
92 |
is( Koha::Patron::Discharge::discharge({ borrowernumber => $patron->borrowernumber }), undef, 'No discharge done if patron has issues' ); |
96 |
Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ), undef, |
93 |
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request' ); |
97 |
'No request done if patron has issues' |
94 |
is_deeply( [ Koha::Patron::Discharge::get_validated ], [], 'There is no validated discharge' ); |
98 |
); |
|
|
99 |
is( |
100 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ), undef, |
101 |
'No discharge done if patron has issues' |
102 |
); |
103 |
is_deeply( [Koha::Patron::Discharge::get_pendings], [], 'There is no pending discharge request' ); |
104 |
is_deeply( [Koha::Patron::Discharge::get_validated], [], 'There is no validated discharge' ); |
95 |
|
105 |
|
96 |
# Discharge not possible with issues and fines |
106 |
# Discharge not possible with issues and fines |
97 |
$patron->account->add_debit( |
107 |
$patron->account->add_debit( |
Lines 102-125
$patron->account->add_debit(
Link Here
|
102 |
} |
112 |
} |
103 |
); |
113 |
); |
104 |
|
114 |
|
105 |
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }); |
115 |
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } ); |
106 |
is( |
116 |
is( |
107 |
$can, 0, |
117 |
$can, 0, |
108 |
'A patron with fines and checkouts cannot be discharged' |
118 |
'A patron with fines and checkouts cannot be discharged' |
109 |
); |
119 |
); |
110 |
is( $problems->{checkouts}, 1, "Patron has checkouts" ); |
120 |
is( $problems->{checkouts}, 1, "Patron has checkouts" ); |
111 |
is( $problems->{debt}, 0.1, "Patron has debt" ); |
121 |
is( $problems->{debt}, 0.1, "Patron has debt" ); |
112 |
|
122 |
|
113 |
AddReturn( $barcode ); |
123 |
AddReturn($barcode); |
114 |
|
124 |
|
115 |
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }); |
125 |
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } ); |
116 |
is( |
126 |
is( |
117 |
$can, 0, |
127 |
$can, 0, |
118 |
'A patron with fines cannot be discharged' |
128 |
'A patron with fines cannot be discharged' |
119 |
); |
129 |
); |
120 |
is( $problems->{checkouts}, undef, "Patron has checkouts" ); |
130 |
is( $problems->{checkouts}, undef, "Patron has checkouts" ); |
121 |
is( $problems->{debt}, 0.1, "Patron has debt" ); |
131 |
is( $problems->{debt}, 0.1, "Patron has debt" ); |
122 |
|
|
|
123 |
|
132 |
|
124 |
$patron->account->pay( |
133 |
$patron->account->pay( |
125 |
{ |
134 |
{ |
Lines 128-160
$patron->account->pay(
Link Here
|
128 |
); |
137 |
); |
129 |
|
138 |
|
130 |
# Discharge possible without issue or fine |
139 |
# Discharge possible without issue or fine |
131 |
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $patron->borrowernumber }); |
140 |
( $can, $problems ) = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $patron->borrowernumber } ); |
132 |
is( |
141 |
is( |
133 |
$can, 1, |
142 |
$can, 1, |
134 |
'A patron without issues or fines can be discharged' |
143 |
'A patron without issues or fines can be discharged' |
135 |
); |
144 |
); |
136 |
is( scalar keys %$problems, 0, "No dishcharge problems found" ); |
145 |
is( scalar keys %$problems, 0, "No dishcharge problems found" ); |
137 |
|
146 |
|
138 |
is(Koha::Patron::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number"); |
147 |
is( Koha::Patron::Discharge::generate_as_pdf, undef, "Confirm failure when lacking borrower number" ); |
139 |
|
148 |
|
140 |
# Verify that the user is not discharged anymore if the restriction has been lifted |
149 |
# Verify that the user is not discharged anymore if the restriction has been lifted |
141 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ); |
150 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ); |
142 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } ); |
151 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron2->borrowernumber } ); |
143 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } ); |
152 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron3->borrowernumber } ); |
144 |
is( Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1, 'The patron has been discharged' ); |
153 |
is( |
145 |
is( Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31', 'The patron has been debarred after discharge' ); |
154 |
Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 1, |
146 |
is( scalar( Koha::Patron::Discharge::get_validated ), 3, 'There are 3 validated discharges' ); |
155 |
'The patron has been discharged' |
147 |
is( scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1, 'There is 1 validated discharge for a given patron' ); |
156 |
); |
148 |
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 |
157 |
is( |
|
|
158 |
Koha::Patrons->find( $patron->borrowernumber )->is_debarred, '9999-12-31', |
159 |
'The patron has been debarred after discharge' |
160 |
); |
161 |
is( scalar(Koha::Patron::Discharge::get_validated), 3, 'There are 3 validated discharges' ); |
162 |
is( |
163 |
scalar( Koha::Patron::Discharge::get_validated( { borrowernumber => $patron->borrowernumber } ) ), 1, |
164 |
'There is 1 validated discharge for a given patron' |
165 |
); |
166 |
is( |
167 |
scalar( Koha::Patron::Discharge::get_validated( { branchcode => $library->{branchcode} } ) ), 2, |
168 |
'There is 2 validated discharges for a given branchcode' |
169 |
); # This is not used in the code yet |
149 |
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } ); |
170 |
Koha::Patron::Debarments::DelUniqueDebarment( { 'borrowernumber' => $patron->borrowernumber, 'type' => 'DISCHARGE' } ); |
150 |
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' ); |
171 |
ok( !Koha::Patrons->find( $patron->borrowernumber )->is_debarred, 'The debarment has been lifted' ); |
151 |
ok( !Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), 'The patron is not discharged after the restriction has been lifted' ); |
172 |
ok( |
|
|
173 |
!Koha::Patron::Discharge::is_discharged( { borrowernumber => $patron->borrowernumber } ), |
174 |
'The patron is not discharged after the restriction has been lifted' |
175 |
); |
152 |
|
176 |
|
153 |
# Verify that the discharge works multiple times |
177 |
# Verify that the discharge works multiple times |
154 |
Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }); |
178 |
Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ); |
155 |
is(scalar( Koha::Patron::Discharge::get_pendings ), 1, 'There is a pending discharge request (second time)'); |
179 |
is( scalar(Koha::Patron::Discharge::get_pendings), 1, 'There is a pending discharge request (second time)' ); |
156 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ); |
180 |
Koha::Patron::Discharge::discharge( { borrowernumber => $patron->borrowernumber } ); |
157 |
is_deeply( [ Koha::Patron::Discharge::get_pendings ], [], 'There is no pending discharge request (second time)'); |
181 |
is_deeply( [Koha::Patron::Discharge::get_pendings], [], 'There is no pending discharge request (second time)' ); |
158 |
|
182 |
|
159 |
SKIP: { |
183 |
SKIP: { |
160 |
skip "Skipping because weasyprint is not installed", |
184 |
skip "Skipping because weasyprint is not installed", |
Lines 171-216
SKIP: {
Link Here
|
171 |
$mocked_ipc->mock( 'run', sub { return 0, 'Some error' } ); |
195 |
$mocked_ipc->mock( 'run', sub { return 0, 'Some error' } ); |
172 |
|
196 |
|
173 |
my $result; |
197 |
my $result; |
174 |
warning_is |
198 |
warning_is { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); } |
175 |
{ $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); } |
199 |
'Some error', |
176 |
'Some error', |
|
|
177 |
'Failed call to run() prints the generated error'; |
200 |
'Failed call to run() prints the generated error'; |
178 |
|
201 |
|
179 |
is( $result, undef, 'undef returned if failed run' ); |
202 |
is( $result, undef, 'undef returned if failed run' ); |
180 |
|
203 |
|
181 |
$mocked_ipc->mock( 'can_run', undef ); |
204 |
$mocked_ipc->mock( 'can_run', undef ); |
182 |
|
205 |
|
183 |
warning_is |
206 |
warning_is { $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); } |
184 |
{ $result = Koha::Patron::Discharge::generate_as_pdf( { borrowernumber => $patron->borrowernumber } ); } |
207 |
'weasyprint not found!', |
185 |
'weasyprint not found!', |
|
|
186 |
'Expected failure because of missing weasyprint'; |
208 |
'Expected failure because of missing weasyprint'; |
187 |
|
209 |
|
188 |
is( $result, undef, 'undef returned if missing weasyprint' ); |
210 |
is( $result, undef, 'undef returned if missing weasyprint' ); |
189 |
} |
211 |
} |
190 |
|
212 |
|
191 |
# FIXME Should be a Koha::Object object |
213 |
# FIXME Should be a Koha::Object object |
192 |
is( ref(Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber })), 'Koha::Schema::Result::Discharge', 'Discharge request sent' ); |
214 |
is( |
|
|
215 |
ref( Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ) ), |
216 |
'Koha::Schema::Result::Discharge', 'Discharge request sent' |
217 |
); |
193 |
|
218 |
|
194 |
subtest 'search_limited' => sub { |
219 |
subtest 'search_limited' => sub { |
195 |
plan tests => 4; |
220 |
plan tests => 4; |
196 |
$dbh->do(q|DELETE FROM discharges|); |
221 |
$dbh->do(q|DELETE FROM discharges|); |
197 |
my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store; |
222 |
my $group_1 = Koha::Library::Group->new( { title => 'TEST Group 1' } )->store; |
198 |
my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store; |
223 |
my $group_2 = Koha::Library::Group->new( { title => 'TEST Group 2' } )->store; |
|
|
224 |
|
199 |
# $patron and $patron2 are from the same library, $patron3 from another one |
225 |
# $patron and $patron2 are from the same library, $patron3 from another one |
200 |
# Logged in user is $patron, superlibrarian |
226 |
# Logged in user is $patron, superlibrarian |
201 |
t::lib::Mocks::mock_userenv({ patron => $patron }); |
227 |
t::lib::Mocks::mock_userenv( { patron => $patron } ); |
202 |
Koha::Library::Group->new({ parent_id => $group_1->id, branchcode => $patron->branchcode })->store(); |
228 |
Koha::Library::Group->new( { parent_id => $group_1->id, branchcode => $patron->branchcode } )->store(); |
203 |
Koha::Library::Group->new({ parent_id => $group_2->id, branchcode => $patron3->branchcode })->store(); |
229 |
Koha::Library::Group->new( { parent_id => $group_2->id, branchcode => $patron3->branchcode } )->store(); |
204 |
Koha::Patron::Discharge::request({ borrowernumber => $patron->borrowernumber }); |
230 |
Koha::Patron::Discharge::request( { borrowernumber => $patron->borrowernumber } ); |
205 |
Koha::Patron::Discharge::request({ borrowernumber => $patron2->borrowernumber }); |
231 |
Koha::Patron::Discharge::request( { borrowernumber => $patron2->borrowernumber } ); |
206 |
Koha::Patron::Discharge::request({ borrowernumber => $patron3->borrowernumber }); |
232 |
Koha::Patron::Discharge::request( { borrowernumber => $patron3->borrowernumber } ); |
207 |
is( scalar( Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' ); |
233 |
is( scalar(Koha::Patron::Discharge::get_pendings), 3, 'With permission, all discharges are visible' ); |
208 |
is( Koha::Patron::Discharge::count({pending => 1}), 3, 'With permission, all discharges are visible' ); |
234 |
is( Koha::Patron::Discharge::count( { pending => 1 } ), 3, 'With permission, all discharges are visible' ); |
209 |
|
235 |
|
210 |
# With patron 3 logged in, only discharges from their group are visible |
236 |
# With patron 3 logged in, only discharges from their group are visible |
211 |
t::lib::Mocks::mock_userenv({ patron => $patron3 }); |
237 |
t::lib::Mocks::mock_userenv( { patron => $patron3 } ); |
212 |
is( scalar( Koha::Patron::Discharge::get_pendings), 1, 'Without permission, only discharge from our group are visible' ); |
238 |
is( |
213 |
is( Koha::Patron::Discharge::count({pending => 1}), 1, 'Without permission, only discharge from our group are visible' ); |
239 |
scalar(Koha::Patron::Discharge::get_pendings), 1, |
|
|
240 |
'Without permission, only discharge from our group are visible' |
241 |
); |
242 |
is( |
243 |
Koha::Patron::Discharge::count( { pending => 1 } ), 1, |
244 |
'Without permission, only discharge from our group are visible' |
245 |
); |
214 |
}; |
246 |
}; |
215 |
|
247 |
|
216 |
$schema->storage->txn_rollback; |
248 |
$schema->storage->txn_rollback; |
217 |
- |
|
|