Bug 20287: Replace occurrences of AddMember with Koha::Patron->new->store->borrowernumber
[koha-ffzg.git] / t / db_dependent / Holds.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use t::lib::TestBuilder;
7
8 use C4::Context;
9
10 use Test::More tests => 55;
11 use MARC::Record;
12 use C4::Biblio;
13 use C4::Items;
14 use C4::Calendar;
15 use Koha::Database;
16 use Koha::DateUtils qw( dt_from_string output_pref );
17 use Koha::Biblios;
18 use Koha::Holds;
19 use Koha::Patrons;
20
21 BEGIN {
22     use FindBin;
23     use lib $FindBin::Bin;
24     use_ok('C4::Reserves');
25 }
26
27 my $schema  = Koha::Database->new->schema;
28 $schema->storage->txn_begin;
29
30 my $builder = t::lib::TestBuilder->new();
31 my $dbh     = C4::Context->dbh;
32
33 # Create two random branches
34 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
35 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
36
37 my $category = $builder->build({ source => 'Category' });
38
39 my $borrowers_count = 5;
40
41 $dbh->do('DELETE FROM itemtypes');
42 $dbh->do('DELETE FROM reserves');
43 my $insert_sth = $dbh->prepare('INSERT INTO itemtypes (itemtype) VALUES (?)');
44 $insert_sth->execute('CAN');
45 $insert_sth->execute('CANNOT');
46 $insert_sth->execute('DUMMY');
47 $insert_sth->execute('ONLY1');
48
49 # Setup Test------------------------
50 # Create a biblio instance for testing
51 my ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
52
53 # Create item instance for testing.
54 my ($item_bibnum, $item_bibitemnum, $itemnumber)
55     = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
56
57 # Create some borrowers
58 my @borrowernumbers;
59 foreach (1..$borrowers_count) {
60     my $borrowernumber = Koha::Patron->new({
61         firstname =>  'my firstname',
62         surname => 'my surname ' . $_,
63         categorycode => $category->{categorycode},
64         branchcode => $branch_1,
65     })->store->borrowernumber;
66     push @borrowernumbers, $borrowernumber;
67 }
68
69 my $biblionumber = $bibnum;
70
71 # Create five item level holds
72 foreach my $borrowernumber ( @borrowernumbers ) {
73     AddReserve(
74         $branch_1,
75         $borrowernumber,
76         $biblionumber,
77         my $bibitems = q{},
78         my $priority = C4::Reserves::CalculatePriority( $biblionumber ),
79         my $resdate,
80         my $expdate,
81         my $notes = q{},
82         $title,
83         my $checkitem = $itemnumber,
84         my $found,
85     );
86 }
87
88 my $biblio = Koha::Biblios->find( $biblionumber );
89 my $holds = $biblio->holds;
90 is( $holds->count, $borrowers_count, 'Test GetReserves()' );
91 is( $holds->next->priority, 1, "Reserve 1 has a priority of 1" );
92 is( $holds->next->priority, 2, "Reserve 2 has a priority of 2" );
93 is( $holds->next->priority, 3, "Reserve 3 has a priority of 3" );
94 is( $holds->next->priority, 4, "Reserve 4 has a priority of 4" );
95 is( $holds->next->priority, 5, "Reserve 5 has a priority of 5" );
96
97 my $item = Koha::Items->find( $itemnumber );
98 $holds = $item->current_holds;
99 my $first_hold = $holds->next;
100 my $reservedate = $first_hold->reservedate;
101 my $borrowernumber = $first_hold->borrowernumber;
102 my $branch_1code = $first_hold->branchcode;
103 my $reserve_id = $first_hold->reserve_id;
104 is( $reservedate, output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), "holds_placed_today should return a valid reserve date");
105 is( $borrowernumber, $borrowernumbers[0], "holds_placed_today should return a valid borrowernumber");
106 is( $branch_1code, $branch_1, "holds_placed_today should return a valid branchcode");
107 ok($reserve_id, "Test holds_placed_today()");
108
109 my $hold = Koha::Holds->find( $reserve_id );
110 ok( $hold, "Koha::Holds found the hold" );
111 my $hold_biblio = $hold->biblio();
112 ok( $hold_biblio, "Got biblio using biblio() method" );
113 ok( $hold_biblio == $hold->biblio(), "biblio method returns stashed biblio" );
114 my $hold_item = $hold->item();
115 ok( $hold_item, "Got item using item() method" );
116 ok( $hold_item == $hold->item(), "item method returns stashed item" );
117 my $hold_branch = $hold->branch();
118 ok( $hold_branch, "Got branch using branch() method" );
119 ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
120 my $hold_found = $hold->found();
121 $hold->set({ found => 'W'})->store();
122 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
123 is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
124
125 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
126 $holds = $patron->holds;
127 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
128
129
130 $holds = $item->current_holds;
131 $first_hold = $holds->next;
132 $borrowernumber = $first_hold->borrowernumber;
133 $branch_1code = $first_hold->branchcode;
134 $reserve_id = $first_hold->reserve_id;
135
136 ModReserve({
137     reserve_id    => $reserve_id,
138     rank          => '4',
139     branchcode    => $branch_1,
140     itemnumber    => $itemnumber,
141     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
142 });
143
144 $hold = Koha::Holds->find( $reserve_id );
145 ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
146 ok( $hold->suspend, "Test ModReserve, suspend hold" );
147 is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
148
149 ModReserve({ # call without reserve_id
150     rank          => '3',
151     biblionumber  => $item_bibnum,
152     itemnumber    => $itemnumber,
153     borrowernumber => $borrowernumber,
154 });
155 $hold = Koha::Holds->find( $reserve_id );
156 ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
157
158 ToggleSuspend( $reserve_id );
159 $hold = Koha::Holds->find( $reserve_id );
160 ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
161
162 ToggleSuspend( $reserve_id, '2012-01-01' );
163 $hold = Koha::Holds->find( $reserve_id );
164 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
165
166 AutoUnsuspendReserves();
167 $hold = Koha::Holds->find( $reserve_id );
168 ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
169
170 SuspendAll(
171     borrowernumber => $borrowernumber,
172     biblionumber   => $biblionumber,
173     suspend => 1,
174     suspend_until => '2012-01-01',
175 );
176 $hold = Koha::Holds->find( $reserve_id );
177 is( $hold->suspend, 1, "Test SuspendAll()" );
178 is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
179
180 SuspendAll(
181     borrowernumber => $borrowernumber,
182     biblionumber   => $biblionumber,
183     suspend => 0,
184 );
185 $hold = Koha::Holds->find( $reserve_id );
186 is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
187 is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
188
189 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
190 AddReserve(
191     $branch_1,
192     $borrowernumbers[0],
193     $biblionumber,
194     my $bibitems = q{},
195     my $priority,
196     my $resdate,
197     my $expdate,
198     my $notes = q{},
199     $title,
200     my $checkitem,
201     my $found,
202 );
203 $patron = Koha::Patrons->find( $borrowernumber );
204 $holds = $patron->holds;
205 my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
206 ModReserveMinusPriority( $itemnumber, $reserveid );
207 $holds = $patron->holds;
208 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
209
210 $holds = $biblio->holds;
211 $hold = $holds->next;
212 AlterPriority( 'top', $hold->reserve_id );
213 $hold = Koha::Holds->find( $reserveid );
214 is( $hold->priority, '1', "Test AlterPriority(), move to top" );
215
216 AlterPriority( 'down', $hold->reserve_id );
217 $hold = Koha::Holds->find( $reserveid );
218 is( $hold->priority, '2', "Test AlterPriority(), move down" );
219
220 AlterPriority( 'up', $hold->reserve_id );
221 $hold = Koha::Holds->find( $reserveid );
222 is( $hold->priority, '1', "Test AlterPriority(), move up" );
223
224 AlterPriority( 'bottom', $hold->reserve_id );
225 $hold = Koha::Holds->find( $reserveid );
226 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
227
228 # Regression test for bug 2394
229 #
230 # If IndependentBranches is ON and canreservefromotherbranches is OFF,
231 # a patron is not permittedo to request an item whose homebranch (i.e.,
232 # owner of the item) is different from the patron's own library.
233 # However, if canreservefromotherbranches is turned ON, the patron can
234 # create such hold requests.
235 #
236 # Note that canreservefromotherbranches has no effect if
237 # IndependentBranches is OFF.
238
239 my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
240 my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
241   = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
242 $dbh->do('DELETE FROM issuingrules');
243 $dbh->do(
244     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
245       VALUES (?, ?, ?, ?, ?)},
246     {},
247     '*', '*', '*', 25, 99
248 );
249 $dbh->do(
250     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
251       VALUES (?, ?, ?, ?, ?)},
252     {},
253     '*', '*', 'CANNOT', 0, 99
254 );
255
256 # make sure some basic sysprefs are set
257 t::lib::Mocks::mock_preference('ReservesControlBranch', 'ItemHomeLibrary');
258 t::lib::Mocks::mock_preference('item-level_itypes', 1);
259
260 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
261 t::lib::Mocks::mock_preference('IndependentBranches', 0);
262 ok(
263     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
264     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
265 );
266
267 # if IndependentBranches is OFF, a $branch_1 patron cannot reserve an $branch_2 item
268 t::lib::Mocks::mock_preference('IndependentBranches', 1);
269 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
270 ok(
271     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
272     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
273 );
274
275 # ... unless canreservefromotherbranches is ON
276 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
277 ok(
278     CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
279     '... unless canreservefromotherbranches is ON (bug 2394)'
280 );
281
282 {
283     # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
284     ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
285     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
286     my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
287     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
288     my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
289     ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
290     my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
291     my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
292     my $hold3 = Koha::Holds->find( $reserveid3 );
293     is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
294     ModReserve({ reserve_id => $reserveid1, rank => 'del' });
295     ModReserve({ reserve_id => $reserveid2, rank => 'del' });
296     is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
297 }
298
299 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
300 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
301 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
302 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
303
304 $hold = Koha::Hold->new(
305     {
306         borrowernumber => $borrowernumbers[0],
307         itemnumber     => $itemnumber,
308         biblionumber   => $item_bibnum,
309     }
310 )->store();
311 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
312     'itemAlreadyOnHold',
313     "Patron cannot place a second item level hold for a given item" );
314 $hold->delete();
315
316 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
317 ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
318 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
319
320 # Regression test for bug 9532
321 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
322 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
323 AddReserve(
324     $branch_1,
325     $borrowernumbers[0],
326     $bibnum,
327     '',
328     1,
329 );
330 is(
331     CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
332     "cannot request item if policy that matches on item-level item type forbids it"
333 );
334 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
335 ok(
336     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
337     "can request item if policy that matches on item type allows it"
338 );
339
340 t::lib::Mocks::mock_preference('item-level_itypes', 0);
341 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
342 ok(
343     CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
344     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
345 );
346
347
348 # Test branch item rules
349
350 $dbh->do('DELETE FROM issuingrules');
351 $dbh->do(
352     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
353       VALUES (?, ?, ?, ?)},
354     {},
355     '*', '*', '*', 25
356 );
357 $dbh->do('DELETE FROM branch_item_rules');
358 $dbh->do('DELETE FROM default_branch_circ_rules');
359 $dbh->do('DELETE FROM default_branch_item_rules');
360 $dbh->do('DELETE FROM default_circ_rules');
361 $dbh->do(q{
362     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
363     VALUES (?, ?, ?, ?)
364 }, {}, $branch_1, 'CANNOT', 0, 'homebranch');
365 $dbh->do(q{
366     INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
367     VALUES (?, ?, ?, ?)
368 }, {}, $branch_1, 'CAN', 1, 'homebranch');
369 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
370 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
371     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
372 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
373     "CanItemBeReserved should return 'notReservable'");
374
375 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
376     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
377 is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
378     'cannotReserveFromOtherBranches',
379     "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
380
381 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
382     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
383 is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
384     "CanItemBeReserved should return 'OK'");
385
386 # Bug 12632
387 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
388 t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
389
390 $dbh->do('DELETE FROM reserves');
391 $dbh->do('DELETE FROM issues');
392 $dbh->do('DELETE FROM items');
393 $dbh->do('DELETE FROM biblio');
394
395 ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
396 ( $item_bibnum, $item_bibitemnum, $itemnumber )
397     = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
398
399 $dbh->do(
400     q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
401       VALUES (?, ?, ?, ?, ?)},
402     {},
403     '*', '*', 'ONLY1', 1, 99
404 );
405 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
406     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
407
408 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
409
410 is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
411     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
412
413
414 # Helper method to set up a Biblio.
415 sub create_helper_biblio {
416     my $itemtype = shift;
417     my $bib = MARC::Record->new();
418     my $title = 'Silence in the library';
419     $bib->append_fields(
420         MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
421         MARC::Field->new('245', ' ', ' ', a => $title),
422         MARC::Field->new('942', ' ', ' ', c => $itemtype),
423     );
424     return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
425 }