|
Line 0
Link Here
|
| 0 |
- |
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 => 4; |
| 21 |
|
| 22 |
use Koha::Database; |
| 23 |
use Koha::Account::Links; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
my $schema = Koha::Database->new->schema; |
| 28 |
$schema->storage->txn_begin; |
| 29 |
|
| 30 |
my $builder = t::lib::TestBuilder->new; |
| 31 |
|
| 32 |
subtest 'Basic collection functionality' => sub { |
| 33 |
plan tests => 3; |
| 34 |
|
| 35 |
$schema->storage->txn_begin; |
| 36 |
|
| 37 |
# Create test data |
| 38 |
my $patron1 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 39 |
my $patron2 = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 40 |
my $biblio = $builder->build_sample_biblio(); |
| 41 |
|
| 42 |
my $hold1 = Koha::Hold->new( |
| 43 |
{ |
| 44 |
borrowernumber => $patron1->borrowernumber, |
| 45 |
biblionumber => $biblio->biblionumber, |
| 46 |
branchcode => $patron1->branchcode, |
| 47 |
reservedate => '2023-01-01', |
| 48 |
priority => 1, |
| 49 |
} |
| 50 |
)->store; |
| 51 |
|
| 52 |
my $hold2 = Koha::Hold->new( |
| 53 |
{ |
| 54 |
borrowernumber => $patron2->borrowernumber, |
| 55 |
biblionumber => $biblio->biblionumber, |
| 56 |
branchcode => $patron2->branchcode, |
| 57 |
reservedate => '2023-01-02', |
| 58 |
priority => 2, |
| 59 |
} |
| 60 |
)->store; |
| 61 |
|
| 62 |
# Create account lines and links |
| 63 |
my $fee1 = $patron1->account->add_debit( |
| 64 |
{ |
| 65 |
amount => 2.00, |
| 66 |
description => 'Hold fee 1', |
| 67 |
type => 'RESERVE', |
| 68 |
interface => 'intranet' |
| 69 |
} |
| 70 |
); |
| 71 |
|
| 72 |
my $fee2 = $patron2->account->add_debit( |
| 73 |
{ |
| 74 |
amount => 3.00, |
| 75 |
description => 'Hold fee 2', |
| 76 |
type => 'RESERVE', |
| 77 |
interface => 'intranet' |
| 78 |
} |
| 79 |
); |
| 80 |
|
| 81 |
$fee1->add_link( 'hold', $hold1->id ); |
| 82 |
$fee2->add_link( 'hold', $hold2->id ); |
| 83 |
|
| 84 |
# Test collection methods |
| 85 |
my $all_links = Koha::Account::Links->search( {} ); |
| 86 |
is( $all_links->count, 2, 'Found both links in collection' ); |
| 87 |
|
| 88 |
my $hold_links = $all_links->by_link_type('hold'); |
| 89 |
is( $hold_links->count, 2, 'by_link_type filters correctly' ); |
| 90 |
|
| 91 |
my $specific_links = $all_links->by_linked_id( $hold1->id ); |
| 92 |
is( $specific_links->count, 1, 'by_linked_id filters correctly' ); |
| 93 |
|
| 94 |
$schema->storage->txn_rollback; |
| 95 |
}; |
| 96 |
|
| 97 |
subtest 'Filtering methods' => sub { |
| 98 |
plan tests => 4; |
| 99 |
|
| 100 |
$schema->storage->txn_begin; |
| 101 |
|
| 102 |
# Create diverse test data |
| 103 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 104 |
my $biblio = $builder->build_sample_biblio(); |
| 105 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 106 |
|
| 107 |
# Create hold |
| 108 |
my $hold = Koha::Hold->new( |
| 109 |
{ |
| 110 |
borrowernumber => $patron->borrowernumber, |
| 111 |
biblionumber => $biblio->biblionumber, |
| 112 |
itemnumber => $item->itemnumber, |
| 113 |
branchcode => $patron->branchcode, |
| 114 |
reservedate => '2023-01-01', |
| 115 |
priority => 1, |
| 116 |
} |
| 117 |
)->store; |
| 118 |
|
| 119 |
# Create checkout |
| 120 |
my $checkout = Koha::Checkout->new( |
| 121 |
{ |
| 122 |
borrowernumber => $patron->borrowernumber, |
| 123 |
itemnumber => $item->itemnumber, |
| 124 |
issuedate => '2023-01-01', |
| 125 |
date_due => '2023-01-15', |
| 126 |
branchcode => $patron->branchcode, |
| 127 |
} |
| 128 |
)->store; |
| 129 |
|
| 130 |
# Create different types of fees |
| 131 |
my $hold_fee = $patron->account->add_debit( |
| 132 |
{ |
| 133 |
amount => 2.00, |
| 134 |
description => 'Hold fee', |
| 135 |
type => 'RESERVE', |
| 136 |
interface => 'intranet' |
| 137 |
} |
| 138 |
); |
| 139 |
|
| 140 |
my $overdue_fee = $patron->account->add_debit( |
| 141 |
{ |
| 142 |
amount => 5.00, |
| 143 |
description => 'Overdue fee', |
| 144 |
type => 'OVERDUE', |
| 145 |
interface => 'intranet' |
| 146 |
} |
| 147 |
); |
| 148 |
|
| 149 |
my $manual_fee = $patron->account->add_debit( |
| 150 |
{ |
| 151 |
amount => 1.00, |
| 152 |
description => 'Manual fee', |
| 153 |
type => 'MANUAL', |
| 154 |
interface => 'intranet' |
| 155 |
} |
| 156 |
); |
| 157 |
|
| 158 |
# Create links |
| 159 |
$hold_fee->add_link( 'hold', $hold->id ); |
| 160 |
$overdue_fee->add_link( 'checkout', $checkout->id ); |
| 161 |
|
| 162 |
# Manual fee has no link |
| 163 |
|
| 164 |
my $all_links = Koha::Account::Links->search( {} ); |
| 165 |
is( $all_links->count, 2, 'Total links created correctly' ); |
| 166 |
|
| 167 |
# Test filtering by type |
| 168 |
my $hold_links = $all_links->by_link_type('hold'); |
| 169 |
is( $hold_links->count, 1, 'Hold links filtered correctly' ); |
| 170 |
|
| 171 |
my $checkout_links = $all_links->by_link_type('checkout'); |
| 172 |
is( $checkout_links->count, 1, 'Checkout links filtered correctly' ); |
| 173 |
|
| 174 |
# Test filtering by ID |
| 175 |
my $hold_id_links = $all_links->by_linked_id( $hold->id ); |
| 176 |
is( $hold_id_links->count, 1, 'Links filtered by ID correctly' ); |
| 177 |
|
| 178 |
$schema->storage->txn_rollback; |
| 179 |
}; |
| 180 |
|
| 181 |
subtest 'Collection methods for related objects' => sub { |
| 182 |
plan tests => 4; |
| 183 |
|
| 184 |
$schema->storage->txn_begin; |
| 185 |
|
| 186 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 187 |
my $biblio1 = $builder->build_sample_biblio(); |
| 188 |
my $biblio2 = $builder->build_sample_biblio(); |
| 189 |
|
| 190 |
# Create multiple holds |
| 191 |
my $hold1 = Koha::Hold->new( |
| 192 |
{ |
| 193 |
borrowernumber => $patron->borrowernumber, |
| 194 |
biblionumber => $biblio1->biblionumber, |
| 195 |
branchcode => $patron->branchcode, |
| 196 |
reservedate => '2023-01-01', |
| 197 |
priority => 1, |
| 198 |
} |
| 199 |
)->store; |
| 200 |
|
| 201 |
my $hold2 = Koha::Hold->new( |
| 202 |
{ |
| 203 |
borrowernumber => $patron->borrowernumber, |
| 204 |
biblionumber => $biblio2->biblionumber, |
| 205 |
branchcode => $patron->branchcode, |
| 206 |
reservedate => '2023-01-02', |
| 207 |
priority => 1, |
| 208 |
} |
| 209 |
)->store; |
| 210 |
|
| 211 |
# Create fees for both holds |
| 212 |
my $fee1 = $patron->account->add_debit( |
| 213 |
{ |
| 214 |
amount => 2.00, |
| 215 |
description => 'Hold fee 1', |
| 216 |
type => 'RESERVE', |
| 217 |
interface => 'intranet' |
| 218 |
} |
| 219 |
); |
| 220 |
|
| 221 |
my $fee2 = $patron->account->add_debit( |
| 222 |
{ |
| 223 |
amount => 3.00, |
| 224 |
description => 'Hold fee 2', |
| 225 |
type => 'RESERVE', |
| 226 |
interface => 'intranet' |
| 227 |
} |
| 228 |
); |
| 229 |
|
| 230 |
# Link fees to holds |
| 231 |
$fee1->add_link( 'hold', $hold1->id ); |
| 232 |
$fee2->add_link( 'hold', $hold2->id ); |
| 233 |
|
| 234 |
# Test holds collection method |
| 235 |
my $links = Koha::Account::Links->search( {} ); |
| 236 |
my @holds = $links->holds(); |
| 237 |
|
| 238 |
is( scalar @holds, 2, 'holds() method returns correct number of holds' ); |
| 239 |
|
| 240 |
my @hold_ids = sort map { $_->id } @holds; |
| 241 |
my @expected_ids = sort ( $hold1->id, $hold2->id ); |
| 242 |
|
| 243 |
is_deeply( \@hold_ids, \@expected_ids, 'holds() method returns correct holds' ); |
| 244 |
|
| 245 |
# Test checkouts collection method (should be empty in this test) |
| 246 |
my @checkouts = $links->checkouts(); |
| 247 |
is( scalar @checkouts, 0, 'checkouts() method returns empty array when no checkouts linked' ); |
| 248 |
|
| 249 |
# Test filtering and then getting related objects |
| 250 |
my $hold1_links = $links->by_linked_id( $hold1->id ); |
| 251 |
my @filtered_holds = $hold1_links->holds(); |
| 252 |
is( scalar @filtered_holds, 1, 'Filtered links return correct number of holds' ); |
| 253 |
|
| 254 |
$schema->storage->txn_rollback; |
| 255 |
}; |
| 256 |
|
| 257 |
subtest 'Integration with account line methods' => sub { |
| 258 |
plan tests => 6; |
| 259 |
|
| 260 |
$schema->storage->txn_begin; |
| 261 |
|
| 262 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 263 |
my $biblio = $builder->build_sample_biblio(); |
| 264 |
my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } ); |
| 265 |
|
| 266 |
# Create hold and checkout |
| 267 |
my $hold = Koha::Hold->new( |
| 268 |
{ |
| 269 |
borrowernumber => $patron->borrowernumber, |
| 270 |
biblionumber => $biblio->biblionumber, |
| 271 |
itemnumber => $item->itemnumber, |
| 272 |
branchcode => $patron->branchcode, |
| 273 |
reservedate => '2023-01-01', |
| 274 |
priority => 1, |
| 275 |
} |
| 276 |
)->store; |
| 277 |
|
| 278 |
my $checkout = Koha::Checkout->new( |
| 279 |
{ |
| 280 |
borrowernumber => $patron->borrowernumber, |
| 281 |
itemnumber => $item->itemnumber, |
| 282 |
issuedate => '2023-01-01', |
| 283 |
date_due => '2023-01-15', |
| 284 |
branchcode => $patron->branchcode, |
| 285 |
} |
| 286 |
)->store; |
| 287 |
|
| 288 |
# Create account line |
| 289 |
my $account_line = $patron->account->add_debit( |
| 290 |
{ |
| 291 |
amount => 4.00, |
| 292 |
description => 'Mixed fee', |
| 293 |
type => 'MANUAL', |
| 294 |
interface => 'intranet' |
| 295 |
} |
| 296 |
); |
| 297 |
|
| 298 |
# Link to both hold and checkout (edge case) |
| 299 |
$account_line->add_link( 'hold', $hold->id ); |
| 300 |
$account_line->add_link( 'checkout', $checkout->id ); |
| 301 |
|
| 302 |
# Test account line can access all its links |
| 303 |
my $links = $account_line->links; |
| 304 |
is( $links->count, 2, 'Account line has both links' ); |
| 305 |
|
| 306 |
# Test account line can get specific types |
| 307 |
my @holds = $account_line->holds; |
| 308 |
is( scalar @holds, 1, 'Account line holds() method works' ); |
| 309 |
is( $holds[0]->id, $hold->id, 'Correct hold returned' ); |
| 310 |
|
| 311 |
my @checkouts = $account_line->checkouts; |
| 312 |
is( scalar @checkouts, 1, 'Account line checkouts() method works' ); |
| 313 |
is( $checkouts[0]->id, $checkout->id, 'Correct checkout returned' ); |
| 314 |
|
| 315 |
# Test hold can find its account lines |
| 316 |
my $hold_fees = $hold->debits; |
| 317 |
is( $hold_fees->count, 1, 'Hold can find its account lines' ); |
| 318 |
|
| 319 |
$schema->storage->txn_rollback; |
| 320 |
}; |
| 321 |
|
| 322 |
$schema->storage->txn_rollback; |