Bug 18807: Fix tests www/batch.t
[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 tests => 26;
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 BAIL_OUT("You must set the environment variable KOHA_INTRANET_URL to ".
50          "point this test to your staff client. If you do not have ".
51          "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
52          "your username and password") unless $intranet;
53
54 $intranet =~ s#/$##;
55
56 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
57 my $jsonresponse;
58
59 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
60 $agent->form_name('loginform');
61 $agent->field( 'password', $password );
62 $agent->field( 'userid',   $user );
63 $agent->field( 'branch',   '' );
64 $agent->click_ok( '', 'login to staff client' );
65
66 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
67
68 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
69 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
70     'go to stage MARC' );
71
72 $agent->post(
73     "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
74     [ 'fileToUpload' => [$file], ],
75     'Content_Type' => 'form-data',
76 );
77 ok( $agent->success, 'uploaded file' );
78
79 $jsonresponse = decode_json $agent->content();
80 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
81 my $fileid = $jsonresponse->{'fileid'};
82
83 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
84     'reopen stage MARC page' );
85 $agent->submit_form_ok(
86     {
87         form_number => 5,
88         fields      => {
89             'uploadedfileid'  => $fileid,
90             'nomatch_action'  => 'create_new',
91             'overlay_action'  => 'replace',
92             'item_action'     => 'always_add',
93             'matcher'         => '',
94             'comments'        => '',
95             'encoding'        => 'UTF-8',
96             'parse_items'     => '1',
97             'runinbackground' => '1',
98             'record_type'     => 'biblio'
99         }
100     },
101     'stage MARC'
102 );
103
104 $jsonresponse = decode_json $agent->content();
105 my $jobID = $jsonresponse->{'jobID'};
106 ok( $jobID, 'have job ID' );
107
108 my $completed = 0;
109
110 # if we haven't completed the batch in two minutes, it's not happening
111 for my $counter ( 1 .. 24 ) {
112     $agent->get(
113         "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
114         "get job progress"
115     );
116     $jsonresponse = decode_json $agent->content();
117     if ( $jsonresponse->{'job_status'} eq 'completed' ) {
118         $completed = 1;
119         last;
120     }
121     warn(
122         (
123             $jsonresponse->{'job_size'}
124             ? floor(
125                 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
126               )
127             : '100'
128         )
129         . "% completed"
130     );
131     sleep 5;
132 }
133 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
134
135 $agent->get_ok(
136     "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
137     'reopen stage MARC page at end of upload'
138 );
139 $agent->submit_form_ok(
140     {
141         form_number => 5,
142         fields      => {
143             'uploadedfileid'  => $fileid,
144             'nomatch_action'  => 'create_new',
145             'overlay_action'  => 'replace',
146             'item_action'     => 'always_add',
147             'matcher'         => '1',
148             'comments'        => '',
149             'encoding'        => 'UTF-8',
150             'parse_items'     => '1',
151             'runinbackground' => '1',
152             'completedJobID'  => $jobID,
153             'record_type'     => 'biblio'
154         }
155     },
156     'stage MARC'
157 );
158
159 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
160
161 my $bookdescription;
162 if ( $marcflavour eq 'UNIMARC' ) {
163     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
164 }
165 else {
166     $bookdescription = 'Data structures';
167 }
168
169 # Save the staged records URI for later use
170 my $staged_records_uri = $agent->uri;
171
172 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
173 # Get datatable for the batch id
174 $agent->get_ok(
175     "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
176     'get the datatable for the new batch id'
177 );
178 $jsonresponse = decode_json $agent->content;
179 like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' );
180 is( $jsonresponse->{ aaData }[0]->{ status }, 'staged', 'record marked as staged' );
181 is( $jsonresponse->{ aaData }[0]->{ overlay_status }, 'no_match', 'record has no matches' );
182
183 # Back to the manage staged records page
184 $agent->get($staged_records_uri);
185 $agent->form_number(6);
186 $agent->field( 'framework', '' );
187 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
188
189 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
190 $jsonresponse = decode_json $agent->content;
191 is( $jsonresponse->{ aaData }[0]->{ status }, 'imported', 'record marked as imported' );
192
193 my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
194
195 $agent->get_ok(
196     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
197     'getting imported bib' );
198 $agent->content_contains( 'Details for ' . $bookdescription,
199     'bib is imported' );
200
201 $agent->get($staged_records_uri);
202 $agent->form_number(5);
203 $agent->click_ok( 'mainformsubmit', "revert import" );
204 $agent->get_ok(
205     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
206     'getting reverted bib' );
207 $agent->content_contains( 'The record you requested does not exist',
208     'bib is gone' );
209
210 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
211 $jsonresponse = decode_json $agent->content;
212 is( $jsonresponse->{ aaData }[0]->{ status }, 'reverted', 'record marked as reverted' );
213
214 1;