3 # Copyright 2023 Rijksmuseum, Koha development team
5 # This file is part of Koha.
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.
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.
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>.
20 # Main object of this unit test is Z3950SearchAuth in C4::Breeding.
25 use Test::More tests => 2;
31 use t::lib::TestBuilder;
35 use Koha::Import::Records;
37 my $schema = Koha::Database->new->schema;
38 my $builder = t::lib::TestBuilder->new;
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}
48 // []; # result count per connection, sum should match with results array
49 $mock_data->{results} = $params->{results} // []; # arrayref of MARC blobs (or even records)
50 $mock_data->{template_params} = {}; # will catch results from param calls
54 $mocks->{record_object} = Test::MockObject->new;
55 $mocks->{record_object}->mock(
58 return $mock_data->{results}->[ $mock_data->{record_number}++ ];
62 $mocks->{result_object} = Test::MockObject->new;
63 $mocks->{result_object}->mock(
66 # Each size call means that we look at new connection results
67 return $mock_data->{result_counts}->[ $mock_data->{result_connection}++ ];
70 $mocks->{result_object}->mock( 'record', sub { $mocks->{record_object} } );
71 $mocks->{result_object}->mock( 'destroy', sub { } );
73 $mocks->{connection_object} = Test::MockObject->new;
74 $mocks->{connection_object}->mock( 'search', sub { $mocks->{result_object}; } );
75 $mocks->{connection_object}->mock( 'search_pqf', sub { $mocks->{result_object}; } );
76 $mocks->{connection_object}->mock( 'error_x', sub { } );
77 $mocks->{connection_object}->mock( 'last_event', sub { return ZOOM::Event::ZEND; } );
78 $mocks->{connection_object}->mock( 'destroy', sub { } );
80 $mocks->{Breeding} = Test::MockModule->new('C4::Breeding');
81 $mocks->{Breeding}->mock( '_create_connection', sub { return $mocks->{connection_object}; } );
83 $mocks->{ZOOM} = Test::MockModule->new('ZOOM');
84 $mocks->{ZOOM}->mock( 'event', sub { return $mock_data->{connection_count}++; } );
86 $mocks->{template_object} = Test::MockObject->new;
87 $mocks->{template_object}
88 ->mock( 'param', sub { shift; $mock_data->{template_params} = { %{ $mock_data->{template_params} }, @_ }; } );
91 $schema->storage->txn_begin;
93 subtest ImportBreedingAuth => sub {
96 my $record = MARC::Record->new();
97 $record->append_fields(
98 MARC::Field->new( '001', '4815162342' ),
99 MARC::Field->new( '100', ' ', ' ', a => 'Jansson, Tove' ),
102 my $breedingid = C4::Breeding::ImportBreedingAuth( $record, "kidclamp", "UTF-8", 'Jansson, Tove' );
103 ok( $breedingid, "We got a breeding id back" );
104 my $breedingid_1 = C4::Breeding::ImportBreedingAuth( $record, "kidclamp", "UTF-8", 'Jansson, Tove' );
105 is( $breedingid, $breedingid_1, "For the same record, we get the same id" );
106 $breedingid_1 = C4::Breeding::ImportBreedingAuth( $record, "marcelr", "UTF-8", 'Jansson, Tove' );
107 is( $breedingid, $breedingid_1, "For the same record in a different file, we get a new id" );
108 my $record_1 = MARC::Record->new();
109 $record_1->append_fields(
110 MARC::Field->new( '001', '8675309' ),
111 MARC::Field->new( '100', ' ', ' ', a => 'Cooper, Susan' ),
113 my $breedingid_2 = C4::Breeding::ImportBreedingAuth( $record_1, "kidclamp", "UTF-8", 'Cooper, Susan' );
114 isnt( $breedingid, $breedingid_2, "For a new record, we get a new id" );
117 subtest 'Z3950SearchAuth' => sub {
122 my $marc8_server = $builder->build(
124 source => 'Z3950server',
126 recordtype => 'authority', servertype => 'zed', host => 'marc8test', servername => 'MARC8 server',
127 syntax => 'USMARC', encoding => 'MARC-8', attributes => undef
131 my $utf8_server = $builder->build(
133 source => 'Z3950server',
135 recordtype => 'authority', servertype => 'zed', host => 'utf8test', servername => 'UTF8 server',
136 syntax => 'USMARC', encoding => 'utf8', attributes => undef
140 my $template = $mocks->{template_object};
142 # First test without any server
143 C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [] }, $template );
144 my $output = $mock_data->{template_params};
145 is_deeply( $output->{servers}, [], 'No servers' );
146 is_deeply( $output->{breeding_loop}, [], 'No data in breedingloop' );
148 # One auth server, but no results
149 init_mock_data( { result_counts => [0] } );
150 C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id} ] }, $template );
151 $output = $mock_data->{template_params};
152 is( $output->{servers}->[0]->{id}, $marc8_server->{id}, 'Server found' );
153 is_deeply( $output->{breeding_loop}, [], 'No data in breedingloop yet' );
155 # One auth server, one MARC8 record
156 my $marc8_record = MARC::Record->new;
157 $marc8_record->append_fields(
158 MARC::Field->new( '001', '1234' ),
159 MARC::Field->new( '100', ' ', ' ', a => 'Cooper, Susan' )
161 init_mock_data( { result_counts => [1], results => [ $marc8_record->as_usmarc ] } );
162 C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id} ] }, $template );
163 $output = $mock_data->{template_params};
164 is( @{ $output->{breeding_loop} }, 1, 'One result in breedingloop' );
165 is( $output->{breeding_loop}->[0]->{heading}, 'Cooper, Susan', 'Check heading' );
166 my $import_record = Koha::Import::Records->find( $output->{breeding_loop}->[0]->{breedingid} );
167 ok( $import_record, 'import record found' );
168 is( $import_record->_result->import_batch->file_name, 'marc8test', 'check file_name (read: host name)' );
170 # Two auth servers, one MARC8 and one UTF8 record per connection
171 my $utf8_record = MARC::Record->new;
172 $utf8_record->append_fields(
173 MARC::Field->new( '001', '2345' ),
174 MARC::Field->new( '110', ' ', ' ', a => '中国人 Company' )
176 $utf8_record->encoding('UTF-8');
177 init_mock_data( { result_counts => [ 1, 1 ], results => [ $marc8_record->as_usmarc, $utf8_record->as_usmarc ] } );
178 C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id}, $utf8_server->{id} ] }, $template );
179 $output = $mock_data->{template_params};
180 is( @{ $output->{servers} }, 2, 'Two servers' );
181 is( @{ $output->{breeding_loop} }, 2, 'Two results in breedingloop' );
182 is( $output->{breeding_loop}->[0]->{heading}, 'Cooper, Susan', 'Check heading result 1' );
183 ok( Koha::Import::Records->find( $output->{breeding_loop}->[0]->{breedingid} ), 'import record 1 found' );
184 is( $output->{breeding_loop}->[1]->{heading}, '中国人 Company', 'Check heading result 2' );
185 ok( Koha::Import::Records->find( $output->{breeding_loop}->[1]->{breedingid} ), 'import record 2 found' );
187 # One auth server, wrong encoding (utf8 from marc8 source)
188 init_mock_data( { result_counts => [1], results => [ $utf8_record->as_usmarc ] } );
189 warning_like { C4::Breeding::Z3950SearchAuth( { srchany => 'a', id => [ $marc8_server->{id} ] }, $template ); }
190 qr/Z3950SearchAuth conversion error.*MARC8 server.*\d+.*failed.*no mapping found for \[0x4E2D\]/,
191 'Dumped conversion error found';
194 $schema->storage->txn_rollback;