9 use Test::More tests => 59;
11 use_ok('Koha::Patron');
13 use t::lib::TestBuilder;
16 my $schema = Koha::Database->new->schema;
17 $schema->storage->txn_begin;
19 my $builder = t::lib::TestBuilder->new;
20 my $yesCatCode = $builder->build({
23 categorycode => 'yesCat',
24 checkprevcheckout => 'yes',
28 my $noCatCode = $builder->build({
31 categorycode => 'noCat',
32 checkprevcheckout => 'no',
36 my $inheritCatCode = $builder->build({
39 categorycode => 'inheritCat',
40 checkprevcheckout => 'inherit',
44 # Create context for some tests late on in the file.
45 my $library = $builder->build({ source => 'Branch' });
46 my $staff = $builder->build({source => 'Borrower'});
48 $staff->{borrowernumber}, 'test', 'MASTERTEST', 'firstname', $library->{branchcode},
49 $library->{branchcode}, 'email@example.org'
51 C4::Context->_new_userenv('DUMMY_SESSION_ID');
52 C4::Context->set_userenv(@USERENV);
53 BAIL_OUT("No userenv") unless C4::Context->userenv;
56 # wants_check_for_previous_checkout
58 # We expect the following result matrix:
60 # (1/0 indicates the return value of WantsCheckPrevCheckout; i.e. 1 says we
61 # should check whether the item was previously issued)
63 # | System Preference | hardyes | softyes | softno | hardno |
64 # |-------------------+-----------------------------------+-----------------------------------+-----------------------------------+-----------------------------------|
65 # | Category Setting | yes | no | inherit | yes | no | inherit | yes | no | inherit | yes | no | inherit |
66 # |-------------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------|
67 # | 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 |
68 # |-------------------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
69 # | 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 |
78 {setting => 'yes', result => 1},
79 {setting => 'no', result => 1},
80 {setting => 'inherit', result => 1},
86 {setting => 'yes', result => 1},
87 {setting => 'no', result => 1},
88 {setting => 'inherit', result => 1},
94 {setting => 'yes', result => 1},
95 {setting => 'no', result => 1},
96 {setting => 'inherit', result => 1},
102 syspref => 'softyes',
107 {setting => 'yes', result => 1},
108 {setting => 'no', result => 0},
109 {setting => 'inherit', result => 1},
115 {setting => 'yes', result => 1},
116 {setting => 'no', result => 0},
117 {setting => 'inherit', result => 0},
121 setting => 'inherit',
123 {setting => 'yes', result => 1},
124 {setting => 'no', result => 0},
125 {setting => 'inherit', result => 1},
136 {setting => 'yes', result => 1},
137 {setting => 'no', result => 0},
138 {setting => 'inherit', result => 1},
144 {setting => 'yes', result => 1},
145 {setting => 'no', result => 0},
146 {setting => 'inherit', result => 0},
150 setting => 'inherit',
152 {setting => 'yes', result => 1},
153 {setting => 'no', result => 0},
154 {setting => 'inherit', result => 0},
165 {setting => 'yes', result => 0},
166 {setting => 'no', result => 0},
167 {setting => 'inherit', result => 0},
173 {setting => 'yes', result => 0},
174 {setting => 'no', result => 0},
175 {setting => 'inherit', result => 0},
179 setting => 'inherit',
181 {setting => 'yes', result => 0},
182 {setting => 'no', result => 0},
183 {setting => 'inherit', result => 0},
191 my $syspref = $_->{syspref};
192 t::lib::Mocks::mock_preference('checkprevcheckout', $syspref);
194 my $code = $_->{setting} . 'Cat';
196 my $kpatron = $builder->build({
197 source => 'Borrower',
199 checkprevcheckout => $_->{setting},
200 categorycode => $code,
203 my $patron = Koha::Patrons->find($kpatron->{borrowernumber});
205 $patron->wants_check_for_previous_checkout, $_->{result},
206 "Predicate with syspref " . $syspref . ", cat " . $code
207 . ", patron " . $_->{setting}
210 } @{$_->{categories}};
213 # do_check_for_previous_checkout
216 # - DESCRIPTION [RETURNVALUE (0/1)]
217 ## PreIssue (sanity checks)
219 # - Diff item, same bib, same patron [0]
220 # - Diff item, diff bib, same patron [0]
221 # - Same item, diff patron [0]
222 # - Diff item, same bib, diff patron [0]
223 # - Diff item, diff bib, diff patron [0]
225 # - Same item, same patron [1]
226 # - Diff item, same bib, same patron [1]
227 # - Diff item, diff bib, same patron [0]
228 # - Same item, diff patron [0]
229 # - Diff item, same bib, diff patron [0]
230 # - Diff item, diff bib, diff patron [0]
232 # - Same item, same patron [1]
233 # - Diff item, same bib, same patron [1]
234 # - Diff item, diff bib, same patron [0]
235 # - Same item, diff patron [0]
236 # - Diff item, same bib, diff patron [0]
237 # - Diff item, diff bib, diff patron [0]
240 # $patron, $different_patron, $items (same bib number), $different_item
241 my $patron = $builder->build({source => 'Borrower'});
242 my $patron_d = $builder->build({source => 'Borrower'});
243 my $item_1 = $builder->build({source => 'Item'});
244 my $item_2 = $builder->build({
246 value => { biblionumber => $item_1->{biblionumber} },
248 my $item_d = $builder->build({source => 'Item'});
252 my ($mapping, $stage) = @_;
254 my $patron = Koha::Patrons->find($_->{patron}->{borrowernumber});
256 $patron->do_check_for_previous_checkout($_->{item}),
257 $_->{result}, $stage . ": " . $_->{msg}
265 msg => "Item, patron [0]",
271 msg => "Diff item, same bib, same patron [0]",
277 msg => "Diff item, diff bib, same patron [0]",
283 msg => "Same item, diff patron [0]",
289 msg => "Diff item, same bib, diff patron [0]",
295 msg => "Diff item, diff bib, diff patron [0]",
302 test_it($cpvmappings, "PreIssue");
304 # Issue item_1 to $patron:
305 my $patron_get_mem = Koha::Patrons->find( $patron->{borrowernumber} )->unblessed;
306 BAIL_OUT("Issue failed")
307 unless AddIssue($patron_get_mem, $item_1->{barcode});
312 msg => "Same item, same patron [1]",
318 msg => "Diff item, same bib, same patron [1]",
324 msg => "Diff item, diff bib, same patron [0]",
330 msg => "Same item, diff patron [0]",
336 msg => "Diff item, same bib, diff patron [0]",
342 msg => "Diff item, diff bib, diff patron [0]",
349 test_it($cpvPmappings, "PostIssue");
351 # Return item_1 from patron:
352 BAIL_OUT("Return Failed") unless AddReturn($item_1->{barcode}, $patron->{branchcode});
355 test_it($cpvPmappings, "PostReturn");
357 # Finally test C4::Circulation::CanBookBeIssued
359 # We have already tested ->wants_check_for_previous_checkout and
360 # ->do_check_for_previous_checkout, so all that remains to be tested is
361 # whetherthe different combinational outcomes of the above return values in
362 # CanBookBeIssued result in the approriate $needsconfirmation.
365 # - DESCRIPTION [RETURNVALUE (0/1)]
366 # - patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout
367 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
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}]
377 # - $item objects (one not issued, another prevIssued)
378 # - $checkprevcheckout pref (first hardno, then hardyes)
381 my $CBBI_patron = $builder->build({source => 'Borrower'});
382 $patron = Koha::Patrons->find( $CBBI_patron->{borrowernumber} );
384 my $new_item = $builder->build({
392 my $prev_item = $builder->build({
400 # Second is Checked Out
401 BAIL_OUT("CanBookBeIssued Issue failed")
402 unless AddIssue($patron->unblessed, $prev_item->{barcode});
405 my $CBBI_mappings = [
410 msg => "patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout"
414 syspref => 'hardyes',
417 msg => "patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout"
423 msg => "patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout"
426 syspref => 'hardyes',
429 msg => "patron, wants_check_for_previous_checkout, do_check_for_previous_checkout"
435 t::lib::Mocks::mock_preference('checkprevcheckout', $_->{syspref});
436 my ( $issuingimpossible, $needsconfirmation ) =
437 C4::Circulation::CanBookBeIssued(
438 $patron, $_->{item}->{barcode}
440 is($needsconfirmation->{PREVISSUE}, $_->{result}, $_->{msg});
443 $schema->storage->txn_rollback;