Bug 24545: Fix license statements
[koha-equinox.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 # 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 # For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same
39 my $file =
40   $marcflavour eq 'UNIMARC'
41   ? "$testdir/data/unimarcrecord.mrc"
42   : "$testdir/data/marc21record.mrc";
43
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};
47
48 if (not defined $intranet) {
49     plan skip_all =>
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";
54 }
55 else {
56     plan tests => 26;
57 }
58
59 $intranet =~ s#/$##;
60
61 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
62 my $jsonresponse;
63
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' );
70
71 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
72
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' },
75     'go to stage MARC' );
76
77 $agent->post(
78     "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
79     [ 'fileToUpload' => [$file], ],
80     'Content_Type' => 'form-data',
81 );
82 ok( $agent->success, 'uploaded file' );
83
84 $jsonresponse = decode_json $agent->content();
85 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
86 my $fileid = $jsonresponse->{'fileid'};
87
88 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
89     'reopen stage MARC page' );
90 $agent->submit_form_ok(
91     {
92         form_number => 5,
93         fields      => {
94             'uploadedfileid'  => $fileid,
95             'nomatch_action'  => 'create_new',
96             'overlay_action'  => 'replace',
97             'item_action'     => 'always_add',
98             'matcher'         => '',
99             'comments'        => '',
100             'encoding'        => 'UTF-8',
101             'parse_items'     => '1',
102             'runinbackground' => '1',
103             'record_type'     => 'biblio'
104         }
105     },
106     'stage MARC'
107 );
108
109 $jsonresponse = decode_json $agent->content();
110 my $jobID = $jsonresponse->{'jobID'};
111 ok( $jobID, 'have job ID' );
112
113 my $completed = 0;
114
115 # if we haven't completed the batch in two minutes, it's not happening
116 for my $counter ( 1 .. 24 ) {
117     $agent->get(
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' ) {
122         $completed = 1;
123         last;
124     }
125     warn(
126         (
127             $jsonresponse->{'job_size'}
128             ? floor(
129                 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
130               )
131             : '100'
132         )
133         . "% completed"
134     );
135     sleep 5;
136 }
137 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
138
139 $agent->get_ok(
140     "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
141     'reopen stage MARC page at end of upload'
142 );
143 $agent->submit_form_ok(
144     {
145         form_number => 5,
146         fields      => {
147             'uploadedfileid'  => $fileid,
148             'nomatch_action'  => 'create_new',
149             'overlay_action'  => 'replace',
150             'item_action'     => 'always_add',
151             'matcher'         => '1',
152             'comments'        => '',
153             'encoding'        => 'UTF-8',
154             'parse_items'     => '1',
155             'runinbackground' => '1',
156             'completedJobID'  => $jobID,
157             'record_type'     => 'biblio'
158         }
159     },
160     'stage MARC'
161 );
162
163 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
164
165 my $bookdescription;
166 if ( $marcflavour eq 'UNIMARC' ) {
167     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
168 }
169 else {
170     $bookdescription = 'Data structures';
171 }
172
173 # Save the staged records URI for later use
174 my $staged_records_uri = $agent->uri;
175
176 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
177 # Get datatable for the batch id
178 $agent->get_ok(
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'
181 );
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' );
186
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" );
192
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' );
196
197 my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
198
199 $agent->get_ok(
200     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
201     'getting imported bib' );
202 $agent->content_contains( 'Details for ' . $bookdescription,
203     'bib is imported' );
204
205 $agent->get($staged_records_uri);
206 $agent->form_number(5);
207 $agent->click_ok( 'mainformsubmit', "revert import" );
208 $agent->get_ok(
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',
212     'bib is gone' );
213
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' );
217