View | Details | Raw Unified | Return to bug 15486
Collapse All | Expand All

(-)a/t/db_dependent/Holds.t (-4 / +132 lines)
Lines 7-15 use t::lib::TestBuilder; Link Here
7
7
8
use C4::Context;
8
use C4::Context;
9
9
10
use Test::More tests => 56;
10
use Test::More tests => 57;
11
use MARC::Record;
11
use MARC::Record;
12
use Koha::Patrons;
13
use C4::Items;
12
use C4::Items;
14
use C4::Biblio;
13
use C4::Biblio;
15
use C4::Reserves;
14
use C4::Reserves;
Lines 18-28 use C4::Calendar; Link Here
18
use Koha::Database;
17
use Koha::Database;
19
use Koha::DateUtils qw( dt_from_string output_pref );
18
use Koha::DateUtils qw( dt_from_string output_pref );
20
use Koha::Biblios;
19
use Koha::Biblios;
20
use Koha::CirculationRules;
21
use Koha::Holds;
21
use Koha::Holds;
22
use Koha::IssuingRules;
22
use Koha::Items;
23
use Koha::Items;
23
use Koha::Libraries;
24
use Koha::Libraries;
24
use Koha::Patrons;
25
use Koha::Patrons;
25
use Koha::CirculationRules;
26
26
27
BEGIN {
27
BEGIN {
28
    use FindBin;
28
    use FindBin;
Lines 519-524 subtest 'Pickup location availability tests' => sub { Link Here
519
       'libraryNotFound', 'Cannot set unknown library as pickup location');
519
       'libraryNotFound', 'Cannot set unknown library as pickup location');
520
};
520
};
521
521
522
$schema->storage->txn_rollback;
523
524
subtest 'CanItemBeReserved / holds_per_day tests' => sub {
525
526
    plan tests => 9;
527
528
    $schema->storage->txn_begin;
529
530
    Koha::Holds->search->delete;
531
    $dbh->do('DELETE FROM issues');
532
    Koha::Items->search->delete;
533
    Koha::Biblios->search->delete;
534
535
    my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
536
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
537
    my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
538
539
    # Create 3 biblios with items
540
    my ($bibnum_1) = create_helper_biblio( $itemtype->itemtype );
541
    my ( undef, undef, $itemnumber_1 ) = AddItem(
542
        {   homebranch    => $library->branchcode,
543
            holdingbranch => $library->branchcode
544
        },
545
        $bibnum
546
    );
547
    my ($bibnum_2) = create_helper_biblio( $itemtype->itemtype );
548
    my ( undef, undef, $itemnumber_2 ) = AddItem(
549
        {   homebranch    => $library->branchcode,
550
            holdingbranch => $library->branchcode
551
        },
552
        $bibnum_2
553
    );
554
    my ($bibnum_3) = create_helper_biblio( $itemtype->itemtype );
555
    my ( undef, undef, $itemnumber_3 ) = AddItem(
556
        {   homebranch    => $library->branchcode,
557
            holdingbranch => $library->branchcode
558
        },
559
        $bibnum_3
560
    );
561
562
    Koha::IssuingRules->search->delete;
563
    my $issuingrule = Koha::IssuingRule->new(
564
        {   categorycode     => '*',
565
            branchcode       => '*',
566
            itemtype         => $itemtype->itemtype,
567
            reservesallowed  => 1,
568
            holds_per_record => 99,
569
            holds_per_day    => 2
570
        }
571
    )->store;
572
573
    is_deeply(
574
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
575
        { status => 'OK' },
576
        'Patron can reserve item with hold limit of 1, no holds placed'
577
    );
578
579
    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
580
581
    is_deeply(
582
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
583
        { status => 'tooManyReserves', limit => 1 },
584
        'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
585
    );
586
587
    # Raise reservesallowed to avoid tooManyReserves from it
588
    $issuingrule->set( { reservesallowed => 3 } )->store;
589
590
    is_deeply(
591
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
592
        { status => 'OK' },
593
        'Patron can reserve item with 2 reserves daily cap'
594
    );
595
596
    # Add a second reserve
597
    my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
598
    is_deeply(
599
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
600
        { status => 'tooManyReservesToday', limit => 2 },
601
        'Patron cannot a third item with 2 reserves daily cap'
602
    );
603
604
    # Update last hold so reservedate is in the past, so 2 holds, but different day
605
    $hold = Koha::Holds->find($res_id);
606
    my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
607
    $hold->reservedate($yesterday)->store;
608
609
    is_deeply(
610
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
611
        { status => 'OK' },
612
        'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
613
    );
614
615
    # Set holds_per_day to 0
616
    $issuingrule->set( { holds_per_day => 0 } )->store;
617
618
    # Delete existing holds
619
    Koha::Holds->search->delete;
620
    is_deeply(
621
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
622
        { status => 'tooManyReservesToday', limit => 0 },
623
        'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
624
    );
625
626
    $issuingrule->set( { holds_per_day => undef } )->store;
627
    Koha::Holds->search->delete;
628
    is_deeply(
629
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
630
        { status => 'OK' },
631
        'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
632
    );
633
    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
634
    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
635
    is_deeply(
636
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
637
        { status => 'OK' },
638
        'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
639
    );
640
    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_3, '', 1, );
641
    is_deeply(
642
        CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
643
        { status => 'tooManyReserves', limit => 3 },
644
        'Unlimited daily holds, but reached reservesallowed'
645
    );
646
647
    $schema->storage->txn_rollback;
648
};
649
650
522
# Helper method to set up a Biblio.
651
# Helper method to set up a Biblio.
523
sub create_helper_biblio {
652
sub create_helper_biblio {
524
    my $itemtype = shift;
653
    my $itemtype = shift;
525
- 

Return to bug 15486