Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2012 C & P Bibliography Services |
4 |
# |
5 |
# This is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 2 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# This is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along with |
15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
16 |
# Suite 330, Boston, MA 02111-1307 USA |
17 |
# |
18 |
|
19 |
use Modern::Perl; |
20 |
use utf8; |
21 |
use Test::More; |
22 |
use Test::WWW::Mechanize; |
23 |
use Data::Dumper; |
24 |
use XML::Simple; |
25 |
use JSON; |
26 |
use File::Basename; |
27 |
use File::Spec; |
28 |
use POSIX; |
29 |
use Encode; |
30 |
|
31 |
my $testdir = File::Spec->rel2abs( dirname(__FILE__) ); |
32 |
|
33 |
my $koha_conf = $ENV{KOHA_CONF}; |
34 |
my $xml = XMLin($koha_conf); |
35 |
|
36 |
use C4::Context; |
37 |
my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21'; |
38 |
|
39 |
# For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same |
40 |
my $file = |
41 |
$marcflavour eq 'UNIMARC' |
42 |
? "$testdir/data/unimarcutf8record.mrc" |
43 |
: "$testdir/data/marc21utf8record.mrc"; |
44 |
|
45 |
my $user = $ENV{KOHA_USER} || $xml->{config}->{user}; |
46 |
my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass}; |
47 |
my $intranet = $ENV{KOHA_INTRANET_URL}; |
48 |
my $opac = $ENV{KOHA_OPAC_URL}; |
49 |
|
50 |
my $zebra_on = ProgProcesses('zebrasrv'); |
51 |
my $indexer_on = ProgProcesses('koha-index'); |
52 |
|
53 |
if ($zebra_on < 2) { |
54 |
plan skip_all => "Tests skip. You must start Zebra Server to do those tests\n"; |
55 |
} |
56 |
|
57 |
if ($indexer_on < 2) { |
58 |
plan skip_all => "Tests skip. You must start Zebra Background indexer to do those tests\n"; |
59 |
} |
60 |
|
61 |
if (not defined $intranet) { |
62 |
plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n"; |
63 |
} |
64 |
if (not defined $opac) { |
65 |
plan skip_all => "Tests skip. You must set env. variable KOHA_INTRANET_URL to do tests\n"; |
66 |
} |
67 |
|
68 |
$intranet =~ s#/$##; |
69 |
$opac =~ s#/$##; |
70 |
|
71 |
my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); |
72 |
my $jsonresponse; |
73 |
|
74 |
# -------------------------------------------------- LOAD RECORD |
75 |
|
76 |
$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' ); |
77 |
$agent->form_name('loginform'); |
78 |
$agent->field( 'password', $password ); |
79 |
$agent->field( 'userid', $user ); |
80 |
$agent->field( 'branch', '' ); |
81 |
$agent->click_ok( '', 'login to staff client' ); |
82 |
|
83 |
$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' ); |
84 |
|
85 |
$agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' ); |
86 |
$agent->follow_link_ok( { text => 'Stage MARC records for import' }, |
87 |
'go to stage MARC' ); |
88 |
|
89 |
$agent->post( |
90 |
"$intranet/cgi-bin/koha/tools/upload-file.pl", |
91 |
[ 'fileToUpload' => [$file], ], |
92 |
'Content_Type' => 'form-data', |
93 |
); |
94 |
ok( $agent->success, 'uploaded file' ); |
95 |
|
96 |
$jsonresponse = decode_json $agent->content(); |
97 |
is( $jsonresponse->{'status'}, 'done', 'upload succeeded' ); |
98 |
my $fileid = $jsonresponse->{'fileid'}; |
99 |
|
100 |
$agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl", |
101 |
'reopen stage MARC page' ); |
102 |
$agent->submit_form_ok( |
103 |
{ |
104 |
form_number => 5, |
105 |
fields => { |
106 |
'uploadedfileid' => $fileid, |
107 |
'nomatch_action' => 'create_new', |
108 |
'overlay_action' => 'replace', |
109 |
'item_action' => 'always_add', |
110 |
'matcher' => '', |
111 |
'comments' => '', |
112 |
'encoding' => 'utf8', |
113 |
'parse_items' => '1', |
114 |
'runinbackground' => '1', |
115 |
} |
116 |
}, |
117 |
'stage MARC' |
118 |
); |
119 |
|
120 |
$jsonresponse = decode_json $agent->content(); |
121 |
my $jobID = $jsonresponse->{'jobID'}; |
122 |
ok( $jobID, 'have job ID' ); |
123 |
|
124 |
my $completed = 0; |
125 |
|
126 |
# if we haven't completed the batch in two minutes, it's not happening |
127 |
for my $counter ( 1 .. 24 ) { |
128 |
$agent->get( |
129 |
"$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID", |
130 |
"get job progress" |
131 |
); |
132 |
$jsonresponse = decode_json $agent->content(); |
133 |
if ( $jsonresponse->{'job_status'} eq 'completed' ) { |
134 |
$completed = 1; |
135 |
last; |
136 |
} |
137 |
warn( |
138 |
( |
139 |
$jsonresponse->{'job_size'} |
140 |
? floor( |
141 |
100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'} |
142 |
) |
143 |
: '100' |
144 |
) |
145 |
. "% completed" |
146 |
); |
147 |
sleep 5; |
148 |
} |
149 |
is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' ); |
150 |
|
151 |
$agent->get_ok( |
152 |
"$intranet/cgi-bin/koha/tools/stage-marc-import.pl", |
153 |
'reopen stage MARC page at end of upload' |
154 |
); |
155 |
$agent->submit_form_ok( |
156 |
{ |
157 |
form_number => 5, |
158 |
fields => { |
159 |
'uploadedfileid' => $fileid, |
160 |
'nomatch_action' => 'create_new', |
161 |
'overlay_action' => 'replace', |
162 |
'item_action' => 'always_add', |
163 |
'matcher' => '1', |
164 |
'comments' => '', |
165 |
'encoding' => 'utf8', |
166 |
'parse_items' => '1', |
167 |
'runinbackground' => '1', |
168 |
'completedJobID' => $jobID, |
169 |
} |
170 |
}, |
171 |
'stage MARC' |
172 |
); |
173 |
|
174 |
$agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' ); |
175 |
|
176 |
|
177 |
$agent->form_number(5); |
178 |
$agent->field( 'framework', '' ); |
179 |
$agent->click_ok( 'mainformsubmit', "imported records into catalog" ); |
180 |
my $webpage = $agent->{content}; |
181 |
|
182 |
$webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx; |
183 |
my $id_batch = $2; |
184 |
|
185 |
#Wait the indexer |
186 |
sleep 35; |
187 |
|
188 |
# -------------------------------------------------- TEST ON OPAC |
189 |
|
190 |
|
191 |
$agent->get_ok( "$opac" , "got opac"); |
192 |
$agent->form_name('searchform'); |
193 |
$agent->field( 'q', 'deuteros' ); |
194 |
$agent->field( 'idx', '' ); |
195 |
$agent->click( ); |
196 |
|
197 |
# ok( $agent->success, 'uploaded file' ); |
198 |
|
199 |
my $text = $agent->text() ; |
200 |
|
201 |
#Tests on UTF-8 |
202 |
|
203 |
ok ( ( length(Encode::encode_utf8($text)) != length($text) ) , 'UTF-8 are multi-byte. Goog') ; |
204 |
ok ($text =~ m/学協会. μμ/, 'UTF-8 chars are correctly present. Good'); |
205 |
|
206 |
|
207 |
|
208 |
#-------------------------------------------------- REVERT |
209 |
|
210 |
$agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' ); |
211 |
$agent->form_name('clean_batch_'.$id_batch); |
212 |
$agent->click(); |
213 |
|
214 |
done_testing(); |
215 |
|
216 |
sub ProgProcesses { |
217 |
return scalar grep /$_[0]/, (split /\n/, `ps -aef`); |
218 |
} |