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

(-)a/t/db_dependent/Reserves.t (-2 / +184 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 68;
20
use Test::More tests => 70;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Warn;
22
use Test::Warn;
23
23
Lines 1362-1364 sub place_item_hold { Link Here
1362
1362
1363
# we reached the finish
1363
# we reached the finish
1364
$schema->storage->txn_rollback();
1364
$schema->storage->txn_rollback();
1365
- 
1365
1366
subtest 'CanBookBeReserved() tests' => sub {
1367
1368
    plan tests => 4;
1369
1370
    $schema->storage->txn_begin;
1371
1372
    my $library = $builder->build_object(
1373
        { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
1374
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1375
    my $itype  = $builder->build_object( { class => 'Koha::ItemTypes' } );
1376
1377
    my $biblio = $builder->build_sample_biblio();
1378
    my $item_1 = $builder->build_sample_item(
1379
        { biblionumber => $biblio->id, itype => $itype->id } );
1380
    my $item_2 = $builder->build_sample_item(
1381
        { biblionumber => $biblio->id, itype => $itype->id } );
1382
1383
    Koha::CirculationRules->delete;
1384
    Koha::CirculationRules->set_rules(
1385
        {
1386
            branchcode   => undef,
1387
            categorycode => undef,
1388
            itemtype     => undef,
1389
            rules        => {
1390
                holds_per_record => 100,
1391
            }
1392
        }
1393
    );
1394
    Koha::CirculationRules->set_rules(
1395
        {
1396
            branchcode   => undef,
1397
            categorycode => undef,
1398
            itemtype     => $itype->id,
1399
            rules        => {
1400
                reservesallowed => 2,
1401
            }
1402
        }
1403
    );
1404
1405
    C4::Reserves::AddReserve(
1406
        {
1407
            branchcode     => $library->id,
1408
            borrowernumber => $patron->id,
1409
            biblionumber   => $biblio->id,
1410
            title          => $biblio->title,
1411
            itemnumber     => $item_1->id
1412
        }
1413
    );
1414
1415
    ## Limit on item type is 2, only one hold, success tests
1416
1417
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 1 );
1418
1419
    my $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1420
        { itemtype => $itype->id } );
1421
    is_deeply( $res, { status => 'OK' },
1422
        'Holds on itemtype limit not reached' );
1423
1424
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 0 );
1425
1426
    $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1427
        { itemtype => $itype->id } );
1428
    is_deeply(
1429
        $res,
1430
        { status => 'OK' },
1431
        'Holds on itemtype not considering biblio-level'
1432
    );
1433
1434
    # Add a second hold, biblio-level and item type-constrained
1435
    C4::Reserves::AddReserve(
1436
        {
1437
            branchcode     => $library->id,
1438
            borrowernumber => $patron->id,
1439
            biblionumber   => $biblio->id,
1440
            title          => $biblio->title,
1441
            itemtype       => $itype->id,
1442
        }
1443
    );
1444
1445
    ## Limit on item type is 2, two holds, one of them biblio-level/item type-constrained
1446
1447
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 1 );
1448
1449
    $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1450
        { itemtype => $itype->id } );
1451
    is_deeply( $res, { status => '' }, 'Holds on itemtype limit reached' );
1452
1453
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 0 );
1454
1455
    $res = CanBookBeReserved( $patron->id, $biblio->id, $library->id,
1456
        { itemtype => $itype->id } );
1457
    is_deeply(
1458
        $res,
1459
        { status => 'OK' },
1460
        'Holds on itemtype not considering biblio-level'
1461
    );
1462
1463
    $schema->storage->txn_rollback;
1464
};
1465
1466
1467
subtest 'CanItemBeReserved() tests' => sub {
1468
1469
    plan tests => 4;
1470
1471
    $schema->storage->txn_begin;
1472
1473
    my $library = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } });
1474
    my $patron  = $builder->build_object({ class => 'Koha::Patrons' });
1475
    my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
1476
1477
    my $biblio = $builder->build_sample_biblio();
1478
    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->id, itype => $itype->id });
1479
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->id, itype => $itype->id });
1480
1481
    Koha::CirculationRules->delete;
1482
    Koha::CirculationRules->set_rules(
1483
        {   branchcode   => undef,
1484
            categorycode => undef,
1485
            itemtype     => undef,
1486
            rules        => {
1487
                holds_per_record => 100,
1488
            }
1489
        }
1490
    );
1491
    Koha::CirculationRules->set_rules(
1492
        {   branchcode   => undef,
1493
            categorycode => undef,
1494
            itemtype     => $itype->id,
1495
            rules        => {
1496
                reservesallowed => 2,
1497
            }
1498
        }
1499
    );
1500
1501
    C4::Reserves::AddReserve(
1502
        {
1503
            branchcode     => $library->id,
1504
            borrowernumber => $patron->id,
1505
            biblionumber   => $biblio->id,
1506
            title          => $biblio->title,
1507
            itemnumber     => $item_1->id
1508
        }
1509
    );
1510
1511
    ## Limit on item type is 2, only one hold, success tests
1512
1513
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 1 );
1514
1515
    my $res = CanItemBeReserved( $patron->id, $item_2->id, $library->id );
1516
    is_deeply( $res, { status => 'OK' }, 'Holds on itemtype limit not reached' );
1517
1518
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 0 );
1519
1520
    $res = CanItemBeReserved( $patron->id, $item_2->id, $library->id );
1521
    is_deeply( $res, { status => 'OK' }, 'Holds on itemtype not considering biblio-level' );
1522
1523
    # Add a second hold, biblio-level and item type-constrained
1524
    C4::Reserves::AddReserve(
1525
        {
1526
            branchcode     => $library->id,
1527
            borrowernumber => $patron->id,
1528
            biblionumber   => $biblio->id,
1529
            title          => $biblio->title,
1530
            itemtype       => $itype->id,
1531
        }
1532
    );
1533
1534
    ## Limit on item type is 2, two holds, one of them biblio-level/item type-constrained
1535
1536
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 1 );
1537
1538
    $res = CanItemBeReserved( $patron->id, $item_2->id, $library->id );
1539
    is_deeply( $res, { status => 'tooManyReserves', limit => 2 }, 'Holds on itemtype limit reached' );
1540
1541
    t::lib::Mocks::mock_preference( 'BiblioHoldItemTypeUseForRules', 0 );
1542
1543
    $res = CanItemBeReserved( $patron->id, $item_2->id, $library->id );
1544
    is_deeply( $res, { status => 'OK' }, 'Holds on itemtype not considering biblio-level' );
1545
1546
    $schema->storage->txn_rollback;
1547
};

Return to bug 28529