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 => 32;
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";
53 my $user = $ENV{KOHA_USER} || $xml->{config}->{user};
54 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
55 my $intranet = $ENV{KOHA_INTRANET_URL};
56 my $opac = $ENV{KOHA_OPAC_URL};
58 # launch the zebra process
59 launch_zebra( $datadir, $koha_conf );
60 if ( not defined $zebra_pid ) {
61 plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
63 # launch the zebra process
65 if ( not defined $indexer_pid ) {
66 plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
68 # test KOHA_INTRANET_URL is set
69 if ( not defined $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
73 if ( not defined $opac ) {
74 plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
80 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
83 # -------------------------------------------------- LOAD RECORD
85 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
86 $agent->form_name('loginform');
87 $agent->field( 'password', $password );
88 $agent->field( 'userid', $user );
89 $agent->field( 'branch', '' );
90 $agent->click_ok( '', 'login to staff client' );
92 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
94 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
95 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
99 "$intranet/cgi-bin/koha/tools/upload-file.pl",
100 [ 'fileToUpload' => [$file], ],
101 'Content_Type' => 'form-data',
103 ok( $agent->success, 'uploaded file' );
105 $jsonresponse = decode_json $agent->content();
106 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
107 my $fileid = $jsonresponse->{'fileid'};
109 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
110 'reopen stage MARC page' );
111 $agent->submit_form_ok(
115 'uploadedfileid' => $fileid,
116 'nomatch_action' => 'create_new',
117 'overlay_action' => 'replace',
118 'item_action' => 'always_add',
121 'encoding' => 'utf8',
122 'parse_items' => '1',
123 'runinbackground' => '1',
129 $jsonresponse = decode_json $agent->content();
130 my $jobID = $jsonresponse->{'jobID'};
131 ok( $jobID, 'have job ID' );
135 # if we haven't completed the batch in two minutes, it's not happening
136 for my $counter ( 1 .. 24 ) {
138 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
141 $jsonresponse = decode_json $agent->content();
142 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
148 $jsonresponse->{'job_size'}
150 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
158 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
161 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
162 'reopen stage MARC page at end of upload'
164 $agent->submit_form_ok(
168 'uploadedfileid' => $fileid,
169 'nomatch_action' => 'create_new',
170 'overlay_action' => 'replace',
171 'item_action' => 'always_add',
174 'encoding' => 'utf8',
175 'parse_items' => '1',
176 'runinbackground' => '1',
177 'completedJobID' => $jobID,
183 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
186 $agent->form_number(5);
187 $agent->field( 'framework', '' );
188 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
189 my $webpage = $agent->{content};
191 $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
193 my $id_bib_number = GetBiblionumberFromImport($id_batch);
195 # wait enough time for the indexer
198 # --------------------------------- TEST INTRANET SEARCH
200 my $publisher = 'Αθήνα';
201 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
202 $agent->form_number(1);
203 $agent->field('idx', 'kw');
204 $agent->field('q', 'deuteros');
206 my $intra_text = $agent->text() ;
207 like( $intra_text, qr|Publisher: $publisher|, );
209 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
210 $agent->form_number(1);
211 $agent->field('idx', 'kw');
212 $agent->field('q', Encode::encode('UTF-8', $publisher));
214 $intra_text = $agent->text();
216 like( $intra_text, qr|Publisher: $publisher|, );
217 my $expected_base = q|search.pl\?idx=kw&q=| . uri_escape_utf8( Encode::encode('UTF-8', $publisher ) );
218 $agent->base_like(qr|$expected_base|, );
220 ok ( ( length(Encode::encode('UTF-8', $intra_text)) != length($intra_text) ) , 'UTF-8 are multi-byte. Goog') ;
221 ok ($intra_text =~ m/学協会. μμ/, 'UTF-8 chars are correctly present. Good');
222 # -------------------------------------------------- TEST ON OPAC
224 $agent->get_ok( "$opac" , "got opac");
225 $agent->form_name('searchform');
226 $agent->field( 'q', 'deuteros' );
227 $agent->field( 'idx', '' );
229 my $opac_text = $agent->text() ;
230 like( $opac_text, qr|Publisher: $publisher|, );
232 $agent->get_ok( "$opac" , "got opac");
233 $agent->form_name('searchform');
234 $agent->field('q', $publisher);
235 $agent->field( 'idx', '' );
237 $opac_text = $agent->text();
239 like( $opac_text, qr|Publisher: $publisher|, );
240 $expected_base = q|opac-search.pl\?q=| . uri_escape_utf8( $publisher );
241 $agent->base_like(qr|$expected_base|, );
243 ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Goog') ;
244 ok ($opac_text =~ m/学協会. μμ/, 'UTF-8 chars are correctly present. Good');
246 #-------------------------------------------------- REVERT
248 $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
249 $agent->form_name('clean_batch_'.$id_batch);
251 $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
252 $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
257 # function that launches the zebra daemon
260 my ( $datadir, $koha_conf ) = @_;
263 if ( $zebra_pid == 0 ) {
264 exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
272 my $rootdir = dirname(__FILE__) . '/../../../';
273 my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
275 $indexer_pid = fork();
277 if ( $indexer_pid == 0 ) {
278 exec("$rebuild_zebra -daemon -sleep 5");
286 kill 9, $zebra_pid if defined $zebra_pid;
287 kill 9, $indexer_pid if defined $indexer_pid;
288 # Clean up the Zebra files since the child process was just shot
293 sub GetBiblionumberFromImport{
294 my ( $batch_id) = @_;
296 my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
297 { order_by => 'import_record_id', order_by_direction => 'DESC' });
298 my $biblionumber = $data->[0]->{'matched_biblionumber'};
300 return $biblionumber;