5 use Test::More tests => 8;
15 use_ok('C4::Reserves');
18 my $dbh = C4::Context->dbh;
21 $dbh->{AutoCommit} = 0;
22 $dbh->{RaiseError} = 1;
24 # Setup Test------------------------
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);
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);
37 diag("\nCreating biblio instance for testing.");
38 my $bib = MARC::Record->new();
39 my $title = 'Silence in the library';
41 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
42 MARC::Field->new('245', ' ', ' ', a => $title),
44 my ($bibnum, $bibitemnum);
45 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
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);
51 # Modify item; setting barcode.
52 my $testbarcode = '97531';
53 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
57 firstname => 'my firstname',
58 surname => 'my surname',
62 my $borrowernumber = AddMember(%data);
63 my $borrower = GetMember( borrowernumber => $borrowernumber );
64 my $biblionumber = $bibnum;
65 my $barcode = $testbarcode;
73 my $checkitem = undef;
76 my @branches = GetBranchesLoop();
77 my $branch = $branches[0][0]{value};
79 AddReserve($branch, $borrowernumber, $biblionumber,
80 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
81 $title, $checkitem, $found);
83 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
85 is($status, "Reserved", "CheckReserves Test 1");
87 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
88 is($status, "Reserved", "CheckReserves Test 2");
90 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
91 is($status, "Reserved", "CheckReserves Test 3");
93 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
94 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
96 'ItemHomeLib' eq GetReservesControlBranch(
97 { homebranch => 'ItemHomeLib' },
98 { branchcode => 'PatronHomeLib' }
99 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
101 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
103 'PatronHomeLib' eq GetReservesControlBranch(
104 { homebranch => 'ItemHomeLib' },
105 { branchcode => 'PatronHomeLib' }
106 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
108 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
111 ### Regression test for bug 10272
114 $requesters{'CPL'} = AddMember(
116 categorycode => 'PT',
117 surname => 'borrower from CPL',
119 $requesters{'FPL'} = AddMember(
121 categorycode => 'PT',
122 surname => 'borrower from FPL',
124 $requesters{'RPL'} = AddMember(
126 categorycode => 'PT',
127 surname => 'borrower from RPL',
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.
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');
142 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
143 VALUES (?, ?, ?, ?)},
148 # CPL allows only its own patrons to request its items
150 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
151 VALUES (?, ?, ?, ?)},
153 'CPL', 10, 1, 'homebranch',
156 # ... while FPL allows anybody to request its items
158 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
159 VALUES (?, ?, ?, ?)},
161 'FPL', 10, 2, 'homebranch',
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),
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({
176 holdingbranch => 'CPL',
177 barcode => 'bug10272_CPL'
179 (undef, undef, $itemnum_fpl) = AddItem({
181 holdingbranch => 'FPL',
182 barcode => 'bug10272_FPL'
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);
195 # Ensure that the item's home library controls hold policy lookup
196 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
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},
205 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
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},
213 'for generous library, its items fill first hold request in line (bug 10272)');