5 use Test
::More tests
=> 3;
7 use t
::lib
::Mocks
qw(mock_preference);
13 use_ok
('Koha::Z3950Responder');
14 use_ok
('Koha::Z3950Responder::ZebraSession');
19 subtest
'test_search' => sub {
23 t
::lib
::Mocks
::mock_preference
('SearchEngine', 'Zebra');
25 my $marc_record_1 = MARC
::Record
->new();
26 $marc_record_1->leader(' cam 22 a 4500');
27 $marc_record_1->append_fields(
28 MARC
::Field
->new('001', '123'),
29 MARC
::Field
->new('020', '', '', a
=> '1-56619-909-3'),
30 MARC
::Field
->new('100', '', '', a
=> 'Author 1'),
31 MARC
::Field
->new('110', '', '', a
=> 'Corp Author'),
32 MARC
::Field
->new('210', '', '', a
=> 'Title 1'),
33 MARC
::Field
->new('245', '', '', a
=> 'Title:', b
=> 'first record'),
34 MARC
::Field
->new('999', '', '', c
=> '1234567'),
37 my $marc_record_2 = MARC
::Record
->new();
38 $marc_record_2->leader(' cam 22 a 4500');
39 $marc_record_2->append_fields(
40 MARC
::Field
->new('001', '234'),
41 MARC
::Field
->new('020', '', '', a
=> '1-56619-909-3'),
42 MARC
::Field
->new('100', '', '', a
=> 'Author 2'),
43 MARC
::Field
->new('110', '', '', a
=> 'Corp Author'),
44 MARC
::Field
->new('210', '', '', a
=> 'Title 2'),
45 MARC
::Field
->new('245', '', '', a
=> 'Title:', b
=> 'second record'),
46 MARC
::Field
->new('999', '', '', c
=> '1234567'),
49 my $context = new Test
::MockModule
('C4::Context');
50 $context->mock('Zconn', sub {
51 my $Zconn = new Test
::MockObject
();
52 $Zconn->mock('connect', sub {});
53 $Zconn->mock('err_code', sub {
56 $Zconn->mock('search_pqf', sub {
57 my $results = new Test
::MockObject
();
58 $results->mock('size', sub {
61 $results->mock('record_immediate', sub {
62 my ($self, $index) = @_;
66 $record = $marc_record_1;
67 } elsif ($index == 1) {
68 $record = $marc_record_2;
70 my $Zrecord = new Test
::MockObject
();
71 $Zrecord->mock('raw', sub {
72 return $record->as_xml();
76 $results->mock('records', sub {});
77 $results->mock('destroy', sub {});
83 my @yaz_options = ( '@:42111' );
84 my $z = Koha
::Z3950Responder
->new( {
86 yaz_options
=> [ @yaz_options ]
93 my $o = new ZOOM
::Options
();
94 $o->option(preferredRecordSyntax
=> 'xml');
95 $o->option(elementSetName
=> 'marcxml');
96 $o->option(databaseName
=> 'biblios');
98 my $Zconn = ZOOM
::Connection
->create($o);
99 ok
($Zconn, 'ZOOM connection created');
101 $Zconn->connect('127.0.0.1:42111', 0);
102 is
($Zconn->errcode(), 0, 'Connection is successful: ' . $Zconn->errmsg());
104 my $rs = $Zconn->search_pqf('@and @attr 1=1 author @attr 1=4 title');
105 is
($Zconn->errcode(), 0, 'Search is successful: ' . $Zconn->errmsg());
107 is
($rs->size(), 2, 'Two results returned');
109 my $returned1 = MARC
::Record
->new_from_xml($rs->record(0)->raw());
110 ok
($returned1, 'Record 1 returned as MARCXML');
111 is
($returned1->as_xml, $marc_record_1->as_xml, 'Record 1 returned properly');
113 my $returned2= MARC
::Record
->new_from_xml($rs->record(1)->raw());
114 ok
($returned2, 'Record 2 returned as MARCXML');
115 is
($returned2->as_xml, $marc_record_2->as_xml, 'Record 2 returned properly');
127 # Fall back to make sure that the Zebra process
128 # and files get cleaned up