#!/usr/bin/perl
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
use Modern::Perl;
-use Test::More tests => 34;
+use Test::More tests => 56;
+
use MARC::Record;
use DateTime::Duration;
$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 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'");
+
# Setup Test------------------------
# Add branches if not existing
$dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
}
-# Helper biblio.
-diag("\nCreating biblio instance for testing.");
+# Create a helper biblio
my $bib = MARC::Record->new();
my $title = 'Silence in the library';
if( C4::Context->preference('marcflavour') eq 'UNIMARC' ) {
my ($bibnum, $bibitemnum);
($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
-# Helper item for that biblio.
-diag("Creating item instance for testing.");
+# Create a helper item instance for testing
my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
# Modify item; setting barcode.
is($status, "Reserved", "CheckReserves Test 1");
+ok(exists($reserve->{reserve_id}), 'CheckReserves() include reserve_id in its response');
+
($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
is($status, "Reserved", "CheckReserves Test 2");
$requesters{'RPL'},
'for generous library, its items fill first hold request in line (bug 10272)');
+my $reserves = GetReservesFromBiblionumber({biblionumber => $biblionumber});
+isa_ok($reserves, 'ARRAY');
+is(scalar @$reserves, 1, "Only one reserves for this biblio");
+my $reserve_id = $reserves->[0]->{reserve_id};
+
+$reserve = GetReserve($reserve_id);
+isa_ok($reserve, 'HASH', "GetReserve return");
+is($reserve->{biblionumber}, $biblionumber);
+
+$reserve = CancelReserve({reserve_id => $reserve_id});
+isa_ok($reserve, 'HASH', "CancelReserve return");
+is($reserve->{biblionumber}, $biblionumber);
+
+$reserve = GetReserve($reserve_id);
+is($reserve, undef, "GetReserve returns undef after deletion");
+
+$reserve = CancelReserve({reserve_id => $reserve_id});
+is($reserve, undef, "CancelReserve return undef if reserve does not exist");
+
+
# Tests for bug 9761 (ConfirmFutureHolds): new CheckReserves lookahead parameter, and corresponding change in AddReturn
# Note that CheckReserve uses its lookahead parameter and does not check ConfirmFutureHolds pref (it should be passed if needed like AddReturn does)
# Test 9761a: Add a reserve without date, CheckReserve should return it
is($p, 3, 'CalculatePriority should now return priority 3');
# End of tests for bug 8918
+# Tests for cancel reserves by users from OPAC.
+$dbh->do('DELETE FROM reserves', undef, ($bibnum));
+AddReserve('CPL', $requesters{'CPL'}, $item_bibnum,
+ $constraint, $bibitems, 1, undef, $expdate, $notes,
+ $title, $checkitem, '');
+my (undef, $canres, undef) = CheckReserves($itemnumber);
+
+is( CanReserveBeCanceledFromOpac(), undef,
+ 'CanReserveBeCanceledFromOpac should return undef if called without any parameter'
+);
+is(
+ CanReserveBeCanceledFromOpac( $canres->{resserve_id} ),
+ undef,
+ 'CanReserveBeCanceledFromOpac should return undef if called without the reserve_id'
+);
+is(
+ CanReserveBeCanceledFromOpac( undef, $requesters{CPL} ),
+ undef,
+ 'CanReserveBeCanceledFromOpac should return undef if called without borrowernumber'
+);
+
+my $cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
+is($cancancel, 1, 'Can user cancel its own reserve');
+
+$cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'FPL'});
+is($cancancel, 0, 'Other user cant cancel reserve');
+
+ModReserveAffect($itemnumber, $requesters{'CPL'}, 1);
+$cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
+is($cancancel, 0, 'Reserve in transfer status cant be canceled');
+
+$dbh->do('DELETE FROM reserves', undef, ($bibnum));
+AddReserve('CPL', $requesters{'CPL'}, $item_bibnum,
+ $constraint, $bibitems, 1, undef, $expdate, $notes,
+ $title, $checkitem, '');
+(undef, $canres, undef) = CheckReserves($itemnumber);
+
+ModReserveAffect($itemnumber, $requesters{'CPL'}, 0);
+$cancancel = CanReserveBeCanceledFromOpac($canres->{reserve_id}, $requesters{'CPL'});
+is($cancancel, 0, 'Reserve in waiting status cant be canceled');
+
+# End of tests for bug 12876
+
+ ####
+####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
+ ####
+
+C4::Context->set_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
+
+#Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
+
+#Set the ageRestriction for the Biblio
+my $record = GetMarcBiblio( $bibnum );
+my ( $ageres_tagid, $ageres_subfieldid ) = GetMarcFromKohaField( "biblioitems.agerestriction" );
+$record->append_fields( MARC::Field->new($ageres_tagid, '', '', $ageres_subfieldid => 'PEGI 16') );
+C4::Biblio::ModBiblio( $record, $bibnum, '' );
+
+is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving an ageRestricted Biblio without a borrower dateofbirth succeeds" );
+
+#Set the dateofbirth for the Borrower making him "too young".
+my $now = DateTime->now();
+C4::Members::SetAge( $borrower, '0015-00-00' );
+C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
+
+is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'ageRestricted', "Reserving a 'PEGI 16' Biblio by a 15 year old borrower fails");
+
+#Set the dateofbirth for the Borrower making him "too old".
+C4::Members::SetAge( $borrower, '0030-00-00' );
+C4::Members::ModMember( borrowernumber => $borrowernumber, dateofbirth => $borrower->{dateofbirth} );
+
+is( C4::Reserves::CanBookBeReserved($borrowernumber, $biblionumber) , 'OK', "Reserving a 'PEGI 16' Biblio by a 30 year old borrower succeeds");
+ ####
+####### EO Bug 13113 <<<
+ ####
+
+my $item = GetItem($itemnumber);
+
+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 $rule = C4::Circulation::GetIssuingRule($categorycode, $itype, $holdingbranch);
+
+$dbh->do(
+ "UPDATE issuingrules SET onshelfholds = 1 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
+ undef,
+ $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
+);
+ok( C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() allowed" );
+$dbh->do(
+ "UPDATE issuingrules SET onshelfholds = 0 WHERE categorycode = ? AND itemtype= ? and branchcode = ?",
+ undef,
+ $rule->{categorycode}, $rule->{itemtype}, $rule->{branchcode}
+);
+ok( !C4::Reserves::OnShelfHoldsAllowed($item, $borrower), "OnShelfHoldsAllowed() disallowed" );
+
$dbh->rollback;
sub count_hold_print_messages {
my $message_count = $dbh->selectall_arrayref(q{
- SELECT COUNT(*) FROM message_queue WHERE letter_code = 'HOLD_PRINT'
+ SELECT COUNT(*)
+ FROM message_queue
+ WHERE letter_code = 'HOLD'
+ AND message_transport_type = 'print'
});
return $message_count->[0]->[0];
}