fa1acc07fbf7f8ff37739d9add59c6676206c93f
[koha.git] / 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 # 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.
10 #
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.
14 #
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
18 #
19
20 use Modern::Perl;
21
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;
30
31 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
32
33 my $koha_conf = $ENV{KOHA_CONF};
34 my $xml       = XMLin($koha_conf);
35
36 use C4::Context;
37 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
38
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";
44
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};
48
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";
55 }
56 else {
57     plan tests => 26;
58 }
59
60 $intranet =~ s#/$##;
61
62 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
63 my $jsonresponse;
64
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' );
71
72 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
73
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' );
77
78 $agent->post(
79     "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
80     [ 'fileToUpload' => [$file], ],
81     'Content_Type' => 'form-data',
82 );
83 ok( $agent->success, 'uploaded file' );
84
85 $jsonresponse = decode_json $agent->content();
86 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
87 my $fileid = $jsonresponse->{'fileid'};
88
89 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
90     'reopen stage MARC page' );
91 $agent->submit_form_ok(
92     {
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'
105         }
106     },
107     'stage MARC'
108 );
109
110 $jsonresponse = decode_json $agent->content();
111 my $jobID = $jsonresponse->{'jobID'};
112 ok( $jobID, 'have job ID' );
113
114 my $completed = 0;
115
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;
125     }
126     warn(
127         (
128             $jsonresponse->{'job_size'}
129             ? floor(
130                 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
131               )
132             : '100'
133         )
134         . "% completed"
135     );
136     sleep 5;
137 }
138 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
139
140 $agent->get_ok(
141     "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
142     'reopen stage MARC page at end of upload'
143 );
144 $agent->submit_form_ok(
145     {
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'
159         }
160     },
161     'stage MARC'
162 );
163
164 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
165
166 my $bookdescription;
167 if ( $marcflavour eq 'UNIMARC' ) {
168     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
169 }
170 else {
171     $bookdescription = 'Data structures';
172 }
173
174 # Save the staged records URI for later use
175 my $staged_records_uri = $agent->uri;
176
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'
182 );
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' );
187
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" );
193
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' );
197
198 my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
199
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' );
205
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' );
214
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' );
218