Bug 20434: Update UNIMARC framework - auth (TM)
[koha.git] / t / db_dependent / www / batch.t
blobfa1acc07fbf7f8ff37739d9add59c6676206c93f
1 #!/usr/bin/perl
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
9 # version.
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
20 use Modern::Perl;
22 use utf8;
23 use Test::More; #See plan tests => \d+ below
24 use Test::WWW::Mechanize;
25 use XML::Simple;
26 use JSON;
27 use File::Basename;
28 use File::Spec;
29 use POSIX;
31 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
33 my $koha_conf = $ENV{KOHA_CONF};
34 my $xml = XMLin($koha_conf);
36 use C4::Context;
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
40 my $file =
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) {
50 plan skip_all =>
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";
56 else {
57 plan tests => 26;
60 $intranet =~ s#/$##;
62 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
63 my $jsonresponse;
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' },
76 'go to stage MARC' );
78 $agent->post(
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(
93 form_number => 5,
94 fields => {
95 'uploadedfileid' => $fileid,
96 'nomatch_action' => 'create_new',
97 'overlay_action' => 'replace',
98 'item_action' => 'always_add',
99 'matcher' => '',
100 'comments' => '',
101 'encoding' => 'UTF-8',
102 'parse_items' => '1',
103 'runinbackground' => '1',
104 'record_type' => 'biblio'
107 'stage MARC'
110 $jsonresponse = decode_json $agent->content();
111 my $jobID = $jsonresponse->{'jobID'};
112 ok( $jobID, 'have job ID' );
114 my $completed = 0;
116 # if we haven't completed the batch in two minutes, it's not happening
117 for my $counter ( 1 .. 24 ) {
118 $agent->get(
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' ) {
123 $completed = 1;
124 last;
126 warn(
128 $jsonresponse->{'job_size'}
129 ? floor(
130 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
132 : '100'
134 . "% completed"
136 sleep 5;
138 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
140 $agent->get_ok(
141 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
142 'reopen stage MARC page at end of upload'
144 $agent->submit_form_ok(
146 form_number => 5,
147 fields => {
148 'uploadedfileid' => $fileid,
149 'nomatch_action' => 'create_new',
150 'overlay_action' => 'replace',
151 'item_action' => 'always_add',
152 'matcher' => '1',
153 'comments' => '',
154 'encoding' => 'UTF-8',
155 'parse_items' => '1',
156 'runinbackground' => '1',
157 'completedJobID' => $jobID,
158 'record_type' => 'biblio'
161 'stage MARC'
164 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
166 my $bookdescription;
167 if ( $marcflavour eq 'UNIMARC' ) {
168 $bookdescription = 'Jeffrey Esakov et Tom Weiss';
170 else {
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
179 $agent->get_ok(
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};
200 $agent->get_ok(
201 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
202 'getting imported bib' );
203 $agent->content_contains( 'Details for ' . $bookdescription,
204 'bib is imported' );
206 $agent->get($staged_records_uri);
207 $agent->form_number(5);
208 $agent->click_ok( 'mainformsubmit', "revert import" );
209 $agent->get_ok(
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',
213 'bib is gone' );
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' );