Line 0
Link Here
|
|
|
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 tests => 20; |
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 |
|
29 |
my $testdir = File::Spec->rel2abs( dirname(__FILE__) ); |
30 |
|
31 |
my $koha_conf = $ENV{KOHA_CONF}; |
32 |
my $xml = XMLin($koha_conf); |
33 |
|
34 |
use C4::Context; |
35 |
my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21'; |
36 |
|
37 |
# For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same |
38 |
my $file = |
39 |
$marcflavour eq 'UNIMARC' |
40 |
? "$testdir/data/unimarcrecord.mrc" |
41 |
: "$testdir/data/marc21record.mrc"; |
42 |
|
43 |
my $user = $ENV{KOHA_USER} || $xml->{config}->{user}; |
44 |
my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass}; |
45 |
my $intranet = $ENV{KOHA_INTRANET_URL}; |
46 |
my $opac = $ENV{KOHA_OPAC_URL}; |
47 |
|
48 |
BAIL_OUT("You must set the environment variable KOHA_INTRANET_URL to ". |
49 |
"point this test to your staff client. If you do not have ". |
50 |
"KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ". |
51 |
"your username and password") unless $intranet; |
52 |
|
53 |
$intranet =~ s#/$##; |
54 |
$opac =~ s#/$##; |
55 |
|
56 |
my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); |
57 |
my $jsonresponse; |
58 |
|
59 |
$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' ); |
60 |
$agent->form_name('loginform'); |
61 |
$agent->field( 'password', $password ); |
62 |
$agent->field( 'userid', $user ); |
63 |
$agent->field( 'branch', '' ); |
64 |
$agent->click_ok( '', 'login to staff client' ); |
65 |
|
66 |
$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' ); |
67 |
|
68 |
$agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' ); |
69 |
$agent->follow_link_ok( { text => 'Stage MARC records for import' }, |
70 |
'go to stage MARC' ); |
71 |
|
72 |
$agent->post( |
73 |
"$intranet/cgi-bin/koha/tools/upload-file.pl", |
74 |
[ 'fileToUpload' => [$file], ], |
75 |
'Content_Type' => 'form-data', |
76 |
); |
77 |
ok( $agent->success, 'uploaded file' ); |
78 |
|
79 |
$jsonresponse = decode_json $agent->content(); |
80 |
is( $jsonresponse->{'status'}, 'done', 'upload succeeded' ); |
81 |
my $fileid = $jsonresponse->{'fileid'}; |
82 |
|
83 |
$agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl", |
84 |
'reopen stage MARC page' ); |
85 |
$agent->submit_form_ok( |
86 |
{ |
87 |
form_number => 5, |
88 |
fields => { |
89 |
'uploadedfileid' => $fileid, |
90 |
'nomatch_action' => 'create_new', |
91 |
'overlay_action' => 'replace', |
92 |
'item_action' => 'always_add', |
93 |
'matcher' => '', |
94 |
'comments' => '', |
95 |
'encoding' => 'utf8', |
96 |
'parse_items' => '1', |
97 |
'runinbackground' => '1', |
98 |
} |
99 |
}, |
100 |
'stage MARC' |
101 |
); |
102 |
|
103 |
$jsonresponse = decode_json $agent->content(); |
104 |
my $jobID = $jsonresponse->{'jobID'}; |
105 |
ok( $jobID, 'have job ID' ); |
106 |
|
107 |
my $completed = 0; |
108 |
|
109 |
# if we haven't completed the batch in two minutes, it's not happening |
110 |
for my $counter ( 1 .. 24 ) { |
111 |
$agent->get( |
112 |
"$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID", |
113 |
"get job progress" |
114 |
); |
115 |
$jsonresponse = decode_json $agent->content(); |
116 |
if ( $jsonresponse->{'job_status'} eq 'completed' ) { |
117 |
$completed = 1; |
118 |
last; |
119 |
} |
120 |
warn( |
121 |
( |
122 |
$jsonresponse->{'job_size'} |
123 |
? floor( |
124 |
100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'} |
125 |
) |
126 |
: '100' |
127 |
) |
128 |
. "% completed" |
129 |
); |
130 |
sleep 5; |
131 |
} |
132 |
is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' ); |
133 |
|
134 |
$agent->get_ok( |
135 |
"$intranet/cgi-bin/koha/tools/stage-marc-import.pl", |
136 |
'reopen stage MARC page at end of upload' |
137 |
); |
138 |
$agent->submit_form_ok( |
139 |
{ |
140 |
form_number => 5, |
141 |
fields => { |
142 |
'uploadedfileid' => $fileid, |
143 |
'nomatch_action' => 'create_new', |
144 |
'overlay_action' => 'replace', |
145 |
'item_action' => 'always_add', |
146 |
'matcher' => '1', |
147 |
'comments' => '', |
148 |
'encoding' => 'utf8', |
149 |
'parse_items' => '1', |
150 |
'runinbackground' => '1', |
151 |
'completedJobID' => $jobID, |
152 |
} |
153 |
}, |
154 |
'stage MARC' |
155 |
); |
156 |
|
157 |
$agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' ); |
158 |
my $bookdescription; |
159 |
if ( $marcflavour eq 'UNIMARC' ) { |
160 |
$bookdescription = 'Jeffrey Esakov et Tom Weiss'; |
161 |
} |
162 |
else { |
163 |
$bookdescription = 'Data structures'; |
164 |
} |
165 |
$agent->content_contains( $bookdescription, 'found book' ); |
166 |
$agent->form_number(5); |
167 |
$agent->field( 'framework', '' ); |
168 |
$agent->click_ok( 'mainformsubmit', "imported records into catalog" ); |
169 |
my $newbib; |
170 |
foreach my $link ( $agent->links() ) { |
171 |
if ( $link->url() =~ m#/cgi-bin/koha/catalogue/detail.pl\?biblionumber=# ) { |
172 |
$newbib = $link->text(); |
173 |
$agent->link_content_like( [$link], qr/$bookdescription/, |
174 |
'successfully imported record' ); |
175 |
last; |
176 |
} |
177 |
} |
178 |
|
179 |
$agent->form_number(4); |
180 |
$agent->click_ok( 'mainformsubmit', "revert import" ); |
181 |
$agent->get_ok( |
182 |
"$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$newbib", |
183 |
'getting reverted bib' ); |
184 |
$agent->content_contains( 'The record you requested does not exist', |
185 |
'bib is gone' ); |