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

(-)a/t/db_dependent/Koha/Hold.t (-1 / +272 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 16;
23
use Test::More tests => 18;
24
24
25
use Test::Exception;
25
use Test::Exception;
26
use Test::MockModule;
26
use Test::MockModule;
Lines 34-39 use Koha::DateUtils qw(dt_from_string); Link Here
34
use Koha::Holds;
34
use Koha::Holds;
35
use Koha::Libraries;
35
use Koha::Libraries;
36
use Koha::Old::Holds;
36
use Koha::Old::Holds;
37
use C4::Reserves    qw( AddReserve ModReserveAffect );
38
use C4::Circulation qw( AddReturn );
37
39
38
my $schema  = Koha::Database->new->schema;
40
my $schema  = Koha::Database->new->schema;
39
my $builder = t::lib::TestBuilder->new;
41
my $builder = t::lib::TestBuilder->new;
Lines 1259-1261 subtest 'strings_map() tests' => sub { Link Here
1259
1261
1260
    $schema->txn_rollback;
1262
    $schema->txn_rollback;
1261
};
1263
};
1264
subtest 'is_hold_group_target, cleanup_hold_group and set_as_hold_group_target tests' => sub {
1265
1266
    plan tests => 16;
1267
1268
    $schema->storage->txn_begin;
1269
1270
    t::lib::Mocks::mock_preference( 'DisplayAddHoldGroups', 1 );
1271
1272
    my $patron_category = $builder->build(
1273
        {
1274
            source => 'Category',
1275
            value  => {
1276
                category_type                 => 'P',
1277
                enrolmentfee                  => 0,
1278
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
1279
            }
1280
        }
1281
    );
1282
1283
    my $library_1 = $builder->build( { source => 'Branch' } );
1284
    my $patron_1  = $builder->build(
1285
        {
1286
            source => 'Borrower',
1287
            value  => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} }
1288
        }
1289
    );
1290
    my $library_2 = $builder->build( { source => 'Branch' } );
1291
    my $patron_2  = $builder->build_object(
1292
        {
1293
            class => 'Koha::Patrons',
1294
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
1295
        }
1296
    );
1297
1298
    my $item = $builder->build_sample_item(
1299
        {
1300
            library => $library_1->{branchcode},
1301
        }
1302
    );
1303
1304
    my $item_2 = $builder->build_sample_item(
1305
        {
1306
            library => $library_1->{branchcode},
1307
        }
1308
    );
1309
1310
    set_userenv($library_2);
1311
    my $reserve_id = AddReserve(
1312
        {
1313
            branchcode     => $library_2->{branchcode},
1314
            borrowernumber => $patron_2->borrowernumber,
1315
            biblionumber   => $item->biblionumber,
1316
            priority       => 1,
1317
        }
1318
    );
1319
1320
    my $reserve_2_id = AddReserve(
1321
        {
1322
            branchcode     => $library_2->{branchcode},
1323
            borrowernumber => $patron_2->borrowernumber,
1324
            biblionumber   => $item_2->biblionumber,
1325
            priority       => 1,
1326
        }
1327
    );
1328
1329
    my $reserve_3_id = AddReserve(
1330
        {
1331
            branchcode     => $library_2->{branchcode},
1332
            borrowernumber => $patron_2->borrowernumber,
1333
            biblionumber   => $item_2->biblionumber,
1334
            priority       => 1,
1335
        }
1336
    );
1337
1338
    my $new_hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id, $reserve_3_id ] );
1339
1340
    set_userenv($library_1);
1341
    my $do_transfer = 1;
1342
    my ( $returned, $messages, $issue, $borrower ) = AddReturn( $item->barcode, $library_1->{branchcode} );
1343
1344
    ok( exists $messages->{ResFound}, 'ResFound key exists' );
1345
1346
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1347
1348
    my $hold = Koha::Holds->find($reserve_id);
1349
    is( $hold->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'First hold belongs to hold group' );
1350
    is( $hold->found,                     'T',                            'Hold is in transit' );
1351
1352
    is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' );
1353
    my $hold_2 = Koha::Holds->find($reserve_2_id);
1354
    is( $hold_2->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'Second hold belongs to hold group' );
1355
    is( $hold_2->is_hold_group_target,      0, 'Second hold is not the hold group target' );
1356
1357
    set_userenv($library_2);
1358
    $do_transfer = 0;
1359
    AddReturn( $item->barcode, $library_2->{branchcode} );
1360
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1361
    $hold = Koha::Holds->find($reserve_id);
1362
    is( $hold->found, 'W', 'Hold is waiting' );
1363
1364
    is( $hold->is_hold_group_target,   1, 'First hold is still the hold group target' );
1365
    is( $hold_2->is_hold_group_target, 0, 'Second hold is still not the hold group target' );
1366
1367
    $hold->cancel();
1368
    is( $hold->is_hold_group_target,   0, 'First hold is no longer the hold group target' );
1369
    is( $hold_2->is_hold_group_target, 0, 'Second hold is still not the hold group target' );
1370
1371
    # Return item for second hold
1372
    AddReturn( $item_2->barcode, $library_2->{branchcode} );
1373
    ModReserveAffect( $item_2->itemnumber, undef, $do_transfer, $reserve_2_id );
1374
    $new_hold_group->discard_changes;
1375
    is( $hold_2->get_from_storage->found, 'W', 'Hold is waiting' );
1376
    is( $hold_2->is_hold_group_target,    1,   'Second hold is now the hold group target' );
1377
1378
    my $hold_3 = Koha::Holds->find($reserve_3_id);
1379
    is( $hold_3->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'Third hold belongs to hold group' );
1380
    is( $hold_3->is_hold_group_target,      0,                              'Third hold is not the hold group target' );
1381
1382
    $hold_2->cancel();
1383
    is(
1384
        $hold_3->hold_group, undef,
1385
        'Third hold was left as the only member of its group. Group was deleted and the hold is no longer part of a hold group'
1386
    );
1387
1388
    $schema->storage->txn_rollback;
1389
};
1390
1391
subtest '_Findgroupreserve in the context of hold groups' => sub {
1392
    plan tests => 13;
1393
1394
    $schema->storage->txn_begin;
1395
1396
    t::lib::Mocks::mock_preference( 'DisplayAddHoldGroups', 1 );
1397
1398
    my $patron_category = $builder->build(
1399
        {
1400
            source => 'Category',
1401
            value  => {
1402
                category_type                 => 'P',
1403
                enrolmentfee                  => 0,
1404
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
1405
            }
1406
        }
1407
    );
1408
1409
    my $library_1 = $builder->build( { source => 'Branch' } );
1410
    my $patron_1  = $builder->build_object(
1411
        {
1412
            class => 'Koha::Patrons',
1413
            value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} }
1414
        }
1415
    );
1416
    my $library_2 = $builder->build( { source => 'Branch' } );
1417
    my $patron_2  = $builder->build_object(
1418
        {
1419
            class => 'Koha::Patrons',
1420
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
1421
        }
1422
    );
1423
1424
    my $item = $builder->build_sample_item(
1425
        {
1426
            library => $library_1->{branchcode},
1427
        }
1428
    );
1429
1430
    my $item_2 = $builder->build_sample_item(
1431
        {
1432
            library => $library_1->{branchcode},
1433
        }
1434
    );
1435
1436
    set_userenv($library_2);
1437
    my $reserve_id = AddReserve(
1438
        {
1439
            branchcode     => $library_2->{branchcode},
1440
            borrowernumber => $patron_2->borrowernumber,
1441
            biblionumber   => $item->biblionumber,
1442
            priority       => 1,
1443
        }
1444
    );
1445
1446
    my $reserve_2_id = AddReserve(
1447
        {
1448
            branchcode     => $library_2->{branchcode},
1449
            borrowernumber => $patron_2->borrowernumber,
1450
            biblionumber   => $item_2->biblionumber,
1451
            priority       => 1,
1452
        }
1453
    );
1454
1455
    my $reserve_3_id = AddReserve(
1456
        {
1457
            branchcode     => $library_2->{branchcode},
1458
            borrowernumber => $patron_1->borrowernumber,
1459
            biblionumber   => $item_2->biblionumber,
1460
            priority       => 2,
1461
        }
1462
    );
1463
1464
    my @reserves = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1465
    is( scalar @reserves,           2,             "No hold group created yet. We should get both holds" );
1466
    is( $reserves[0]->{reserve_id}, $reserve_2_id, "We got the expected reserve" );
1467
1468
    my $new_hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id ] );
1469
1470
    set_userenv($library_1);
1471
    my $do_transfer = 1;
1472
    my ( $returned, $messages, $issue, $borrower ) = AddReturn( $item->barcode, $library_1->{branchcode} );
1473
1474
    ok( exists $messages->{ResFound}, 'ResFound key exists' );
1475
1476
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1477
1478
    my $hold = Koha::Holds->find($reserve_id);
1479
    is( $hold->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'First hold belongs to hold group' );
1480
    is( $hold->found,                     'T',                            'Hold is in transit' );
1481
1482
    is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' );
1483
1484
    my @new_reserves = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1485
    is( scalar @new_reserves, 1, "reserve_2_id is now part of a group that has a target. Should not be here." );
1486
    is(
1487
        $new_reserves[0]->{reserve_id}, $reserve_3_id,
1488
        "reserve_2_id is now part of a group that has a target. Should not be here."
1489
    );
1490
1491
    $hold->cancel();
1492
1493
    my @reserves_after_cancel = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1494
    is(
1495
        scalar @reserves_after_cancel, 2,
1496
        "reserve_2_id should be back in the pool as it's no longer part of a group."
1497
    );
1498
    is( $reserves_after_cancel[0]->{reserve_id}, $reserve_2_id, "We got the expected reserve" );
1499
    is( $reserves_after_cancel[1]->{reserve_id}, $reserve_3_id, "We got the expected reserve" );
1500
1501
    my $first_reserve_id_again = AddReserve(
1502
        {
1503
            branchcode     => $library_2->{branchcode},
1504
            borrowernumber => $patron_2->borrowernumber,
1505
            biblionumber   => $item->biblionumber,
1506
            priority       => 1,
1507
        }
1508
    );
1509
1510
    # Fake the holds queue
1511
    my $dbh = C4::Context->dbh;
1512
    $dbh->do(
1513
        q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)}, undef,
1514
        (
1515
            $patron_1->borrowernumber, $item->biblionumber, $item->itemnumber, $item->homebranch, 0,
1516
            $first_reserve_id_again
1517
        )
1518
    );
1519
1520
    my @reserves_after_holds_queue = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1521
    is( scalar @new_reserves, 1, "Only reserve_3_id should exist." );
1522
    is(
1523
        $new_reserves[0]->{reserve_id}, $reserve_3_id,
1524
        "reserve_3_id should not be here."
1525
    );
1526
};
1527
1528
sub set_userenv {
1529
    my ($library) = @_;
1530
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
1531
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
1532
}
(-)a/t/db_dependent/Reserves/HoldGroup.t (-2 / +84 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 1;
22
use Test::More tests => 2;
23
23
24
use t::lib::TestBuilder;
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use C4::Reserves    qw( AddReserve ModReserveAffect );
28
use C4::Circulation qw( AddReturn );
25
29
26
my $schema  = Koha::Database->new->schema;
30
my $schema  = Koha::Database->new->schema;
27
my $builder = t::lib::TestBuilder->new;
31
my $builder = t::lib::TestBuilder->new;
Lines 51-53 subtest 'holds tests' => sub { Link Here
51
55
52
    $schema->storage->txn_rollback;
56
    $schema->storage->txn_rollback;
53
};
57
};
54
- 
58
59
subtest "target_hold_id tests" => sub {
60
61
    plan tests => 2;
62
63
    $schema->storage->txn_begin;
64
65
    t::lib::Mocks::mock_preference( 'DisplayAddHoldGroups', 1 );
66
67
    my $patron_category = $builder->build(
68
        {
69
            source => 'Category',
70
            value  => {
71
                category_type                 => 'P',
72
                enrolmentfee                  => 0,
73
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
74
            }
75
        }
76
    );
77
78
    my $library_1 = $builder->build( { source => 'Branch' } );
79
    my $library_2 = $builder->build( { source => 'Branch' } );
80
    my $patron_2  = $builder->build_object(
81
        {
82
            class => 'Koha::Patrons',
83
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
84
        }
85
    );
86
87
    my $item = $builder->build_sample_item(
88
        {
89
            library => $library_1->{branchcode},
90
        }
91
    );
92
93
    my $item_2 = $builder->build_sample_item(
94
        {
95
            library => $library_1->{branchcode},
96
        }
97
    );
98
99
    set_userenv($library_2);
100
    my $reserve_id = AddReserve(
101
        {
102
            branchcode     => $library_2->{branchcode},
103
            borrowernumber => $patron_2->borrowernumber,
104
            biblionumber   => $item->biblionumber,
105
            priority       => 1,
106
        }
107
    );
108
109
    my $reserve_2_id = AddReserve(
110
        {
111
            branchcode     => $library_2->{branchcode},
112
            borrowernumber => $patron_2->borrowernumber,
113
            biblionumber   => $item_2->biblionumber,
114
            priority       => 1,
115
        }
116
    );
117
118
    my $hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id ] );
119
120
    set_userenv($library_1);
121
    my $do_transfer = 1;
122
    AddReturn( $item->barcode, $library_1->{branchcode} );
123
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
124
    my $hold = Koha::Holds->find($reserve_id);
125
    is( $hold_group->get_from_storage->target_hold_id, $hold->reserve_id, 'target_hold_id is correct' );
126
    $hold->fill();
127
    is( $hold_group->get_from_storage, undef, 'hold group no longer exists' );
128
129
    $schema->storage->txn_rollback;
130
};
131
132
sub set_userenv {
133
    my ($library) = @_;
134
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
135
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
136
}

Return to bug 40529