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 |
use Test::NoWarnings; |
22 |
|
23 |
use Koha::Database; |
24 |
use Koha::File::Transports; |
25 |
|
26 |
use t::lib::TestBuilder; |
27 |
|
28 |
my $schema = Koha::Database->new->schema; |
29 |
my $builder = t::lib::TestBuilder->new; |
30 |
|
31 |
subtest 'Polymorphic object creation' => sub { |
32 |
plan tests => 4; |
33 |
|
34 |
$schema->storage->txn_begin; |
35 |
|
36 |
# Test SFTP transport polymorphism |
37 |
my $sftp_transport = $builder->build_object( |
38 |
{ |
39 |
class => 'Koha::File::Transports', |
40 |
value => { |
41 |
transport => 'sftp', |
42 |
name => 'Test SFTP', |
43 |
host => 'test.example.com', |
44 |
} |
45 |
} |
46 |
); |
47 |
|
48 |
is( |
49 |
ref($sftp_transport), 'Koha::File::Transport::SFTP', |
50 |
'SFTP transport should be polymorphic Koha::File::Transport::SFTP object' |
51 |
); |
52 |
|
53 |
can_ok( $sftp_transport, '_write_key_file' ); |
54 |
|
55 |
# Test FTP transport polymorphism |
56 |
my $ftp_transport = $builder->build_object( |
57 |
{ |
58 |
class => 'Koha::File::Transports', |
59 |
value => { |
60 |
transport => 'ftp', |
61 |
name => 'Test FTP', |
62 |
host => 'ftp.example.com', |
63 |
} |
64 |
} |
65 |
); |
66 |
|
67 |
is( |
68 |
ref($ftp_transport), 'Koha::File::Transport::FTP', |
69 |
'FTP transport should be polymorphic Koha::File::Transport::FTP object' |
70 |
); |
71 |
|
72 |
can_ok( $ftp_transport, 'connect' ); |
73 |
|
74 |
$schema->storage->txn_rollback; |
75 |
}; |
76 |
|
77 |
subtest 'search() tests' => sub { |
78 |
plan tests => 3; |
79 |
|
80 |
$schema->storage->txn_begin; |
81 |
|
82 |
# Create test transports |
83 |
my $sftp_transport = $builder->build_object( |
84 |
{ |
85 |
class => 'Koha::File::Transports', |
86 |
value => { transport => 'sftp', name => 'Test SFTP' } |
87 |
} |
88 |
); |
89 |
my $ftp_transport = $builder->build_object( |
90 |
{ |
91 |
class => 'Koha::File::Transports', |
92 |
value => { transport => 'ftp', name => 'Test FTP' } |
93 |
} |
94 |
); |
95 |
|
96 |
# Search for both transports |
97 |
my $transports = Koha::File::Transports->search( { id => { -in => [ $sftp_transport->id, $ftp_transport->id ] } } ); |
98 |
|
99 |
is( |
100 |
ref($transports), 'Koha::File::Transports', |
101 |
'search() should return Koha::File::Transports object' |
102 |
); |
103 |
|
104 |
# Check each object type from resultset |
105 |
my @objects = $transports->as_list; |
106 |
my %refs = map { $_->transport => ref($_) } @objects; |
107 |
|
108 |
is( |
109 |
$refs{sftp}, 'Koha::File::Transport::SFTP', |
110 |
'SFTP object from search should be polymorphic' |
111 |
); |
112 |
is( |
113 |
$refs{ftp}, 'Koha::File::Transport::FTP', |
114 |
'FTP object from search should be polymorphic' |
115 |
); |
116 |
|
117 |
$schema->storage->txn_rollback; |
118 |
}; |
119 |
|
120 |
subtest 'find() tests' => sub { |
121 |
plan tests => 2; |
122 |
|
123 |
$schema->storage->txn_begin; |
124 |
|
125 |
# Create test transports |
126 |
my $sftp_transport = $builder->build_object( |
127 |
{ |
128 |
class => 'Koha::File::Transports', |
129 |
value => { transport => 'sftp', name => 'Test SFTP' } |
130 |
} |
131 |
); |
132 |
my $ftp_transport = $builder->build_object( |
133 |
{ |
134 |
class => 'Koha::File::Transports', |
135 |
value => { transport => 'ftp', name => 'Test FTP' } |
136 |
} |
137 |
); |
138 |
|
139 |
# Test find() returns correct polymorphic types |
140 |
my $found_sftp = Koha::File::Transports->find( $sftp_transport->id ); |
141 |
my $found_ftp = Koha::File::Transports->find( $ftp_transport->id ); |
142 |
|
143 |
is( |
144 |
ref($found_sftp), 'Koha::File::Transport::SFTP', |
145 |
'find() should return polymorphic SFTP object' |
146 |
); |
147 |
is( |
148 |
ref($found_ftp), 'Koha::File::Transport::FTP', |
149 |
'find() should return polymorphic FTP object' |
150 |
); |
151 |
|
152 |
$schema->storage->txn_rollback; |
153 |
}; |