10 use Test::More tests => 61;
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 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
51 # wants_check_for_previous_checkout
53 # We expect the following result matrix:
55 # (1/0 indicates the return value of WantsCheckPrevCheckout; i.e. 1 says we
56 # should check whether the item was previously issued)
58 # | System Preference | hardyes | softyes | softno | hardno |
59 # |-------------------+-----------------------------------+-----------------------------------+-----------------------------------+-----------------------------------|
60 # | Category Setting | yes | no | inherit | yes | no | inherit | yes | no | inherit | yes | no | inherit |
61 # |-------------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------|
62 # | 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 |
63 # |-------------------+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
64 # | 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 |
73 {setting => 'yes', result => 1},
74 {setting => 'no', result => 1},
75 {setting => 'inherit', result => 1},
81 {setting => 'yes', result => 1},
82 {setting => 'no', result => 1},
83 {setting => 'inherit', result => 1},
89 {setting => 'yes', result => 1},
90 {setting => 'no', result => 1},
91 {setting => 'inherit', result => 1},
102 {setting => 'yes', result => 1},
103 {setting => 'no', result => 0},
104 {setting => 'inherit', result => 1},
110 {setting => 'yes', result => 1},
111 {setting => 'no', result => 0},
112 {setting => 'inherit', result => 0},
116 setting => 'inherit',
118 {setting => 'yes', result => 1},
119 {setting => 'no', result => 0},
120 {setting => 'inherit', result => 1},
131 {setting => 'yes', result => 1},
132 {setting => 'no', result => 0},
133 {setting => 'inherit', result => 1},
139 {setting => 'yes', result => 1},
140 {setting => 'no', result => 0},
141 {setting => 'inherit', result => 0},
145 setting => 'inherit',
147 {setting => 'yes', result => 1},
148 {setting => 'no', result => 0},
149 {setting => 'inherit', result => 0},
160 {setting => 'yes', result => 0},
161 {setting => 'no', result => 0},
162 {setting => 'inherit', result => 0},
168 {setting => 'yes', result => 0},
169 {setting => 'no', result => 0},
170 {setting => 'inherit', result => 0},
174 setting => 'inherit',
176 {setting => 'yes', result => 0},
177 {setting => 'no', result => 0},
178 {setting => 'inherit', result => 0},
186 my $syspref = $_->{syspref};
187 t::lib::Mocks::mock_preference('checkprevcheckout', $syspref);
189 my $code = $_->{setting} . 'Cat';
191 my $kpatron = $builder->build({
192 source => 'Borrower',
194 checkprevcheckout => $_->{setting},
195 categorycode => $code,
198 my $patron = Koha::Patrons->find($kpatron->{borrowernumber});
200 $patron->wants_check_for_previous_checkout, $_->{result},
201 "Predicate with syspref " . $syspref . ", cat " . $code
202 . ", patron " . $_->{setting}
205 } @{$_->{categories}};
208 # do_check_for_previous_checkout
211 # - DESCRIPTION [RETURNVALUE (0/1)]
212 ## PreIssue (sanity checks)
214 # - Diff item, same bib, same patron [0]
215 # - Diff item, diff bib, same patron [0]
216 # - Same item, diff patron [0]
217 # - Diff item, same bib, diff patron [0]
218 # - Diff item, diff bib, diff patron [0]
220 # - Same item, same patron [1]
221 # - Diff item, same bib, same patron [1]
222 # - Diff item, diff bib, same patron [0]
223 # - Same item, diff patron [0]
224 # - Diff item, same bib, diff patron [0]
225 # - Diff item, diff bib, diff patron [0]
227 # - Same item, same patron [1]
228 # - Diff item, same bib, same patron [1]
229 # - Diff item, diff bib, same patron [0]
230 # - Same item, diff patron [0]
231 # - Diff item, same bib, diff patron [0]
232 # - Diff item, diff bib, diff patron [0]
235 # $patron, $different_patron, $items (same bib number), $different_item
236 my $patron = $builder->build({source => 'Borrower'});
237 my $patron_d = $builder->build({source => 'Borrower'});
239 my $biblio = $builder->build_sample_biblio;
240 $biblio->serial(0)->store;
241 my $item_1 = $builder->build_sample_item({biblionumber => $biblio->biblionumber})->unblessed;
242 my $item_2 = $builder->build_sample_item({biblionumber => $biblio->biblionumber})->unblessed;
243 my $item_d = $builder->build_sample_item->unblessed;
247 my ($mapping, $stage) = @_;
249 my $patron = Koha::Patrons->find($_->{patron}->{borrowernumber});
251 $patron->do_check_for_previous_checkout($_->{item}),
252 $_->{result}, $stage . ": " . $_->{msg}
260 msg => "Item, patron [0]",
266 msg => "Diff item, same bib, same patron [0]",
272 msg => "Diff item, diff bib, same patron [0]",
278 msg => "Same item, diff patron [0]",
284 msg => "Diff item, same bib, diff patron [0]",
290 msg => "Diff item, diff bib, diff patron [0]",
297 test_it($cpvmappings, "PreIssue");
299 # Issue item_1 to $patron:
300 my $patron_get_mem = Koha::Patrons->find( $patron->{borrowernumber} )->unblessed;
301 BAIL_OUT("Issue failed")
302 unless AddIssue($patron_get_mem, $item_1->{barcode});
307 msg => "Same item, same patron [1]",
313 msg => "Diff item, same bib, same patron [1]",
319 msg => "Diff item, diff bib, same patron [0]",
325 msg => "Same item, diff patron [0]",
331 msg => "Diff item, same bib, diff patron [0]",
337 msg => "Diff item, diff bib, diff patron [0]",
344 test_it($cpvPmappings, "PostIssue");
346 # Return item_1 from patron:
347 BAIL_OUT("Return Failed") unless AddReturn($item_1->{barcode}, $patron->{branchcode});
350 test_it($cpvPmappings, "PostReturn");
352 # Finally test C4::Circulation::CanBookBeIssued
354 # We have already tested ->wants_check_for_previous_checkout and
355 # ->do_check_for_previous_checkout, so all that remains to be tested is
356 # whetherthe different combinational outcomes of the above return values in
357 # CanBookBeIssued result in the approriate $needsconfirmation.
360 # - DESCRIPTION [RETURNVALUE (0/1)]
361 # - patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout
362 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
363 # - patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout
364 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
365 # - patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout
366 # [!$issuingimpossible,!$needsconfirmation->{PREVISSUE}]
367 # - patron, wants_check_for_previous_checkout, do_check_for_previous_checkout
368 # [!$issuingimpossible,$needsconfirmation->{PREVISSUE}]
372 # - $item objects (one not issued, another prevIssued)
373 # - $checkprevcheckout pref (first hardno, then hardyes)
376 my $patron_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
377 my $CBBI_patron = $builder->build({source => 'Borrower', value => { categorycode => $patron_category->{categorycode} }});
378 $patron = Koha::Patrons->find( $CBBI_patron->{borrowernumber} );
381 my $new_item = $builder->build_sample_item->unblessed;
382 my $prev_item = $builder->build_sample_item->unblessed;
383 # Second is Checked Out
384 BAIL_OUT("CanBookBeIssued Issue failed")
385 unless AddIssue($patron->unblessed, $prev_item->{barcode});
388 my $CBBI_mappings = [
393 msg => "patron, !wants_check_for_previous_checkout, !do_check_for_previous_checkout"
397 syspref => 'hardyes',
400 msg => "patron, wants_check_for_previous_checkout, !do_check_for_previous_checkout"
406 msg => "patron, !wants_check_for_previous_checkout, do_check_for_previous_checkout"
409 syspref => 'hardyes',
412 msg => "patron, wants_check_for_previous_checkout, do_check_for_previous_checkout"
418 t::lib::Mocks::mock_preference('checkprevcheckout', $_->{syspref});
419 my ( $issuingimpossible, $needsconfirmation ) =
420 C4::Circulation::CanBookBeIssued(
421 $patron, $_->{item}->{barcode}
423 is($needsconfirmation->{PREVISSUE}, $_->{result}, $_->{msg});
426 $schema->storage->txn_rollback;
428 subtest 'Check previous checkouts for serial' => sub {
430 $schema->storage->txn_begin;
432 my $library = $builder->build_object({ class => 'Koha::Libraries'});
434 my $patron = $builder->build_object({
435 class => 'Koha::Patrons',
437 branchcode => $library->branchcode
440 t::lib::Mocks::mock_userenv({ patron => $patron });
442 my $biblio = $builder->build_sample_biblio;
443 $biblio->serial(1)->store;
445 my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
446 my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
448 AddIssue($patron->unblessed, $item1->barcode);
450 is($patron->do_check_for_previous_checkout($item1->unblessed), 1, 'Check only one item if bibliographic record is serial');
451 is($patron->do_check_for_previous_checkout($item2->unblessed), 0, 'Check only one item if bibliographic record is serial');
453 $schema->storage->txn_rollback;
456 subtest 'Check previous checkouts with delay' => sub {
458 $schema->storage->txn_begin;
459 my $library = $builder->build_object({ class => 'Koha::Libraries'});
460 my $biblio = $builder->build_sample_biblio;
461 my $patron = $builder->build({source => 'Borrower'});
462 my $item_object = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
464 my $issue = Koha::Checkout->new({ branchcode => $library->branchcode, borrowernumber => $patron->{borrowernumber}, itemnumber => $item_object->itemnumber })->store;
465 my $returndate = dt_from_string()->subtract( days => 3 );
466 my $return = AddReturn($item_object->barcode, $library->branchcode, undef, $returndate);
468 t::lib::Mocks::mock_preference('checkprevcheckout', 'hardyes');
469 t::lib::Mocks::mock_preference('checkprevcheckoutdelay', 0);
470 my $patron1 = Koha::Patrons->find($patron->{borrowernumber});
472 $patron1->do_check_for_previous_checkout($item_object->unblessed),
473 1, "Checking CheckPrevCheckoutDelay disabled"
475 t::lib::Mocks::mock_preference('checkprevcheckoutdelay', 5);
477 $patron1->do_check_for_previous_checkout($item_object->unblessed),
478 1, "Checking CheckPrevCheckoutDelay enabled within delay"
480 t::lib::Mocks::mock_preference('checkprevcheckoutdelay', 1);
482 $patron1->do_check_for_previous_checkout($item_object->unblessed),
483 0, "Checking CheckPrevCheckoutDelay enabled after delay"
485 $schema->storage->txn_rollback;