3 # This file is part of Koha.
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.
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.
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>.
21 use Test::More; #See plan tests => \d+ below
22 use Test::WWW::Mechanize;
29 use File::Temp qw/ tempdir /;
36 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
37 # global variables that will be used when forking
40 our $datadir = tempdir();;
42 my $koha_conf = $ENV{KOHA_CONF};
43 my $xml = XMLin($koha_conf);
45 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
48 $marcflavour eq 'UNIMARC'
49 ? "$testdir/data/unimarcutf8record.mrc"
50 : "$testdir/data/marc21utf8record.mrc";
53 $marcflavour eq 'UNIMARC'
54 ? "$testdir/data/unimarclatin1utf8rec.mrc"
55 : "$testdir/data/marc21latin1utf8rec.mrc";
58 $marcflavour eq 'UNIMARC'
59 ? "$testdir/data/unimarcutf8supprec.mrc"
60 : "$testdir/data/marc21utf8supprec.mrc";
62 my $user = $ENV{KOHA_USER} || $xml->{config}->{user};
63 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
64 my $intranet = $ENV{KOHA_INTRANET_URL};
65 my $opac = $ENV{KOHA_OPAC_URL};
68 # test KOHA_INTRANET_URL is set
69 if ( not $intranet ) {
70 plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
72 # test KOHA_OPAC_URL is set
74 plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
83 #-------------------------------- Test with greek and corean chars;
84 # launch the zebra saerch process
85 launch_zebra( $datadir, $koha_conf );
86 if ( not defined $zebra_pid ) {
87 plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
89 # launch the zebra index process
91 if ( not defined $indexer_pid ) {
92 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
95 my $utf8_reg1 = qr/学協会. μμ/;
96 test_search($file1,'Αθήνα', 'deuteros', $utf8_reg1);
99 #--------------------------------- Test with only utf-8 chars in the latin-1 range;
100 launch_zebra( $datadir, $koha_conf );
101 if ( not defined $zebra_pid ) {
102 plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
105 if ( not defined $indexer_pid ) {
106 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
108 my $utf8_reg2 = qr/Tòmas/;
109 test_search($file2,'Ramòn', 'Tòmas',$utf8_reg2);
111 #--------------------------------- Test with supplementary utf-8 chars;
112 launch_zebra( $datadir, $koha_conf );
113 if ( not defined $zebra_pid ) {
114 plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
117 if ( not defined $indexer_pid ) {
118 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
120 my $utf8_reg3 = qr/😀/;
121 test_search($file3, "𠻺tomasito𠻺", 'A tiny record', $utf8_reg3);
126 my $publisher = $_[1];
127 my $search_key = $_[2];
128 my $utf8_reg = $_[3];
130 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
133 # -------------------------------------------------- LOAD RECORD
135 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
136 $agent->form_name('loginform');
137 $agent->field( 'password', $password );
138 $agent->field( 'userid', $user );
139 $agent->field( 'branch', '' );
140 $agent->click_ok( '', 'login to staff interface' );
142 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
144 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
145 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
146 'go to stage MARC' );
149 "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
150 [ 'fileToUpload' => [$file], ],
151 'Content_Type' => 'form-data',
153 ok( $agent->success, 'uploaded file' );
155 $jsonresponse = decode_json $agent->content();
156 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
157 my $fileid = $jsonresponse->{'fileid'};
159 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
160 'reopen stage MARC page' );
161 $agent->submit_form_ok(
165 'uploadedfileid' => $fileid,
166 'nomatch_action' => 'create_new',
167 'overlay_action' => 'replace',
168 'item_action' => 'always_add',
171 'encoding' => 'utf8',
172 'parse_items' => '1',
173 'runinbackground' => '1',
174 'record_type' => 'biblio'
180 $jsonresponse = decode_json $agent->content();
181 my $jobID = $jsonresponse->{'jobID'};
182 ok( $jobID, 'have job ID' );
186 # if we haven't completed the batch in two minutes, it's not happening
187 for my $counter ( 1 .. 24 ) {
189 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID"
190 ); # get job progress
191 $jsonresponse = decode_json $agent->content();
192 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
198 $jsonresponse->{'job_size'}
200 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
208 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
211 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
212 'reopen stage MARC page at end of upload'
214 $agent->submit_form_ok(
218 'uploadedfileid' => $fileid,
219 'nomatch_action' => 'create_new',
220 'overlay_action' => 'replace',
221 'item_action' => 'always_add',
224 'encoding' => 'utf8',
225 'parse_items' => '1',
226 'runinbackground' => '1',
227 'completedJobID' => $jobID,
228 'record_type' => 'biblio'
234 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
237 $agent->form_number(6);
238 $agent->field( 'framework', '' );
239 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
240 my $webpage = $agent->{content};
242 $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
244 my $id_bib_number = GetBiblionumberFromImport($id_batch);
246 # wait enough time for the indexer
249 # --------------------------------- TEST INTRANET SEARCH
252 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
253 $agent->form_number(5);
254 $agent->field('idx', 'kw');
255 $agent->field('q', $search_key);
257 my $intra_text = $agent->text() ;
259 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
260 $agent->form_number(5);
261 $agent->field('idx', 'kw');
262 $agent->field('q', $publisher);
264 $intra_text = $agent->text();
266 my $expected_base = q|search.pl\?advsearch=1&idx=kw&q=| . uri_escape_utf8( $publisher );
267 $agent->base_like(qr|$expected_base|, );
269 ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Good') ;
270 ok ($intra_text =~ $utf8_reg, 'UTF-8 chars are correctly present. Good');
271 # -------------------------------------------------- TEST ON OPAC
273 $agent->get_ok( "$opac" , "got opac");
274 $agent->form_name('searchform');
275 $agent->field( 'q', $search_key );
276 $agent->field( 'idx', '' );
278 my $opac_text = $agent->text() ;
280 $agent->get_ok( "$opac" , "got opac");
281 $agent->form_name('searchform');
282 $agent->field('q', $publisher);
283 $agent->field( 'idx', '' );
285 $opac_text = $agent->text();
287 $expected_base = q|opac-search.pl\?(idx=&)?q=| . uri_escape_utf8( $publisher );
288 $agent->base_like(qr|$expected_base|, );
289 # Test added on BZ 14909 in addition to making the empty idx= optional
290 # in the previous regex
291 $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
294 ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
295 ok ($opac_text =~ $utf8_reg, 'UTF-8 chars are correctly present. Good');
297 #-------------------------------------------------- REVERT
299 $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
300 $agent->form_name('clean_batch_'.$id_batch);
302 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
303 $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
310 # function that launches the zebra daemon
313 my ( $datadir, $koha_conf ) = @_;
316 if ( $zebra_pid == 0 ) {
317 exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
325 my $rootdir = dirname(__FILE__) . '/../../../';
326 my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
328 $indexer_pid = fork();
330 if ( $indexer_pid == 0 ) {
331 exec("$rebuild_zebra -daemon -sleep 5");
339 kill 9, $zebra_pid if defined $zebra_pid;
340 kill 9, $indexer_pid if defined $indexer_pid;
341 # Clean up the Zebra files since the child process was just shot
346 sub GetBiblionumberFromImport{
347 my ( $batch_id) = @_;
349 my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
350 { order_by => 'import_record_id', order_by_direction => 'DESC' });
351 my $biblionumber = $data->[0]->{'matched_biblionumber'};
353 return $biblionumber;