Bug 10817: return hold items info in SIP Patron Info Response
[koha.git] / t / db_dependent / Holds.t
index c7a95fd..74cecfa 100755 (executable)
@@ -2,12 +2,16 @@
 
 use strict;
 use warnings;
+
+use t::lib::Mocks;
+use C4::Context;
 use C4::Branch;
 
-use Test::More tests => 19;
+use Test::More tests => 22;
 use MARC::Record;
 use C4::Biblio;
 use C4::Items;
+use C4::Members;
 
 BEGIN {
     use FindBin;
@@ -15,6 +19,12 @@ BEGIN {
     use_ok('C4::Reserves');
 }
 
+my $dbh = C4::Context->dbh;
+
+# Start transaction
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
 my $borrowers_count = 5;
 
 # Setup Test------------------------
@@ -26,14 +36,16 @@ my ($bibnum, $title, $bibitemnum) = create_helper_biblio();
 diag("Creating item instance for testing.");
 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
 
-# Get a borrower
-my $dbh = C4::Context->dbh;
-my $query = "SELECT borrowernumber FROM borrowers LIMIT $borrowers_count";
-my $sth = $dbh->prepare($query);
-$sth->execute;
+# Create some borrowers
 my @borrowernumbers;
-while ( my $row = $sth->fetchrow_hashref ) {
-    push( @borrowernumbers, $row->{'borrowernumber'} );
+foreach (1..$borrowers_count) {
+    my $borrowernumber = AddMember(
+        firstname =>  'my firstname',
+        surname => 'my surname ' . $_,
+        categorycode => 'S',
+        branchcode => 'CPL',
+    );
+    push @borrowernumbers, $borrowernumber;
 }
 
 my $biblionumber   = $bibnum;
@@ -155,17 +167,53 @@ AlterPriority( 'bottom', $reserve->{'reserve_id'} );
 $reserve = GetReserve( $reserve->{'reserve_id'} );
 ok( $reserve->{'priority'} eq '5', "Test AlterPriority(), move to bottom" );
 
-# Delete the reserves
-diag("Deleting holds.");
-$dbh->do("DELETE FROM reserves WHERE biblionumber = ?", undef, ( $biblionumber ) );
+# Regression test for bug 2394
+#
+# If IndependentBranches is ON and canreservefromotherbranches is OFF,
+# a patron is not permittedo to request an item whose homebranch (i.e.,
+# owner of the item) is different from the patron's own library.
+# However, if canreservefromotherbranches is turned ON, the patron can
+# create such hold requests.
+#
+# Note that canreservefromotherbranches has no effect if
+# IndependentBranches is OFF.
+
+my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio();
+my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
+  = AddItem({ homebranch => 'MPL', holdingbranch => 'MPL' } , $foreign_bibnum);
+$dbh->do('DELETE FROM issuingrules');
+$dbh->do(
+    q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
+      VALUES (?, ?, ?, ?)}, 
+    {},
+    '*', '*', '*', 25
+);
+
+# make sure some basic sysprefs are set
+t::lib::Mocks::mock_preference('ReservesControlBranch', 'homebranch');
+t::lib::Mocks::mock_preference('item-level_itypes', 1);
+
+# if IndependentBranches is OFF, a CPL patron can reserve an MPL item
+t::lib::Mocks::mock_preference('IndependentBranches', 0);
+ok(
+    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber),
+    'CPL patron allowed to reserve MPL item with IndependentBranches OFF (bug 2394)'
+);
 
-# Delete item.
-diag("Deleting item testing instance.");
-DelItem($dbh, $bibnum, $itemnumber);
+# if IndependentBranches is OFF, a CPL patron cannot reserve an MPL item
+t::lib::Mocks::mock_preference('IndependentBranches', 1);
+t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
+ok(
+    ! CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber),
+    'CPL patron NOT allowed to reserve MPL item with IndependentBranches ON ... (bug 2394)'
+);
 
-# Delete helper Biblio.
-diag("Deleting biblio testing instance.");
-DelBiblio($bibnum);
+# ... unless canreservefromotherbranches is ON
+t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
+ok(
+    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber),
+    '... unless canreservefromotherbranches is ON (bug 2394)'
+);
 
 # Helper method to set up a Biblio.
 sub create_helper_biblio {