5 use Test
::More tests
=> 3;
6 use t
::lib
::Mocks
qw(mock_preference);
12 use_ok
('Koha::Z3950Responder');
13 use_ok
('Koha::Z3950Responder::GenericSession');
18 subtest
'test_search' => sub {
22 t
::lib
::Mocks
::mock_preference
('SearchEngine', 'Elasticsearch');
24 my $marc_record_1 = MARC
::Record
->new();
25 $marc_record_1->leader(' cam 22 a 4500');
26 $marc_record_1->append_fields(
27 MARC
::Field
->new('001', '123'),
28 MARC
::Field
->new('020', '', '', a
=> '1-56619-909-3'),
29 MARC
::Field
->new('100', '', '', a
=> 'Author 1'),
30 MARC
::Field
->new('110', '', '', a
=> 'Corp Author'),
31 MARC
::Field
->new('210', '', '', a
=> 'Title 1'),
32 MARC
::Field
->new('245', '', '', a
=> 'Title:', b
=> 'first record'),
33 MARC
::Field
->new('999', '', '', c
=> '1234567'),
36 my $marc_record_2 = MARC
::Record
->new();
37 $marc_record_2->leader(' cam 22 a 4500');
38 $marc_record_2->append_fields(
39 MARC
::Field
->new('001', '234'),
40 MARC
::Field
->new('020', '', '', a
=> '1-56619-909-3'),
41 MARC
::Field
->new('100', '', '', a
=> 'Author 2'),
42 MARC
::Field
->new('110', '', '', a
=> 'Corp Author'),
43 MARC
::Field
->new('210', '', '', a
=> 'Title 2'),
44 MARC
::Field
->new('245', '', '', a
=> 'Title:', b
=> 'second record'),
45 MARC
::Field
->new('999', '', '', c
=> '1234567'),
48 my $yaml = new Test
::MockModule
('YAML');
49 $yaml->mock('LoadFile', sub {
60 my $builder = new Test
::MockModule
('Koha::SearchEngine::Elasticsearch::QueryBuilder');
61 $builder->mock('build_query_compat', sub {
62 my ( $self, $operators, $operands ) = @_;
64 return (undef, $operands->[0]);
67 my $search = new Test
::MockModule
('Koha::SearchEngine::Elasticsearch::Search');
68 $search->mock('simple_search_compat', sub {
69 my ( $self, $query ) = @_;
71 return (1, undef, 0) unless $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(another))))';
73 my @records = ($marc_record_1, $marc_record_2);
74 return (undef, \
@records, 2);
79 my @yaz_options = ( '@:42111' );
80 my $z = Koha
::Z3950Responder
->new( {
82 yaz_options
=> [ @yaz_options ]
89 my $o = new ZOOM
::Options
();
90 $o->option(preferredRecordSyntax
=> 'xml');
91 $o->option(elementSetName
=> 'marcxml');
92 $o->option(databaseName
=> 'biblios');
94 my $Zconn = ZOOM
::Connection
->create($o);
95 ok
($Zconn, 'ZOOM connection created');
97 $Zconn->connect('127.0.0.1:42111', 0);
98 is
($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
100 my $rs = $Zconn->search_pqf('@and @attr 1=1 @attr 4=1 author @or @attr 1=4 title(s) @attr 1=4 another');
101 is
($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
103 is
($rs->size(), 2, 'Two results returned');
105 my $returned1 = MARC
::Record
->new_from_xml($rs->record(0)->raw());
106 ok
($returned1, 'Record 1 returned as MARCXML');
107 is
($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
109 my $returned2= MARC
::Record
->new_from_xml($rs->record(1)->raw());
110 ok
($returned2, 'Record 2 returned as MARCXML');
111 is
($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
113 is
($rs->record(2), undef, 'Record 3 does not exist');
125 # Fall back to make sure that the Zebra process
126 # and files get cleaned up