Bug 18984: Remove NORMARC support
[srvgit] / t / db_dependent / www / batch.t
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 # Copyright 2017 Koha Development Team
5 #
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.
10 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use utf8;
22 use Test::More; #See plan tests => \d+ below
23 use Test::WWW::Mechanize;
24 use XML::Simple;
25 use JSON;
26 use File::Basename;
27 use File::Spec;
28 use POSIX;
29
30 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
31
32 my $koha_conf = $ENV{KOHA_CONF};
33 my $xml       = XMLin($koha_conf);
34
35 use C4::Context;
36 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
37
38 my $file =
39   $marcflavour eq 'UNIMARC'
40   ? "$testdir/data/unimarcrecord.mrc"
41   : "$testdir/data/marc21record.mrc";
42
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};
46
47 if (not defined $intranet) {
48     plan skip_all =>
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";
53 }
54 else {
55     plan tests => 26;
56 }
57
58 $intranet =~ s#/$##;
59
60 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
61 my $jsonresponse;
62
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' );
69
70 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
71
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' },
74     'go to stage MARC' );
75
76 $agent->post(
77     "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
78     [ 'fileToUpload' => [$file], ],
79     'Content_Type' => 'form-data',
80 );
81 ok( $agent->success, 'uploaded file' );
82
83 $jsonresponse = decode_json $agent->content();
84 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
85 my $fileid = $jsonresponse->{'fileid'};
86
87 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
88     'reopen stage MARC page' );
89 $agent->submit_form_ok(
90     {
91         form_number => 5,
92         fields      => {
93             'uploadedfileid'  => $fileid,
94             'nomatch_action'  => 'create_new',
95             'overlay_action'  => 'replace',
96             'item_action'     => 'always_add',
97             'matcher'         => '',
98             'comments'        => '',
99             'encoding'        => 'UTF-8',
100             'parse_items'     => '1',
101             'runinbackground' => '1',
102             'record_type'     => 'biblio'
103         }
104     },
105     'stage MARC'
106 );
107
108 $jsonresponse = decode_json $agent->content();
109 my $jobID = $jsonresponse->{'jobID'};
110 ok( $jobID, 'have job ID' );
111
112 my $completed = 0;
113
114 # if we haven't completed the batch in two minutes, it's not happening
115 for my $counter ( 1 .. 24 ) {
116     $agent->get(
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' ) {
121         $completed = 1;
122         last;
123     }
124     warn(
125         (
126             $jsonresponse->{'job_size'}
127             ? floor(
128                 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
129               )
130             : '100'
131         )
132         . "% completed"
133     );
134     sleep 5;
135 }
136 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
137
138 $agent->get_ok(
139     "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
140     'reopen stage MARC page at end of upload'
141 );
142 $agent->submit_form_ok(
143     {
144         form_number => 5,
145         fields      => {
146             'uploadedfileid'  => $fileid,
147             'nomatch_action'  => 'create_new',
148             'overlay_action'  => 'replace',
149             'item_action'     => 'always_add',
150             'matcher'         => '1',
151             'comments'        => '',
152             'encoding'        => 'UTF-8',
153             'parse_items'     => '1',
154             'runinbackground' => '1',
155             'completedJobID'  => $jobID,
156             'record_type'     => 'biblio'
157         }
158     },
159     'stage MARC'
160 );
161
162 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
163
164 my $bookdescription;
165 if ( $marcflavour eq 'UNIMARC' ) {
166     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
167 }
168 else {
169     $bookdescription = 'Data structures';
170 }
171
172 # Save the staged records URI for later use
173 my $staged_records_uri = $agent->uri;
174
175 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
176 # Get datatable for the batch id
177 $agent->get_ok(
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'
180 );
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' );
185
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" );
191
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' );
195
196 my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
197
198 $agent->get_ok(
199     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
200     'getting imported bib' );
201 $agent->content_contains( 'Details for ' . $bookdescription,
202     'bib is imported' );
203
204 $agent->get($staged_records_uri);
205 $agent->form_number(5);
206 $agent->click_ok( 'mainformsubmit', "revert import" );
207 $agent->get_ok(
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',
211     'bib is gone' );
212
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' );
216