Lines 17-23
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 18; |
20 |
use Test::More tests => 20; |
21 |
use Test::NoWarnings; |
21 |
use Test::NoWarnings; |
22 |
use Test::MockModule; |
22 |
use Test::MockModule; |
23 |
use Test::Mojo; |
23 |
use Test::Mojo; |
Lines 39-44
use Koha::Biblioitems;
Link Here
|
39 |
use Koha::Items; |
39 |
use Koha::Items; |
40 |
use Koha::CirculationRules; |
40 |
use Koha::CirculationRules; |
41 |
use Koha::Patron::Debarments qw(AddDebarment); |
41 |
use Koha::Patron::Debarments qw(AddDebarment); |
|
|
42 |
use Koha::Holds; |
42 |
|
43 |
|
43 |
my $schema = Koha::Database->new->schema; |
44 |
my $schema = Koha::Database->new->schema; |
44 |
my $builder = t::lib::TestBuilder->new(); |
45 |
my $builder = t::lib::TestBuilder->new(); |
Lines 1885-1887
subtest 'PUT /holds/{hold_id}/lowest_priority tests' => sub {
Link Here
|
1885 |
|
1886 |
|
1886 |
$schema->storage->txn_rollback; |
1887 |
$schema->storage->txn_rollback; |
1887 |
}; |
1888 |
}; |
|
|
1889 |
|
1890 |
subtest 'debits embedding tests' => sub { |
1891 |
|
1892 |
plan tests => 8; |
1893 |
|
1894 |
$schema->storage->txn_begin; |
1895 |
|
1896 |
# Setup proper permissions and authentication |
1897 |
my $categorycode = $builder->build( { source => 'Category' } )->{categorycode}; |
1898 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
1899 |
my $password = 'thePassword123'; |
1900 |
|
1901 |
my $librarian = $builder->build_object( |
1902 |
{ |
1903 |
class => 'Koha::Patrons', |
1904 |
value => { |
1905 |
categorycode => $categorycode, |
1906 |
branchcode => $branchcode, |
1907 |
flags => 80, # borrowers (16) + reserveforothers (64) = 80 |
1908 |
} |
1909 |
} |
1910 |
); |
1911 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
1912 |
my $userid = $librarian->userid; |
1913 |
|
1914 |
my $patron = $builder->build_object( |
1915 |
{ |
1916 |
class => 'Koha::Patrons', |
1917 |
value => { |
1918 |
categorycode => $categorycode, |
1919 |
branchcode => $branchcode, |
1920 |
} |
1921 |
} |
1922 |
); |
1923 |
my $biblio = $builder->build_sample_biblio(); |
1924 |
|
1925 |
# Create a hold using the proper API method |
1926 |
my $reserve_id = C4::Reserves::AddReserve( |
1927 |
{ |
1928 |
branchcode => $branchcode, |
1929 |
borrowernumber => $patron->borrowernumber, |
1930 |
biblionumber => $biblio->biblionumber, |
1931 |
priority => 1, |
1932 |
} |
1933 |
); |
1934 |
|
1935 |
my $hold = Koha::Holds->find($reserve_id); |
1936 |
|
1937 |
# Create account lines and link them to the hold |
1938 |
my $fee1 = $patron->account->add_debit( |
1939 |
{ |
1940 |
amount => 2.50, |
1941 |
description => 'Hold placement fee', |
1942 |
type => 'RESERVE', |
1943 |
interface => 'intranet' |
1944 |
} |
1945 |
); |
1946 |
$fee1->add_link( 'hold', $hold->id ); |
1947 |
|
1948 |
# Test without embedding - should not include debits |
1949 |
$t->get_ok( "//$userid:$password@/api/v1/holds?hold_id=" . $hold->id )->status_is(200)->json_has('/0/hold_id') |
1950 |
->json_hasnt( '/0/debits', 'No debits without embedding' ); |
1951 |
|
1952 |
# Test with debits embedding - should include debits |
1953 |
$t->get_ok( |
1954 |
"//$userid:$password@/api/v1/holds?hold_id=" . $hold->id, |
1955 |
{ 'x-koha-embed' => 'debits' } |
1956 |
)->status_is(200)->json_has( '/0/debits', 'debits present when embedded' ) |
1957 |
->json_is( '/0/debits/0/amount', 2.50, 'Account line amount correct' ); |
1958 |
|
1959 |
$schema->storage->txn_rollback; |
1960 |
}; |
1961 |
|
1962 |
subtest 'POST with debits embedding tests' => sub { |
1963 |
|
1964 |
plan tests => 4; |
1965 |
|
1966 |
$schema->storage->txn_begin; |
1967 |
|
1968 |
# Setup proper permissions and authentication |
1969 |
my $categorycode = $builder->build( { source => 'Category' } )->{categorycode}; |
1970 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
1971 |
my $password = 'thePassword123'; |
1972 |
|
1973 |
my $librarian = $builder->build_object( |
1974 |
{ |
1975 |
class => 'Koha::Patrons', |
1976 |
value => { |
1977 |
categorycode => $categorycode, |
1978 |
branchcode => $branchcode, |
1979 |
flags => 80, # borrowers (16) + reserveforothers (64) = 80 |
1980 |
} |
1981 |
} |
1982 |
); |
1983 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
1984 |
my $userid = $librarian->userid; |
1985 |
|
1986 |
my $patron = $builder->build_object( |
1987 |
{ |
1988 |
class => 'Koha::Patrons', |
1989 |
value => { |
1990 |
categorycode => $categorycode, |
1991 |
branchcode => $branchcode, |
1992 |
} |
1993 |
} |
1994 |
); |
1995 |
my $biblio = $builder->build_sample_biblio(); |
1996 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
1997 |
|
1998 |
# Set up system preference to charge fees for holds |
1999 |
t::lib::Mocks::mock_preference( 'HoldFeeMode', 'any_time_is_placed' ); |
2000 |
|
2001 |
# Create a circulation rule with hold fee |
2002 |
Koha::CirculationRules->set_rules( |
2003 |
{ |
2004 |
categorycode => $patron->categorycode, |
2005 |
branchcode => undef, |
2006 |
itemtype => undef, |
2007 |
rules => { |
2008 |
hold_fee => 2.50, |
2009 |
} |
2010 |
} |
2011 |
); |
2012 |
|
2013 |
# Make sure pickup location checks doesn't get in the middle |
2014 |
my $mock_biblio = Test::MockModule->new('Koha::Biblio'); |
2015 |
$mock_biblio->mock( 'pickup_locations', sub { return Koha::Libraries->search; } ); |
2016 |
my $mock_item = Test::MockModule->new('Koha::Item'); |
2017 |
$mock_item->mock( 'pickup_locations', sub { return Koha::Libraries->search } ); |
2018 |
|
2019 |
# Test POST with embedding (should include automatically charged debits in response) |
2020 |
$t->post_ok( |
2021 |
"//$userid:$password@/api/v1/holds" => { 'x-koha-embed' => 'debits' } => json => { |
2022 |
patron_id => $patron->borrowernumber, |
2023 |
biblio_id => $biblio->biblionumber, |
2024 |
item_id => $item->itemnumber, |
2025 |
pickup_library_id => $branchcode, |
2026 |
} |
2027 |
)->status_is(201)->json_has( '/debits', 'debits embedded in POST response' ) |
2028 |
->json_is( '/debits/0/amount', 2.50, 'Hold fee automatically charged and embedded' ); |
2029 |
|
2030 |
$schema->storage->txn_rollback; |
2031 |
}; |