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 => 21; |
23 |
use Test::More tests => 23; |
24 |
|
24 |
|
25 |
use Test::Exception; |
25 |
use Test::Exception; |
26 |
use Test::MockModule; |
26 |
use Test::MockModule; |
Lines 2202-2204
subtest 'charge_hold_fee() tests' => sub {
Link Here
|
2202 |
|
2202 |
|
2203 |
$schema->storage->txn_rollback; |
2203 |
$schema->storage->txn_rollback; |
2204 |
}; |
2204 |
}; |
|
|
2205 |
|
2206 |
subtest 'debits() tests' => sub { |
2207 |
|
2208 |
plan tests => 5; |
2209 |
|
2210 |
$schema->storage->txn_begin; |
2211 |
|
2212 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
2213 |
my $biblio = $builder->build_sample_biblio; |
2214 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
2215 |
|
2216 |
# Create a hold |
2217 |
my $hold = $builder->build_object( |
2218 |
{ |
2219 |
class => 'Koha::Holds', |
2220 |
value => { |
2221 |
borrowernumber => $patron->borrowernumber, |
2222 |
biblionumber => $biblio->biblionumber, |
2223 |
itemnumber => $item->itemnumber, |
2224 |
branchcode => $patron->branchcode, |
2225 |
} |
2226 |
} |
2227 |
); |
2228 |
|
2229 |
# Test 1: Hold with no debits returns empty collection |
2230 |
my $debits = $hold->debits; |
2231 |
isa_ok( $debits, 'Koha::Account::Debits', 'debits() returns a Koha::Account::Debits object' ); |
2232 |
is( $debits->count, 0, 'debits() returns empty collection when no debits exist' ); |
2233 |
|
2234 |
# Test 2: Create some account lines (debits and credits) linked to the hold |
2235 |
my $account = $patron->account; |
2236 |
|
2237 |
# Add a debit (hold fee) |
2238 |
my $debit1 = $account->add_debit( |
2239 |
{ |
2240 |
amount => 5.00, |
2241 |
description => 'Hold fee', |
2242 |
type => 'RESERVE', |
2243 |
user_id => 1, |
2244 |
library_id => $patron->branchcode, |
2245 |
interface => 'intranet', |
2246 |
hold_id => $hold->id, |
2247 |
} |
2248 |
); |
2249 |
|
2250 |
# Add another debit (expiration fee) |
2251 |
my $debit2 = $account->add_debit( |
2252 |
{ |
2253 |
amount => 2.00, |
2254 |
description => 'Hold expiration fee', |
2255 |
type => 'RESERVE_EXPIRED', |
2256 |
user_id => 1, |
2257 |
library_id => $patron->branchcode, |
2258 |
interface => 'intranet', |
2259 |
hold_id => $hold->id, |
2260 |
} |
2261 |
); |
2262 |
|
2263 |
# Add a credit (should not appear in debits) |
2264 |
my $credit = $account->add_credit( |
2265 |
{ |
2266 |
amount => 3.00, |
2267 |
description => 'Credit for hold', |
2268 |
type => 'CREDIT', |
2269 |
user_id => 1, |
2270 |
library_id => $patron->branchcode, |
2271 |
interface => 'intranet', |
2272 |
} |
2273 |
); |
2274 |
|
2275 |
# Link credit to hold manually since add_credit doesn't have hold_id parameter |
2276 |
$credit->reserve_id( $hold->id )->store; |
2277 |
|
2278 |
# Test 3: Hold debits returns only debit lines, ordered by timestamp descending |
2279 |
$debits = $hold->debits; |
2280 |
is( $debits->count, 2, 'debits() returns correct count of debit lines only' ); |
2281 |
|
2282 |
my @debit_amounts = sort { $b <=> $a } map { $_->amount + 0 } $debits->as_list; |
2283 |
is_deeply( \@debit_amounts, [ 5.0, 2.0 ], 'debits() returns correct amounts in descending order' ); |
2284 |
|
2285 |
# Test 4: Verify both debit types are present |
2286 |
my @debit_types = sort map { $_->debit_type_code } $debits->as_list; |
2287 |
is_deeply( \@debit_types, [ 'RESERVE', 'RESERVE_EXPIRED' ], 'debits() returns both expected debit types' ); |
2288 |
|
2289 |
$schema->storage->txn_rollback; |
2290 |
}; |
2291 |
|
2292 |
subtest '_move_to_old() account migration tests' => sub { |
2293 |
|
2294 |
plan tests => 6; |
2295 |
|
2296 |
$schema->storage->txn_begin; |
2297 |
|
2298 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
2299 |
my $biblio = $builder->build_sample_biblio; |
2300 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
2301 |
|
2302 |
# Create a hold |
2303 |
my $hold = $builder->build_object( |
2304 |
{ |
2305 |
class => 'Koha::Holds', |
2306 |
value => { |
2307 |
borrowernumber => $patron->borrowernumber, |
2308 |
biblionumber => $biblio->biblionumber, |
2309 |
itemnumber => $item->itemnumber, |
2310 |
branchcode => $patron->branchcode, |
2311 |
} |
2312 |
} |
2313 |
); |
2314 |
|
2315 |
my $account = $patron->account; |
2316 |
|
2317 |
# Create account lines linked to the hold |
2318 |
my $debit1 = $account->add_debit( |
2319 |
{ |
2320 |
amount => 5.00, |
2321 |
description => 'Hold fee', |
2322 |
type => 'RESERVE', |
2323 |
user_id => 1, |
2324 |
library_id => $patron->branchcode, |
2325 |
interface => 'intranet', |
2326 |
hold_id => $hold->id, |
2327 |
} |
2328 |
); |
2329 |
|
2330 |
my $debit2 = $account->add_debit( |
2331 |
{ |
2332 |
amount => 2.00, |
2333 |
description => 'Hold expiration fee', |
2334 |
type => 'RESERVE_EXPIRED', |
2335 |
user_id => 1, |
2336 |
library_id => $patron->branchcode, |
2337 |
interface => 'intranet', |
2338 |
hold_id => $hold->id, |
2339 |
} |
2340 |
); |
2341 |
|
2342 |
# Test initial state |
2343 |
is( $debit1->reserve_id, $hold->id, 'Account line initially has reserve_id set' ); |
2344 |
is( $debit1->old_reserve_id, undef, 'Account line initially has old_reserve_id unset' ); |
2345 |
is( $debit2->reserve_id, $hold->id, 'Second account line initially has reserve_id set' ); |
2346 |
|
2347 |
# Call _move_to_old |
2348 |
my $old_hold = $hold->_move_to_old; |
2349 |
|
2350 |
# Verify account lines were updated |
2351 |
$debit1->discard_changes; |
2352 |
$debit2->discard_changes; |
2353 |
|
2354 |
is( $debit1->reserve_id, undef, 'Account line reserve_id cleared after _move_to_old' ); |
2355 |
is( $debit1->old_reserve_id, $hold->id, 'Account line old_reserve_id set after _move_to_old' ); |
2356 |
is( $debit2->old_reserve_id, $hold->id, 'Second account line old_reserve_id set after _move_to_old' ); |
2357 |
|
2358 |
$schema->storage->txn_rollback; |
2359 |
}; |