Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Koha / Z3950Responder / GenericSession.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use utf8;
5
6 use Test::More tests => 3;
7 use Test::WWW::Mechanize;
8 use t::lib::Mocks qw(mock_preference);
9
10 use File::Basename;
11 use File::Copy;
12 use FindBin qw($Bin);
13 use XML::LibXML;
14 use YAML;
15 use ZOOM;
16
17 BEGIN {
18     use_ok('Koha::Z3950Responder');
19     use_ok('Koha::Z3950Responder::GenericSession');
20 }
21
22 our $child;
23
24 subtest 'test_search' => sub {
25
26     plan tests => 20;
27
28     t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
29
30     my $marc_record_1 = MARC::Record->new();
31     $marc_record_1->leader('     cam  22      a 4500');
32     $marc_record_1->append_fields(
33         MARC::Field->new('001', '123'),
34         MARC::Field->new('020', '', '', a => '1-56619-909-3'),
35         MARC::Field->new('100', '', '', a => 'Author 1'),
36         MARC::Field->new('110', '', '', a => 'Corp Author'),
37         MARC::Field->new('210', '', '', a => 'Title 1'),
38         MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
39         MARC::Field->new('999', '', '', c => '1234567'),
40     );
41
42     my $marc_record_2 = MARC::Record->new();
43     $marc_record_2->leader('     cam  22      a 4500');
44     $marc_record_2->append_fields(
45         MARC::Field->new('001', '234'),
46         MARC::Field->new('020', '', '', a => '1-56619-909-3'),
47         MARC::Field->new('100', '', '', a => 'Author 2'),
48         MARC::Field->new('110', '', '', a => 'Corp Author'),
49         MARC::Field->new('210', '', '', a => 'Title 2'),
50         MARC::Field->new('245', '', '', a => 'Title:', b => 'second record'),
51         MARC::Field->new('999', '', '', c => '1234567'),
52     );
53
54     my $yaml = new Test::MockModule('YAML');
55     $yaml->mock('LoadFile', sub {
56         return {
57             biblios => {
58                 use => {
59                     1 => 'author',
60                     4 => 'title',
61                     1003 => 'author'
62                 }
63             }
64         };
65     });
66
67     my $builder = new Test::MockModule('Koha::SearchEngine::Elasticsearch::QueryBuilder');
68     $builder->mock('build_query_compat', sub {
69         my ( $self, $operators, $operands ) = @_;
70
71         return (undef, $operands->[0]);
72     });
73
74     my $search = new Test::MockModule('Koha::SearchEngine::Elasticsearch::Search');
75     $search->mock('simple_search_compat', sub {
76         my ( $self, $query ) = @_;
77
78         return ('unexpected query', undef, 0) unless $query eq '((author:(author)) AND ((title:(title\(s\))) OR (title:(speciäl))))' || $query eq "(simple search)";
79
80         my @records = ($marc_record_1, $marc_record_2);
81         return (undef, \@records, 2);
82     });
83
84     $child = fork();
85     if ($child == 0) {
86         my $config_dir = $Bin . '/';
87         my $z = Koha::Z3950Responder->new( {
88             config_dir => $config_dir
89         });
90         $z->start();
91         exit;
92     }
93     sleep(10); # Just a try to see if it fixes Jenkins
94
95     # Z39.50 protocol tests
96     my $o = new ZOOM::Options();
97     $o->option(preferredRecordSyntax => 'xml');
98     $o->option(elementSetName => 'marcxml');
99     $o->option(databaseName => 'biblios');
100
101     my $Zconn = ZOOM::Connection->create($o);
102     ok($Zconn, 'ZOOM connection created');
103
104     $Zconn->connect('localhost:42111', 0);
105     is($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
106
107     my $rs = $Zconn->search_pqf('@and @attr 1=1 @attr 4=1 author @or @attr 1=4 title(s) @attr 1=4 speciäl');
108     is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
109
110     is($rs->size(), 2, 'Two results returned');
111
112     my $returned1 = MARC::Record->new_from_xml($rs->record(0)->raw(), 'UTF-8');
113     ok($returned1, 'Record 1 returned as MARCXML');
114     is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
115
116     my $returned2= MARC::Record->new_from_xml($rs->record(1)->raw(), 'UTF-8');
117     ok($returned2, 'Record 2 returned as MARCXML');
118     is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
119
120     $rs = $Zconn->search_pqf('"simple search"');
121     is($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
122
123     # SRU protocol tests
124     my $base = 'http://localhost:42111';
125     my $ns = 'http://www.loc.gov/zing/srw/';
126     my $marc_ns = 'http://www.loc.gov/MARC21/slim';
127     my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
128
129     $agent->get_ok("$base?version=1.1", 'Retrieve explain response');
130     my $dom = XML::LibXML->load_xml(string => $agent->content());
131     my @nodes = $dom->getElementsByTagNameNS($ns, 'explainResponse');
132     is(scalar(@nodes), 1, 'explainResponse returned');
133
134     $agent->get_ok("$base/biblios?operation=searchRetrieve&recordSchema=marcxml&version=1.1&maximumRecords=10&query=", 'Try bad search query');
135     $dom = XML::LibXML->load_xml(string => $agent->content());
136     @nodes = $dom->getElementsByTagNameNS($ns, 'diagnostics');
137     is(scalar(@nodes), 1, 'diagnostics returned for bad query');
138
139     $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%3dspeciäl))", 'Retrieve search results');
140     $dom = XML::LibXML->load_xml(string => $agent->content());
141     @nodes = $dom->getElementsByTagNameNS($ns, 'searchRetrieveResponse');
142     is(scalar(@nodes), 1, 'searchRetrieveResponse returned');
143     my @records = $nodes[0]->getElementsByTagNameNS($marc_ns, 'record');
144     is(scalar(@records), 2, 'Two results returned');
145
146     $returned1 = MARC::Record->new_from_xml($records[0]->toString(), 'UTF-8');
147     ok($returned1, 'Record 1 returned as MARCXML');
148     is($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
149
150     $returned2= MARC::Record->new_from_xml($records[1]->toString(), 'UTF-8');
151     ok($returned2, 'Record 2 returned as MARCXML');
152     is($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
153
154     cleanup();
155 };
156
157 sub cleanup {
158     if ($child) {
159         kill 9, $child;
160         $child = undef;
161     }
162 }
163
164 # Fall back to make sure that the server process gets cleaned up
165 END {
166     cleanup();
167 }