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

(-)a/t/db_dependent/Koha/Hold.t (-1 / +270 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 => 15;
23
use Test::More tests => 17;
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 1200-1202 subtest 'change_type() tests' => sub { Link Here
1200
1202
1201
    $schema->storage->txn_rollback;
1203
    $schema->storage->txn_rollback;
1202
};
1204
};
1205
1206
subtest 'is_hold_group_target, cleanup_hold_group and set_as_hold_group_target tests' => sub {
1207
1208
    plan tests => 16;
1209
1210
    $schema->storage->txn_begin;
1211
1212
    my $patron_category = $builder->build(
1213
        {
1214
            source => 'Category',
1215
            value  => {
1216
                category_type                 => 'P',
1217
                enrolmentfee                  => 0,
1218
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
1219
            }
1220
        }
1221
    );
1222
1223
    my $library_1 = $builder->build( { source => 'Branch' } );
1224
    my $patron_1  = $builder->build(
1225
        {
1226
            source => 'Borrower',
1227
            value  => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} }
1228
        }
1229
    );
1230
    my $library_2 = $builder->build( { source => 'Branch' } );
1231
    my $patron_2  = $builder->build_object(
1232
        {
1233
            class => 'Koha::Patrons',
1234
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
1235
        }
1236
    );
1237
1238
    my $item = $builder->build_sample_item(
1239
        {
1240
            library => $library_1->{branchcode},
1241
        }
1242
    );
1243
1244
    my $item_2 = $builder->build_sample_item(
1245
        {
1246
            library => $library_1->{branchcode},
1247
        }
1248
    );
1249
1250
    set_userenv($library_2);
1251
    my $reserve_id = AddReserve(
1252
        {
1253
            branchcode     => $library_2->{branchcode},
1254
            borrowernumber => $patron_2->borrowernumber,
1255
            biblionumber   => $item->biblionumber,
1256
            priority       => 1,
1257
        }
1258
    );
1259
1260
    my $reserve_2_id = AddReserve(
1261
        {
1262
            branchcode     => $library_2->{branchcode},
1263
            borrowernumber => $patron_2->borrowernumber,
1264
            biblionumber   => $item_2->biblionumber,
1265
            priority       => 1,
1266
        }
1267
    );
1268
1269
    my $reserve_3_id = AddReserve(
1270
        {
1271
            branchcode     => $library_2->{branchcode},
1272
            borrowernumber => $patron_2->borrowernumber,
1273
            biblionumber   => $item_2->biblionumber,
1274
            priority       => 1,
1275
        }
1276
    );
1277
1278
    my $new_hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id, $reserve_3_id ] );
1279
1280
    set_userenv($library_1);
1281
    my $do_transfer = 1;
1282
    my ( $returned, $messages, $issue, $borrower ) = AddReturn( $item->barcode, $library_1->{branchcode} );
1283
1284
    ok( exists $messages->{ResFound}, 'ResFound key exists' );
1285
1286
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1287
1288
    my $hold = Koha::Holds->find($reserve_id);
1289
    is( $hold->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'First hold belongs to hold group' );
1290
    is( $hold->found,                     'T',                            'Hold is in transit' );
1291
1292
    is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' );
1293
    my $hold_2 = Koha::Holds->find($reserve_2_id);
1294
    is( $hold_2->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'Second hold belongs to hold group' );
1295
    is( $hold_2->is_hold_group_target,      0, 'Second hold is not the hold group target' );
1296
1297
    set_userenv($library_2);
1298
    $do_transfer = 0;
1299
    AddReturn( $item->barcode, $library_2->{branchcode} );
1300
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1301
    $hold = Koha::Holds->find($reserve_id);
1302
    is( $hold->found, 'W', 'Hold is waiting' );
1303
1304
    is( $hold->is_hold_group_target,   1, 'First hold is still the hold group target' );
1305
    is( $hold_2->is_hold_group_target, 0, 'Second hold is still not the hold group target' );
1306
1307
    $hold->cancel();
1308
    is( $hold->is_hold_group_target,   0, 'First hold is no longer the hold group target' );
1309
    is( $hold_2->is_hold_group_target, 0, 'Second hold is still not the hold group target' );
1310
1311
    # Return item for second hold
1312
    AddReturn( $item_2->barcode, $library_2->{branchcode} );
1313
    ModReserveAffect( $item_2->itemnumber, undef, $do_transfer, $reserve_2_id );
1314
    $new_hold_group->discard_changes;
1315
    is( $hold_2->get_from_storage->found, 'W', 'Hold is waiting' );
1316
    is( $hold_2->is_hold_group_target,    1,   'Second hold is now the hold group target' );
1317
1318
    my $hold_3 = Koha::Holds->find($reserve_3_id);
1319
    is( $hold_3->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'Third hold belongs to hold group' );
1320
    is( $hold_3->is_hold_group_target,      0,                              'Third hold is not the hold group target' );
1321
1322
    $hold_2->cancel();
1323
    is(
1324
        $hold_3->hold_group, undef,
1325
        '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'
1326
    );
1327
1328
    $schema->storage->txn_rollback;
1329
};
1330
1331
subtest '_Findgroupreserve in the context of hold groups' => sub {
1332
    plan tests => 13;
1333
1334
    $schema->storage->txn_begin;
1335
1336
    my $patron_category = $builder->build(
1337
        {
1338
            source => 'Category',
1339
            value  => {
1340
                category_type                 => 'P',
1341
                enrolmentfee                  => 0,
1342
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
1343
            }
1344
        }
1345
    );
1346
1347
    my $library_1 = $builder->build( { source => 'Branch' } );
1348
    my $patron_1  = $builder->build_object(
1349
        {
1350
            class => 'Koha::Patrons',
1351
            value => { branchcode => $library_1->{branchcode}, categorycode => $patron_category->{categorycode} }
1352
        }
1353
    );
1354
    my $library_2 = $builder->build( { source => 'Branch' } );
1355
    my $patron_2  = $builder->build_object(
1356
        {
1357
            class => 'Koha::Patrons',
1358
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
1359
        }
1360
    );
1361
1362
    my $item = $builder->build_sample_item(
1363
        {
1364
            library => $library_1->{branchcode},
1365
        }
1366
    );
1367
1368
    my $item_2 = $builder->build_sample_item(
1369
        {
1370
            library => $library_1->{branchcode},
1371
        }
1372
    );
1373
1374
    set_userenv($library_2);
1375
    my $reserve_id = AddReserve(
1376
        {
1377
            branchcode     => $library_2->{branchcode},
1378
            borrowernumber => $patron_2->borrowernumber,
1379
            biblionumber   => $item->biblionumber,
1380
            priority       => 1,
1381
        }
1382
    );
1383
1384
    my $reserve_2_id = AddReserve(
1385
        {
1386
            branchcode     => $library_2->{branchcode},
1387
            borrowernumber => $patron_2->borrowernumber,
1388
            biblionumber   => $item_2->biblionumber,
1389
            priority       => 1,
1390
        }
1391
    );
1392
1393
    my $reserve_3_id = AddReserve(
1394
        {
1395
            branchcode     => $library_2->{branchcode},
1396
            borrowernumber => $patron_1->borrowernumber,
1397
            biblionumber   => $item_2->biblionumber,
1398
            priority       => 2,
1399
        }
1400
    );
1401
1402
    my @reserves = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1403
    is( scalar @reserves,           2,             "No hold group created yet. We should get both holds" );
1404
    is( $reserves[0]->{reserve_id}, $reserve_2_id, "We got the expected reserve" );
1405
1406
    my $new_hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id ] );
1407
1408
    set_userenv($library_1);
1409
    my $do_transfer = 1;
1410
    my ( $returned, $messages, $issue, $borrower ) = AddReturn( $item->barcode, $library_1->{branchcode} );
1411
1412
    ok( exists $messages->{ResFound}, 'ResFound key exists' );
1413
1414
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
1415
1416
    my $hold = Koha::Holds->find($reserve_id);
1417
    is( $hold->hold_group->hold_group_id, $new_hold_group->hold_group_id, 'First hold belongs to hold group' );
1418
    is( $hold->found,                     'T',                            'Hold is in transit' );
1419
1420
    is( $hold->is_hold_group_target, 1, 'First hold is the hold group target' );
1421
1422
    my @new_reserves = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1423
    is( scalar @new_reserves, 1, "reserve_2_id is now part of a group that has a target. Should not be here." );
1424
    is(
1425
        $new_reserves[0]->{reserve_id}, $reserve_3_id,
1426
        "reserve_2_id is now part of a group that has a target. Should not be here."
1427
    );
1428
1429
    $hold->cancel();
1430
1431
    my @reserves_after_cancel = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1432
    is(
1433
        scalar @reserves_after_cancel, 2,
1434
        "reserve_2_id should be back in the pool as it's no longer part of a group."
1435
    );
1436
    is( $reserves_after_cancel[0]->{reserve_id}, $reserve_2_id, "We got the expected reserve" );
1437
    is( $reserves_after_cancel[1]->{reserve_id}, $reserve_3_id, "We got the expected reserve" );
1438
1439
    my $first_reserve_id_again = AddReserve(
1440
        {
1441
            branchcode     => $library_2->{branchcode},
1442
            borrowernumber => $patron_2->borrowernumber,
1443
            biblionumber   => $item->biblionumber,
1444
            priority       => 1,
1445
        }
1446
    );
1447
1448
    # Fake the holds queue
1449
    my $dbh = C4::Context->dbh;
1450
    $dbh->do(
1451
        q{INSERT INTO hold_fill_targets VALUES (?, ?, ?, ?, ?,?)}, undef,
1452
        (
1453
            $patron_1->borrowernumber, $item->biblionumber, $item->itemnumber, $item->homebranch, 0,
1454
            $first_reserve_id_again
1455
        )
1456
    );
1457
1458
    my @reserves_after_holds_queue = C4::Reserves::_Findgroupreserve( $item_2->biblionumber, $item_2->id, 0, [] );
1459
    is( scalar @new_reserves, 1, "Only reserve_3_id should exist." );
1460
    is(
1461
        $new_reserves[0]->{reserve_id}, $reserve_3_id,
1462
        "reserve_3_id should not be here."
1463
    );
1464
};
1465
1466
sub set_userenv {
1467
    my ($library) = @_;
1468
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
1469
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
1470
}
1471
(-)a/t/db_dependent/Reserves/HoldGroup.t (-2 / +82 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
    my $patron_category = $builder->build(
66
        {
67
            source => 'Category',
68
            value  => {
69
                category_type                 => 'P',
70
                enrolmentfee                  => 0,
71
                BlockExpiredPatronOpacActions => 'follow_syspref_BlockExpiredPatronOpacActions',
72
            }
73
        }
74
    );
75
76
    my $library_1 = $builder->build( { source => 'Branch' } );
77
    my $library_2 = $builder->build( { source => 'Branch' } );
78
    my $patron_2  = $builder->build_object(
79
        {
80
            class => 'Koha::Patrons',
81
            value => { branchcode => $library_2->{branchcode}, categorycode => $patron_category->{categorycode} }
82
        }
83
    );
84
85
    my $item = $builder->build_sample_item(
86
        {
87
            library => $library_1->{branchcode},
88
        }
89
    );
90
91
    my $item_2 = $builder->build_sample_item(
92
        {
93
            library => $library_1->{branchcode},
94
        }
95
    );
96
97
    set_userenv($library_2);
98
    my $reserve_id = AddReserve(
99
        {
100
            branchcode     => $library_2->{branchcode},
101
            borrowernumber => $patron_2->borrowernumber,
102
            biblionumber   => $item->biblionumber,
103
            priority       => 1,
104
        }
105
    );
106
107
    my $reserve_2_id = AddReserve(
108
        {
109
            branchcode     => $library_2->{branchcode},
110
            borrowernumber => $patron_2->borrowernumber,
111
            biblionumber   => $item_2->biblionumber,
112
            priority       => 1,
113
        }
114
    );
115
116
    my $hold_group = $patron_2->create_hold_group( [ $reserve_id, $reserve_2_id ] );
117
118
    set_userenv($library_1);
119
    my $do_transfer = 1;
120
    AddReturn( $item->barcode, $library_1->{branchcode} );
121
    ModReserveAffect( $item->itemnumber, undef, $do_transfer, $reserve_id );
122
    my $hold = Koha::Holds->find($reserve_id);
123
    is( $hold_group->get_from_storage->target_hold_id, $hold->reserve_id, 'target_hold_id is correct' );
124
    $hold->fill();
125
    is( $hold_group->get_from_storage, undef, 'hold group no longer exists' );
126
127
    $schema->storage->txn_rollback;
128
};
129
130
sub set_userenv {
131
    my ($library) = @_;
132
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
133
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
134
}

Return to bug 40529