3 # Copyright 2012 C & P Bibliography Services
4 # Copyright 2017 Koha Development Team
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More; #See plan tests => \d+ below
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';
39 $marcflavour eq 'UNIMARC'
40 ? "$testdir/data/unimarcrecord.mrc"
41 : "$testdir/data/marc21record.mrc";
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};
47 if (not defined $intranet) {
49 "You must set the environment variable KOHA_INTRANET_URL to ".
50 "point this test to your staff interface. If you do not have ".
51 "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
52 "your username and password";
60 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
63 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
64 $agent->form_name('loginform');
65 $agent->field( 'password', $password );
66 $agent->field( 'userid', $user );
67 $agent->field( 'branch', '' );
68 $agent->click_ok( '', 'login to staff interface' );
70 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
72 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
73 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
77 "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
78 [ 'fileToUpload' => [$file], ],
79 'Content_Type' => 'form-data',
81 ok( $agent->success, 'uploaded file' );
83 $jsonresponse = decode_json $agent->content();
84 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
85 my $fileid = $jsonresponse->{'fileid'};
87 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
88 'reopen stage MARC page' );
89 $agent->submit_form_ok(
93 'uploadedfileid' => $fileid,
94 'nomatch_action' => 'create_new',
95 'overlay_action' => 'replace',
96 'item_action' => 'always_add',
99 'encoding' => 'UTF-8',
100 'parse_items' => '1',
101 'runinbackground' => '1',
102 'record_type' => 'biblio'
108 $jsonresponse = decode_json $agent->content();
109 my $jobID = $jsonresponse->{'jobID'};
110 ok( $jobID, 'have job ID' );
114 # if we haven't completed the batch in two minutes, it's not happening
115 for my $counter ( 1 .. 24 ) {
117 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID"
118 ); # get job progress
119 $jsonresponse = decode_json $agent->content();
120 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
126 $jsonresponse->{'job_size'}
128 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
136 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
139 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
140 'reopen stage MARC page at end of upload'
142 $agent->submit_form_ok(
146 'uploadedfileid' => $fileid,
147 'nomatch_action' => 'create_new',
148 'overlay_action' => 'replace',
149 'item_action' => 'always_add',
152 'encoding' => 'UTF-8',
153 'parse_items' => '1',
154 'runinbackground' => '1',
155 'completedJobID' => $jobID,
156 'record_type' => 'biblio'
162 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
165 if ( $marcflavour eq 'UNIMARC' ) {
166 $bookdescription = 'Jeffrey Esakov et Tom Weiss';
169 $bookdescription = 'Data structures';
172 # Save the staged records URI for later use
173 my $staged_records_uri = $agent->uri;
175 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
176 # Get datatable for the batch id
178 "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
179 'get the datatable for the new batch id'
181 $jsonresponse = decode_json $agent->content;
182 like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' );
183 is( $jsonresponse->{ aaData }[0]->{ status }, 'staged', 'record marked as staged' );
184 is( $jsonresponse->{ aaData }[0]->{ overlay_status }, 'no_match', 'record has no matches' );
186 # Back to the manage staged records page
187 $agent->get($staged_records_uri);
188 $agent->form_number(6);
189 $agent->field( 'framework', '' );
190 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
192 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
193 $jsonresponse = decode_json $agent->content;
194 is( $jsonresponse->{ aaData }[0]->{ status }, 'imported', 'record marked as imported' );
196 my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
199 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
200 'getting imported bib' );
201 $agent->content_contains( 'Details for ' . $bookdescription,
204 $agent->get($staged_records_uri);
205 $agent->form_number(5);
206 $agent->click_ok( 'mainformsubmit', "revert import" );
208 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
209 'getting reverted bib' );
210 $agent->content_contains( 'The record you requested does not exist',
213 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
214 $jsonresponse = decode_json $agent->content;
215 is( $jsonresponse->{ aaData }[0]->{ status }, 'reverted', 'record marked as reverted' );