Bug 10289: UT: Reserves.t needs to create its own data
[koha.git] / t / db_dependent / Reserves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 4;
6 use MARC::Record;
7
8 use C4::Branch;
9 use C4::Biblio;
10 use C4::Items;
11 use C4::Members;
12
13 BEGIN {
14     use_ok('C4::Reserves');
15 }
16
17 # Setup Test------------------------
18 # Helper biblio.
19 diag("\nCreating biblio instance for testing.");
20 my $bib = MARC::Record->new();
21 my $title = 'Silence in the library';
22 $bib->append_fields(
23     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
24     MARC::Field->new('245', ' ', ' ', a => $title),
25 );
26 my ($bibnum, $bibitemnum);
27 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
28 # Helper item for that biblio.
29 diag("Creating item instance for testing.");
30 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
31
32 # Modify item; setting barcode.
33 my $testbarcode = '97531';
34 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
35
36 # Create a borrower
37 my %data = (
38     cardnumber => 'CARDNUMBER42',
39     firstname =>  'my firstname',
40     surname => 'my surname',
41     categorycode => 'S',
42     branchcode => 'CPL',
43 );
44 my $borrowernumber = AddMember(%data);
45 my $borrower = GetMember( borrowernumber => $borrowernumber );
46 my $biblionumber   = $bibnum;
47 my $barcode        = $testbarcode;
48
49 my $constraint     = 'a';
50 my $bibitems       = '';
51 my $priority       = '1';
52 my $resdate        = undef;
53 my $expdate        = undef;
54 my $notes          = '';
55 my $checkitem      = undef;
56 my $found          = undef;
57
58 my @branches = GetBranchesLoop();
59 my $branch = $branches[0][0]{value};
60
61 AddReserve($branch,    $borrowernumber, $biblionumber,
62         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
63         $title,      $checkitem, $found);
64
65 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
66
67 is($status, "Reserved", "CheckReserves Test 1");
68
69 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
70 is($status, "Reserved", "CheckReserves Test 2");
71
72 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
73 is($status, "Reserved", "CheckReserves Test 3");
74
75
76 # Teardown Test---------------------
77 # Delete item.
78 diag("Deleting item testing instance.");
79 my $dbh = C4::Context->dbh;
80 DelItem($dbh, $bibnum, $itemnumber);
81
82 # Delete helper Biblio.
83 diag("Deleting biblio testing instance.");
84 DelBiblio($bibnum);
85
86 # Delete borrower
87 diag("Deleting borrower.");
88 DelMember($borrowernumber);