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>.
22 use Test::WWW::Mechanize;
29 use File::Temp qw/ tempdir /;
35 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
36 # global variables that will be used when forking
39 our $datadir = tempdir();;
41 my $koha_conf = $ENV{KOHA_CONF};
42 my $xml = XMLin($koha_conf);
44 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
46 # For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same
48 $marcflavour eq 'UNIMARC'
49 ? "$testdir/data/unimarcutf8record.mrc"
50 : "$testdir/data/marc21utf8record.mrc";
52 my $user = $ENV{KOHA_USER} || $xml->{config}->{user};
53 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
54 my $intranet = $ENV{KOHA_INTRANET_URL};
55 my $opac = $ENV{KOHA_OPAC_URL};
57 # launch the zebra process
58 launch_zebra( $datadir, $koha_conf );
59 if ( not defined $zebra_pid ) {
60 plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
62 # launch the zebra process
64 if ( not defined $indexer_pid ) {
65 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
67 # test KOHA_INTRANET_URL is set
68 if ( not defined $intranet ) {
69 plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n";
71 # test KOHA_OPAC_URL is set
72 if ( not defined $opac ) {
73 plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
79 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
82 # -------------------------------------------------- LOAD RECORD
84 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
85 $agent->form_name('loginform');
86 $agent->field( 'password', $password );
87 $agent->field( 'userid', $user );
88 $agent->field( 'branch', '' );
89 $agent->click_ok( '', 'login to staff client' );
91 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
93 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
94 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
98 "$intranet/cgi-bin/koha/tools/upload-file.pl",
99 [ 'fileToUpload' => [$file], ],
100 'Content_Type' => 'form-data',
102 ok( $agent->success, 'uploaded file' );
104 $jsonresponse = decode_json $agent->content();
105 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
106 my $fileid = $jsonresponse->{'fileid'};
108 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
109 'reopen stage MARC page' );
110 $agent->submit_form_ok(
114 'uploadedfileid' => $fileid,
115 'nomatch_action' => 'create_new',
116 'overlay_action' => 'replace',
117 'item_action' => 'always_add',
120 'encoding' => 'utf8',
121 'parse_items' => '1',
122 'runinbackground' => '1',
128 $jsonresponse = decode_json $agent->content();
129 my $jobID = $jsonresponse->{'jobID'};
130 ok( $jobID, 'have job ID' );
134 # if we haven't completed the batch in two minutes, it's not happening
135 for my $counter ( 1 .. 24 ) {
137 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
140 $jsonresponse = decode_json $agent->content();
141 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
147 $jsonresponse->{'job_size'}
149 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
157 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
160 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
161 'reopen stage MARC page at end of upload'
163 $agent->submit_form_ok(
167 'uploadedfileid' => $fileid,
168 'nomatch_action' => 'create_new',
169 'overlay_action' => 'replace',
170 'item_action' => 'always_add',
173 'encoding' => 'utf8',
174 'parse_items' => '1',
175 'runinbackground' => '1',
176 'completedJobID' => $jobID,
182 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
185 $agent->form_number(5);
186 $agent->field( 'framework', '' );
187 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
188 my $webpage = $agent->{content};
190 $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
192 my $id_bib_number = GetBiblionumberFromImport($id_batch);
194 # wait enough time for the indexer
199 # -------------------------------------------------- TEST ON OPAC
202 $agent->get_ok( "$opac" , "got opac");
203 $agent->form_name('searchform');
204 $agent->field( 'q', 'deuteros' );
205 $agent->field( 'idx', '' );
208 # ok( $agent->success, 'uploaded file' );
210 my $text = $agent->text() ;
214 ok ( ( length(Encode::encode_utf8($text)) != length($text) ) , 'UTF-8 are multi-byte. Goog') ;
215 ok ($text =~ m/学協会. μμ/, 'UTF-8 chars are correctly present. Good');
219 #-------------------------------------------------- REVERT
221 $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
222 $agent->form_name('clean_batch_'.$id_batch);
224 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
225 $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
232 # function that launches the zebra daemon
235 my ( $datadir, $koha_conf ) = @_;
238 if ( $zebra_pid == 0 ) {
239 exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
247 my $rootdir = dirname(__FILE__) . '/../../../';
248 my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
250 $indexer_pid = fork();
252 if ( $indexer_pid == 0 ) {
253 exec("$rebuild_zebra -daemon -sleep 5");
261 kill 9, $zebra_pid if defined $zebra_pid;
262 kill 9, $indexer_pid if defined $indexer_pid;
263 # Clean up the Zebra files since the child process was just shot
268 sub GetBiblionumberFromImport{
269 my ( $batch_id) = @_;
271 my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
272 { order_by => 'import_record_id', order_by_direction => 'DESC' });
273 my $biblionumber = $data->[0]->{'matched_biblionumber'};
275 return $biblionumber;