3 # Copyright 2012 C & P Bibliography Services
4 # Copyright 2017 Koha Development Team
6 # This is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
11 # This is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
17 # Suite 330, Boston, MA 02111-1307 USA
23 use Test
::More
; #See plan tests => \d+ below
24 use Test
::WWW
::Mechanize
;
31 my $testdir = File
::Spec
->rel2abs( dirname
(__FILE__
) );
33 my $koha_conf = $ENV{KOHA_CONF
};
34 my $xml = XMLin
($koha_conf);
37 my $marcflavour = C4
::Context
->preference('marcflavour') || 'MARC21';
39 # For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same
41 $marcflavour eq 'UNIMARC'
42 ?
"$testdir/data/unimarcrecord.mrc"
43 : "$testdir/data/marc21record.mrc";
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
};
49 if (not defined $intranet) {
51 "You must set the environment variable KOHA_INTRANET_URL to ".
52 "point this test to your staff client. If you do not have ".
53 "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
54 "your username and password";
62 my $agent = Test
::WWW
::Mechanize
->new( autocheck
=> 1 );
65 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
66 $agent->form_name('loginform');
67 $agent->field( 'password', $password );
68 $agent->field( 'userid', $user );
69 $agent->field( 'branch', '' );
70 $agent->click_ok( '', 'login to staff client' );
72 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
74 $agent->follow_link_ok( { url_regex
=> qr/tools-home/i }, 'open tools module' );
75 $agent->follow_link_ok( { text
=> 'Stage MARC records for import' },
79 "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
80 [ 'fileToUpload' => [$file], ],
81 'Content_Type' => 'form-data',
83 ok
( $agent->success, 'uploaded file' );
85 $jsonresponse = decode_json
$agent->content();
86 is
( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
87 my $fileid = $jsonresponse->{'fileid'};
89 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
90 'reopen stage MARC page' );
91 $agent->submit_form_ok(
95 'uploadedfileid' => $fileid,
96 'nomatch_action' => 'create_new',
97 'overlay_action' => 'replace',
98 'item_action' => 'always_add',
101 'encoding' => 'UTF-8',
102 'parse_items' => '1',
103 'runinbackground' => '1',
104 'record_type' => 'biblio'
110 $jsonresponse = decode_json
$agent->content();
111 my $jobID = $jsonresponse->{'jobID'};
112 ok
( $jobID, 'have job ID' );
116 # if we haven't completed the batch in two minutes, it's not happening
117 for my $counter ( 1 .. 24 ) {
119 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID"
120 ); # get job progress
121 $jsonresponse = decode_json
$agent->content();
122 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
128 $jsonresponse->{'job_size'}
130 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
138 is
( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
141 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
142 'reopen stage MARC page at end of upload'
144 $agent->submit_form_ok(
148 'uploadedfileid' => $fileid,
149 'nomatch_action' => 'create_new',
150 'overlay_action' => 'replace',
151 'item_action' => 'always_add',
154 'encoding' => 'UTF-8',
155 'parse_items' => '1',
156 'runinbackground' => '1',
157 'completedJobID' => $jobID,
158 'record_type' => 'biblio'
164 $agent->follow_link_ok( { text
=> 'Manage staged records' }, 'view batch' );
167 if ( $marcflavour eq 'UNIMARC' ) {
168 $bookdescription = 'Jeffrey Esakov et Tom Weiss';
171 $bookdescription = 'Data structures';
174 # Save the staged records URI for later use
175 my $staged_records_uri = $agent->uri;
177 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
178 # Get datatable for the batch id
180 "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
181 'get the datatable for the new batch id'
183 $jsonresponse = decode_json
$agent->content;
184 like
( $jsonresponse->{ aaData
}[0]->{ citation
}, qr/$bookdescription/, 'found book' );
185 is
( $jsonresponse->{ aaData
}[0]->{ status
}, 'staged', 'record marked as staged' );
186 is
( $jsonresponse->{ aaData
}[0]->{ overlay_status
}, 'no_match', 'record has no matches' );
188 # Back to the manage staged records page
189 $agent->get($staged_records_uri);
190 $agent->form_number(6);
191 $agent->field( 'framework', '' );
192 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
194 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
195 $jsonresponse = decode_json
$agent->content;
196 is
( $jsonresponse->{ aaData
}[0]->{ status
}, 'imported', 'record marked as imported' );
198 my $biblionumber = $jsonresponse->{aaData
}[0]->{matched
};
201 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
202 'getting imported bib' );
203 $agent->content_contains( 'Details for ' . $bookdescription,
206 $agent->get($staged_records_uri);
207 $agent->form_number(5);
208 $agent->click_ok( 'mainformsubmit', "revert import" );
210 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
211 'getting reverted bib' );
212 $agent->content_contains( 'The record you requested does not exist',
215 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
216 $jsonresponse = decode_json
$agent->content;
217 is
( $jsonresponse->{ aaData
}[0]->{ status
}, 'reverted', 'record marked as reverted' );