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