|
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} |
| 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 |
| 51 |
} |
| 52 |
|
| 53 |
sub mock_objects { |
| 54 |
$mocks->{record_object} = Test::MockObject->new; |
| 55 |
$mocks->{record_object}->mock( |
| 56 |
'raw', |
| 57 |
sub { |
| 58 |
return $mock_data->{results}->[ $mock_data->{record_number}++ ]; |
| 59 |
} |
| 60 |
); |
| 61 |
|
| 62 |
$mocks->{result_object} = Test::MockObject->new; |
| 63 |
$mocks->{result_object}->mock( |
| 64 |
'size', |
| 65 |
sub { |
| 66 |
# Each size call means that we look at new connection results |
| 67 |
return $mock_data->{result_counts}->[ $mock_data->{result_connection}++ ]; |
| 68 |
} |
| 69 |
); |
| 70 |
$mocks->{result_object}->mock( 'record', sub { $mocks->{record_object} } ); |
| 71 |
$mocks->{result_object}->mock( 'destroy', sub { } ); |
| 72 |
|
| 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 { } ); |
| 79 |
|
| 80 |
$mocks->{Breeding} = Test::MockModule->new('C4::Breeding'); |
| 81 |
$mocks->{Breeding}->mock( '_create_connection', sub { return $mocks->{connection_object}; } ); |
| 82 |
|
| 83 |
$mocks->{ZOOM} = Test::MockModule->new('ZOOM'); |
| 84 |
$mocks->{ZOOM}->mock( 'event', sub { return $mock_data->{connection_count}++; } ); |
| 85 |
|
| 86 |
$mocks->{template_object} = Test::MockObject->new; |
| 87 |
$mocks->{template_object} |
| 88 |
->mock( 'param', sub { shift; $mock_data->{template_params} = { %{ $mock_data->{template_params} }, @_ }; } ); |
| 89 |
} |
| 90 |
|
| 91 |
$schema->storage->txn_begin; |
| 92 |
|
| 93 |
subtest ImportBreedingAuth => sub { |
| 94 |
plan tests => 4; |
| 95 |
|
| 96 |
my $record = MARC::Record->new(); |
| 97 |
$record->append_fields( |
| 98 |
MARC::Field->new( '001', '4815162342' ), |
| 99 |
MARC::Field->new( '100', ' ', ' ', a => 'Jansson, Tove' ), |
| 100 |
); |
| 101 |
|
| 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' ), |
| 112 |
); |
| 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" ); |
| 115 |
}; |
| 116 |
|
| 117 |
subtest 'Z3950SearchAuth' => sub { |
| 118 |
plan tests => 15; |
| 119 |
|
| 120 |
init_mock_data(); |
| 121 |
mock_objects(); |
| 122 |
my $marc8_server = $builder->build( |
| 123 |
{ |
| 124 |
source => 'Z3950server', |
| 125 |
value => { |
| 126 |
recordtype => 'authority', servertype => 'zed', host => 'marc8test', servername => 'MARC8 server', |
| 127 |
syntax => 'USMARC', encoding => 'MARC-8', attributes => undef |
| 128 |
}, |
| 129 |
} |
| 130 |
); |
| 131 |
my $utf8_server = $builder->build( |
| 132 |
{ |
| 133 |
source => 'Z3950server', |
| 134 |
value => { |
| 135 |
recordtype => 'authority', servertype => 'zed', host => 'utf8test', servername => 'UTF8 server', |
| 136 |
syntax => 'USMARC', encoding => 'utf8', attributes => undef |
| 137 |
}, |
| 138 |
} |
| 139 |
); |
| 140 |
my $template = $mocks->{template_object}; |
| 141 |
|
| 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' ); |
| 147 |
|
| 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' ); |
| 154 |
|
| 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' ) |
| 160 |
); |
| 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)' ); |
| 169 |
|
| 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' ) |
| 175 |
); |
| 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' ); |
| 186 |
|
| 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'; |
| 192 |
}; |
| 193 |
|
| 194 |
$schema->storage->txn_rollback; |