Bug 9761: Preliminary measures for adding a unit test
[koha-ffzg.git] / t / db_dependent / Reserves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 8;
6 use MARC::Record;
7
8 use C4::Branch;
9 use C4::Biblio;
10 use C4::Items;
11 use C4::Members;
12 use C4::Circulation;
13
14 BEGIN {
15     use_ok('C4::Reserves');
16 }
17
18 my $dbh = C4::Context->dbh;
19
20 # Start transaction
21 $dbh->{AutoCommit} = 0;
22 $dbh->{RaiseError} = 1;
23
24 # Setup Test------------------------
25
26 # Add branches if not existing
27 foreach my $addbra ('CPL', 'FPL', 'RPL') {
28     $dbh->do("INSERT INTO branches (branchcode,branchname) VALUES (?,?)", undef, ($addbra,"$addbra branch")) unless GetBranchName($addbra);
29 }
30
31 # Add categories if not existing
32 foreach my $addcat ('S', 'PT') {
33     $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
34 }
35
36 # Helper biblio.
37 diag("\nCreating biblio instance for testing.");
38 my $bib = MARC::Record->new();
39 my $title = 'Silence in the library';
40 $bib->append_fields(
41     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
42     MARC::Field->new('245', ' ', ' ', a => $title),
43 );
44 my ($bibnum, $bibitemnum);
45 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
46
47 # Helper item for that biblio.
48 diag("Creating item instance for testing.");
49 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
50
51 # Modify item; setting barcode.
52 my $testbarcode = '97531';
53 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
54
55 # Create a borrower
56 my %data = (
57     firstname =>  'my firstname',
58     surname => 'my surname',
59     categorycode => 'S',
60     branchcode => 'CPL',
61 );
62 my $borrowernumber = AddMember(%data);
63 my $borrower = GetMember( borrowernumber => $borrowernumber );
64 my $biblionumber   = $bibnum;
65 my $barcode        = $testbarcode;
66
67 my $constraint     = 'a';
68 my $bibitems       = '';
69 my $priority       = '1';
70 my $resdate        = undef;
71 my $expdate        = undef;
72 my $notes          = '';
73 my $checkitem      = undef;
74 my $found          = undef;
75
76 my @branches = GetBranchesLoop();
77 my $branch = $branches[0][0]{value};
78
79 AddReserve($branch,    $borrowernumber, $biblionumber,
80         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
81         $title,      $checkitem, $found);
82
83 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
84
85 is($status, "Reserved", "CheckReserves Test 1");
86
87 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
88 is($status, "Reserved", "CheckReserves Test 2");
89
90 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
91 is($status, "Reserved", "CheckReserves Test 3");
92
93 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
94 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
95 ok(
96     'ItemHomeLib' eq GetReservesControlBranch(
97         { homebranch => 'ItemHomeLib' },
98         { branchcode => 'PatronHomeLib' }
99     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
100 );
101 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
102 ok(
103     'PatronHomeLib' eq GetReservesControlBranch(
104         { homebranch => 'ItemHomeLib' },
105         { branchcode => 'PatronHomeLib' }
106     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
107 );
108 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
109
110 ###
111 ### Regression test for bug 10272
112 ###
113 my %requesters = ();
114 $requesters{'CPL'} = AddMember(
115     branchcode   => 'CPL',
116     categorycode => 'PT',
117     surname      => 'borrower from CPL',
118 );
119 $requesters{'FPL'} = AddMember(
120     branchcode   => 'FPL',
121     categorycode => 'PT',
122     surname      => 'borrower from FPL',
123 );
124 $requesters{'RPL'} = AddMember(
125     branchcode   => 'RPL',
126     categorycode => 'PT',
127     surname      => 'borrower from RPL',
128 );
129
130 # Configure rules so that CPL allows only CPL patrons
131 # to request its items, while FPL will allow its items
132 # to fill holds from anywhere.
133
134 $dbh->do('DELETE FROM issuingrules');
135 $dbh->do('DELETE FROM branch_item_rules');
136 $dbh->do('DELETE FROM branch_borrower_circ_rules');
137 $dbh->do('DELETE FROM default_borrower_circ_rules');
138 $dbh->do('DELETE FROM default_branch_item_rules');
139 $dbh->do('DELETE FROM default_branch_circ_rules');
140 $dbh->do('DELETE FROM default_circ_rules');
141 $dbh->do(
142     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
143       VALUES (?, ?, ?, ?)},
144     {},
145     '*', '*', '*', 25
146 );
147
148 # CPL allows only its own patrons to request its items
149 $dbh->do(
150     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
151       VALUES (?, ?, ?, ?)},
152     {},
153     'CPL', 10, 1, 'homebranch',
154 );
155
156 # ... while FPL allows anybody to request its items
157 $dbh->do(
158     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
159       VALUES (?, ?, ?, ?)},
160     {},
161     'FPL', 10, 2, 'homebranch',
162 );
163
164 # helper biblio for the bug 10272 regression test
165 my $bib2 = MARC::Record->new();
166 $bib2->append_fields(
167     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
168     MARC::Field->new('245', ' ', ' ', a => $title),
169 );
170
171 # create one item belonging to FPL and one belonging to CPL
172 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
173 my ($itemnum_cpl, $itemnum_fpl);
174 (undef, undef, $itemnum_cpl) = AddItem({
175         homebranch => 'CPL',
176         holdingbranch => 'CPL',
177         barcode => 'bug10272_CPL'
178     } , $bibnum2);
179 (undef, undef, $itemnum_fpl) = AddItem({
180         homebranch => 'FPL',
181         holdingbranch => 'FPL',
182         barcode => 'bug10272_FPL'
183     } , $bibnum2);
184
185 AddReserve('RPL',  $requesters{'RPL'}, $bibnum2,
186            $constraint, $bibitems,  1, $resdate, $expdate, $notes,
187            $title,      $checkitem, $found);
188 AddReserve('FPL',  $requesters{'FPL'}, $bibnum2,
189            $constraint, $bibitems,  2, $resdate, $expdate, $notes,
190            $title,      $checkitem, $found);
191 AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
192            $constraint, $bibitems,  3, $resdate, $expdate, $notes,
193            $title,      $checkitem, $found);
194
195 # Ensure that the item's home library controls hold policy lookup
196 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
197
198 my $messages;
199 # Return the CPL item at FPL.  The hold that should be triggered is
200 # the one placed by the CPL patron, as the other two patron's hold
201 # requests cannot be filled by that item per policy.
202 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
203 is( $messages->{ResFound}->{borrowernumber},
204     $requesters{'CPL'},
205     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
206
207 # Return the FPL item at FPL.  The hold that should be triggered is
208 # the one placed by the RPL patron, as that patron is first in line
209 # and RPL imposes no restrictions on whose holds its items can fill.
210 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
211 is( $messages->{ResFound}->{borrowernumber},
212     $requesters{'RPL'},
213     'for generous library, its items fill first hold request in line (bug 10272)');
214
215 $dbh->rollback;