Merge remote-tracking branch 'origin/new/bug_7986'
[koha.git] / t / db_dependent / Reserves.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use C4::Branch;
6
7 use Test::More tests => 4;
8 use MARC::Record;
9 use C4::Biblio;
10 use C4::Items;
11
12 BEGIN {
13         use FindBin;
14         use lib $FindBin::Bin;
15         use_ok('C4::Reserves');
16 }
17
18 # Setup Test------------------------
19 # Helper biblio.
20 diag("\nCreating biblio instance for testing.");
21 my ($bibnum, $title, $bibitemnum) = create_helper_biblio();
22
23 # Helper item for that biblio.
24 diag("Creating item instance for testing.");
25 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
26
27 # Modify item; setting barcode.
28 my $testbarcode = '97531';
29 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
30
31 # Get a borrower
32 my $dbh = C4::Context->dbh;
33 my $query = qq/SELECT borrowernumber
34     FROM   borrowers
35     LIMIT  1/;
36 my $sth = $dbh->prepare($query);
37 $sth->execute;
38 my $borrower = $sth->fetchrow_hashref;
39
40 my $borrowernumber = $borrower->{'borrowernumber'};
41 my $biblionumber   = $bibnum;
42 my $barcode        = $testbarcode;
43
44 my $constraint     = 'a';
45 my $bibitems       = '';
46 my $priority       = '1';
47 my $resdate        = undef;
48 my $expdate        = undef;
49 my $notes          = '';
50 my $checkitem      = undef;
51 my $found          = undef;
52
53 my @branches = GetBranchesLoop();
54 my $branch = $branches[0][0]{value};
55
56 AddReserve($branch,    $borrowernumber, $biblionumber,
57         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
58         $title,      $checkitem, $found);
59         
60 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
61 ok($status eq "Reserved", "CheckReserves Test 1");
62
63 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
64 ok($status eq "Reserved", "CheckReserves Test 2");
65
66 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
67 ok($status eq "Reserved", "CheckReserves Test 3");
68
69
70 # Teardown Test---------------------
71 # Delete item.
72 diag("Deleting item testing instance.");
73 DelItem($dbh, $bibnum, $itemnumber);
74
75 # Delete helper Biblio.
76 diag("Deleting biblio testing instance.");
77 DelBiblio($bibnum);
78
79 # Helper method to set up a Biblio.
80 sub create_helper_biblio {
81     my $bib = MARC::Record->new();
82     my $title = 'Silence in the library';
83     $bib->append_fields(
84         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
85         MARC::Field->new('245', ' ', ' ', a => $title),
86     );
87     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
88 }