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';
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
};
48 if (not defined $intranet) {
50 "You must set the environment variable KOHA_INTRANET_URL to ".
51 "point this test to your staff client. If you do not have ".
52 "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
53 "your username and password";
61 my $agent = Test
::WWW
::Mechanize
->new( autocheck
=> 1 );
64 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
65 $agent->form_name('loginform');
66 $agent->field( 'password', $password );
67 $agent->field( 'userid', $user );
68 $agent->field( 'branch', '' );
69 $agent->click_ok( '', 'login to staff client' );
71 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
73 $agent->follow_link_ok( { url_regex
=> qr/tools-home/i }, 'open tools module' );
74 $agent->follow_link_ok( { text
=> 'Stage MARC records for import' },
78 "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
79 [ 'fileToUpload' => [$file], ],
80 'Content_Type' => 'form-data',
82 ok
( $agent->success, 'uploaded file' );
84 $jsonresponse = decode_json
$agent->content();
85 is
( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
86 my $fileid = $jsonresponse->{'fileid'};
88 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
89 'reopen stage MARC page' );
90 $agent->submit_form_ok(
94 'uploadedfileid' => $fileid,
95 'nomatch_action' => 'create_new',
96 'overlay_action' => 'replace',
97 'item_action' => 'always_add',
100 'encoding' => 'UTF-8',
101 'parse_items' => '1',
102 'runinbackground' => '1',
103 'record_type' => 'biblio'
109 $jsonresponse = decode_json
$agent->content();
110 my $jobID = $jsonresponse->{'jobID'};
111 ok
( $jobID, 'have job ID' );
115 # if we haven't completed the batch in two minutes, it's not happening
116 for my $counter ( 1 .. 24 ) {
118 "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID"
119 ); # get job progress
120 $jsonresponse = decode_json
$agent->content();
121 if ( $jsonresponse->{'job_status'} eq 'completed' ) {
127 $jsonresponse->{'job_size'}
129 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
137 is
( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
140 "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
141 'reopen stage MARC page at end of upload'
143 $agent->submit_form_ok(
147 'uploadedfileid' => $fileid,
148 'nomatch_action' => 'create_new',
149 'overlay_action' => 'replace',
150 'item_action' => 'always_add',
153 'encoding' => 'UTF-8',
154 'parse_items' => '1',
155 'runinbackground' => '1',
156 'completedJobID' => $jobID,
157 'record_type' => 'biblio'
163 $agent->follow_link_ok( { text
=> 'Manage staged records' }, 'view batch' );
166 if ( $marcflavour eq 'UNIMARC' ) {
167 $bookdescription = 'Jeffrey Esakov et Tom Weiss';
170 $bookdescription = 'Data structures';
173 # Save the staged records URI for later use
174 my $staged_records_uri = $agent->uri;
176 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
177 # Get datatable for the batch id
179 "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
180 'get the datatable for the new batch id'
182 $jsonresponse = decode_json
$agent->content;
183 like
( $jsonresponse->{ aaData
}[0]->{ citation
}, qr/$bookdescription/, 'found book' );
184 is
( $jsonresponse->{ aaData
}[0]->{ status
}, 'staged', 'record marked as staged' );
185 is
( $jsonresponse->{ aaData
}[0]->{ overlay_status
}, 'no_match', 'record has no matches' );
187 # Back to the manage staged records page
188 $agent->get($staged_records_uri);
189 $agent->form_number(6);
190 $agent->field( 'framework', '' );
191 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
193 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
194 $jsonresponse = decode_json
$agent->content;
195 is
( $jsonresponse->{ aaData
}[0]->{ status
}, 'imported', 'record marked as imported' );
197 my $biblionumber = $jsonresponse->{aaData
}[0]->{matched
};
200 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
201 'getting imported bib' );
202 $agent->content_contains( 'Details for ' . $bookdescription,
205 $agent->get($staged_records_uri);
206 $agent->form_number(5);
207 $agent->click_ok( 'mainformsubmit', "revert import" );
209 "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
210 'getting reverted bib' );
211 $agent->content_contains( 'The record you requested does not exist',
214 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
215 $jsonresponse = decode_json
$agent->content;
216 is
( $jsonresponse->{ aaData
}[0]->{ status
}, 'reverted', 'record marked as reverted' );