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 tests => 66;
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';
47 # For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same
49 $marcflavour eq 'UNIMARC'
50 ? "$testdir/data/unimarcutf8record.mrc"
51 : "$testdir/data/marc21utf8record.mrc";
54 $marcflavour eq 'UNIMARC'
55 ? "$testdir/data/unimarclatin1utf8rec.mrc"
56 : "$testdir/data/marc21latin1utf8rec.mrc";
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};
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";
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";
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";
82 # launch the zebra index process
84 if ( not defined $indexer_pid ) {
85 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
88 my $utf8_reg1 = qr/学協会. μμ/;
89 test_search($file1,'Αθήνα', 'deuteros', $utf8_reg1);
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";
98 if ( not defined $indexer_pid ) {
99 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
101 my $utf8_reg2 = qr/Tòmas/;
102 test_search($file2,'Ramòn', 'Tòmas',$utf8_reg2);
108 my $publisher = $_[1];
109 my $search_key = $_[2];
110 my $utf8_reg = $_[3];
112 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
115 # -------------------------------------------------- LOAD RECORD
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' );
124 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
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' );
131 "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
132 [ 'fileToUpload' => [$file], ],
133 'Content_Type' => 'form-data',
135 ok( $agent->success, 'uploaded file' );
137 $jsonresponse = decode_json $agent->content();
138 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
139 my $fileid = $jsonresponse->{'fileid'};
141 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
142 'reopen stage MARC page' );
143 $agent->submit_form_ok(
147 'uploadedfileid' => $fileid,
148 'nomatch_action' => 'create_new',
149 'overlay_action' => 'replace',
150 'item_action' => 'always_add',
153 'encoding' => 'utf8',
154 'parse_items' => '1',
155 'runinbackground' => '1',
156 'record_type' => 'biblio'
162 $jsonresponse = decode_json $agent->content();
163 my $jobID = $jsonresponse->{'jobID'};
164 ok( $jobID, 'have job ID' );
168 # if we haven't completed the batch in two minutes, it's not happening
169 for my $counter ( 1 .. 24 ) {
171 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
174 $jsonresponse = decode_json $agent->content();
175 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
181 $jsonresponse->{'job_size'}
183 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
191 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
194 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
195 'reopen stage MARC page at end of upload'
197 $agent->submit_form_ok(
201 'uploadedfileid' => $fileid,
202 'nomatch_action' => 'create_new',
203 'overlay_action' => 'replace',
204 'item_action' => 'always_add',
207 'encoding' => 'utf8',
208 'parse_items' => '1',
209 'runinbackground' => '1',
210 'completedJobID' => $jobID,
211 'record_type' => 'biblio'
217 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
220 $agent->form_number(6);
221 $agent->field( 'framework', '' );
222 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
223 my $webpage = $agent->{content};
225 $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
227 my $id_bib_number = GetBiblionumberFromImport($id_batch);
229 # wait enough time for the indexer
232 # --------------------------------- TEST INTRANET SEARCH
235 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
236 $agent->form_number(1);
237 $agent->field('idx', 'kw');
238 $agent->field('q', $search_key);
240 my $intra_text = $agent->text() ;
241 like( $intra_text, qr|Publisher: $publisher|, );
243 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
244 $agent->form_number(1);
245 $agent->field('idx', 'kw');
246 $agent->field('q', $publisher);
248 $intra_text = $agent->text();
250 like( $intra_text, qr|Publisher: $publisher|, );
251 my $expected_base = q|search.pl\?idx=kw&q=| . uri_escape_utf8( $publisher );
252 $agent->base_like(qr|$expected_base|, );
254 ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Good') ;
255 ok ($intra_text =~ $utf8_reg, 'UTF-8 chars are correctly present. Good');
256 # -------------------------------------------------- TEST ON OPAC
258 $agent->get_ok( "$opac" , "got opac");
259 $agent->form_name('searchform');
260 $agent->field( 'q', $search_key );
261 $agent->field( 'idx', '' );
263 my $opac_text = $agent->text() ;
264 like( $opac_text, qr|Publisher: $publisher|, );
266 $agent->get_ok( "$opac" , "got opac");
267 $agent->form_name('searchform');
268 $agent->field('q', $publisher);
269 $agent->field( 'idx', '' );
271 $opac_text = $agent->text();
273 like( $opac_text, qr|Publisher: $publisher|, );
274 $expected_base = q|opac-search.pl\?(idx=&)?q=| . uri_escape_utf8( $publisher );
275 $agent->base_like(qr|$expected_base|, );
276 # Test added on BZ 14909 in addition to making the empty idx= optional
277 # in the previous regex
278 $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
281 ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
282 ok ($opac_text =~ $utf8_reg, 'UTF-8 chars are correctly present. Good');
284 #-------------------------------------------------- REVERT
286 $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
287 $agent->form_name('clean_batch_'.$id_batch);
289 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
290 $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
297 # function that launches the zebra daemon
300 my ( $datadir, $koha_conf ) = @_;
303 if ( $zebra_pid == 0 ) {
304 exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
312 my $rootdir = dirname(__FILE__) . '/../../../';
313 my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
315 $indexer_pid = fork();
317 if ( $indexer_pid == 0 ) {
318 exec("$rebuild_zebra -daemon -sleep 5");
326 kill 9, $zebra_pid if defined $zebra_pid;
327 kill 9, $indexer_pid if defined $indexer_pid;
328 # Clean up the Zebra files since the child process was just shot
333 sub GetBiblionumberFromImport{
334 my ( $batch_id) = @_;
336 my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
337 { order_by => 'import_record_id', order_by_direction => 'DESC' });
338 my $biblionumber = $data->[0]->{'matched_biblionumber'};
340 return $biblionumber;