Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
use Modern::Perl; |
3 |
|
4 |
use C4::Members; |
5 |
use C4::Circulation; |
6 |
use Koha::Categories; |
7 |
use Koha::Category; |
8 |
use Koha::Database; |
9 |
use Koha::Patrons; |
10 |
use Koha::Patron; |
11 |
use Koha::Items; |
12 |
use Koha::Item; |
13 |
|
14 |
use Test::More tests => 55; |
15 |
|
16 |
use_ok('Koha::Patron::CheckPrevIssue'); |
17 |
|
18 |
use Koha::Patron::CheckPrevIssue qw( WantsCheckPrevIssue CheckPrevIssue ); |
19 |
|
20 |
use t::lib::TestBuilder; |
21 |
|
22 |
my $schema = Koha::Database->new->schema; |
23 |
$schema->storage->txn_begin; |
24 |
|
25 |
my $builder = t::lib::TestBuilder->new; |
26 |
my $yesCatCode = $builder->build({ |
27 |
source => 'Category', |
28 |
value => { |
29 |
categorycode => 'yesCat', |
30 |
checkprevissue => 'yes', |
31 |
}, |
32 |
}); |
33 |
|
34 |
my $noCatCode = $builder->build({ |
35 |
source => 'Category', |
36 |
value => { |
37 |
categorycode => 'noCat', |
38 |
checkprevissue => 'no', |
39 |
}, |
40 |
}); |
41 |
|
42 |
my $inheritCatCode = $builder->build({ |
43 |
source => 'Category', |
44 |
value => { |
45 |
categorycode => 'inheritCat', |
46 |
checkprevissue => 'inherit', |
47 |
}, |
48 |
}); |
49 |
|
50 |
# WantsCheckPrevIssue |
51 |
|
52 |
# We expect the following result matrix: |
53 |
# |
54 |
# (1/0 indicates the return value of WantsCheckPrevIssue; i.e. 1 says we |
55 |
# should check whether the item was previously issued) |
56 |
# |
57 |
# | System Preference | hardyes | softyes | softno | hardno | |
58 |
# |-------------------+-----------------------------------+-----------------------------------+-----------------------------------+-----------------------------------| |
59 |
# | Category Setting | yes | no | inherit | yes | no | inherit | yes | no | inherit | yes | no | inherit | |
60 |
# |-------------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------| |
61 |
# | Patron Setting | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | y | n | i | |
62 |
# |-------------------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |
63 |
# | Expected Result | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
64 |
|
65 |
my $mappings = [ |
66 |
{ |
67 |
syspref => 'hardyes', |
68 |
categories => [ |
69 |
{ |
70 |
setting => 'yes', |
71 |
borrowers => [ |
72 |
{setting => 'yes', result => 1}, |
73 |
{setting => 'no', result => 1}, |
74 |
{setting => 'inherit', result => 1}, |
75 |
], |
76 |
}, |
77 |
{ |
78 |
setting => 'no', |
79 |
borrowers => [ |
80 |
{setting => 'yes', result => 1}, |
81 |
{setting => 'no', result => 1}, |
82 |
{setting => 'inherit', result => 1}, |
83 |
], |
84 |
}, |
85 |
{ |
86 |
setting => 'inherit', |
87 |
borrowers => [ |
88 |
{setting => 'yes', result => 1}, |
89 |
{setting => 'no', result => 1}, |
90 |
{setting => 'inherit', result => 1}, |
91 |
], |
92 |
}, |
93 |
], |
94 |
}, |
95 |
{ |
96 |
syspref => 'softyes', |
97 |
categories => [ |
98 |
{ |
99 |
setting => 'yes', |
100 |
borrowers => [ |
101 |
{setting => 'yes', result => 1}, |
102 |
{setting => 'no', result => 0}, |
103 |
{setting => 'inherit', result => 1}, |
104 |
], |
105 |
}, |
106 |
{ |
107 |
setting => 'no', |
108 |
borrowers => [ |
109 |
{setting => 'yes', result => 1}, |
110 |
{setting => 'no', result => 0}, |
111 |
{setting => 'inherit', result => 0}, |
112 |
], |
113 |
}, |
114 |
{ |
115 |
setting => 'inherit', |
116 |
borrowers => [ |
117 |
{setting => 'yes', result => 1}, |
118 |
{setting => 'no', result => 0}, |
119 |
{setting => 'inherit', result => 1}, |
120 |
], |
121 |
}, |
122 |
], |
123 |
}, |
124 |
{ |
125 |
syspref => 'softno', |
126 |
categories => [ |
127 |
{ |
128 |
setting => 'yes', |
129 |
borrowers => [ |
130 |
{setting => 'yes', result => 1}, |
131 |
{setting => 'no', result => 0}, |
132 |
{setting => 'inherit', result => 1}, |
133 |
], |
134 |
}, |
135 |
{ |
136 |
setting => 'no', |
137 |
borrowers => [ |
138 |
{setting => 'yes', result => 1}, |
139 |
{setting => 'no', result => 0}, |
140 |
{setting => 'inherit', result => 0}, |
141 |
], |
142 |
}, |
143 |
{ |
144 |
setting => 'inherit', |
145 |
borrowers => [ |
146 |
{setting => 'yes', result => 1}, |
147 |
{setting => 'no', result => 0}, |
148 |
{setting => 'inherit', result => 0}, |
149 |
], |
150 |
}, |
151 |
], |
152 |
}, |
153 |
{ |
154 |
syspref => 'hardno', |
155 |
categories => [ |
156 |
{ |
157 |
setting => 'yes', |
158 |
borrowers => [ |
159 |
{setting => 'yes', result => 0}, |
160 |
{setting => 'no', result => 0}, |
161 |
{setting => 'inherit', result => 0}, |
162 |
], |
163 |
}, |
164 |
{ |
165 |
setting => 'no', |
166 |
borrowers => [ |
167 |
{setting => 'yes', result => 0}, |
168 |
{setting => 'no', result => 0}, |
169 |
{setting => 'inherit', result => 0}, |
170 |
], |
171 |
}, |
172 |
{ |
173 |
setting => 'inherit', |
174 |
borrowers => [ |
175 |
{setting => 'yes', result => 0}, |
176 |
{setting => 'no', result => 0}, |
177 |
{setting => 'inherit', result => 0}, |
178 |
], |
179 |
}, |
180 |
], |
181 |
}, |
182 |
]; |
183 |
|
184 |
map { |
185 |
my $syspref = $_->{syspref}; |
186 |
map { |
187 |
my $code = $_->{setting} . 'Cat'; |
188 |
map { |
189 |
my $brw = { |
190 |
checkprevissue => $_->{setting}, |
191 |
categorycode => $code, |
192 |
}; |
193 |
is( |
194 |
WantsCheckPrevIssue($brw, $syspref), $_->{result}, |
195 |
"Predicate with syspref " . $syspref . ", cat " . $code |
196 |
. ", brw " . $_->{setting} |
197 |
); |
198 |
} @{$_->{borrowers}}; |
199 |
} @{$_->{categories}}; |
200 |
} @{$mappings}; |
201 |
|
202 |
# CheckPrevIssue |
203 |
|
204 |
# We want to test: |
205 |
# - DESCRIPTION [RETURNVALUE (0/1)] |
206 |
## PreIssue (sanity checks) |
207 |
# - Item, patron [0] |
208 |
# - Diff item, same bib, same patron [0] |
209 |
# - Diff item, diff bib, same patron [0] |
210 |
# - Same item, diff patron [0] |
211 |
# - Diff item, same bib, diff patron [0] |
212 |
# - Diff item, diff bib, diff patron [0] |
213 |
## PostIssue |
214 |
# - Same item, same patron [1] |
215 |
# - Diff item, same bib, same patron [1] |
216 |
# - Diff item, diff bib, same patron [0] |
217 |
# - Same item, diff patron [0] |
218 |
# - Diff item, same bib, diff patron [0] |
219 |
# - Diff item, diff bib, diff patron [0] |
220 |
## PostReturn |
221 |
# - Same item, same patron [1] |
222 |
# - Diff item, same bib, same patron [1] |
223 |
# - Diff item, diff bib, same patron [0] |
224 |
# - Same item, diff patron [0] |
225 |
# - Diff item, same bib, diff patron [0] |
226 |
# - Diff item, diff bib, diff patron [0] |
227 |
|
228 |
# Requirements: |
229 |
# $patron, $different_patron, $items (same bib number), $different_item |
230 |
my $patron = $builder->build({source => 'Borrower'}); |
231 |
my $patron_d = $builder->build({source => 'Borrower'}); |
232 |
my $item_1 = $builder->build({source => 'Item'}); |
233 |
my $item_2 = $builder->build({ |
234 |
source => 'Item', |
235 |
value => { biblionumber => $item_1->{biblionumber} }, |
236 |
}); |
237 |
my $item_d = $builder->build({source => 'Item'}); |
238 |
|
239 |
## Testing Sub |
240 |
sub test_it { |
241 |
my ($mapping, $stage) = @_; |
242 |
map { |
243 |
is(CheckPrevIssue( |
244 |
$_->{patron}, $_->{item}), $_->{result}, $stage . ": " . $_->{msg} |
245 |
); |
246 |
} @{$mapping}; |
247 |
}; |
248 |
|
249 |
## Initial Mappings |
250 |
my $cpvmappings = [ |
251 |
{ |
252 |
msg => "Item, patron [0]", |
253 |
item => $item_1, |
254 |
patron => $patron, |
255 |
result => 0, |
256 |
}, |
257 |
{ |
258 |
msg => "Diff item, same bib, same patron [0]", |
259 |
item => $item_2, |
260 |
patron => $patron, |
261 |
result => 0, |
262 |
}, |
263 |
{ |
264 |
msg => "Diff item, diff bib, same patron [0]", |
265 |
item => $item_d, |
266 |
patron => $patron, |
267 |
result => 0, |
268 |
}, |
269 |
{ |
270 |
msg => "Same item, diff patron [0]", |
271 |
item => $item_1, |
272 |
patron => $patron_d, |
273 |
result => 0, |
274 |
}, |
275 |
{ |
276 |
msg => "Diff item, same bib, diff patron [0]", |
277 |
item => $item_2, |
278 |
patron => $patron_d, |
279 |
result => 0, |
280 |
}, |
281 |
{ |
282 |
msg => "Diff item, diff bib, diff patron [0]", |
283 |
item => $item_d, |
284 |
patron => $patron_d, |
285 |
result => 0, |
286 |
}, |
287 |
]; |
288 |
|
289 |
test_it($cpvmappings, "PreIssue"); |
290 |
|
291 |
# Issue item_1 to $patron: |
292 |
my @USERENV = ( |
293 |
$patron->{borrowernumber}, 'test', 'MASTERTEST', 'firstname', 'CPL', |
294 |
'CPL', 'email@example.org' |
295 |
); |
296 |
C4::Context->_new_userenv('DUMMY_SESSION_ID'); |
297 |
C4::Context->set_userenv(@USERENV); |
298 |
BAIL_OUT("No userenv") unless C4::Context->userenv; |
299 |
|
300 |
my $borrower = GetMember(%{{borrowernumber => $patron->{borrowernumber}}}); |
301 |
|
302 |
BAIL_OUT("Issue failed") unless AddIssue($borrower, $item_1->{barcode}); |
303 |
|
304 |
# Then test: |
305 |
my $cpvPmappings = [ |
306 |
{ |
307 |
msg => "Same item, same patron [1]", |
308 |
item => $item_1, |
309 |
patron => $patron, |
310 |
result => 1, |
311 |
}, |
312 |
{ |
313 |
msg => "Diff item, same bib, same patron [1]", |
314 |
item => $item_2, |
315 |
patron => $patron, |
316 |
result => 1, |
317 |
}, |
318 |
{ |
319 |
msg => "Diff item, diff bib, same patron [0]", |
320 |
item => $item_d, |
321 |
patron => $patron, |
322 |
result => 0, |
323 |
}, |
324 |
{ |
325 |
msg => "Same item, diff patron [0]", |
326 |
item => $item_1, |
327 |
patron => $patron_d, |
328 |
result => 0, |
329 |
}, |
330 |
{ |
331 |
msg => "Diff item, same bib, diff patron [0]", |
332 |
item => $item_2, |
333 |
patron => $patron_d, |
334 |
result => 0, |
335 |
}, |
336 |
{ |
337 |
msg => "Diff item, diff bib, diff patron [0]", |
338 |
item => $item_d, |
339 |
patron => $patron_d, |
340 |
result => 0, |
341 |
}, |
342 |
]; |
343 |
|
344 |
test_it($cpvPmappings, "PostIssue"); |
345 |
|
346 |
# Return item_1 from patron: |
347 |
BAIL_OUT("Return Failed") unless AddReturn($item_1->{barcode}, $patron->{branchcode}); |
348 |
|
349 |
# Then: |
350 |
test_it($cpvPmappings, "PostReturn"); |
351 |
|
352 |
$schema->storage->txn_rollback; |
353 |
|
354 |
1; |