Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2023 Rijksmuseum, Koha development team |
4 |
# |
5 |
# This file is part of Koha. |
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 <http://www.gnu.org/licenses>. |
19 |
|
20 |
# Main object of this unit test is Z3950SearchAuth in C4::Breeding. |
21 |
|
22 |
use Modern::Perl; |
23 |
use utf8; |
24 |
|
25 |
use Test::More tests => 2; |
26 |
use Test::MockModule; |
27 |
use Test::MockObject; |
28 |
use Test::Warn; |
29 |
use ZOOM; |
30 |
|
31 |
use t::lib::TestBuilder; |
32 |
|
33 |
use C4::Breeding; |
34 |
use Koha::Database; |
35 |
use Koha::Import::Records; |
36 |
|
37 |
my $schema = Koha::Database->new->schema; |
38 |
my $builder = t::lib::TestBuilder->new; |
39 |
my $mocks = {}; |
40 |
my $mock_data = {}; |
41 |
|
42 |
sub init_mock_data { |
43 |
my $params = shift; |
44 |
$mock_data->{connection_count} = 1; # for event loop |
45 |
$mock_data->{record_number} = 0; # record pointer in result loop |
46 |
$mock_data->{result_connection} = 0; # current connection in result loop |
47 |
$mock_data->{result_counts} = $params->{result_counts} // []; # result count per connection, sum should match with results array |
48 |
$mock_data->{results} = $params->{results} // []; # arrayref of MARC blobs (or even records) |
49 |
$mock_data->{template_params} = {}; # will catch results from param calls |
50 |
} |
51 |
|
52 |
sub mock_objects { |
53 |
$mocks->{record_object} = Test::MockObject->new; |
54 |
$mocks->{record_object}->mock( 'raw', sub { |
55 |
return $mock_data->{results}->[ $mock_data->{record_number}++ ]; |
56 |
}); |
57 |
|
58 |
$mocks->{result_object} = Test::MockObject->new; |
59 |
$mocks->{result_object}->mock( 'size', sub { |
60 |
# Each size call means that we look at new connection results |
61 |
return $mock_data->{result_counts}->[ $mock_data->{result_connection}++ ]; |
62 |
}); |
63 |
$mocks->{result_object}->mock( 'record', sub { $mocks->{record_object} } ); |
64 |
$mocks->{result_object}->mock( 'destroy', sub {} ); |
65 |
|
66 |
$mocks->{connection_object} = Test::MockObject->new; |
67 |
$mocks->{connection_object}->mock( 'search', sub { $mocks->{result_object}; } ); |
68 |
$mocks->{connection_object}->mock( 'search_pqf', sub { $mocks->{result_object}; } ); |
69 |
$mocks->{connection_object}->mock( 'error_x', sub {} ); |
70 |
$mocks->{connection_object}->mock( 'last_event', sub { return ZOOM::Event::ZEND; } ); |
71 |
$mocks->{connection_object}->mock( 'destroy', sub {} ); |
72 |
|
73 |
$mocks->{Breeding} = Test::MockModule->new( 'C4::Breeding' ); |
74 |
$mocks->{Breeding}->mock( '_create_connection', sub { return $mocks->{connection_object}; } ); |
75 |
|
76 |
$mocks->{ZOOM} = Test::MockModule->new( 'ZOOM' ); |
77 |
$mocks->{ZOOM}->mock( 'event', sub { return $mock_data->{connection_count}++; } ); |
78 |
|
79 |
$mocks->{template_object} = Test::MockObject->new; |
80 |
$mocks->{template_object}->mock( 'param', sub { shift; $mock_data->{template_params} = { %{$mock_data->{template_params}}, @_ }; } ); |
81 |
} |
82 |
|
83 |
$schema->storage->txn_begin; |
84 |
|
85 |
subtest ImportBreedingAuth => sub { |
86 |
plan tests => 4; |
87 |
|
88 |
my $record = MARC::Record->new(); |
89 |
$record->append_fields( |
90 |
MARC::Field->new('001', '4815162342'), |
91 |
MARC::Field->new('100', ' ', ' ', a => 'Jansson, Tove'), |
92 |
); |
93 |
|
94 |
my $breedingid = C4::Breeding::ImportBreedingAuth($record,"kidclamp","UTF-8",'Jansson, Tove' ); |
95 |
ok( $breedingid, "We got a breeding id back"); |
96 |
my $breedingid_1 = C4::Breeding::ImportBreedingAuth($record,"kidclamp","UTF-8",'Jansson, Tove' ); |
97 |
is( $breedingid, $breedingid_1, "For the same record, we get the same id"); |
98 |
$breedingid_1 = C4::Breeding::ImportBreedingAuth($record,"marcelr","UTF-8",'Jansson, Tove' ); |
99 |
is( $breedingid, $breedingid_1, "For the same record in a different file, we get a new id"); |
100 |
my $record_1 = MARC::Record->new(); |
101 |
$record_1->append_fields( |
102 |
MARC::Field->new('001', '8675309'), |
103 |
MARC::Field->new('100', ' ', ' ', a => 'Cooper, Susan'), |
104 |
); |
105 |
my $breedingid_2 = C4::Breeding::ImportBreedingAuth($record_1,"kidclamp","UTF-8",'Cooper, Susan' ); |
106 |
isnt( $breedingid, $breedingid_2, "For a new record, we get a new id"); |
107 |
}; |
108 |
|
109 |
subtest 'Z3950SearchAuth' => sub { |
110 |
plan tests => 15; |
111 |
|
112 |
init_mock_data(); |
113 |
mock_objects(); |
114 |
my $marc8_server = $builder->build({ source => 'Z3950server', |
115 |
value => { recordtype => 'authority', servertype => 'zed', host => 'marc8test', servername => 'MARC8 server', |
116 |
syntax => 'USMARC', encoding => 'MARC-8', attributes => undef }, |
117 |
}); |
118 |
my $utf8_server = $builder->build({ source => 'Z3950server', |
119 |
value => { recordtype => 'authority', servertype => 'zed', host => 'utf8test', servername => 'UTF8 server', |
120 |
syntax => 'USMARC', encoding => 'utf8', attributes => undef }, |
121 |
}); |
122 |
my $template = $mocks->{template_object}; |
123 |
|
124 |
# First test without any server |
125 |
C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [] }, $template ); |
126 |
my $output = $mock_data->{template_params}; |
127 |
is_deeply( $output->{servers}, [], 'No servers' ); |
128 |
is_deeply( $output->{breeding_loop}, [], 'No data in breedingloop' ); |
129 |
|
130 |
# One auth server, but no results |
131 |
init_mock_data({ result_counts => [ 0 ] }); |
132 |
C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id} ] }, $template ); |
133 |
$output = $mock_data->{template_params}; |
134 |
is( $output->{servers}->[0]->{id}, $marc8_server->{id}, 'Server found' ); |
135 |
is_deeply( $output->{breeding_loop}, [], 'No data in breedingloop yet' ); |
136 |
|
137 |
# One auth server, one MARC8 record |
138 |
my $marc8_record = MARC::Record->new; |
139 |
$marc8_record->append_fields( MARC::Field->new('001', '1234'), MARC::Field->new('100', ' ', ' ', a => 'Cooper, Susan') ); |
140 |
init_mock_data({ result_counts => [ 1 ], results => [ $marc8_record->as_usmarc ] }); |
141 |
C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id} ] }, $template ); |
142 |
$output = $mock_data->{template_params}; |
143 |
is( @{$output->{breeding_loop}}, 1, 'One result in breedingloop' ); |
144 |
is( $output->{breeding_loop}->[0]->{heading}, 'Cooper, Susan', 'Check heading' ); |
145 |
my $import_record = Koha::Import::Records->find( $output->{breeding_loop}->[0]->{breedingid} ); |
146 |
ok( $import_record, 'import record found' ); |
147 |
is( $import_record->_result->import_batch->file_name, 'marc8test', 'check file_name (read: host name)' ); |
148 |
|
149 |
# Two auth servers, one MARC8 and one UTF8 record per connection |
150 |
my $utf8_record = MARC::Record->new; |
151 |
$utf8_record->append_fields( MARC::Field->new('001', '2345'), MARC::Field->new('110', ' ', ' ', a => '中国人 Company') ); |
152 |
$utf8_record->encoding('UTF-8'); |
153 |
init_mock_data({ result_counts => [1, 1], results => [ $marc8_record->as_usmarc, $utf8_record->as_usmarc ] }); |
154 |
C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id}, $utf8_server->{id} ] }, $template ); |
155 |
$output = $mock_data->{template_params}; |
156 |
is( @{$output->{servers}}, 2, 'Two servers' ); |
157 |
is( @{$output->{breeding_loop}}, 2, 'Two results in breedingloop' ); |
158 |
is( $output->{breeding_loop}->[0]->{heading}, 'Cooper, Susan', 'Check heading result 1' ); |
159 |
ok( Koha::Import::Records->find( $output->{breeding_loop}->[0]->{breedingid} ), 'import record 1 found' ); |
160 |
is( $output->{breeding_loop}->[1]->{heading}, '中国人 Company', 'Check heading result 2' ); |
161 |
ok( Koha::Import::Records->find( $output->{breeding_loop}->[1]->{breedingid} ), 'import record 2 found' ); |
162 |
|
163 |
# One auth server, wrong encoding (utf8 from marc8 source) |
164 |
init_mock_data({ result_counts => [ 1 ], results => [ $utf8_record->as_usmarc ] }); |
165 |
warning_like { C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id} ] }, $template ); } |
166 |
qr/Z3950SearchAuth conversion error.*MARC8 server.*\d+.*failed.*no mapping found for \[0x4E2D\]/, 'Dumped conversion error found'; |
167 |
}; |
168 |
|
169 |
$schema->storage->txn_rollback; |