Bug 20997: Add unit tests for Koha::Account::Line::apply
[koha.git] / t / db_dependent / Breeding.t
1 #!/usr/bin/perl
2
3 # Copyright 2014 Rijksmuseum
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use FindBin;
23 use Test::More tests => 3;
24 use Test::Warn;
25
26 use C4::Breeding;
27 use Koha::XSLT_Handler;
28
29 #Main object of this unit test is the Breeding module and its subroutines
30 #A start has been made to define tests for subroutines of Z3950Search.
31 #These subroutines are actually internal, but these tests may pave the way for
32 #a more comprehensive test of Z3950Search itself.
33 #TODO
34 #Furthermore, we need additional tests for:
35 #Z3950SearchAuth, BreedingSearch, ImportBreedingAuth
36
37 #Group 1: testing _build_query and _translate_query (part of Z3950Search)
38 subtest '_build_query' => sub {
39     plan tests => 14;
40     test_build_translate_query();
41 };
42 #Group 2: testing _create_connection (part of Z3950Search)
43 subtest '_create_connection' => sub {
44     plan tests => 5;
45     test_create_connection();
46 };
47 #Group 3: testing _do_xslt_proc (part of Z3950Search)
48 subtest '_do_xslt_proc' => sub {
49     plan tests => 6;
50     test_do_xslt();
51 };
52
53 #-------------------------------------------------------------------------------
54
55 sub test_build_translate_query {
56     my $str;
57     #First pass no parameters
58     my @queries= C4::Breeding::_build_query( {} );
59     is( defined $queries[0] && $queries[0] eq '' && defined $queries[1] &&
60         $queries[1] eq '', 1, '_build_query gets no parameters');
61
62     #We now pass one parameter
63     my $pars1= { isbn => '234567' };
64     @queries= C4::Breeding::_build_query( $pars1 );
65     #Passed only one par: zquery should start with @attr 1=\d+
66     is( $queries[0] =~ /^\@attr 1=\d+/, 1, 'Z39.50 query with one parameter');
67     $str=$pars1->{isbn};
68     #Find back ISBN?
69     is( $queries[0] =~ /$str/, 1, 'First Z39.50 query contains ISBN');
70     #SRU query should contain translation for ISBN
71     my $server= { sru_fields => 'isbn=ie-es-bee-en,srchany=overal' };
72     my $squery= C4::Breeding::_translate_query( $server, $queries[1] );
73     is( $squery =~ /ie-es-bee-en/, 1, 'SRU query has translated ISBN index');
74     #Another try with fallback to any
75     $server= { sru_fields => 'srchany=overal' };
76     $squery= C4::Breeding::_translate_query( $server, $queries[1] );
77     is( $squery =~ /overal/, 1, 'SRU query fallback to translated any');
78     #Another try even without any
79     $server= { sru_fields => 'this,is,bad,input' };
80     $squery= C4::Breeding::_translate_query( $server, $queries[1] );
81     is( $squery =~ /$str/ && $squery !~ /=/, 1, 'SRU query without indexes');
82
83     #We now pass two parameters
84     my $pars2= { isbn => '123456', title => 'You should read this.' };
85     @queries= C4::Breeding::_build_query( $pars2 );
86     #The Z39.50 query should start with @and (we passed two pars)
87     is( $queries[0] =~ /^\@and/, 1, 'Second Z39.50 query starts with @and');
88     #We should also find two @attr 1=\d+
89     my @matches= $queries[0] =~ /\@attr 1=\d+/g;
90     is( @matches == 2, 1, 'Second Z39.50 query includes two @attr 1=');
91     #We should find text of both parameters in the query
92     $str= $pars2->{isbn};
93     is( $queries[0] =~ /\"$str\"/, 1,
94         'Second query contains ISBN enclosed by double quotes');
95     $str= $pars2->{title};
96     is( $queries[0] =~ /\"$str\"/, 1,
97         'Second query contains title enclosed by double quotes');
98
99     #SRU revisited
100     $server= { sru_fields => 'isbn=nb,title=dc.title,srchany=overal' };
101     $squery= C4::Breeding::_translate_query( $server, $queries[1] );
102     is ( $squery =~ /dc.title/ && $squery =~ / and / &&
103         $squery =~ /nb=/, 1, 'SRU query with two parameters');
104
105     #We now pass a third wrong parameter (should not make a difference)
106     my $pars3= { isbn => '123456', title => 'You should read this.', xyz => 1 };
107     my @queries2= C4::Breeding::_build_query( $pars3 );
108     is( $queries[0] eq $queries2[0] && $queries[1] eq $queries2[1], 1,
109         'Third query makes no difference');
110
111     # Check that indexes with equal signs are ok
112     $server = { sru_fields => 'subjectsubdiv=aut.type=ram_pe and aut.accesspoint' };
113     my $pars4 = { subjectsubdiv => 'mysubjectsubdiv' };
114     @queries = C4::Breeding::_auth_build_query( $pars4 );
115     my $zquery = C4::Breeding::_translate_query( $server, $queries[1] );
116     is ( $zquery, 'aut.type=ram_pe and aut.accesspoint="mysubjectsubdiv"', 'SRU query with equal sign in index');
117
118     # Check that indexes with double-quotes are ok
119     $server = { sru_fields => 'subject=(aut.type any "geo ram_nc ram_ge ram_pe ram_co") and aut.accesspoint' };
120     my $pars5 = { subject => 'mysubject' };
121     @queries = C4::Breeding::_auth_build_query( $pars5 );
122     $zquery = C4::Breeding::_translate_query( $server, $queries[1] );
123     is ( $zquery, '(aut.type any "geo ram_nc ram_ge ram_pe ram_co") and aut.accesspoint="mysubject"', 'SRU query with double quotes in index');
124 }
125
126 sub test_create_connection {
127     #TODO This is just a *simple* start
128
129     my $str;
130     my $server= { servertype => 'zed', db => 'MyDatabase',
131         host => 'really-not-a-domain-i-hope.nl', port => 80,
132     };
133     my $obj= C4::Breeding::_create_connection( $server );
134
135     #We should get back an object, even if it did not connect
136     is( ref $obj eq 'ZOOM::Connection', 1, 'Got back a ZOOM connection');
137
138     #Remember: it is async
139     my $i= ZOOM::event( [ $obj ] );
140     if( $i == 1 ) {
141         #We could examine ZOOM::event_str( $obj->last_event )
142         #For now we are satisfied with an error message
143         #Probably: Connect failed
144         is( ($obj->errmsg//'') ne '', 1, 'Connection failed as expected');
145
146     } else {
147         ok( 1, 'No ZOOM event found: skipped errmsg' );
148     }
149
150     #Checking the databaseName for Z39.50 server
151     $str=$obj->option('databaseName')//'';
152     is( $str eq $server->{db}, 1, 'Check ZOOM option for database');
153
154     #Another test for SRU
155     $obj->destroy();
156     $server->{ servertype } = 'sru';
157     $server->{ sru_options } =  'just_testing=fun';
158     $obj= C4::Breeding::_create_connection( $server );
159     #In this case we expect no databaseName, but we expect just_testing
160     $str=$obj->option('databaseName');
161     is( $str, undef, 'No databaseName for SRU connection');
162     $str=$obj->option('just_testing')//'';
163     is( $str eq 'fun', 1, 'Additional ZOOM option for SRU found');
164     $obj->destroy();
165 }
166
167 sub test_do_xslt {
168     my $biblio = MARC::Record->new();
169     $biblio->append_fields(
170         MARC::Field->new('100', ' ', ' ', a => 'John Writer'),
171         MARC::Field->new('245', ' ', ' ', a => 'Just a title'),
172     );
173     my $file= $FindBin::Bin.'/XSLT_Handler/test01.xsl';
174     my $server= { add_xslt => $file };
175     my $engine=Koha::XSLT_Handler->new;
176
177     #ready for the main test
178     my @res = C4::Breeding::_do_xslt_proc( $biblio, $server, $engine );
179     is( $res[1], undef, 'No error returned' );
180     is( ref $res[0], 'MARC::Record', 'Got back MARC record');
181     is( $res[0]->subfield('990','a'), 'I saw you', 'Found 990a in the record');
182
183     #forcing an error on the xslt side
184     $server->{add_xslt} = 'notafile.xsl';
185     @res = C4::Breeding::_do_xslt_proc( $biblio, $server, $engine );
186     is( $res[1], Koha::XSLT_Handler::XSLTH_ERR_2, 'Error code found' );
187     #We still expect the original record back
188     is( ref $res[0], 'MARC::Record', 'Still got back MARC record' );
189     is ( $res[0]->subfield('245','a'), 'Just a title',
190         'At least the title is the same :)' );
191 }