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

(-)a/t/db_dependent/HoldsQueue.t (-2 / +147 lines)
Lines 8-14 Link Here
8
8
9
use Modern::Perl;
9
use Modern::Perl;
10
10
11
use Test::More tests => 55;
11
use Test::More tests => 56;
12
use Data::Dumper;
12
use Data::Dumper;
13
13
14
use C4::Calendar;
14
use C4::Calendar;
Lines 1522-1527 sub test_queue { Link Here
1522
1522
1523
}
1523
}
1524
1524
1525
subtest "Test _checkHoldPolicy" => sub {
1526
    plan tests => 25;
1527
1528
    my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
1529
    my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
1530
    my $library_nongroup = $builder->build_object( { class => 'Koha::Libraries' } );
1531
    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
1532
    my $patron  = $builder->build_object(
1533
        {
1534
            class => "Koha::Patrons",
1535
            value => {
1536
                branchcode => $library1->branchcode,
1537
                categorycode => $category->categorycode,
1538
            }
1539
        }
1540
    );
1541
    my $biblio = $builder->build_sample_biblio();
1542
    my $item1  = $builder->build_sample_item(
1543
        {
1544
            biblionumber => $biblio->biblionumber,
1545
            library      => $library1->branchcode,
1546
            exclude_from_local_holds_priority => 0,
1547
        }
1548
    );
1549
    my $item2 = $builder->build_sample_item(
1550
        {
1551
            biblionumber => $biblio->biblionumber,
1552
            library      => $library1->branchcode,
1553
            exclude_from_local_holds_priority => 0,
1554
        }
1555
    );
1556
1557
    my $item3 = $builder->build_sample_item(
1558
        {
1559
            biblionumber => $biblio->biblionumber,
1560
            library      => $library1->branchcode,
1561
            exclude_from_local_holds_priority => 0,
1562
        }
1563
    );
1564
1565
    $reserve_id = AddReserve(
1566
        {
1567
            branchcode     => $item1->homebranch,
1568
            borrowernumber => $patron->borrowernumber,
1569
            biblionumber   => $biblio->id,
1570
            priority       => 1
1571
        }
1572
    );
1573
    ok( $reserve_id, "Hold was created");
1574
    my $requests = C4::HoldsQueue::GetPendingHoldRequestsForBib($biblio->biblionumber);
1575
    is( @$requests, 1, "Got correct number of holds");
1576
1577
    my $request = $requests->[0];
1578
    is( $request->{biblionumber}, $biblio->id, "Hold has correct biblio");
1579
    is( $request->{borrowernumber}, $patron->id, "Hold has correct borrower");
1580
    is( $request->{borrowerbranch}, $patron->branchcode, "Hold has correct borrowerbranch");
1581
1582
    my $hold = Koha::Holds->find( $reserve_id );
1583
    ok( $hold, "Found hold" );
1584
1585
    my $item = {
1586
        holdallowed              => 1,
1587
        homebranch               => $request->{borrowerbranch}, # library1
1588
        hold_fulfillment_policy  => 'any'
1589
    };
1590
1591
    # Base case should work
1592
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true" );
1593
1594
    # Test holdallowed = 0
1595
    $item->{holdallowed} = 0;
1596
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if holdallowed = 0" );
1597
1598
    # Test holdallowed = 1
1599
    $item->{holdallowed} = 1;
1600
    $item->{homebranch} = $library_nongroup->id;
1601
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if holdallowed = 1 and branches do not match" );
1602
1603
    $item->{homebranch} = $request->{borrowerbranch};
1604
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if holdallowed = 1 and branches do match" );
1605
1606
    # Test holdallowed = 3
1607
    $item->{holdallowed} = 3;
1608
    $item->{homebranch} = $library_nongroup->id;
1609
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if branchode doesn't match, holdallowed = 3 and no group branches exist" );
1610
    $item->{homebranch} = $request->{borrowerbranch};
1611
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if branchode matches, holdallowed = 3 and no group branches exist" );
1612
1613
    # Create library groups hierarchy
1614
    my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
1615
    my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1616
    my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode}} );
1617
1618
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if holdallowed = 3 and no group branches exist" );
1619
1620
    $group1->delete;
1621
1622
    # Test hold_fulfillment_policy = holdgroup
1623
    $item->{hold_fulfillment_policy} = 'holdgroup';
1624
    $item->{homebranch} = $library_nongroup->id;
1625
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns true if library is not part of hold group, branches don't match and hfp = holdgroup" );
1626
    $item->{homebranch} = $request->{borrowerbranch};
1627
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is not part of hold group, branches match and hfp = holdgroup" );
1628
1629
    $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1630
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
1631
1632
    $item->{homebranch} = $library2->id;
1633
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
1634
    $item->{homebranch} = $library1->id;
1635
1636
    $group1->delete;
1637
1638
    # Test hold_fulfillment_policy = homebranch
1639
    $item->{hold_fulfillment_policy} = 'homebranch';
1640
    $item->{homebranch} = $library_nongroup->id;
1641
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if hfp = homebranch and pickup branch != item homebranch" );
1642
1643
    $item->{homebranch} = $request->{borrowerbranch};
1644
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if hfp = homebranch and pickup branch = item homebranch" );
1645
1646
    # Test hold_fulfillment_policy = holdingbranch
1647
    $item->{hold_fulfillment_policy} = 'holdingbranch';
1648
    $item->{holdingbranch} = $library_nongroup->id;
1649
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if hfp = holdingbranch and pickup branch != item holdingbranch" );
1650
1651
    $item->{holdingbranch} = $request->{borrowerbranch};
1652
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if hfp = holdingbranch and pickup branch = item holdingbranch" );
1653
1654
    # Test hold_fulfillment_policy = patrongroup
1655
    $item->{hold_fulfillment_policy} = 'patrongroup';
1656
    $item->{borrowerbranch} = $library1->id;
1657
1658
    $item->{homebranch} = $library_nongroup->id;
1659
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if library is not part of hold group, branches don't match, hfp = patrongroup" );
1660
    $item->{homebranch} = $request->{borrowerbranch};
1661
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns false if library is not part of hold group, branches match, hfp = patrongroup" );
1662
1663
    $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
1664
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
1665
1666
    $item->{borrowerbranch} = $library2->id;
1667
    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
1668
    $item->{borrowerbranch} = $library1->id;
1669
};
1670
1525
sub dump_records {
1671
sub dump_records {
1526
    my ($tablename) = @_;
1672
    my ($tablename) = @_;
1527
    return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
1673
    return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
1528
- 

Return to bug 27068