Bug 19483: Fix test plan in t/db_dependent/www/*
[srvgit] / 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; #See plan tests => \d+ below
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 elsif ( not defined $opac ) {
70    plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
71 }
72 else {
73     plan tests => 66;
74 }
75
76 $intranet =~ s#/$##;
77 $opac     =~ s#/$##;
78
79 #-------------------------------- Test with greek and corean chars;
80 # launch the zebra saerch process
81 launch_zebra( $datadir, $koha_conf );
82 if ( not defined $zebra_pid ) {
83     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
84 }
85 # launch the zebra index process
86 launch_indexer( );
87 if ( not defined $indexer_pid ) {
88     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
89 }
90
91 my $utf8_reg1 = qr/学協会. μμ/;
92 test_search($file1,'Αθήνα', 'deuteros', $utf8_reg1);
93
94
95 #--------------------------------- Test with only utf-8 chars in the latin-1 range;
96 launch_zebra( $datadir, $koha_conf );
97 if ( not defined $zebra_pid ) {
98     plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
99 }
100 launch_indexer( );
101 if ( not defined $indexer_pid ) {
102     plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
103 }
104 my $utf8_reg2 = qr/Tòmas/;
105 test_search($file2,'Ramòn', 'Tòmas',$utf8_reg2);
106
107
108 sub test_search{
109     #Params
110     my $file = $_[0];
111     my $publisher = $_[1];
112     my $search_key = $_[2];
113     my $utf8_reg = $_[3];
114
115     my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
116     my $jsonresponse;
117
118     # -------------------------------------------------- LOAD RECORD
119
120     $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
121     $agent->form_name('loginform');
122     $agent->field( 'password', $password );
123     $agent->field( 'userid',   $user );
124     $agent->field( 'branch',   '' );
125     $agent->click_ok( '', 'login to staff client' );
126
127     $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
128
129     $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
130     $agent->follow_link_ok( { text => 'Stage MARC records for import' },
131         'go to stage MARC' );
132
133     $agent->post(
134         "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
135         [ 'fileToUpload' => [$file], ],
136         'Content_Type' => 'form-data',
137     );
138     ok( $agent->success, 'uploaded file' );
139
140     $jsonresponse = decode_json $agent->content();
141     is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
142     my $fileid = $jsonresponse->{'fileid'};
143
144     $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
145         'reopen stage MARC page' );
146     $agent->submit_form_ok(
147         {
148             form_number => 5,
149             fields      => {
150                 'uploadedfileid'  => $fileid,
151                 'nomatch_action'  => 'create_new',
152                 'overlay_action'  => 'replace',
153                 'item_action'     => 'always_add',
154                 'matcher'         => '',
155                 'comments'        => '',
156                 'encoding'        => 'utf8',
157                 'parse_items'     => '1',
158                 'runinbackground' => '1',
159                 'record_type'     => 'biblio'
160             }
161         },
162         'stage MARC'
163     );
164
165     $jsonresponse = decode_json $agent->content();
166     my $jobID = $jsonresponse->{'jobID'};
167     ok( $jobID, 'have job ID' );
168
169     my $completed = 0;
170
171     # if we haven't completed the batch in two minutes, it's not happening
172     for my $counter ( 1 .. 24 ) {
173         $agent->get(
174             "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID"
175         ); # get job progress
176         $jsonresponse = decode_json $agent->content();
177         if ( $jsonresponse->{'job_status'} eq 'completed' ) {
178             $completed = 1;
179             last;
180         }
181         warn(
182             (
183                 $jsonresponse->{'job_size'}
184                 ? floor(
185                     100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
186                   )
187                 : '100'
188             )
189             . "% completed"
190         );
191         sleep 5;
192     }
193     is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
194
195     $agent->get_ok(
196         "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
197         'reopen stage MARC page at end of upload'
198     );
199     $agent->submit_form_ok(
200         {
201             form_number => 5,
202             fields      => {
203                 'uploadedfileid'  => $fileid,
204                 'nomatch_action'  => 'create_new',
205                 'overlay_action'  => 'replace',
206                 'item_action'     => 'always_add',
207                 'matcher'         => '1',
208                 'comments'        => '',
209                 'encoding'        => 'utf8',
210                 'parse_items'     => '1',
211                 'runinbackground' => '1',
212                 'completedJobID'  => $jobID,
213                 'record_type'     => 'biblio'
214             }
215         },
216         'stage MARC'
217     );
218
219     $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
220
221
222     $agent->form_number(6);
223     $agent->field( 'framework', '' );
224     $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
225     my $webpage = $agent->{content};
226
227     $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
228     my $id_batch = $2;
229     my $id_bib_number = GetBiblionumberFromImport($id_batch);
230
231     # wait enough time for the indexer
232     sleep 10;
233
234     # --------------------------------- TEST INTRANET SEARCH
235
236
237     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
238     $agent->form_number(5);
239     $agent->field('idx', 'kw');
240     $agent->field('q', $search_key);
241     $agent->click();
242     my $intra_text = $agent->text() ;
243     like( $intra_text, qr|Publisher: $publisher|, );
244
245     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
246     $agent->form_number(5);
247     $agent->field('idx', 'kw');
248     $agent->field('q', $publisher);
249     $agent->click();
250     $intra_text = $agent->text();
251
252     like( $intra_text, qr|Publisher: $publisher|, );
253     my $expected_base = q|search.pl\?idx=kw&q=| . uri_escape_utf8( $publisher );
254     $agent->base_like(qr|$expected_base|, );
255
256     ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Good') ;
257     ok ($intra_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
258     # -------------------------------------------------- TEST ON OPAC
259
260     $agent->get_ok( "$opac" , "got opac");
261     $agent->form_name('searchform');
262     $agent->field( 'q',   $search_key );
263     $agent->field( 'idx',   '' );
264     $agent->click( );
265     my $opac_text = $agent->text() ;
266     like( $opac_text, qr|Publisher: $publisher|, );
267
268     $agent->get_ok( "$opac" , "got opac");
269     $agent->form_name('searchform');
270     $agent->field('q', $publisher);
271     $agent->field( 'idx',   '' );
272     $agent->click();
273     $opac_text = $agent->text();
274
275     like( $opac_text, qr|Publisher: $publisher|, );
276     $expected_base = q|opac-search.pl\?(idx=&)?q=| . uri_escape_utf8( $publisher );
277     $agent->base_like(qr|$expected_base|, );
278     # Test added on BZ 14909 in addition to making the empty idx= optional
279     # in the previous regex
280     $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
281
282
283     ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
284     ok ($opac_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
285
286     #-------------------------------------------------- REVERT
287
288     $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
289     $agent->form_name('clean_batch_'.$id_batch);
290     $agent->click();
291     $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
292     $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
293
294     # clean
295     cleanup();
296 }
297
298
299 # function that launches the zebra daemon
300 sub launch_zebra {
301
302     my ( $datadir, $koha_conf ) = @_;
303
304     $zebra_pid = fork();
305     if ( $zebra_pid == 0 ) {
306         exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
307         exit;
308     }
309     sleep( 1 );
310 }
311
312 sub launch_indexer {
313
314     my $rootdir       = dirname(__FILE__) . '/../../../';
315     my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
316
317     $indexer_pid = fork();
318
319     if ( $indexer_pid == 0 ) {
320         exec("$rebuild_zebra -daemon -sleep 5");
321         exit;
322     }
323     sleep( 1 );
324 }
325
326 sub cleanup {
327
328     kill 9, $zebra_pid   if defined $zebra_pid;
329     kill 9, $indexer_pid if defined $indexer_pid;
330     # Clean up the Zebra files since the child process was just shot
331     rmtree $datadir;
332
333 }
334
335 sub GetBiblionumberFromImport{
336     my ( $batch_id) = @_;
337     use C4::ImportBatch;
338     my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
339                     { order_by => 'import_record_id', order_by_direction => 'DESC' });
340     my $biblionumber = $data->[0]->{'matched_biblionumber'};
341
342     return $biblionumber;
343 }
344
345 END {
346     cleanup();
347 };
348