View | Details | Raw Unified | Return to bug 41020
Collapse All | Expand All

(-)a/Koha/MarcOrderAccount.pm (+21 lines)
Lines 52-57 sub budget { Link Here
52
    return Koha::Acquisition::Fund->_new_from_dbic($budget_rs);
52
    return Koha::Acquisition::Fund->_new_from_dbic($budget_rs);
53
}
53
}
54
54
55
=head3 file_transport
56
57
Returns the Koha::File::Transport::(S)FTP object for this account, if one is configured.
58
59
    my $transport = $account->file_transport;
60
61
=cut
62
63
sub file_transport {
64
    my ($self) = @_;
65
    my $transport_rs = $self->_result->file_transport;
66
    return unless $transport_rs;
67
68
    # Use Koha::File::Transports to determine the correct polymorphic class
69
    require Koha::File::Transports;
70
    my $transports = Koha::File::Transports->new;
71
    my $class      = $transports->object_class($transport_rs);
72
73
    return $class->_new_from_dbic($transport_rs);
74
}
75
55
=head3 _type
76
=head3 _type
56
77
57
=cut
78
=cut
(-)a/admin/marc_order_accounts.pl (-3 / +3 lines)
Lines 36-44 use Koha::MarcOrder; Link Here
36
use Koha::Acquisition::Booksellers;
36
use Koha::Acquisition::Booksellers;
37
use Koha::MarcOrderAccount;
37
use Koha::MarcOrderAccount;
38
use Koha::MarcOrderAccounts;
38
use Koha::MarcOrderAccounts;
39
use Koha::File::Transports;
39
40
40
my $input = CGI->new;
41
my $input = CGI->new;
41
our $schema = Koha::Database->new()->schema();
42
42
43
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44
    {
44
    {
Lines 62-71 if ( $op eq 'acct_form' ) { Link Here
62
    $template->param( available_matchers => \@matchers );
62
    $template->param( available_matchers => \@matchers );
63
63
64
    # Get available file transports for selection
64
    # Get available file transports for selection
65
    my @file_transports = $schema->resultset('FileTransport')->search(
65
    my @file_transports = Koha::File::Transports->search(
66
        {},
66
        {},
67
        { order_by => { -asc => 'name' } }
67
        { order_by => { -asc => 'name' } }
68
    );
68
    )->as_list;
69
    $template->param( file_transports => \@file_transports );
69
    $template->param( file_transports => \@file_transports );
70
70
71
    show_account( $input, $template );
71
    show_account( $input, $template );
(-)a/misc/cronjobs/marc_ordering_process.pl (-2 / +1 lines)
Lines 70-76 use File::Copy qw( copy move ); Link Here
70
use Koha::Script -cron;
70
use Koha::Script -cron;
71
use Koha::MarcOrder;
71
use Koha::MarcOrder;
72
use Koha::MarcOrderAccounts;
72
use Koha::MarcOrderAccounts;
73
use Koha::File::Transports;
74
73
75
use C4::Log qw( cronlogaction );
74
use C4::Log qw( cronlogaction );
76
75
Lines 112-118 foreach my $acct (@accounts) { Link Here
112
111
113
    my $working_dir = $acct->download_directory;
112
    my $working_dir = $acct->download_directory;
114
113
115
    my $file_transport = $acct->file_transport_id ? Koha::File::Transports->find( $acct->file_transport_id ) : undef;
114
    my $file_transport = $acct->file_transport;
116
    if ($file_transport) {
115
    if ($file_transport) {
117
        if ( $file_transport->connect() ) {
116
        if ( $file_transport->connect() ) {
118
            my $download_dir = $file_transport->download_directory;
117
            my $download_dir = $file_transport->download_directory;
(-)a/t/db_dependent/Koha/MarcOrderAccount.t (-1 / +263 lines)
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
};

Return to bug 41020