3 # Copyright 2012 C & P Bibliography Services
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
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.
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
22 use Test::More tests => 20;
23 use Test::WWW::Mechanize;
30 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
32 my $koha_conf = $ENV{KOHA_CONF};
33 my $xml = XMLin($koha_conf);
36 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
38 # For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same
40 $marcflavour eq 'UNIMARC'
41 ? "$testdir/data/unimarcrecord.mrc"
42 : "$testdir/data/marc21record.mrc";
44 my $user = $ENV{KOHA_USER} || $xml->{config}->{user};
45 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
46 my $intranet = $ENV{KOHA_INTRANET_URL};
47 my $opac = $ENV{KOHA_OPAC_URL};
49 BAIL_OUT("You must set the environment variable KOHA_INTRANET_URL to ".
50 "point this test to your staff client. If you do not have ".
51 "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
52 "your username and password") unless $intranet;
57 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
60 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
61 $agent->form_name('loginform');
62 $agent->field( 'password', $password );
63 $agent->field( 'userid', $user );
64 $agent->field( 'branch', '' );
65 $agent->click_ok( '', 'login to staff client' );
67 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
69 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
70 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
74 "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
75 [ 'fileToUpload' => [$file], ],
76 'Content_Type' => 'form-data',
78 ok( $agent->success, 'uploaded file' );
80 $jsonresponse = decode_json $agent->content();
81 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
82 my $fileid = $jsonresponse->{'fileid'};
84 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
85 'reopen stage MARC page' );
86 $agent->submit_form_ok(
90 'uploadedfileid' => $fileid,
91 'nomatch_action' => 'create_new',
92 'overlay_action' => 'replace',
93 'item_action' => 'always_add',
98 'runinbackground' => '1',
99 'record_type' => 'biblio'
105 $jsonresponse = decode_json $agent->content();
106 my $jobID = $jsonresponse->{'jobID'};
107 ok( $jobID, 'have job ID' );
111 # if we haven't completed the batch in two minutes, it's not happening
112 for my $counter ( 1 .. 24 ) {
114 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
117 $jsonresponse = decode_json $agent->content();
118 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
124 $jsonresponse->{'job_size'}
126 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
134 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
137 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
138 'reopen stage MARC page at end of upload'
140 $agent->submit_form_ok(
144 'uploadedfileid' => $fileid,
145 'nomatch_action' => 'create_new',
146 'overlay_action' => 'replace',
147 'item_action' => 'always_add',
150 'encoding' => 'utf8',
151 'parse_items' => '1',
152 'runinbackground' => '1',
153 'completedJobID' => $jobID,
154 'record_type' => 'biblio'
160 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
163 if ( $marcflavour eq 'UNIMARC' ) {
164 $bookdescription = 'Jeffrey Esakov et Tom Weiss';
167 $bookdescription = 'Data structures';
170 # Save the staged records URI for later use
171 my $staged_records_uri = $agent->uri;
173 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
174 # Get datatable for the batch id
176 "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
177 'get the datatable for the new batch id'
179 $jsonresponse = decode_json $agent->content;
180 like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' );
181 my $biblionumber = $jsonresponse->{ aaData }[0]->{ import_record_id };
182 # Back to the manage staged records page
183 $agent->get($staged_records_uri);
184 $agent->form_number(6);
185 $agent->field( 'framework', '' );
186 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
188 $agent->form_number(5);
189 $agent->click_ok( 'mainformsubmit', "revert import" );
191 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
192 'getting reverted bib' );
193 $agent->content_contains( 'The record you requested does not exist',