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

Return to bug 16787