10 use Test::More tests => 59;
12 use_ok('Koha::Patron');
14 use t::lib::TestBuilder;
17 my $schema = Koha::Database->new->schema;
18 $schema->storage->txn_begin;
20 my $builder = t::lib::TestBuilder->new;
21 my $yesCatCode = $builder->build({
24 categorycode => 'yesCat',
25 checkprevcheckout => 'yes',
29 my $noCatCode = $builder->build({
32 categorycode => 'noCat',
33 checkprevcheckout => 'no',
37 my $inheritCatCode = $builder->build({
40 categorycode => 'inheritCat',
41 checkprevcheckout => 'inherit',
45 # Create context for some tests late on in the file.
46 my $library = $builder->build({ source => 'Branch' });
47 my $staff = $builder->build({source => 'Borrower'});
49 $staff->{borrowernumber}, 'test', 'MASTERTEST', 'firstname', $library->{branchcode},
50 $library->{branchcode}, 'email@example.org'
52 C4::Context->_new_userenv('DUMMY_SESSION_ID');
53 C4::Context->set_userenv(@USERENV);
54 BAIL_OUT("No userenv") unless C4::Context->userenv;
57 # wants_check_for_previous_checkout
59 # We expect the following result matrix:
61 # (1/0 indicates the return value of WantsCheckPrevCheckout; i.e. 1 says we
62 # should check whether the item was previously issued)
64 # | System Preference | hardyes | softyes | softno | hardno |
65 # |-------------------+-----------------------------------+-----------------------------------+-----------------------------------+-----------------------------------|
66 # | Category Setting | yes | no | inherit | yes | no | inherit | yes | no | inherit | yes | no | inherit |
67 # |-------------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------|
68 # | 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 |
69 # |-------------------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
70 # | 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 |
79 {setting => 'yes', result => 1},
80 {setting => 'no', result => 1},
81 {setting => 'inherit', result => 1},
87 {setting => 'yes', result => 1},
88 {setting => 'no', result => 1},
89 {setting => 'inherit', result => 1},
95 {setting => 'yes', result => 1},
96 {setting => 'no', result => 1},
97 {setting => 'inherit', result => 1},
103 syspref => 'softyes',
108 {setting => 'yes', result => 1},
109 {setting => 'no', result => 0},
110 {setting => 'inherit', result => 1},
116 {setting => 'yes', result => 1},
117 {setting => 'no', result => 0},
118 {setting => 'inherit', result => 0},
122 setting => 'inherit',
124 {setting => 'yes', result => 1},
125 {setting => 'no', result => 0},
126 {setting => 'inherit', result => 1},
137 {setting => 'yes', result => 1},
138 {setting => 'no', result => 0},
139 {setting => 'inherit', result => 1},
145 {setting => 'yes', result => 1},
146 {setting => 'no', result => 0},
147 {setting => 'inherit', result => 0},
151 setting => 'inherit',
153 {setting => 'yes', result => 1},
154 {setting => 'no', result => 0},
155 {setting => 'inherit', result => 0},
166 {setting => 'yes', result => 0},
167 {setting => 'no', result => 0},
168 {setting => 'inherit', result => 0},
174 {setting => 'yes', result => 0},
175 {setting => 'no', result => 0},
176 {setting => 'inherit', result => 0},
180 setting => 'inherit',
182 {setting => 'yes', result => 0},
183 {setting => 'no', result => 0},
184 {setting => 'inherit', result => 0},
192 my $syspref = $_->{syspref};
193 t::lib::Mocks::mock_preference('checkprevcheckout', $syspref);
195 my $code = $_->{setting} . 'Cat';
197 my $kpatron = $builder->build({
198 source => 'Borrower',
200 checkprevcheckout => $_->{setting},
201 categorycode => $code,
204 my $patron = Koha::Patrons->find($kpatron->{borrowernumber});
206 $patron->wants_check_for_previous_checkout, $_->{result},
207 "Predicate with syspref " . $syspref . ", cat " . $code
208 . ", patron " . $_->{setting}
211 } @{$_->{categories}};
214 # do_check_for_previous_checkout
217 # - DESCRIPTION [RETURNVALUE (0/1)]
218 ## PreIssue (sanity checks)
220 # - Diff item, same bib, same patron [0]
221 # - Diff item, diff bib, same patron [0]
222 # - Same item, diff patron [0]
223 # - Diff item, same bib, diff patron [0]
224 # - Diff item, diff bib, diff patron [0]
226 # - Same item, same patron [1]
227 # - Diff item, same bib, same patron [1]
228 # - Diff item, diff bib, same patron [0]
229 # - Same item, diff patron [0]
230 # - Diff item, same bib, diff patron [0]
231 # - Diff item, diff bib, diff patron [0]
233 # - Same item, same patron [1]
234 # - Diff item, same bib, same patron [1]
235 # - Diff item, diff bib, same patron [0]
236 # - Same item, diff patron [0]
237 # - Diff item, same bib, diff patron [0]
238 # - Diff item, diff bib, diff patron [0]
241 # $patron, $different_patron, $items (same bib number), $different_item
242 my $patron = $builder->build({source => 'Borrower'});
243 my $patron_d = $builder->build({source => 'Borrower'});
244 my $item_1 = $builder->build({source => 'Item'});
245 my $item_2 = $builder->build({
247 value => { biblionumber => $item_1->{biblionumber} },
249 my $item_d = $builder->build({source => 'Item'});
253 my ($mapping, $stage) = @_;
255 my $patron = Koha::Patrons->find($_->{patron}->{borrowernumber});
257 $patron->do_check_for_previous_checkout($_->{item}),
258 $_->{result}, $stage . ": " . $_->{msg}
266 msg => "Item, patron [0]",
272 msg => "Diff item, same bib, same patron [0]",
278 msg => "Diff item, diff bib, same patron [0]",
284 msg => "Same item, diff patron [0]",
290 msg => "Diff item, same bib, diff patron [0]",
296 msg => "Diff item, diff bib, diff patron [0]",
303 test_it($cpvmappings, "PreIssue");
305 # Issue item_1 to $patron:
307 GetMember(%{{borrowernumber => $patron->{borrowernumber}}});
308 BAIL_OUT("Issue failed")
309 unless AddIssue($patron_get_mem, $item_1->{barcode});
314 msg => "Same item, same patron [1]",
320 msg => "Diff item, same bib, same patron [1]",
326 msg => "Diff item, diff bib, same patron [0]",
332 msg => "Same item, diff patron [0]",
338 msg => "Diff item, same bib, diff patron [0]",
344 msg => "Diff item, diff bib, diff patron [0]",
351 test_it($cpvPmappings, "PostIssue");
353 # Return item_1 from patron:
354 BAIL_OUT("Return Failed") unless AddReturn($item_1->{barcode}, $patron->{branchcode});
357 test_it($cpvPmappings, "PostReturn");
359 # Finally test C4::Circulation::CanBookBeIssued
361 # We have already tested ->wants_check_for_previous_checkout and
362 # ->do_check_for_previous_checkout, so all that remains to be tested is
363 # whetherthe different combinational outcomes of the above return values in
364 # CanBookBeIssued result in the approriate $needsconfirmation.
367 # - DESCRIPTION [RETURNVALUE (0/1)]
368 # - patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout
369 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
370 # - patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout
371 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
372 # - patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout
373 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
374 # - patron, wants_check_for_previous_checkout, do_check_for_previous_checkout
375 # [!$issuingimpossible,$needsconfirmation->{PREVISSUE}]
378 # - $patron_from_GetMember
379 # - $item objects (one not issued, another prevIssued)
380 # - $checkprevcheckout pref (first hardno, then hardyes)
383 my $CBBI_patron = $builder->build({source => 'Borrower'});
384 my $p_from_GetMember =
385 GetMember(%{{borrowernumber => $CBBI_patron->{borrowernumber}}});
387 my $new_item = $builder->build({
395 my $prev_item = $builder->build({
403 # Second is Checked Out
404 BAIL_OUT("CanBookBeIssued Issue failed")
405 unless AddIssue($p_from_GetMember, $prev_item->{barcode});
408 my $CBBI_mappings = [
413 msg => "patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout"
417 syspref => 'hardyes',
420 msg => "patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout"
426 msg => "patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout"
429 syspref => 'hardyes',
432 msg => "patron, wants_check_for_previous_checkout, do_check_for_previous_checkout"
438 t::lib::Mocks::mock_preference('checkprevcheckout', $_->{syspref});
439 my ( $issuingimpossible, $needsconfirmation ) =
440 C4::Circulation::CanBookBeIssued(
441 $p_from_GetMember, $_->{item}->{barcode}
443 is($needsconfirmation->{PREVISSUE}, $_->{result}, $_->{msg});
446 $schema->storage->txn_rollback;