Bug 15108: OAI-PMH provider improvements
[koha.git] / t / db_dependent / www / batch.t
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 #
5 # This is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # This is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17 #
18
19 use Modern::Perl;
20
21 use utf8;
22 use Test::More tests => 24;
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 BAIL_OUT("You must set the environment variable KOHA_INTRANET_URL to ".
49          "point this test to your staff client. If you do not have ".
50          "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
51          "your username and password") unless $intranet;
52
53 $intranet =~ s#/$##;
54
55 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
56 my $jsonresponse;
57
58 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
59 $agent->form_name('loginform');
60 $agent->field( 'password', $password );
61 $agent->field( 'userid',   $user );
62 $agent->field( 'branch',   '' );
63 $agent->click_ok( '', 'login to staff client' );
64
65 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
66
67 $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
68 $agent->follow_link_ok( { text => 'Stage MARC records for import' },
69     'go to stage MARC' );
70
71 $agent->post(
72     "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
73     [ 'fileToUpload' => [$file], ],
74     'Content_Type' => 'form-data',
75 );
76 ok( $agent->success, 'uploaded file' );
77
78 $jsonresponse = decode_json $agent->content();
79 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
80 my $fileid = $jsonresponse->{'fileid'};
81
82 $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
83     'reopen stage MARC page' );
84 $agent->submit_form_ok(
85     {
86         form_number => 5,
87         fields      => {
88             'uploadedfileid'  => $fileid,
89             'nomatch_action'  => 'create_new',
90             'overlay_action'  => 'replace',
91             'item_action'     => 'always_add',
92             'matcher'         => '',
93             'comments'        => '',
94             'encoding'        => 'utf8',
95             'parse_items'     => '1',
96             'runinbackground' => '1',
97             'record_type'     => 'biblio'
98         }
99     },
100     'stage MARC'
101 );
102
103 $jsonresponse = decode_json $agent->content();
104 my $jobID = $jsonresponse->{'jobID'};
105 ok( $jobID, 'have job ID' );
106
107 my $completed = 0;
108
109 # if we haven't completed the batch in two minutes, it's not happening
110 for my $counter ( 1 .. 24 ) {
111     $agent->get(
112         "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID",
113         "get job progress"
114     );
115     $jsonresponse = decode_json $agent->content();
116     if ( $jsonresponse->{'job_status'} eq 'completed' ) {
117         $completed = 1;
118         last;
119     }
120     warn(
121         (
122             $jsonresponse->{'job_size'}
123             ? floor(
124                 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
125               )
126             : '100'
127         )
128         . "% completed"
129     );
130     sleep 5;
131 }
132 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
133
134 $agent->get_ok(
135     "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
136     'reopen stage MARC page at end of upload'
137 );
138 $agent->submit_form_ok(
139     {
140         form_number => 5,
141         fields      => {
142             'uploadedfileid'  => $fileid,
143             'nomatch_action'  => 'create_new',
144             'overlay_action'  => 'replace',
145             'item_action'     => 'always_add',
146             'matcher'         => '1',
147             'comments'        => '',
148             'encoding'        => 'utf8',
149             'parse_items'     => '1',
150             'runinbackground' => '1',
151             'completedJobID'  => $jobID,
152             'record_type'     => 'biblio'
153         }
154     },
155     'stage MARC'
156 );
157
158 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
159
160 my $bookdescription;
161 if ( $marcflavour eq 'UNIMARC' ) {
162     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
163 }
164 else {
165     $bookdescription = 'Data structures';
166 }
167
168 # Save the staged records URI for later use
169 my $staged_records_uri = $agent->uri;
170
171 my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
172 # Get datatable for the batch id
173 $agent->get_ok(
174     "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
175     'get the datatable for the new batch id'
176 );
177 $jsonresponse = decode_json $agent->content;
178 like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' );
179 is( $jsonresponse->{ aaData }[0]->{ status }, 'staged', 'record marked as staged' );
180 is( $jsonresponse->{ aaData }[0]->{ overlay_status }, 'no_match', 'record has no matches' );
181
182 my $biblionumber = $jsonresponse->{ aaData }[0]->{ import_record_id };
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 $agent->get($staged_records_uri);
194 $agent->form_number(5);
195 $agent->click_ok( 'mainformsubmit', "revert import" );
196 $agent->get_ok(
197     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
198     'getting reverted bib' );
199 $agent->content_contains( 'The record you requested does not exist',
200     'bib is gone' );
201
202 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
203 $jsonresponse = decode_json $agent->content;
204 is( $jsonresponse->{ aaData }[0]->{ status }, 'reverted', 'record marked as reverted' );
205
206 1;