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

(-)a/t/db_dependent/Koha/Hold.t (-1 / +272 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 18;
23
use Test::More tests => 20;
24
24
25
use Test::Exception;
25
use Test::Exception;
26
use Test::MockModule;
26
use Test::MockModule;
Lines 36-41 use Koha::DateUtils qw(dt_from_string); Link Here
36
use Koha::Holds;
36
use Koha::Holds;
37
use Koha::Libraries;
37
use Koha::Libraries;
38
use Koha::Old::Holds;
38
use Koha::Old::Holds;
39
use C4::Reserves    qw( AddReserve ModReserveAffect );
40
use C4::Circulation qw( AddReturn );
39
41
40
my $schema  = Koha::Database->new->schema;
42
my $schema  = Koha::Database->new->schema;
41
my $builder = t::lib::TestBuilder->new;
43
my $builder = t::lib::TestBuilder->new;
Lines 1847-1849 subtest 'move_hold() tests' => sub { Link Here
1847
1849
1848
    $schema->storage->txn_rollback;
1850
    $schema->storage->txn_rollback;
1849
};
1851
};
1852
subtest 'is_hold_group_target, cleanup_hold_group and set_as_hold_group_target tests' => sub {
1853
1854
    plan tests => 16;
1855
1856
    $schema->storage->txn_begin;
1857
1858
    t::lib::Mocks::mock_preference( 'DisplayAddHoldGroups', 1 );
1859
1860
    my $patron_category = $builder->build(
1861
        {
1862
            source => 'Category',
1863
            value  => {
1864
                category_type                 => 'P',
1865
                enrolmentfee                  => 0,
1866
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
1867
            }
1868
        }
1869
    );
1870
1871
    my $library_1 = $builder->build( { source => 'Branch' } );
1872
    my $patron_1  = $builder->build(
1873
        {
1874
            source => 'Borrower',
1875
            value  => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} }
1876
        }
1877
    );
1878
    my $library_2 = $builder->build( { source => 'Branch' } );
1879
    my $patron_2  = $builder->build_object(
1880
        {
1881
            class => 'Koha::Patrons',
1882
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
1883
        }
1884
    );
1885
1886
    my $item = $builder->build_sample_item(
1887
        {
1888
            library => $library_1->{branchcode},
1889
        }
1890
    );
1891
1892
    my $item_2 = $builder->build_sample_item(
1893
        {
1894
            library => $library_1->{branchcode},
1895
        }
1896
    );
1897
1898
    set_userenv($library_2);
1899
    my $reserve_id = AddReserve(
1900
        {
1901
            branchcode     => $library_2->{branchcode},
1902
            borrowernumber => $patron_2->borrowernumber,
1903
            biblionumber   => $item->biblionumber,
1904
            priority       => 1,
1905
        }
1906
    );
1907
1908
    my $reserve_2_id = AddReserve(
1909
        {
1910
            branchcode     => $library_2->{branchcode},
1911
            borrowernumber => $patron_2->borrowernumber,
1912
            biblionumber   => $item_2->biblionumber,
1913
            priority       => 1,
1914
        }
1915
    );
1916
1917
    my $reserve_3_id = AddReserve(
1918
        {
1919
            branchcode     => $library_2->{branchcode},
1920
            borrowernumber => $patron_2->borrowernumber,
1921
            biblionumber   => $item_2->biblionumber,
1922
            priority       => 1,
1923
        }
1924
    );
1925
1926
    my $new_hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id, $reserve_3_id ] );
1927
1928
    set_userenv($library_1);
1929
    my $do_transfer = 1;
1930
    my ( $returned, $messages, $issue, $borrower ) = AddReturn( $item->barcode, $library_1->{branchcode} );
1931
1932
    ok( exists $messages->{ResFound}, 'ResFound key exists' );
1933
1934
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1935
1936
    my $hold = Koha::Holds->find($reserve_id);
1937
    is( $hold->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'First hold belongs to hold group' );
1938
    is( $hold->found,                     'T',                            'Hold is in transit' );
1939
1940
    is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' );
1941
    my $hold_2 = Koha::Holds->find($reserve_2_id);
1942
    is( $hold_2->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'Second hold belongs to hold group' );
1943
    is( $hold_2->is_hold_group_target,      0, 'Second hold is not the hold group target' );
1944
1945
    set_userenv($library_2);
1946
    $do_transfer = 0;
1947
    AddReturn( $item->barcode, $library_2->{branchcode} );
1948
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1949
    $hold = Koha::Holds->find($reserve_id);
1950
    is( $hold->found, 'W', 'Hold is waiting' );
1951
1952
    is( $hold->is_hold_group_target,   1, 'First hold is still the hold group target' );
1953
    is( $hold_2->is_hold_group_target, 0, 'Second hold is still not the hold group target' );
1954
1955
    $hold->cancel();
1956
    is( $hold->is_hold_group_target,   0, 'First hold is no longer the hold group target' );
1957
    is( $hold_2->is_hold_group_target, 0, 'Second hold is still not the hold group target' );
1958
1959
    # Return item for second hold
1960
    AddReturn( $item_2->barcode, $library_2->{branchcode} );
1961
    ModReserveAffect( $item_2->itemnumber, undef, $do_transfer, $reserve_2_id );
1962
    $new_hold_group->discard_changes;
1963
    is( $hold_2->get_from_storage->found, 'W', 'Hold is waiting' );
1964
    is( $hold_2->is_hold_group_target,    1,   'Second hold is now the hold group target' );
1965
1966
    my $hold_3 = Koha::Holds->find($reserve_3_id);
1967
    is( $hold_3->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'Third hold belongs to hold group' );
1968
    is( $hold_3->is_hold_group_target,      0,                              'Third hold is not the hold group target' );
1969
1970
    $hold_2->cancel();
1971
    is(
1972
        $hold_3->hold_group, undef,
1973
        'Third hold was left as the only member of its group. Group was deleted and the hold is no longer part of a hold group'
1974
    );
1975
1976
    $schema->storage->txn_rollback;
1977
};
1978
1979
subtest '_Findgroupreserve in the context of hold groups' => sub {
1980
    plan tests => 13;
1981
1982
    $schema->storage->txn_begin;
1983
1984
    t::lib::Mocks::mock_preference( 'DisplayAddHoldGroups', 1 );
1985
1986
    my $patron_category = $builder->build(
1987
        {
1988
            source => 'Category',
1989
            value  => {
1990
                category_type                 => 'P',
1991
                enrolmentfee                  => 0,
1992
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
1993
            }
1994
        }
1995
    );
1996
1997
    my $library_1 = $builder->build( { source => 'Branch' } );
1998
    my $patron_1  = $builder->build_object(
1999
        {
2000
            class => 'Koha::Patrons',
2001
            value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} }
2002
        }
2003
    );
2004
    my $library_2 = $builder->build( { source => 'Branch' } );
2005
    my $patron_2  = $builder->build_object(
2006
        {
2007
            class => 'Koha::Patrons',
2008
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
2009
        }
2010
    );
2011
2012
    my $item = $builder->build_sample_item(
2013
        {
2014
            library => $library_1->{branchcode},
2015
        }
2016
    );
2017
2018
    my $item_2 = $builder->build_sample_item(
2019
        {
2020
            library => $library_1->{branchcode},
2021
        }
2022
    );
2023
2024
    set_userenv($library_2);
2025
    my $reserve_id = AddReserve(
2026
        {
2027
            branchcode     => $library_2->{branchcode},
2028
            borrowernumber => $patron_2->borrowernumber,
2029
            biblionumber   => $item->biblionumber,
2030
            priority       => 1,
2031
        }
2032
    );
2033
2034
    my $reserve_2_id = AddReserve(
2035
        {
2036
            branchcode     => $library_2->{branchcode},
2037
            borrowernumber => $patron_2->borrowernumber,
2038
            biblionumber   => $item_2->biblionumber,
2039
            priority       => 1,
2040
        }
2041
    );
2042
2043
    my $reserve_3_id = AddReserve(
2044
        {
2045
            branchcode     => $library_2->{branchcode},
2046
            borrowernumber => $patron_1->borrowernumber,
2047
            biblionumber   => $item_2->biblionumber,
2048
            priority       => 2,
2049
        }
2050
    );
2051
2052
    my @reserves = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
2053
    is( scalar @reserves,           2,             "No hold group created yet. We should get both holds" );
2054
    is( $reserves[0]->{reserve_id}, $reserve_2_id, "We got the expected reserve" );
2055
2056
    my $new_hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id ] );
2057
2058
    set_userenv($library_1);
2059
    my $do_transfer = 1;
2060
    my ( $returned, $messages, $issue, $borrower ) = AddReturn( $item->barcode, $library_1->{branchcode} );
2061
2062
    ok( exists $messages->{ResFound}, 'ResFound key exists' );
2063
2064
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
2065
2066
    my $hold = Koha::Holds->find($reserve_id);
2067
    is( $hold->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'First hold belongs to hold group' );
2068
    is( $hold->found,                     'T',                            'Hold is in transit' );
2069
2070
    is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' );
2071
2072
    my @new_reserves = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
2073
    is( scalar @new_reserves, 1, "reserve_2_id is now part of a group that has a target. Should not be here." );
2074
    is(
2075
        $new_reserves[0]->{reserve_id}, $reserve_3_id,
2076
        "reserve_2_id is now part of a group that has a target. Should not be here."
2077
    );
2078
2079
    $hold->cancel();
2080
2081
    my @reserves_after_cancel = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
2082
    is(
2083
        scalar @reserves_after_cancel, 2,
2084
        "reserve_2_id should be back in the pool as it's no longer part of a group."
2085
    );
2086
    is( $reserves_after_cancel[0]->{reserve_id}, $reserve_2_id, "We got the expected reserve" );
2087
    is( $reserves_after_cancel[1]->{reserve_id}, $reserve_3_id, "We got the expected reserve" );
2088
2089
    my $first_reserve_id_again = AddReserve(
2090
        {
2091
            branchcode     => $library_2->{branchcode},
2092
            borrowernumber => $patron_2->borrowernumber,
2093
            biblionumber   => $item->biblionumber,
2094
            priority       => 1,
2095
        }
2096
    );
2097
2098
    # Fake the holds queue
2099
    my $dbh = C4::Context->dbh;
2100
    $dbh->do(
2101
        q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)}, undef,
2102
        (
2103
            $patron_1->borrowernumber, $item->biblionumber, $item->itemnumber, $item->homebranch, 0,
2104
            $first_reserve_id_again
2105
        )
2106
    );
2107
2108
    my @reserves_after_holds_queue = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
2109
    is( scalar @new_reserves, 1, "Only reserve_3_id should exist." );
2110
    is(
2111
        $new_reserves[0]->{reserve_id}, $reserve_3_id,
2112
        "reserve_3_id should not be here."
2113
    );
2114
};
2115
2116
sub set_userenv {
2117
    my ($library) = @_;
2118
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
2119
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
2120
}
(-)a/t/db_dependent/Reserves/HoldGroup.t (-2 / +84 lines)
Lines 20-28 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 2;
23
use Test::More tests => 3;
24
24
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
26
use t::lib::Mocks;
27
28
use C4::Reserves    qw( AddReserve ModReserveAffect );
29
use C4::Circulation qw( AddReturn );
26
30
27
my $schema  = Koha::Database->new->schema;
31
my $schema  = Koha::Database->new->schema;
28
my $builder = t::lib::TestBuilder->new;
32
my $builder = t::lib::TestBuilder->new;
Lines 52-54 subtest 'holds tests' => sub { Link Here
52
56
53
    $schema->storage->txn_rollback;
57
    $schema->storage->txn_rollback;
54
};
58
};
55
- 
59
60
subtest "target_hold_id tests" => sub {
61
62
    plan tests => 2;
63
64
    $schema->storage->txn_begin;
65
66
    t::lib::Mocks::mock_preference( 'DisplayAddHoldGroups', 1 );
67
68
    my $patron_category = $builder->build(
69
        {
70
            source => 'Category',
71
            value  => {
72
                category_type                 => 'P',
73
                enrolmentfee                  => 0,
74
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
75
            }
76
        }
77
    );
78
79
    my $library_1 = $builder->build( { source => 'Branch' } );
80
    my $library_2 = $builder->build( { source => 'Branch' } );
81
    my $patron_2  = $builder->build_object(
82
        {
83
            class => 'Koha::Patrons',
84
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
85
        }
86
    );
87
88
    my $item = $builder->build_sample_item(
89
        {
90
            library => $library_1->{branchcode},
91
        }
92
    );
93
94
    my $item_2 = $builder->build_sample_item(
95
        {
96
            library => $library_1->{branchcode},
97
        }
98
    );
99
100
    set_userenv($library_2);
101
    my $reserve_id = AddReserve(
102
        {
103
            branchcode     => $library_2->{branchcode},
104
            borrowernumber => $patron_2->borrowernumber,
105
            biblionumber   => $item->biblionumber,
106
            priority       => 1,
107
        }
108
    );
109
110
    my $reserve_2_id = AddReserve(
111
        {
112
            branchcode     => $library_2->{branchcode},
113
            borrowernumber => $patron_2->borrowernumber,
114
            biblionumber   => $item_2->biblionumber,
115
            priority       => 1,
116
        }
117
    );
118
119
    my $hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id ] );
120
121
    set_userenv($library_1);
122
    my $do_transfer = 1;
123
    AddReturn( $item->barcode, $library_1->{branchcode} );
124
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
125
    my $hold = Koha::Holds->find($reserve_id);
126
    is( $hold_group->get_from_storage->target_hold_id, $hold->reserve_id, 'target_hold_id is correct' );
127
    $hold->fill();
128
    is( $hold_group->get_from_storage, undef, 'hold group no longer exists' );
129
130
    $schema->storage->txn_rollback;
131
};
132
133
sub set_userenv {
134
    my ($library) = @_;
135
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
136
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
137
}

Return to bug 40529