Bug 10872: UT for GetHiddenItemnumbers and POD fix
[koha.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 # Helper biblio.
26 diag("\nCreating biblio instance for testing.");
27 my $bib = MARC::Record->new();
28 my $title = 'Silence in the library';
29 $bib->append_fields(
30     MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
31     MARC::Field->new('245', ' ', ' ', a => $title),
32 );
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);
38
39 # Modify item; setting barcode.
40 my $testbarcode = '97531';
41 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
42
43 # Create a borrower
44 my %data = (
45     firstname =>  'my firstname',
46     surname => 'my surname',
47     categorycode => 'S',
48     branchcode => 'CPL',
49 );
50 my $borrowernumber = AddMember(%data);
51 my $borrower = GetMember( borrowernumber => $borrowernumber );
52 my $biblionumber   = $bibnum;
53 my $barcode        = $testbarcode;
54
55 my $constraint     = 'a';
56 my $bibitems       = '';
57 my $priority       = '1';
58 my $resdate        = undef;
59 my $expdate        = undef;
60 my $notes          = '';
61 my $checkitem      = undef;
62 my $found          = undef;
63
64 my @branches = GetBranchesLoop();
65 my $branch = $branches[0][0]{value};
66
67 AddReserve($branch,    $borrowernumber, $biblionumber,
68         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
69         $title,      $checkitem, $found);
70
71 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
72
73 is($status, "Reserved", "CheckReserves Test 1");
74
75 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
76 is($status, "Reserved", "CheckReserves Test 2");
77
78 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
79 is($status, "Reserved", "CheckReserves Test 3");
80
81 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
82 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
83 ok(
84     'ItemHomeLib' eq GetReservesControlBranch(
85         { homebranch => 'ItemHomeLib' },
86         { branchcode => 'PatronHomeLib' }
87     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
88 );
89 C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
90 ok(
91     'PatronHomeLib' eq GetReservesControlBranch(
92         { homebranch => 'ItemHomeLib' },
93         { branchcode => 'PatronHomeLib' }
94     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
95 );
96 C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
97
98 ###
99 ### Regression test for bug 10272
100 ###
101 my %requesters = ();
102 $requesters{'CPL'} = AddMember(
103     branchcode   => 'CPL',
104     categorycode => 'PT',
105     surname      => 'borrower from CPL',
106 );
107 $requesters{'FPL'} = AddMember(
108     branchcode   => 'FPL',
109     categorycode => 'PT',
110     surname      => 'borrower from FPL',
111 );
112 $requesters{'RPL'} = AddMember(
113     branchcode   => 'RPL',
114     categorycode => 'PT',
115     surname      => 'borrower from RPL',
116 );
117
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.
121
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');
129 $dbh->do(
130     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
131       VALUES (?, ?, ?, ?)},
132     {},
133     '*', '*', '*', 25
134 );
135
136 # CPL allows only its own patrons to request its items
137 $dbh->do(
138     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
139       VALUES (?, ?, ?, ?)},
140     {},
141     'CPL', 10, 1, 'homebranch',
142 );
143
144 # ... while FPL allows anybody to request its items
145 $dbh->do(
146     q{INSERT INTO default_branch_circ_rules (branchcode, maxissueqty, holdallowed, returnbranch)
147       VALUES (?, ?, ?, ?)},
148     {},
149     'FPL', 10, 2, 'homebranch',
150 );
151
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),
157 );
158
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({
163         homebranch => 'CPL',
164         holdingbranch => 'CPL',
165         barcode => 'bug10272_CPL'
166     } , $bibnum2);
167 (undef, undef, $itemnum_fpl) = AddItem({
168         homebranch => 'FPL',
169         holdingbranch => 'FPL',
170         barcode => 'bug10272_FPL'
171     } , $bibnum2);
172
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);
182
183 # Ensure that the item's home library controls hold policy lookup
184 C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
185
186 my $messages;
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},
192     $requesters{'CPL'},
193     'restrictive library\'s items only fill requests by own patrons (bug 10272)');
194
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},
200     $requesters{'RPL'},
201     'for generous library, its items fill first hold request in line (bug 10272)');