Bug 13264: More tests for Latin-1 vs. UTF-8 interpretation
[koha.git] / t / db_dependent / www / search_utf8.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use utf8;
21 use Test::More tests => 64;
22 use Test::WWW::Mechanize;
23 use Data::Dumper;
24 use XML::Simple;
25 use JSON;
26 use File::Basename;
27 use File::Path;
28 use File::Spec;
29 use File::Temp qw/ tempdir /;
30 use POSIX;
31 use Encode;
32 use URI::Escape;
33
34 use C4::Context;
35
36 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
37 # global variables that will be used when forking
38 our $zebra_pid;
39 our $indexer_pid;
40 our $datadir = tempdir();;
41
42 my $koha_conf = $ENV{KOHA_CONF};
43 my $xml       = XMLin($koha_conf);
44
45 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
46
47 # For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same
48 my $file1 =
49   $marcflavour eq 'UNIMARC'
50   ? "$testdir/data/unimarcutf8record.mrc"
51   : "$testdir/data/marc21utf8record.mrc";
52
53 my $file2 =
54   $marcflavour eq 'UNIMARC'
55   ? "$testdir/data/unimarclatin1utf8rec.mrc"
56   : "$testdir/data/marc21latin1utf8rec.mrc";
57
58 my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
59 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
60 my $intranet = $ENV{KOHA_INTRANET_URL};
61 my $opac     = $ENV{KOHA_OPAC_URL};
62
63
64 # test KOHA_INTRANET_URL is set
65 if ( not defined $intranet ) {
66    plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
67 }
68 # test KOHA_OPAC_URL is set
69 if ( not defined $opac ) {
70    plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
71 }
72
73 $intranet =~ s#/$##;
74 $opac     =~ s#/$##;
75
76 #-------------------------------- Test with greek and corean chars;
77 # launch the zebra saerch process
78 launch_zebra( $datadir, $koha_conf );
79 if ( not defined $zebra_pid ) {
80     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
81 }
82 # launch the zebra index process
83 launch_indexer( );
84 if ( not defined $indexer_pid ) {
85     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
86 }
87
88 my $utf8_reg1 = qr/学協会. μμ/;
89 test_search($file1,'Αθήνα', 'deuteros', $utf8_reg1);
90
91
92 #--------------------------------- Test with only utf-8 chars in the latin-1 range;
93 launch_zebra( $datadir, $koha_conf );
94 if ( not defined $zebra_pid ) {
95     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
96 }
97 launch_indexer( );
98 if ( not defined $indexer_pid ) {
99     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
100 }
101 my $utf8_reg2 = qr/Tòmas/;
102 test_search($file2,'Ramòn', 'Tòmas',$utf8_reg2);
103
104
105 sub test_search{
106     #Params
107     my $file = $_[0];
108     my $publisher = $_[1];
109     my $search_key = $_[2];
110     my $utf8_reg = $_[3];
111
112     my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
113     my $jsonresponse;
114
115     # -------------------------------------------------- LOAD RECORD
116
117     $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
118     $agent->form_name('loginform');
119     $agent->field( 'password', $password );
120     $agent->field( 'userid',   $user );
121     $agent->field( 'branch',   '' );
122     $agent->click_ok( '', 'login to staff client' );
123
124     $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
125
126     $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
127     $agent->follow_link_ok( { text => 'Stage MARC records for import' },
128         'go to stage MARC' );
129
130     $agent->post(
131         "$intranet/cgi-bin/koha/tools/upload-file.pl",
132         [ 'fileToUpload' => [$file], ],
133         'Content_Type' => 'form-data',
134     );
135     ok( $agent->success, 'uploaded file' );
136
137     $jsonresponse = decode_json $agent->content();
138     is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
139     my $fileid = $jsonresponse->{'fileid'};
140
141     $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
142         'reopen stage MARC page' );
143     $agent->submit_form_ok(
144         {
145             form_number => 5,
146             fields      => {
147                 'uploadedfileid'  => $fileid,
148                 'nomatch_action'  => 'create_new',
149                 'overlay_action'  => 'replace',
150                 'item_action'     => 'always_add',
151                 'matcher'         => '',
152                 'comments'        => '',
153                 'encoding'        => 'utf8',
154                 'parse_items'     => '1',
155                 'runinbackground' => '1',
156             }
157         },
158         'stage MARC'
159     );
160
161     $jsonresponse = decode_json $agent->content();
162     my $jobID = $jsonresponse->{'jobID'};
163     ok( $jobID, 'have job ID' );
164
165     my $completed = 0;
166
167     # if we haven't completed the batch in two minutes, it's not happening
168     for my $counter ( 1 .. 24 ) {
169         $agent->get(
170             "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
171             "get job progress"
172         );
173         $jsonresponse = decode_json $agent->content();
174         if ( $jsonresponse->{'job_status'} eq 'completed' ) {
175             $completed = 1;
176             last;
177         }
178         warn(
179             (
180                 $jsonresponse->{'job_size'}
181                 ? floor(
182                     100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
183                   )
184                 : '100'
185             )
186             . "% completed"
187         );
188         sleep 5;
189     }
190     is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
191
192     $agent->get_ok(
193         "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
194         'reopen stage MARC page at end of upload'
195     );
196     $agent->submit_form_ok(
197         {
198             form_number => 5,
199             fields      => {
200                 'uploadedfileid'  => $fileid,
201                 'nomatch_action'  => 'create_new',
202                 'overlay_action'  => 'replace',
203                 'item_action'     => 'always_add',
204                 'matcher'         => '1',
205                 'comments'        => '',
206                 'encoding'        => 'utf8',
207                 'parse_items'     => '1',
208                 'runinbackground' => '1',
209                 'completedJobID'  => $jobID,
210             }
211         },
212         'stage MARC'
213     );
214
215     $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
216
217
218     $agent->form_number(5);
219     $agent->field( 'framework', '' );
220     $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
221     my $webpage = $agent->{content};
222
223     $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
224     my $id_batch = $2;
225     my $id_bib_number = GetBiblionumberFromImport($id_batch);
226
227     # wait enough time for the indexer
228     sleep 10;
229
230     # --------------------------------- TEST INTRANET SEARCH
231
232
233     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
234     $agent->form_number(1);
235     $agent->field('idx', 'kw');
236     $agent->field('q', $search_key);
237     $agent->click();
238     my $intra_text = $agent->text() ;
239     like( $intra_text, qr|Publisher: $publisher|, );
240
241     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
242     $agent->form_number(1);
243     $agent->field('idx', 'kw');
244     $agent->field('q', $publisher);
245     $agent->click();
246     $intra_text = $agent->text();
247
248     like( $intra_text, qr|Publisher: $publisher|, );
249     my $expected_base = q|search.pl\?idx=kw&q=| . uri_escape_utf8( $publisher );
250     $agent->base_like(qr|$expected_base|, );
251
252     ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Goog') ;
253     ok ($intra_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
254     # -------------------------------------------------- TEST ON OPAC
255
256     $agent->get_ok( "$opac" , "got opac");
257     $agent->form_name('searchform');
258     $agent->field( 'q',   $search_key );
259     $agent->field( 'idx',   '' );
260     $agent->click( );
261     my $opac_text = $agent->text() ;
262     like( $opac_text, qr|Publisher: $publisher|, );
263
264     $agent->get_ok( "$opac" , "got opac");
265     $agent->form_name('searchform');
266     $agent->field('q', $publisher);
267     $agent->field( 'idx',   '' );
268     $agent->click();
269     $opac_text = $agent->text();
270
271     like( $opac_text, qr|Publisher: $publisher|, );
272     $expected_base = q|opac-search.pl\?idx=&q=| . uri_escape_utf8( $publisher );
273     $agent->base_like(qr|$expected_base|, );
274
275     ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Goog') ;
276     ok ($opac_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
277
278     #-------------------------------------------------- REVERT
279
280     $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
281     $agent->form_name('clean_batch_'.$id_batch);
282     $agent->click();
283     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
284     $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
285
286     # clean
287     cleanup();
288 }
289
290
291 # function that launches the zebra daemon
292 sub launch_zebra {
293
294     my ( $datadir, $koha_conf ) = @_;
295
296     $zebra_pid = fork();
297     if ( $zebra_pid == 0 ) {
298         exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
299         exit;
300     }
301     sleep( 1 );
302 }
303
304 sub launch_indexer {
305
306     my $rootdir       = dirname(__FILE__) . '/../../../';
307     my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
308
309     $indexer_pid = fork();
310
311     if ( $indexer_pid == 0 ) {
312         exec("$rebuild_zebra -daemon -sleep 5");
313         exit;
314     }
315     sleep( 1 );
316 }
317
318 sub cleanup {
319
320     kill 9, $zebra_pid   if defined $zebra_pid;
321     kill 9, $indexer_pid if defined $indexer_pid;
322     # Clean up the Zebra files since the child process was just shot
323     rmtree $datadir;
324
325 }
326
327 sub GetBiblionumberFromImport{
328     my ( $batch_id) = @_;
329     use C4::ImportBatch;
330     my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
331                     { order_by => 'import_record_id', order_by_direction => 'DESC' });
332     my $biblionumber = $data->[0]->{'matched_biblionumber'};
333
334     return $biblionumber;
335 }
336 1;