|
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 => 23; |
23 |
use Test::More tests => 25; |
| 24 |
|
24 |
|
| 25 |
use Test::Exception; |
25 |
use Test::Exception; |
| 26 |
use Test::MockModule; |
26 |
use Test::MockModule; |
|
Lines 2509-2514
subtest 'charge_hold_fee() tests' => sub {
Link Here
|
| 2509 |
$schema->storage->txn_rollback; |
2509 |
$schema->storage->txn_rollback; |
| 2510 |
}; |
2510 |
}; |
| 2511 |
|
2511 |
|
|
|
2512 |
subtest 'debits() tests' => sub { |
| 2513 |
|
| 2514 |
plan tests => 5; |
| 2515 |
|
| 2516 |
$schema->storage->txn_begin; |
| 2517 |
|
| 2518 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 2519 |
my $biblio = $builder->build_sample_biblio; |
| 2520 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 2521 |
|
| 2522 |
# Create a hold |
| 2523 |
my $hold = $builder->build_object( |
| 2524 |
{ |
| 2525 |
class => 'Koha::Holds', |
| 2526 |
value => { |
| 2527 |
borrowernumber => $patron->borrowernumber, |
| 2528 |
biblionumber => $biblio->biblionumber, |
| 2529 |
itemnumber => $item->itemnumber, |
| 2530 |
branchcode => $patron->branchcode, |
| 2531 |
} |
| 2532 |
} |
| 2533 |
); |
| 2534 |
|
| 2535 |
# Test 1: Hold with no debits returns empty collection |
| 2536 |
my $debits = $hold->debits; |
| 2537 |
isa_ok( $debits, 'Koha::Account::Debits', 'debits() returns a Koha::Account::Debits object' ); |
| 2538 |
is( $debits->count, 0, 'debits() returns empty collection when no debits exist' ); |
| 2539 |
|
| 2540 |
# Test 2: Create some account lines (debits and credits) linked to the hold |
| 2541 |
my $account = $patron->account; |
| 2542 |
|
| 2543 |
# Add a debit (hold fee) |
| 2544 |
my $debit1 = $account->add_debit( |
| 2545 |
{ |
| 2546 |
amount => 5.00, |
| 2547 |
description => 'Hold fee', |
| 2548 |
type => 'RESERVE', |
| 2549 |
user_id => 1, |
| 2550 |
library_id => $patron->branchcode, |
| 2551 |
interface => 'intranet', |
| 2552 |
hold_id => $hold->id, |
| 2553 |
} |
| 2554 |
); |
| 2555 |
|
| 2556 |
# Add another debit (expiration fee) |
| 2557 |
my $debit2 = $account->add_debit( |
| 2558 |
{ |
| 2559 |
amount => 2.00, |
| 2560 |
description => 'Hold expiration fee', |
| 2561 |
type => 'RESERVE_EXPIRED', |
| 2562 |
user_id => 1, |
| 2563 |
library_id => $patron->branchcode, |
| 2564 |
interface => 'intranet', |
| 2565 |
hold_id => $hold->id, |
| 2566 |
} |
| 2567 |
); |
| 2568 |
|
| 2569 |
# Add a credit (should not appear in debits) |
| 2570 |
my $credit = $account->add_credit( |
| 2571 |
{ |
| 2572 |
amount => 3.00, |
| 2573 |
description => 'Credit for hold', |
| 2574 |
type => 'CREDIT', |
| 2575 |
user_id => 1, |
| 2576 |
library_id => $patron->branchcode, |
| 2577 |
interface => 'intranet', |
| 2578 |
} |
| 2579 |
); |
| 2580 |
|
| 2581 |
# Link credit to hold manually since add_credit doesn't have hold_id parameter |
| 2582 |
$credit->reserve_id( $hold->id )->store; |
| 2583 |
|
| 2584 |
# Test 3: Hold debits returns only debit lines, ordered by timestamp descending |
| 2585 |
$debits = $hold->debits; |
| 2586 |
is( $debits->count, 2, 'debits() returns correct count of debit lines only' ); |
| 2587 |
|
| 2588 |
my @debit_amounts = sort { $b <=> $a } map { $_->amount + 0 } $debits->as_list; |
| 2589 |
is_deeply( \@debit_amounts, [ 5.0, 2.0 ], 'debits() returns correct amounts in descending order' ); |
| 2590 |
|
| 2591 |
# Test 4: Verify both debit types are present |
| 2592 |
my @debit_types = sort map { $_->debit_type_code } $debits->as_list; |
| 2593 |
is_deeply( \@debit_types, [ 'RESERVE', 'RESERVE_EXPIRED' ], 'debits() returns both expected debit types' ); |
| 2594 |
|
| 2595 |
$schema->storage->txn_rollback; |
| 2596 |
}; |
| 2597 |
|
| 2598 |
subtest '_move_to_old() account migration tests' => sub { |
| 2599 |
|
| 2600 |
plan tests => 6; |
| 2601 |
|
| 2602 |
$schema->storage->txn_begin; |
| 2603 |
|
| 2604 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 2605 |
my $biblio = $builder->build_sample_biblio; |
| 2606 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 2607 |
|
| 2608 |
# Create a hold |
| 2609 |
my $hold = $builder->build_object( |
| 2610 |
{ |
| 2611 |
class => 'Koha::Holds', |
| 2612 |
value => { |
| 2613 |
borrowernumber => $patron->borrowernumber, |
| 2614 |
biblionumber => $biblio->biblionumber, |
| 2615 |
itemnumber => $item->itemnumber, |
| 2616 |
branchcode => $patron->branchcode, |
| 2617 |
} |
| 2618 |
} |
| 2619 |
); |
| 2620 |
|
| 2621 |
my $account = $patron->account; |
| 2622 |
|
| 2623 |
# Create account lines linked to the hold |
| 2624 |
my $debit1 = $account->add_debit( |
| 2625 |
{ |
| 2626 |
amount => 5.00, |
| 2627 |
description => 'Hold fee', |
| 2628 |
type => 'RESERVE', |
| 2629 |
user_id => 1, |
| 2630 |
library_id => $patron->branchcode, |
| 2631 |
interface => 'intranet', |
| 2632 |
hold_id => $hold->id, |
| 2633 |
} |
| 2634 |
); |
| 2635 |
|
| 2636 |
my $debit2 = $account->add_debit( |
| 2637 |
{ |
| 2638 |
amount => 2.00, |
| 2639 |
description => 'Hold expiration fee', |
| 2640 |
type => 'RESERVE_EXPIRED', |
| 2641 |
user_id => 1, |
| 2642 |
library_id => $patron->branchcode, |
| 2643 |
interface => 'intranet', |
| 2644 |
hold_id => $hold->id, |
| 2645 |
} |
| 2646 |
); |
| 2647 |
|
| 2648 |
# Test initial state |
| 2649 |
is( $debit1->reserve_id, $hold->id, 'Account line initially has reserve_id set' ); |
| 2650 |
is( $debit1->old_reserve_id, undef, 'Account line initially has old_reserve_id unset' ); |
| 2651 |
is( $debit2->reserve_id, $hold->id, 'Second account line initially has reserve_id set' ); |
| 2652 |
|
| 2653 |
# Call _move_to_old |
| 2654 |
my $old_hold = $hold->_move_to_old; |
| 2655 |
|
| 2656 |
# Verify account lines were updated |
| 2657 |
$debit1->discard_changes; |
| 2658 |
$debit2->discard_changes; |
| 2659 |
|
| 2660 |
is( $debit1->reserve_id, undef, 'Account line reserve_id cleared after _move_to_old' ); |
| 2661 |
is( $debit1->old_reserve_id, $hold->id, 'Account line old_reserve_id set after _move_to_old' ); |
| 2662 |
is( $debit2->old_reserve_id, $hold->id, 'Second account line old_reserve_id set after _move_to_old' ); |
| 2663 |
|
| 2664 |
$schema->storage->txn_rollback; |
| 2665 |
}; |
| 2666 |
|
| 2512 |
sub set_userenv { |
2667 |
sub set_userenv { |
| 2513 |
my ($library) = @_; |
2668 |
my ($library) = @_; |
| 2514 |
my $staff = $builder->build_object( { class => "Koha::Patrons" } ); |
2669 |
my $staff = $builder->build_object( { class => "Koha::Patrons" } ); |