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 diag("\nCreating biblio instance for testing.");
27 my $bib = MARC::Record->new();
28 my $title = 'Silence in the library';
30 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
31 MARC::Field->new('245', ' ', ' ', a => $title),
33 my ($bibnum, $bibitemnum);
34 ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
35 # Helper item for that biblio.
36 diag("Creating item instance for testing.");
37 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
39 # Modify item; setting barcode.
40 my $testbarcode = '97531';
41 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
45 firstname => 'my firstname',
46 surname => 'my surname',
50 my $borrowernumber = AddMember(%data);
51 my $borrower = GetMember( borrowernumber => $borrowernumber );
52 my $biblionumber = $bibnum;
53 my $barcode = $testbarcode;
61 my $checkitem = undef;
64 my @branches = GetBranchesLoop();
65 my $branch = $branches[0][0]{value};
67 AddReserve($branch, $borrowernumber, $biblionumber,
68 $constraint, $bibitems, $priority, $resdate, $expdate, $notes,
69 $title, $checkitem, $found);
71 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
73 is($status, "Reserved", "CheckReserves Test 1");
75 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
76 is($status, "Reserved", "CheckReserves Test 2");
78 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
79 is($status, "Reserved", "CheckReserves Test 3");
81 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
82 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
84 'ItemHomeLib' eq GetReservesControlBranch(
85 { homebranch => 'ItemHomeLib' },
86 { branchcode => 'PatronHomeLib' }
87 ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
89 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
91 'PatronHomeLib' eq GetReservesControlBranch(
92 { homebranch => 'ItemHomeLib' },
93 { branchcode => 'PatronHomeLib' }
94 ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
96 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
99 ### Regression test for bug 10272
102 $requesters{'CPL'} = AddMember(
104 categorycode => 'PT',
105 surname => 'borrower from CPL',
107 $requesters{'FPL'} = AddMember(
109 categorycode => 'PT',
110 surname => 'borrower from FPL',
112 $requesters{'RPL'} = AddMember(
114 categorycode => 'PT',
115 surname => 'borrower from RPL',
118 # Configure rules so that CPL allows only CPL patrons
119 # to request its items, while FPL will allow its items
120 # to fill holds from anywhere.
122 $dbh->do('DELETE FROM issuingrules');
123 $dbh->do('DELETE FROM branch_item_rules');
124 $dbh->do('DELETE FROM branch_borrower_circ_rules');
125 $dbh->do('DELETE FROM default_borrower_circ_rules');
126 $dbh->do('DELETE FROM default_branch_item_rules');
127 $dbh->do('DELETE FROM default_branch_circ_rules');
128 $dbh->do('DELETE FROM default_circ_rules');
130 q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
131 VALUES (?, ?, ?, ?)},
136 # CPL allows only its own patrons to request its items
138 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
139 VALUES (?, ?, ?, ?)},
141 'CPL', 10, 1, 'homebranch',
144 # ... while FPL allows anybody to request its items
146 q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
147 VALUES (?, ?, ?, ?)},
149 'FPL', 10, 2, 'homebranch',
152 # helper biblio for the bug 10272 regression test
153 my $bib2 = MARC::Record->new();
154 $bib2->append_fields(
155 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
156 MARC::Field->new('245', ' ', ' ', a => $title),
159 # create one item belonging to FPL and one belonging to CPL
160 my ($bibnum2, $bibitemnum2) = AddBiblio($bib, '');
161 my ($itemnum_cpl, $itemnum_fpl);
162 (undef, undef, $itemnum_cpl) = AddItem({
164 holdingbranch => 'CPL',
165 barcode => 'bug10272_CPL'
167 (undef, undef, $itemnum_fpl) = AddItem({
169 holdingbranch => 'FPL',
170 barcode => 'bug10272_FPL'
173 AddReserve('RPL', $requesters{'RPL'}, $bibnum2,
174 $constraint, $bibitems, 1, $resdate, $expdate, $notes,
175 $title, $checkitem, $found);
176 AddReserve('FPL', $requesters{'FPL'}, $bibnum2,
177 $constraint, $bibitems, 2, $resdate, $expdate, $notes,
178 $title, $checkitem, $found);
179 AddReserve('CPL', $requesters{'CPL'}, $bibnum2,
180 $constraint, $bibitems, 3, $resdate, $expdate, $notes,
181 $title, $checkitem, $found);
183 # Ensure that the item's home library controls hold policy lookup
184 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
187 # Return the CPL item at FPL. The hold that should be triggered is
188 # the one placed by the CPL patron, as the other two patron's hold
189 # requests cannot be filled by that item per policy.
190 (undef, $messages, undef, undef) = AddReturn('bug10272_CPL', 'FPL');
191 is( $messages->{ResFound}->{borrowernumber},
193 'restrictive library\'s items only fill requests by own patrons (bug 10272)');
195 # Return the FPL item at FPL. The hold that should be triggered is
196 # the one placed by the RPL patron, as that patron is first in line
197 # and RPL imposes no restrictions on whose holds its items can fill.
198 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
199 is( $messages->{ResFound}->{borrowernumber},
201 'for generous library, its items fill first hold request in line (bug 10272)');