|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright 2025 Koha Development team |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 5; |
| 23 |
use Test::NoWarnings; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
use Koha::Database; |
| 28 |
use Koha::MarcOrderAccounts; |
| 29 |
|
| 30 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
| 33 |
subtest 'vendor() relationship' => sub { |
| 34 |
plan tests => 2; |
| 35 |
|
| 36 |
$schema->storage->txn_begin; |
| 37 |
|
| 38 |
my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } ); |
| 39 |
my $account = $builder->build_object( |
| 40 |
{ |
| 41 |
class => 'Koha::MarcOrderAccounts', |
| 42 |
value => { vendor_id => $vendor->id } |
| 43 |
} |
| 44 |
); |
| 45 |
|
| 46 |
is( |
| 47 |
ref( $account->vendor ), |
| 48 |
'Koha::Acquisition::Bookseller', |
| 49 |
'->vendor should return a Koha::Acquisition::Bookseller object' |
| 50 |
); |
| 51 |
|
| 52 |
is( |
| 53 |
$account->vendor->id, |
| 54 |
$vendor->id, |
| 55 |
'Vendor relationship returns the correct vendor' |
| 56 |
); |
| 57 |
|
| 58 |
$schema->storage->txn_rollback; |
| 59 |
}; |
| 60 |
|
| 61 |
subtest 'budget() relationship' => sub { |
| 62 |
plan tests => 2; |
| 63 |
|
| 64 |
$schema->storage->txn_begin; |
| 65 |
|
| 66 |
my $budget = $builder->build_object( { class => 'Koha::Acquisition::Funds' } ); |
| 67 |
my $account = $builder->build_object( |
| 68 |
{ |
| 69 |
class => 'Koha::MarcOrderAccounts', |
| 70 |
value => { budget_id => $budget->budget_id } |
| 71 |
} |
| 72 |
); |
| 73 |
|
| 74 |
is( |
| 75 |
ref( $account->budget ), |
| 76 |
'Koha::Acquisition::Fund', |
| 77 |
'->budget should return a Koha::Acquisition::Fund object' |
| 78 |
); |
| 79 |
|
| 80 |
is( |
| 81 |
$account->budget->budget_id, |
| 82 |
$budget->budget_id, |
| 83 |
'Budget relationship returns the correct budget' |
| 84 |
); |
| 85 |
|
| 86 |
$schema->storage->txn_rollback; |
| 87 |
}; |
| 88 |
|
| 89 |
subtest 'file_transport() relationship' => sub { |
| 90 |
plan tests => 8; |
| 91 |
|
| 92 |
$schema->storage->txn_begin; |
| 93 |
|
| 94 |
# Test with no file transport (NULL foreign key) |
| 95 |
my $account_no_transport = $builder->build_object( |
| 96 |
{ |
| 97 |
class => 'Koha::MarcOrderAccounts', |
| 98 |
value => { file_transport_id => undef } |
| 99 |
} |
| 100 |
); |
| 101 |
|
| 102 |
is( |
| 103 |
$account_no_transport->file_transport, |
| 104 |
undef, |
| 105 |
'->file_transport should return undef when no transport is configured' |
| 106 |
); |
| 107 |
|
| 108 |
# Test with SFTP transport |
| 109 |
my $sftp_transport = $builder->build_object( |
| 110 |
{ |
| 111 |
class => 'Koha::File::Transports', |
| 112 |
value => { |
| 113 |
transport => 'sftp', |
| 114 |
name => 'Test SFTP', |
| 115 |
host => 'test.example.com', |
| 116 |
} |
| 117 |
} |
| 118 |
); |
| 119 |
|
| 120 |
my $account_sftp = $builder->build_object( |
| 121 |
{ |
| 122 |
class => 'Koha::MarcOrderAccounts', |
| 123 |
value => { file_transport_id => $sftp_transport->id } |
| 124 |
} |
| 125 |
); |
| 126 |
|
| 127 |
my $transport = $account_sftp->file_transport; |
| 128 |
|
| 129 |
is( |
| 130 |
ref($transport), |
| 131 |
'Koha::File::Transport::SFTP', |
| 132 |
'->file_transport should return polymorphic SFTP object for SFTP transport' |
| 133 |
); |
| 134 |
|
| 135 |
is( |
| 136 |
$transport->id, |
| 137 |
$sftp_transport->id, |
| 138 |
'Transport relationship returns the correct transport ID' |
| 139 |
); |
| 140 |
|
| 141 |
can_ok( $transport, 'connect' ); |
| 142 |
|
| 143 |
# Test with FTP transport |
| 144 |
my $ftp_transport = $builder->build_object( |
| 145 |
{ |
| 146 |
class => 'Koha::File::Transports', |
| 147 |
value => { |
| 148 |
transport => 'ftp', |
| 149 |
name => 'Test FTP', |
| 150 |
host => 'ftp.example.com', |
| 151 |
} |
| 152 |
} |
| 153 |
); |
| 154 |
|
| 155 |
my $account_ftp = $builder->build_object( |
| 156 |
{ |
| 157 |
class => 'Koha::MarcOrderAccounts', |
| 158 |
value => { file_transport_id => $ftp_transport->id } |
| 159 |
} |
| 160 |
); |
| 161 |
|
| 162 |
my $ftp = $account_ftp->file_transport; |
| 163 |
|
| 164 |
is( |
| 165 |
ref($ftp), |
| 166 |
'Koha::File::Transport::FTP', |
| 167 |
'->file_transport should return polymorphic FTP object for FTP transport' |
| 168 |
); |
| 169 |
|
| 170 |
# Test with Local transport |
| 171 |
my $local_transport = $builder->build_object( |
| 172 |
{ |
| 173 |
class => 'Koha::File::Transports', |
| 174 |
value => { |
| 175 |
transport => 'local', |
| 176 |
name => 'Test Local', |
| 177 |
host => 'localhost', |
| 178 |
} |
| 179 |
} |
| 180 |
); |
| 181 |
|
| 182 |
my $account_local = $builder->build_object( |
| 183 |
{ |
| 184 |
class => 'Koha::MarcOrderAccounts', |
| 185 |
value => { file_transport_id => $local_transport->id } |
| 186 |
} |
| 187 |
); |
| 188 |
|
| 189 |
my $local = $account_local->file_transport; |
| 190 |
|
| 191 |
is( |
| 192 |
ref($local), |
| 193 |
'Koha::File::Transport::Local', |
| 194 |
'->file_transport should return polymorphic Local object for local transport' |
| 195 |
); |
| 196 |
|
| 197 |
can_ok( $local, 'download_file' ); |
| 198 |
can_ok( $local, 'delete_file' ); |
| 199 |
|
| 200 |
$schema->storage->txn_rollback; |
| 201 |
}; |
| 202 |
|
| 203 |
subtest 'file_transport() respects prefetched relationships' => sub { |
| 204 |
plan tests => 4; |
| 205 |
|
| 206 |
$schema->storage->txn_begin; |
| 207 |
|
| 208 |
# Create a transport and account |
| 209 |
my $sftp_transport = $builder->build_object( |
| 210 |
{ |
| 211 |
class => 'Koha::File::Transports', |
| 212 |
value => { |
| 213 |
transport => 'sftp', |
| 214 |
name => 'Test SFTP Prefetch', |
| 215 |
host => 'prefetch.example.com', |
| 216 |
} |
| 217 |
} |
| 218 |
); |
| 219 |
|
| 220 |
my $account = $builder->build_object( |
| 221 |
{ |
| 222 |
class => 'Koha::MarcOrderAccounts', |
| 223 |
value => { file_transport_id => $sftp_transport->id } |
| 224 |
} |
| 225 |
); |
| 226 |
|
| 227 |
# Search with prefetch to load the relationship |
| 228 |
my $account_with_prefetch = Koha::MarcOrderAccounts->search( |
| 229 |
{ id => $account->id }, |
| 230 |
{ prefetch => 'file_transport' } |
| 231 |
)->next; |
| 232 |
|
| 233 |
# Enable query logging to count queries |
| 234 |
my @queries; |
| 235 |
my $original_debug = $schema->storage->debug; |
| 236 |
$schema->storage->debugcb( |
| 237 |
sub { |
| 238 |
my ( $op, $info ) = @_; |
| 239 |
push @queries, $info if $op eq 'SELECT'; |
| 240 |
} |
| 241 |
); |
| 242 |
$schema->storage->debug(1); |
| 243 |
|
| 244 |
# Access the prefetched relationship |
| 245 |
my $transport = $account_with_prefetch->file_transport; |
| 246 |
|
| 247 |
# Disable query logging |
| 248 |
$schema->storage->debug($original_debug); |
| 249 |
|
| 250 |
is( scalar @queries, 0, 'No additional queries when accessing prefetched file_transport' ); |
| 251 |
|
| 252 |
is( |
| 253 |
ref($transport), |
| 254 |
'Koha::File::Transport::SFTP', |
| 255 |
'Prefetched transport returns correct polymorphic class' |
| 256 |
); |
| 257 |
|
| 258 |
is( $transport->id, $sftp_transport->id, 'Prefetched transport has correct ID' ); |
| 259 |
|
| 260 |
is( $transport->name, 'Test SFTP Prefetch', 'Prefetched transport has correct data' ); |
| 261 |
|
| 262 |
$schema->storage->txn_rollback; |
| 263 |
}; |