5 use Test::More tests => 3;
6 use Test::WWW::Mechanize;
7 use t::lib::Mocks qw(mock_preference);
17 use_ok('Koha::Z3950Responder');
18 use_ok('Koha::Z3950Responder::GenericSession');
23 subtest 'test_search' => sub {
27 t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
29 my $marc_record_1 = MARC::Record->new();
30 $marc_record_1->leader(' cam 22 a 4500');
31 $marc_record_1->append_fields(
32 MARC::Field->new('001', '123'),
33 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
34 MARC::Field->new('100', '', '', a => 'Author 1'),
35 MARC::Field->new('110', '', '', a => 'Corp Author'),
36 MARC::Field->new('210', '', '', a => 'Title 1'),
37 MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
38 MARC::Field->new('999', '', '', c => '1234567'),
41 my $marc_record_2 = MARC::Record->new();
42 $marc_record_2->leader(' cam 22 a 4500');
43 $marc_record_2->append_fields(
44 MARC::Field->new('001', '234'),
45 MARC::Field->new('020', '', '', a => '1-56619-909-3'),
46 MARC::Field->new('100', '', '', a => 'Author 2'),
47 MARC::Field->new('110', '', '', a => 'Corp Author'),
48 MARC::Field->new('210', '', '', a => 'Title 2'),
49 MARC::Field->new('245', '', '', a => 'Title:', b => 'second record'),
50 MARC::Field->new('999', '', '', c => '1234567'),
53 my $yaml = new Test::MockModule('YAML');
54 $yaml->mock('LoadFile', sub {
66 my $builder = new Test::MockModule('Koha::SearchEngine::Elasticsearch::QueryBuilder');
67 $builder->mock('build_query_compat', sub {
68 my ( $self, $operators, $operands ) = @_;
70 return (undef, $operands->[0]);
73 my $search = new Test::MockModule('Koha::SearchEngine::Elasticsearch::Search');
74 $search->mock('simple_search_compat', sub {
75 my ( $self, $query ) = @_;
77 return ('unexpected query', undef, 0) unless $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(another))))';
79 my @records = ($marc_record_1, $marc_record_2);
80 return (undef, \@records, 2);
85 my $config_dir = $Bin . '/';
86 my $z = Koha::Z3950Responder->new( {
87 config_dir => $config_dir
94 # Z39.50 protocol tests
95 my $o = new ZOOM::Options();
96 $o->option(preferredRecordSyntax => 'xml');
97 $o->option(elementSetName => 'marcxml');
98 $o->option(databaseName => 'biblios');
100 my $Zconn = ZOOM::Connection->create($o);
101 ok($Zconn, 'ZOOM connection created');
103 $Zconn->connect('localhost:42111', 0);
104 is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
106 my $rs = $Zconn->search_pqf('@and @attr 1=1 @attr 4=1 author @or @attr 1=4 title(s) @attr 1=4 another');
107 is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
109 is($rs->size(), 2, 'Two results returned');
111 my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw());
112 ok($returned1, 'Record 1 returned as MARCXML');
113 is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
115 my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw());
116 ok($returned2, 'Record 2 returned as MARCXML');
117 is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
120 my $base = 'http://localhost:42111';
121 my $ns = 'http://www.loc.gov/zing/srw/';
122 my $marc_ns = 'http://www.loc.gov/MARC21/slim';
123 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
125 $agent->get_ok("$base?version=1.1", 'Retrieve explain response');
126 my $dom = XML::LibXML->load_xml(string => $agent->content());
127 my @nodes = $dom->getElementsByTagNameNS($ns, 'explainResponse');
128 is(scalar(@nodes), 1, 'explainResponse returned');
130 $agent->get_ok("$base/biblios?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=", 'Try bad search query');
131 $dom = XML::LibXML->load_xml(string => $agent->content());
132 @nodes = $dom->getElementsByTagNameNS($ns, 'diagnostics');
133 is(scalar(@nodes), 1, 'diagnostics returned for bad query');
135 $agent->get_ok("$base/biblios?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=(dc.author%3dauthor AND (dc.title%3d\"title(s)\" OR dc.title%3danother))", 'Retrieve search results');
136 $dom = XML::LibXML->load_xml(string => $agent->content());
137 @nodes = $dom->getElementsByTagNameNS($ns, 'searchRetrieveResponse');
138 is(scalar(@nodes), 1, 'searchRetrieveResponse returned');
139 my @records = $nodes[0]->getElementsByTagNameNS($marc_ns, 'record');
140 is(scalar(@records), 2, 'Two results returned');
142 $returned1 = MARC::Record->new_from_xml($records[0]->toString());
143 ok($returned1, 'Record 1 returned as MARCXML');
144 is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
146 $returned2= MARC::Record->new_from_xml($records[1]->toString());
147 ok($returned2, 'Record 2 returned as MARCXML');
148 is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
160 # Fall back to make sure that the server process gets cleaned up