9 use Test::More tests => 60;
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 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
50 # wants_check_for_previous_checkout
52 # We expect the following result matrix:
54 # (1/0 indicates the return value of WantsCheckPrevCheckout; i.e. 1 says we
55 # should check whether the item was previously issued)
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 |
72 {setting => 'yes', result => 1},
73 {setting => 'no', result => 1},
74 {setting => 'inherit', result => 1},
80 {setting => 'yes', result => 1},
81 {setting => 'no', result => 1},
82 {setting => 'inherit', result => 1},
88 {setting => 'yes', result => 1},
89 {setting => 'no', result => 1},
90 {setting => 'inherit', result => 1},
101 {setting => 'yes', result => 1},
102 {setting => 'no', result => 0},
103 {setting => 'inherit', result => 1},
109 {setting => 'yes', result => 1},
110 {setting => 'no', result => 0},
111 {setting => 'inherit', result => 0},
115 setting => 'inherit',
117 {setting => 'yes', result => 1},
118 {setting => 'no', result => 0},
119 {setting => 'inherit', result => 1},
130 {setting => 'yes', result => 1},
131 {setting => 'no', result => 0},
132 {setting => 'inherit', result => 1},
138 {setting => 'yes', result => 1},
139 {setting => 'no', result => 0},
140 {setting => 'inherit', result => 0},
144 setting => 'inherit',
146 {setting => 'yes', result => 1},
147 {setting => 'no', result => 0},
148 {setting => 'inherit', result => 0},
159 {setting => 'yes', result => 0},
160 {setting => 'no', result => 0},
161 {setting => 'inherit', result => 0},
167 {setting => 'yes', result => 0},
168 {setting => 'no', result => 0},
169 {setting => 'inherit', result => 0},
173 setting => 'inherit',
175 {setting => 'yes', result => 0},
176 {setting => 'no', result => 0},
177 {setting => 'inherit', result => 0},
185 my $syspref = $_->{syspref};
186 t::lib::Mocks::mock_preference('checkprevcheckout', $syspref);
188 my $code = $_->{setting} . 'Cat';
190 my $kpatron = $builder->build({
191 source => 'Borrower',
193 checkprevcheckout => $_->{setting},
194 categorycode => $code,
197 my $patron = Koha::Patrons->find($kpatron->{borrowernumber});
199 $patron->wants_check_for_previous_checkout, $_->{result},
200 "Predicate with syspref " . $syspref . ", cat " . $code
201 . ", patron " . $_->{setting}
204 } @{$_->{categories}};
207 # do_check_for_previous_checkout
210 # - DESCRIPTION [RETURNVALUE (0/1)]
211 ## PreIssue (sanity checks)
213 # - Diff item, same bib, same patron [0]
214 # - Diff item, diff bib, same patron [0]
215 # - Same item, diff patron [0]
216 # - Diff item, same bib, diff patron [0]
217 # - Diff item, diff bib, diff patron [0]
219 # - Same item, same patron [1]
220 # - Diff item, same bib, same patron [1]
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]
234 # $patron, $different_patron, $items (same bib number), $different_item
235 my $patron = $builder->build({source => 'Borrower'});
236 my $patron_d = $builder->build({source => 'Borrower'});
238 my $biblio = $builder->build_sample_biblio;
239 $biblio->serial(0)->store;
240 my $item_1 = $builder->build_sample_item({biblionumber => $biblio->biblionumber})->unblessed;
241 my $item_2 = $builder->build_sample_item({biblionumber => $biblio->biblionumber})->unblessed;
242 my $item_d = $builder->build_sample_item->unblessed;
246 my ($mapping, $stage) = @_;
248 my $patron = Koha::Patrons->find($_->{patron}->{borrowernumber});
250 $patron->do_check_for_previous_checkout($_->{item}),
251 $_->{result}, $stage . ": " . $_->{msg}
259 msg => "Item, patron [0]",
265 msg => "Diff item, same bib, same patron [0]",
271 msg => "Diff item, diff bib, same patron [0]",
277 msg => "Same item, diff patron [0]",
283 msg => "Diff item, same bib, diff patron [0]",
289 msg => "Diff item, diff bib, diff patron [0]",
296 test_it($cpvmappings, "PreIssue");
298 # Issue item_1 to $patron:
299 my $patron_get_mem = Koha::Patrons->find( $patron->{borrowernumber} )->unblessed;
300 BAIL_OUT("Issue failed")
301 unless AddIssue($patron_get_mem, $item_1->{barcode});
306 msg => "Same item, same patron [1]",
312 msg => "Diff item, same bib, same patron [1]",
318 msg => "Diff item, diff bib, same patron [0]",
324 msg => "Same item, diff patron [0]",
330 msg => "Diff item, same bib, diff patron [0]",
336 msg => "Diff item, diff bib, diff patron [0]",
343 test_it($cpvPmappings, "PostIssue");
345 # Return item_1 from patron:
346 BAIL_OUT("Return Failed") unless AddReturn($item_1->{barcode}, $patron->{branchcode});
349 test_it($cpvPmappings, "PostReturn");
351 # Finally test C4::Circulation::CanBookBeIssued
353 # We have already tested ->wants_check_for_previous_checkout and
354 # ->do_check_for_previous_checkout, so all that remains to be tested is
355 # whetherthe different combinational outcomes of the above return values in
356 # CanBookBeIssued result in the approriate $needsconfirmation.
359 # - DESCRIPTION [RETURNVALUE (0/1)]
360 # - patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout
361 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
362 # - patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout
363 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
364 # - patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout
365 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
366 # - patron, wants_check_for_previous_checkout, do_check_for_previous_checkout
367 # [!$issuingimpossible,$needsconfirmation->{PREVISSUE}]
371 # - $item objects (one not issued, another prevIssued)
372 # - $checkprevcheckout pref (first hardno, then hardyes)
375 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
376 my $CBBI_patron = $builder->build({source => 'Borrower', value => { categorycode => $patron_category->{categorycode} }});
377 $patron = Koha::Patrons->find( $CBBI_patron->{borrowernumber} );
380 my $new_item = $builder->build({
386 biblionumber => $builder->build( { source => 'Biblioitem' } )->{biblionumber},
389 my $prev_item = $builder->build({
395 biblionumber => $builder->build( { source => 'Biblioitem' } )->{biblionumber},
398 # Second is Checked Out
399 BAIL_OUT("CanBookBeIssued Issue failed")
400 unless AddIssue($patron->unblessed, $prev_item->{barcode});
403 my $CBBI_mappings = [
408 msg => "patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout"
412 syspref => 'hardyes',
415 msg => "patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout"
421 msg => "patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout"
424 syspref => 'hardyes',
427 msg => "patron, wants_check_for_previous_checkout, do_check_for_previous_checkout"
433 t::lib::Mocks::mock_preference('checkprevcheckout', $_->{syspref});
434 my ( $issuingimpossible, $needsconfirmation ) =
435 C4::Circulation::CanBookBeIssued(
436 $patron, $_->{item}->{barcode}
438 is($needsconfirmation->{PREVISSUE}, $_->{result}, $_->{msg});
441 $schema->storage->txn_rollback;
443 subtest 'Check previous checkouts for serial' => sub {
445 $schema->storage->txn_begin;
447 my $library = $builder->build_object({ class => 'Koha::Libraries'});
449 my $patron = $builder->build_object({
450 class => 'Koha::Patrons',
452 branchcode => $library->branchcode
455 t::lib::Mocks::mock_userenv({ patron => $patron });
457 my $biblio = $builder->build_sample_biblio;
458 $biblio->serial(1)->store;
460 my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
461 my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
463 AddIssue($patron->unblessed, $item1->barcode);
465 is($patron->do_check_for_previous_checkout($item1->unblessed), 1, 'Check only one item if bibliographic record is serial');
466 is($patron->do_check_for_previous_checkout($item2->unblessed), 0, 'Check only one item if bibliographic record is serial');
468 $schema->storage->txn_rollback;