Bug 10663: tests for CanBookBeRenewed
[koha.git] / t / db_dependent / Reserves.t
index ca4d42e..0e0852a 100755 (executable)
@@ -1,46 +1,62 @@
 #!/usr/bin/perl
 
-use strict;
-use warnings;
-use C4::Branch;
+use Modern::Perl;
 
 use Test::More tests => 4;
+use MARC::Record;
+
+use C4::Branch;
+use C4::Biblio;
+use C4::Items;
+use C4::Members;
 
 BEGIN {
-       use FindBin;
-       use lib $FindBin::Bin;
-       use_ok('C4::Reserves');
+    use_ok('C4::Reserves');
 }
 
 my $dbh = C4::Context->dbh;
-my $query = qq/SELECT borrowernumber
-    FROM   borrowers
-    LIMIT  1/;
-my $sth = $dbh->prepare($query);
-$sth->execute;
-my $borrower = $sth->fetchrow_hashref;
-
-$query = qq/SELECT biblionumber, title, itemnumber, barcode
-    FROM biblio
-    LEFT JOIN items USING (biblionumber)
-    WHERE barcode <> ""
-    AND barcode IS NOT NULL
-    LIMIT  1/;
-$sth = $dbh->prepare($query);
-$sth->execute;
-my $biblio = $sth->fetchrow_hashref;
-
-
-my $borrowernumber = $borrower->{'borrowernumber'};
-my $biblionumber   = $biblio->{'biblionumber'};
-my $itemnumber     = $biblio->{'itemnumber'};
-my $barcode        = $biblio->{'barcode'};
+
+# Start transaction
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+# Setup Test------------------------
+# Helper biblio.
+diag("\nCreating biblio instance for testing.");
+my $bib = MARC::Record->new();
+my $title = 'Silence in the library';
+$bib->append_fields(
+    MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
+    MARC::Field->new('245', ' ', ' ', a => $title),
+);
+my ($bibnum, $bibitemnum);
+($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
+# Helper item for that biblio.
+diag("Creating item instance for testing.");
+my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
+
+# Modify item; setting barcode.
+my $testbarcode = '97531';
+ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
+
+# Create a borrower
+my %data = (
+    firstname =>  'my firstname',
+    surname => 'my surname',
+    categorycode => 'S',
+    branchcode => 'CPL',
+);
+my $borrowernumber = AddMember(%data);
+my $borrower = GetMember( borrowernumber => $borrowernumber );
+my $biblionumber   = $bibnum;
+my $barcode        = $testbarcode;
 
 my $constraint     = 'a';
 my $bibitems       = '';
 my $priority       = '1';
+my $resdate        = undef;
+my $expdate        = undef;
 my $notes          = '';
-my $title          = $biblio->{'title'};
 my $checkitem      = undef;
 my $found          = undef;
 
@@ -48,15 +64,15 @@ my @branches = GetBranchesLoop();
 my $branch = $branches[0][0]{value};
 
 AddReserve($branch,    $borrowernumber, $biblionumber,
-        $constraint, $bibitems,  $priority,       $notes,
+        $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
         $title,      $checkitem, $found);
-        
+
 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
-ok($status eq "Reserved", "CheckReserves Test 1");
+
+is($status, "Reserved", "CheckReserves Test 1");
 
 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
-ok($status eq "Reserved", "CheckReserves Test 2");
+is($status, "Reserved", "CheckReserves Test 2");
 
 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
-ok($status eq "Reserved", "CheckReserves Test 3");
-
+is($status, "Reserved", "CheckReserves Test 3");