|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 6; |
| 21 |
use Test::Exception; |
| 22 |
use Test::Warn; |
| 23 |
|
| 24 |
use Koha::Database; |
| 25 |
use Koha::Account::Link; |
| 26 |
use Koha::Account::Links; |
| 27 |
|
| 28 |
use t::lib::TestBuilder; |
| 29 |
use t::lib::Mocks; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
$schema->storage->txn_begin; |
| 33 |
|
| 34 |
my $builder = t::lib::TestBuilder->new; |
| 35 |
|
| 36 |
subtest 'Basic object creation and validation' => sub { |
| 37 |
plan tests => 8; |
| 38 |
|
| 39 |
# Create test data |
| 40 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 41 |
my $biblio = $builder->build_sample_biblio(); |
| 42 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 43 |
|
| 44 |
# Create a hold |
| 45 |
my $hold = Koha::Hold->new( |
| 46 |
{ |
| 47 |
borrowernumber => $patron->borrowernumber, |
| 48 |
biblionumber => $biblio->biblionumber, |
| 49 |
itemnumber => $item->itemnumber, |
| 50 |
branchcode => $patron->branchcode, |
| 51 |
reservedate => '2023-01-01', |
| 52 |
priority => 1, |
| 53 |
} |
| 54 |
)->store; |
| 55 |
|
| 56 |
# Create an account line |
| 57 |
my $account_line = $patron->account->add_debit( |
| 58 |
{ |
| 59 |
amount => 5.00, |
| 60 |
description => 'Hold fee', |
| 61 |
type => 'RESERVE', |
| 62 |
interface => 'intranet' |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
# Test link creation |
| 67 |
my $link = Koha::Account::Link->new( |
| 68 |
{ |
| 69 |
accountlines_id => $account_line->id, |
| 70 |
link_type => 'hold', |
| 71 |
linked_id => $hold->id, |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
is( $link->accountlines_id, $account_line->id, 'Account line ID set correctly' ); |
| 76 |
is( $link->link_type, 'hold', 'Link type set correctly' ); |
| 77 |
is( $link->linked_id, $hold->id, 'Linked ID set correctly' ); |
| 78 |
|
| 79 |
# Test validation passes |
| 80 |
ok( $link->_validate_link_integrity(), 'Link validation passes for valid data' ); |
| 81 |
|
| 82 |
# Test store |
| 83 |
lives_ok { $link->store() } 'Link stores without error'; |
| 84 |
ok( $link->id, 'Link has ID after storing' ); |
| 85 |
|
| 86 |
# Test linked_object method |
| 87 |
my $linked_obj = $link->linked_object; |
| 88 |
isa_ok( $linked_obj, 'Koha::Schema::Result::Reserve', 'linked_object returns correct type' ); |
| 89 |
is( $linked_obj->reserve_id, $hold->id, 'linked_object returns correct hold' ); |
| 90 |
}; |
| 91 |
|
| 92 |
subtest 'Link validation' => sub { |
| 93 |
plan tests => 4; |
| 94 |
|
| 95 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 96 |
my $account_line = $patron->account->add_debit( |
| 97 |
{ |
| 98 |
amount => 3.00, |
| 99 |
description => 'Test fee', |
| 100 |
type => 'MANUAL', |
| 101 |
interface => 'intranet' |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
# Test invalid link type |
| 106 |
my $invalid_link = Koha::Account::Link->new( |
| 107 |
{ |
| 108 |
accountlines_id => $account_line->id, |
| 109 |
link_type => 'invalid_type', |
| 110 |
linked_id => 999, |
| 111 |
} |
| 112 |
); |
| 113 |
|
| 114 |
ok( !$invalid_link->_validate_link_integrity(), 'Validation fails for invalid link type' ); |
| 115 |
|
| 116 |
# Test nonexistent linked entity |
| 117 |
my $nonexistent_link = Koha::Account::Link->new( |
| 118 |
{ |
| 119 |
accountlines_id => $account_line->id, |
| 120 |
link_type => 'hold', |
| 121 |
linked_id => 99999, # Doesn't exist |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
ok( !$nonexistent_link->_validate_link_integrity(), 'Validation fails for nonexistent linked entity' ); |
| 126 |
|
| 127 |
# Test store with invalid data |
| 128 |
throws_ok { |
| 129 |
$invalid_link->store() |
| 130 |
} |
| 131 |
'Koha::Exceptions::Account::InvalidLinkType', 'Store throws InvalidLinkType exception for invalid link'; |
| 132 |
|
| 133 |
throws_ok { |
| 134 |
$nonexistent_link->store() |
| 135 |
} |
| 136 |
'Koha::Exceptions::Account::InvalidLinkTarget', 'Store throws InvalidLinkTarget exception for nonexistent entity'; |
| 137 |
}; |
| 138 |
|
| 139 |
subtest 'Linked Koha object access' => sub { |
| 140 |
plan tests => 3; |
| 141 |
|
| 142 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 143 |
my $biblio = $builder->build_sample_biblio(); |
| 144 |
my $hold = Koha::Hold->new( |
| 145 |
{ |
| 146 |
borrowernumber => $patron->borrowernumber, |
| 147 |
biblionumber => $biblio->biblionumber, |
| 148 |
branchcode => $patron->branchcode, |
| 149 |
reservedate => '2023-01-01', |
| 150 |
priority => 1, |
| 151 |
} |
| 152 |
)->store; |
| 153 |
|
| 154 |
my $account_line = $patron->account->add_debit( |
| 155 |
{ |
| 156 |
amount => 2.00, |
| 157 |
description => 'Hold fee', |
| 158 |
type => 'RESERVE', |
| 159 |
interface => 'intranet' |
| 160 |
} |
| 161 |
); |
| 162 |
|
| 163 |
my $link = Koha::Account::Link->new( |
| 164 |
{ |
| 165 |
accountlines_id => $account_line->id, |
| 166 |
link_type => 'hold', |
| 167 |
linked_id => $hold->id, |
| 168 |
} |
| 169 |
)->store; |
| 170 |
|
| 171 |
# Test linked_koha_object method |
| 172 |
my $koha_hold = $link->linked_koha_object; |
| 173 |
isa_ok( $koha_hold, 'Koha::Hold', 'linked_koha_object returns Koha::Hold object' ); |
| 174 |
is( $koha_hold->id, $hold->id, 'Returned object has correct ID' ); |
| 175 |
is( $koha_hold->borrowernumber, $patron->borrowernumber, 'Returned object has correct patron' ); |
| 176 |
}; |
| 177 |
|
| 178 |
subtest 'Account line integration' => sub { |
| 179 |
plan tests => 4; |
| 180 |
|
| 181 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 182 |
my $biblio = $builder->build_sample_biblio(); |
| 183 |
my $hold = Koha::Hold->new( |
| 184 |
{ |
| 185 |
borrowernumber => $patron->borrowernumber, |
| 186 |
biblionumber => $biblio->biblionumber, |
| 187 |
branchcode => $patron->branchcode, |
| 188 |
reservedate => '2023-01-01', |
| 189 |
priority => 1, |
| 190 |
} |
| 191 |
)->store; |
| 192 |
|
| 193 |
my $account_line = $patron->account->add_debit( |
| 194 |
{ |
| 195 |
amount => 1.50, |
| 196 |
description => 'Hold fee', |
| 197 |
type => 'RESERVE', |
| 198 |
interface => 'intranet' |
| 199 |
} |
| 200 |
); |
| 201 |
|
| 202 |
# Test add_link method |
| 203 |
my $link = $account_line->add_link( 'hold', $hold->id ); |
| 204 |
isa_ok( $link, 'Koha::Account::Link', 'add_link returns Link object' ); |
| 205 |
is( $link->accountlines_id, $account_line->id, 'Link has correct account line ID' ); |
| 206 |
|
| 207 |
# Test links method |
| 208 |
my $links = $account_line->links; |
| 209 |
isa_ok( $links, 'Koha::Account::Links', 'links returns Links collection' ); |
| 210 |
is( $links->count, 1, 'Account line has one link' ); |
| 211 |
}; |
| 212 |
|
| 213 |
subtest 'Hold integration' => sub { |
| 214 |
plan tests => 3; |
| 215 |
|
| 216 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 217 |
my $biblio = $builder->build_sample_biblio(); |
| 218 |
my $hold = Koha::Hold->new( |
| 219 |
{ |
| 220 |
borrowernumber => $patron->borrowernumber, |
| 221 |
biblionumber => $biblio->biblionumber, |
| 222 |
branchcode => $patron->branchcode, |
| 223 |
reservedate => '2023-01-01', |
| 224 |
priority => 1, |
| 225 |
} |
| 226 |
)->store; |
| 227 |
|
| 228 |
# Create multiple account lines for this hold |
| 229 |
my $fee1 = $patron->account->add_debit( |
| 230 |
{ |
| 231 |
amount => 2.00, |
| 232 |
description => 'Hold placement fee', |
| 233 |
type => 'RESERVE', |
| 234 |
interface => 'intranet' |
| 235 |
} |
| 236 |
); |
| 237 |
|
| 238 |
my $fee2 = $patron->account->add_debit( |
| 239 |
{ |
| 240 |
amount => 1.00, |
| 241 |
description => 'Hold collection fee', |
| 242 |
type => 'RESERVE_EXPIRED', |
| 243 |
interface => 'intranet' |
| 244 |
} |
| 245 |
); |
| 246 |
|
| 247 |
# Link both fees to the hold |
| 248 |
$fee1->add_link( 'hold', $hold->id ); |
| 249 |
$fee2->add_link( 'hold', $hold->id ); |
| 250 |
|
| 251 |
# Test hold can find its account lines |
| 252 |
my $debits = $hold->debits; |
| 253 |
isa_ok( $debits, 'Koha::Account::Debits', 'debits returns Debits collection' ); |
| 254 |
is( $debits->search( {} )->count, 2, 'Hold has two linked debit lines' ); |
| 255 |
|
| 256 |
my $total_fees = $debits->total_outstanding; |
| 257 |
is( $total_fees, 3.00, 'Total fees calculated correctly' ); |
| 258 |
}; |
| 259 |
|
| 260 |
subtest 'Unique constraint and duplicate prevention' => sub { |
| 261 |
plan tests => 2; |
| 262 |
|
| 263 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 264 |
my $biblio = $builder->build_sample_biblio(); |
| 265 |
my $hold = Koha::Hold->new( |
| 266 |
{ |
| 267 |
borrowernumber => $patron->borrowernumber, |
| 268 |
biblionumber => $biblio->biblionumber, |
| 269 |
branchcode => $patron->branchcode, |
| 270 |
reservedate => '2023-01-01', |
| 271 |
priority => 1, |
| 272 |
} |
| 273 |
)->store; |
| 274 |
|
| 275 |
my $account_line = $patron->account->add_debit( |
| 276 |
{ |
| 277 |
amount => 5.00, |
| 278 |
description => 'Hold fee', |
| 279 |
type => 'RESERVE', |
| 280 |
interface => 'intranet' |
| 281 |
} |
| 282 |
); |
| 283 |
|
| 284 |
# Create first link |
| 285 |
my $link1 = $account_line->add_link( 'hold', $hold->id ); |
| 286 |
ok( $link1->id, 'First link created successfully' ); |
| 287 |
|
| 288 |
# Try to create duplicate link |
| 289 |
throws_ok { |
| 290 |
$account_line->add_link( 'hold', $hold->id ) |
| 291 |
} |
| 292 |
qr/duplicate/i, 'Duplicate link creation fails with unique constraint error'; |
| 293 |
}; |
| 294 |
|
| 295 |
$schema->storage->txn_rollback; |