Bug 13937: (QA follow-up) Make tests work with YAZ 4 and 5.
[koha.git] / t / db_dependent / Koha / Z3950Responder / GenericSession.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 3;
6 use Test::WWW::Mechanize;
7 use t::lib::Mocks qw(mock_preference);
8
9 use File::Basename;
10 use File::Copy;
11 use FindBin qw($Bin);
12 use XML::LibXML;
13 use YAML;
14 use ZOOM;
15
16 BEGIN {
17     use_ok('Koha::Z3950Responder');
18     use_ok('Koha::Z3950Responder::GenericSession');
19 }
20
21 our $child;
22
23 subtest 'test_search' => sub {
24
25     plan tests => 19;
26
27     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
28
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'),
39     );
40
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'),
51     );
52
53     my $yaml = new Test::MockModule('YAML');
54     $yaml->mock('LoadFile', sub {
55         return {
56             biblios => {
57                 use => {
58                     1 => 'author',
59                     4 => 'title',
60                     1003 => 'author'
61                 }
62             }
63         };
64     });
65
66     my $builder = new Test::MockModule('Koha::SearchEngine::Elasticsearch::QueryBuilder');
67     $builder->mock('build_query_compat', sub {
68         my ( $self, $operators, $operands ) = @_;
69
70         return (undef, $operands->[0]);
71     });
72
73     my $search = new Test::MockModule('Koha::SearchEngine::Elasticsearch::Search');
74     $search->mock('simple_search_compat', sub {
75         my ( $self, $query ) = @_;
76
77         return ('unexpected query', undef, 0) unless $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(another))))';
78
79         my @records = ($marc_record_1, $marc_record_2);
80         return (undef, \@records, 2);
81     });
82
83     $child = fork();
84     if ($child == 0) {
85         my $config_dir = $Bin . '/';
86         my $z = Koha::Z3950Responder->new( {
87             config_dir => $config_dir
88         });
89         $z->start();
90         exit;
91     }
92     sleep(1);
93
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');
99
100     my $Zconn = ZOOM::Connection->create($o);
101     ok($Zconn, 'ZOOM connection created');
102
103     $Zconn->connect('localhost:42111', 0);
104     is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
105
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());
108
109     is($rs->size(), 2, 'Two results returned');
110
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');
114
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');
118
119     # SRU protocol tests
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 );
124
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');
129
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');
134
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');
141
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');
145
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');
149
150     cleanup();
151 };
152
153 sub cleanup {
154     if ($child) {
155         kill 9, $child;
156         $child = undef;
157     }
158 }
159
160 # Fall back to make sure that the server process gets cleaned up
161 END {
162     cleanup();
163 }