use MARC::Record;
use DateTime::Duration;
-use C4::Biblio;
use C4::Circulation;
use C4::Items;
+use C4::Biblio;
use C4::Members;
use C4::Reserves;
use Koha::Caches;
my $builder = t::lib::TestBuilder->new;
-my $frameworkcode = q||;
+my $frameworkcode = q//;
+
+
+t::lib::Mocks::mock_preference('ReservesNeedReturns', 1);
# Somewhat arbitrary field chosen for age restriction unit tests. Must be added to db before the framework is cached
$dbh->do("update marc_subfield_structure set kohafield='biblioitems.agerestriction' where tagfield='521' and tagsubfield='a' and frameworkcode=?", undef, $frameworkcode);
my $cache = Koha::Caches->get_instance;
$cache->clear_from_cache("MarcStructure-0-$frameworkcode");
$cache->clear_from_cache("MarcStructure-1-$frameworkcode");
-$cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
+$cache->clear_from_cache("default_value_for_mod_marc-");
$cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
## Setup Test
my $itemtype = $builder->build(
{ source => 'Itemtype', value => { notforloan => undef } } )->{itemtype};
-C4::Context->set_userenv(
- undef, undef, undef, undef, undef, undef, $branch_1
-);
+t::lib::Mocks::mock_userenv({ branchcode => $branch_1 });
# Create a helper biblio
my $bib = MARC::Record->new();
branchcode => $branch_1,
);
Koha::Patron::Categories->find($category_1)->set({ enrolmentfee => 0})->store;
-my $borrowernumber = AddMember(%data);
+my $borrowernumber = Koha::Patron->new(\%data)->store->borrowernumber;
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
my $biblionumber = $bibnum;
my $barcode = $testbarcode;
### Regression test for bug 10272
###
my %requesters = ();
-$requesters{$branch_1} = AddMember(
+$requesters{$branch_1} = Koha::Patron->new({
branchcode => $branch_1,
categorycode => $category_2,
surname => "borrower from $branch_1",
-);
+})->store->borrowernumber;
for my $i ( 2 .. 5 ) {
- $requesters{"CPL$i"} = AddMember(
+ $requesters{"CPL$i"} = Koha::Patron->new({
branchcode => $branch_1,
categorycode => $category_2,
surname => "borrower $i from $branch_1",
- );
+ })->store->borrowernumber;
}
-$requesters{$branch_2} = AddMember(
+$requesters{$branch_2} = Koha::Patron->new({
branchcode => $branch_2,
categorycode => $category_2,
surname => "borrower from $branch_2",
-);
-$requesters{$branch_3} = AddMember(
+})->store->borrowernumber;
+$requesters{$branch_3} = Koha::Patron->new({
branchcode => $branch_3,
categorycode => $category_2,
surname => "borrower from $branch_3",
-);
+})->store->borrowernumber;
# Configure rules so that $branch_1 allows only $branch_1 patrons
# to request its items, while $branch_2 will allow its items
'item that is captured to fill a hold cannot be deleted',
);
-my $letter = ReserveSlip($branch_1, $requesters{$branch_1}, $bibnum);
+my $letter = ReserveSlip( { branchcode => $branch_1, borrowernumber => $requesters{$branch_1}, biblionumber => $bibnum } );
ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
# Tests for bug 9788: Does Koha::Item->current_holds return a future wait?
$record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
C4::Biblio::ModBiblio( $record, $bibnum, $frameworkcode );
-is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
+is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
#Set the dateofbirth for the Borrower making them "too young".
$borrower->{dateofbirth} = DateTime->now->add( years => -15 );
-C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
+Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
-is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
+is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
#Set the dateofbirth for the Borrower making them "too old".
$borrower->{dateofbirth} = DateTime->now->add( years => -30 );
-C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
+Koha::Patrons->find( $borrowernumber )->set({ dateofbirth => $borrower->{dateofbirth} })->store;
-is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
+is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber)->{status} , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
####
####### EO Bug 13113 <<<
####
ok( C4::Reserves::IsAvailableForItemLevelRequest($item, $borrower), "Reserving a book on item level" );
-my $itype = C4::Reserves::_get_itype($item);
-my $categorycode = $borrower->{categorycode};
-my $holdingbranch = $item->{holdingbranch};
-my $issuing_rule = Koha::IssuingRules->get_effective_issuing_rule(
- {
- categorycode => $categorycode,
- itemtype => $itype,
- branchcode => $holdingbranch
- }
-);
-
-$dbh->do(
- "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
- undef,
- $issuing_rule->categorycode, $issuing_rule->itemtype, $issuing_rule->branchcode
-);
-ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
-$dbh->do(
- "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
- undef,
- $issuing_rule->categorycode, $issuing_rule->itemtype, $issuing_rule->branchcode
-);
-ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
-
# tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
# hold from A pos 1, today, no fut holds: MoveReserve should fill it
$dbh->do('DELETE FROM reserves', undef, ($bibnum));
$cache->clear_from_cache("MarcStructure-0-$frameworkcode");
$cache->clear_from_cache("MarcStructure-1-$frameworkcode");
-$cache->clear_from_cache("default_value_for_mod_marc-$frameworkcode");
+$cache->clear_from_cache("default_value_for_mod_marc-");
$cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
subtest '_koha_notify_reserve() tests' => sub {
}
})->{borrowernumber};
- my $hold = $builder->build({
- source => 'Reserve',
- value => {
- borrowernumber=>$hold_borrower
- }
- });
+ C4::Reserves::AddReserve(
+ $item->{homebranch}, $hold_borrower,
+ $item->{biblionumber} );
- ModReserveAffect($hold->{itemnumber}, $hold->{borrowernumber}, 0);
+ ModReserveAffect($item->{itemnumber}, $hold_borrower, 0);
my $sms_message_address = $schema->resultset('MessageQueue')->search({
letter_code => 'HOLD',
message_transport_type => 'sms',
};
+subtest 'ReservesNeedReturns' => sub {
+ plan tests => 4;
+
+ my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
+ my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
+ my $item_info = {
+ biblionumber => $biblioitem->biblionumber,
+ biblioitemnumber => $biblioitem->biblioitemnumber,
+ homebranch => $library->branchcode,
+ holdingbranch => $library->branchcode,
+ itype => $itemtype->itemtype,
+ };
+ my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
+ my $patron = $builder->build_object(
+ {
+ class => 'Koha::Patrons',
+ value => { branchcode => $library->branchcode, }
+ }
+ );
+
+ my $priority = 1;
+ my ( $hold_id, $hold );
+
+ t::lib::Mocks::mock_preference('ReservesNeedReturns', 0); # '0' means 'Automatically mark a hold as found and waiting'
+ $hold_id = C4::Reserves::AddReserve(
+ $library->branchcode, $patron->borrowernumber,
+ $item->biblionumber, '',
+ $priority, undef,
+ undef, '',
+ "title for fee", $item->itemnumber,
+ );
+ $hold = Koha::Holds->find($hold_id);
+ is( $hold->priority, 0, 'If ReservesNeedReturns is 0, priority must have been set to 0' );
+ is( $hold->found, 'W', 'If ReservesNeedReturns is 0, found must have been set waiting' );
+
+ $hold->delete; # cleanup
+
+ t::lib::Mocks::mock_preference('ReservesNeedReturns', 1); # '0' means "Don't automatically mark a hold as found and waiting"
+ $hold_id = C4::Reserves::AddReserve(
+ $library->branchcode, $patron->borrowernumber,
+ $item->biblionumber, '',
+ $priority, undef,
+ undef, '',
+ "title for fee", $item->itemnumber,
+ );
+ $hold = Koha::Holds->find($hold_id);
+ is( $hold->priority, $priority, 'If ReservesNeedReturns is 1, priority must not have been set to changed' );
+ is( $hold->found, undef, 'If ReservesNeedReturns is 1, found must not have been set waiting' );
+};
+
+subtest 'ChargeReserveFee tests' => sub {
+
+ plan tests => 8;
+
+ my $library = $builder->build_object({ class => 'Koha::Libraries' });
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+
+ my $fee = 20;
+ my $title = 'A title';
+
+ my $context = Test::MockModule->new('C4::Context');
+ $context->mock( userenv => { branch => $library->id } );
+
+ my $line = C4::Reserves::ChargeReserveFee( $patron->id, $fee, $title );
+
+ is( ref($line), 'Koha::Account::Line' , 'Returns a Koha::Account::Line object');
+ ok( $line->is_debit, 'Generates a debit line' );
+ is( $line->accounttype, 'Res' , 'generates Res accounttype');
+ is( $line->borrowernumber, $patron->id , 'generated line belongs to the passed patron');
+ is( $line->amount, $fee , 'amount set correctly');
+ is( $line->amountoutstanding, $fee , 'amountoutstanding set correctly');
+ is( $line->description, "Reserve Charge - $title" , 'Hardcoded description is generated');
+ is( $line->branchcode, $library->id , "Library id is picked from userenv and stored correctly" );
+};
+
sub count_hold_print_messages {
my $message_count = $dbh->selectall_arrayref(q{
SELECT COUNT(*)