View | Details | Raw Unified | Return to bug 16787
Collapse All | Expand All

(-)a/t/db_dependent/Holds.t (-33 / +132 lines)
Lines 7-13 use t::lib::TestBuilder; Link Here
7
7
8
use C4::Context;
8
use C4::Context;
9
9
10
use Test::More tests => 54;
10
use Test::More tests => 52;
11
use MARC::Record;
11
use MARC::Record;
12
use C4::Biblio;
12
use C4::Biblio;
13
use C4::Items;
13
use C4::Items;
Lines 231-237 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" ); Link Here
231
my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
231
my ($foreign_bibnum, $foreign_title, $foreign_bibitemnum) = create_helper_biblio('DUMMY');
232
my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
232
my ($foreign_item_bibnum, $foreign_item_bibitemnum, $foreign_itemnumber)
233
  = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
233
  = AddItem({ homebranch => $branch_2, holdingbranch => $branch_2 } , $foreign_bibnum);
234
$dbh->do('DELETE FROM issuingrules');
234
delete_all_rules();
235
$dbh->do(
235
$dbh->do(
236
    q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
236
    q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
237
      VALUES (?, ?, ?, ?, ?)},
237
      VALUES (?, ?, ?, ?, ?)},
Lines 309-355 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 ); Link Here
309
ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
309
ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
310
ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
310
ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
311
311
312
# Regression test for bug 9532
312
subtest 'CanItemBeReserved' => sub {
313
($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
313
    plan tests => 2;
314
($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
315
AddReserve(
316
    $branch_1,
317
    $borrowernumbers[0],
318
    $bibnum,
319
    '',
320
    1,
321
);
322
is(
323
    CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
324
    "cannot request item if policy that matches on item-level item type forbids it"
325
);
326
ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
327
ok(
328
    CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
329
    "can request item if policy that matches on item type allows it"
330
);
331
314
332
t::lib::Mocks::mock_preference('item-level_itypes', 0);
315
    delete_all_rules();
333
ModItem({ itype => undef }, $item_bibnum, $itemnumber);
316
334
ok(
317
    $dbh->do(
335
    CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
318
        q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record) VALUES (?, ?, ?, ?, ?)}, {},
336
    "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
319
        '*', '*', 'CANNOT', 0, 99
337
);
320
    );
321
    $dbh->do(
322
        q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record) VALUES (?, ?, ?, ?, ?)}, {},
323
        '*', '*', 'CAN', 2, 2
324
    );
325
326
    subtest 'noReservesAllowed' => sub {
327
        plan tests => 4;
328
329
        my ( $biblionumber_cannot ) = create_helper_biblio('CANNOT');
330
        my ( undef, undef, $itemnumber_1_can ) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblionumber_cannot);
331
        my ( undef, undef, $itemnumber_1_cannot ) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblionumber_cannot);
332
333
        my ($biblionumber_can) = create_helper_biblio('CAN');
334
        my ( undef, undef, $itemnumber_2_can ) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblionumber_can);
335
        my ( undef, undef, $itemnumber_2_cannot ) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $biblionumber_can);
336
337
        Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
338
339
        t::lib::Mocks::mock_preference('item-level_itypes', 1);
340
        is(
341
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_2_cannot), 'noReservesAllowed',
342
            "With item level set, rule from item must be picked (CANNOT)"
343
        );
344
        is(
345
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_1_can), 'OK',
346
            "With item level set, rule from item must be picked (CAN)"
347
        );
348
        t::lib::Mocks::mock_preference('item-level_itypes', 0);
349
        is(
350
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_1_can), 'noReservesAllowed',
351
            "With biblio level set, rule from biblio must be picked (CANNOT)"
352
        );
353
        is(
354
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_2_cannot), 'OK',
355
            "With biblio level set, rule from biblio must be picked (CAN)"
356
        );
357
    };
358
359
    subtest 'tooManyHoldsForThisRecord + tooManyReserves + itemAlreadyOnHold' => sub {
360
        plan tests => 7;
361
362
        my ($biblionumber_1) = create_helper_biblio('CAN');
363
        my ( undef, undef, $itemnumber_11) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblionumber_1);
364
        my ( undef, undef, $itemnumber_12) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblionumber_1);
365
        my ($biblionumber_2) = create_helper_biblio('CAN');
366
        my ( undef, undef, $itemnumber_21) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblionumber_2);
367
        my ( undef, undef, $itemnumber_22) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $biblionumber_2);
368
369
        Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
370
371
        # Biblio-level hold
372
        AddReserve(
373
            $branch_1,
374
            $borrowernumbers[0],
375
            $biblionumber_1,
376
            '',
377
            1,
378
        );
379
        for my $item_level ( 0..1 ) {
380
            t::lib::Mocks::mock_preference('item-level_itypes', $item_level);
381
            is(
382
                # FIXME This is not really correct, but CanItemBeReserved does not check if biblio-level holds already exist
383
                CanItemBeReserved( $borrowernumbers[0], $itemnumber_11), 'OK',
384
                "A biblio-level hold already exists - another hold can be placed on a specific item item"
385
            );
386
        }
387
388
        Koha::Holds->search({borrowernumber => $borrowernumbers[0]})->delete;
389
        # Item-level hold
390
        AddReserve(
391
            $branch_1,
392
            $borrowernumbers[0],
393
            $biblionumber_1,
394
            '',
395
            1,
396
            undef, undef, undef, undef, $itemnumber_11
397
        );
398
        $dbh->do(q{UPDATE issuingrules set reservesallowed=5, holds_per_record=1});
399
        is(
400
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_12), 'tooManyHoldsForThisRecord',
401
            "A item-level hold already exists and holds_per_record=1, another hold cannot be placed on this record"
402
        );
403
        $dbh->do(q{UPDATE issuingrules set reservesallowed=1, holds_per_record=1});
404
        is(
405
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_12), 'tooManyHoldsForThisRecord',
406
            "A item-level hold already exists and holds_per_record=1 - tooManyHoldsForThisRecord has priority over tooManyReserves"
407
        );
408
        $dbh->do(q{UPDATE issuingrules set reservesallowed=5, holds_per_record=2});
409
        is(
410
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_12), 'OK',
411
            "A item-level hold already exists but holds_per_record=2- another item-level hold can be placed on this record"
412
        );
413
414
        AddReserve(
415
            $branch_1,
416
            $borrowernumbers[0],
417
            $biblionumber_2,
418
            '',
419
            1,
420
            undef, undef, undef, undef, $itemnumber_21
421
        );
422
        $dbh->do(q{UPDATE issuingrules set reservesallowed=2, holds_per_record=2});
423
        is(
424
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_21), 'itemAlreadyOnHold',
425
            "A item-level holds already exists on this item, itemAlreadyOnHold should be raised"
426
        );
427
        is(
428
            CanItemBeReserved( $borrowernumbers[0], $itemnumber_22), 'tooManyReserves',
429
            "This patron has already placed reservesallowed holds, tooManyReserves should be raised"
430
        );
431
    };
432
};
338
433
339
434
340
# Test branch item rules
435
# Test branch item rules
341
436
342
$dbh->do('DELETE FROM issuingrules');
437
delete_all_rules();
343
$dbh->do(
438
$dbh->do(
344
    q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
439
    q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed)
345
      VALUES (?, ?, ?, ?)},
440
      VALUES (?, ?, ?, ?)},
346
    {},
441
    {},
347
    '*', '*', '*', 25
442
    '*', '*', '*', 25
348
);
443
);
349
$dbh->do('DELETE FROM branch_item_rules');
350
$dbh->do('DELETE FROM default_branch_circ_rules');
351
$dbh->do('DELETE FROM default_branch_item_rules');
352
$dbh->do('DELETE FROM default_circ_rules');
353
$dbh->do(q{
444
$dbh->do(q{
354
    INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
445
    INSERT INTO branch_item_rules (branchcode, itemtype, holdallowed, returnbranch)
355
    VALUES (?, ?, ?, ?)
446
    VALUES (?, ?, ?, ?)
Lines 415-417 sub create_helper_biblio { Link Here
415
    );
506
    );
416
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
507
    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
417
}
508
}
418
- 
509
510
sub delete_all_rules {
511
    my $dbh = C4::Context->dbh;
512
    $dbh->do('DELETE FROM issuingrules');
513
    $dbh->do('DELETE FROM branch_item_rules');
514
    $dbh->do('DELETE FROM default_branch_circ_rules');
515
    $dbh->do('DELETE FROM default_branch_item_rules');
516
    $dbh->do('DELETE FROM default_circ_rules');
517
}

Return to bug 16787