Bug 31069: Remove ExplodedTerms dependency on templates
[koha.git] / t / lib / Mocks / Zebra.pm
1 package t::lib::Mocks::Zebra;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Test::More;
20 use File::Basename qw(dirname );
21 use File::Temp qw( tempdir );
22 use File::Path qw( rmtree );
23 use JSON qw( decode_json );
24 use C4::ImportBatch;
25
26 =head1 NAME
27
28 t::lib::Mocks::Zebra - Trying to mock zebra index
29
30 IMPORTANT NOTE: This module is not working as you may think it could work.
31
32 It will effectively create a new koha-conf.xml file in a temporary directory with zebra config files correctly.
33 So it will not affect the koha-conf used by plack (and so the UI).
34
35 If you pass koha_conf to the constructor the usual zebra db will be used, otherwise a new koha-conf.xml file will be generated
36 and the usual zebra db will not be affected. However you must pass $ENV{KOHA_CONF} if you want to test the UI.
37
38 =cut
39
40
41 sub new {
42     my ( $class, $params ) = @_;
43
44     my $marcflavour = $params->{marcflavour} ? lc($params->{marcflavour}) : 'marc21';
45     my $koha_conf = $params->{koha_conf};
46
47     my $datadir = tempdir();
48     my $zebra_db_dir;
49     unless ( $koha_conf ) {
50         system(dirname(__FILE__) . "/../../db_dependent/zebra_config.pl $datadir $marcflavour");
51
52         Koha::Caches->get_instance('config')->flush_all;
53         $koha_conf = "$datadir/etc/koha-conf.xml";
54         my $context = C4::Context->new($koha_conf);
55         $context->set_context();
56         $zebra_db_dir = "$datadir/etc/koha/zebradb/";
57     } else {
58         $koha_conf = $ENV{KOHA_CONF};
59         $zebra_db_dir = dirname($koha_conf);
60     }
61
62     my $self = {
63         datadir   => $datadir,
64         koha_conf => $koha_conf,
65         zebra_db_dir => $zebra_db_dir,
66         intranet  => $params->{intranet},
67         opac      => $params->{opac}
68     };
69
70     return bless $self, $class;
71 }
72
73 # function that launches the zebra daemon
74 sub launch_zebra {
75     my ( $self ) = @_;
76
77     my $datadir = $self->{datadir};
78     my $koha_conf = $self->{koha_conf};
79
80     unlink("$datadir/zebra.log");
81     my $zebra_pid = fork();
82     if ( $zebra_pid == 0 ) {
83         exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
84         exit;
85     }
86     sleep( 1 );
87     $self->{zebra_pid} = $zebra_pid;
88 }
89
90 sub launch_indexer {
91     my ($self) = @_;
92     my $rootdir       = dirname(__FILE__) . '/../../../';
93     my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
94
95     my $indexer_pid = fork();
96
97     if ( $indexer_pid == 0 ) {
98         exec("$rebuild_zebra -daemon -sleep 5");
99         exit;
100     }
101     sleep( 1 );
102     $self->{indexer_pid} = $indexer_pid;
103 }
104
105 sub load_records {
106     my ( $self, $marc_dir, $marc_format, $record_type, $init ) = @_;
107
108     my $datadir = $self->{datadir};
109     my $zebra_cfg = $self->{zebra_db_dir}
110       . ( $record_type eq 'biblios'
111         ? '/zebra-biblios-dom.cfg'
112         : '/zebra-authorities-dom.cfg' );
113
114     my @cmds;
115     push @cmds, "zebraidx -c $zebra_cfg  -v none,fatal -g $marc_format -d $record_type init" if $init;
116     push @cmds, "zebraidx -c $zebra_cfg  -v none,fatal -g $marc_format -d $record_type update $marc_dir";
117     push @cmds, "zebraidx -c $zebra_cfg  -v none,fatal -g $marc_format -d $record_type commit";
118
119     for my $cmd ( @cmds ) {
120         system($cmd);
121     }
122 }
123
124 sub load_records_ui {
125     my ( $self, $file ) = @_;
126     my $jsonresponse;
127     my $cgi_root = $self->{intranet} . '/cgi-bin/koha';
128
129     our $agent = Test::WWW::Mechanize->new( autocheck => 1 );
130     $agent->get_ok( "$cgi_root/mainpage.pl", 'connect to intranet' );
131     $agent->form_name('loginform');
132     $agent->field( 'userid', $ENV{KOHA_PASS} );
133     $agent->field( 'password', $ENV{KOHA_USER} );
134     $agent->field( 'branch',   '' );
135     $agent->click_ok( '', 'login to staff interface' );
136
137     $agent->get_ok( "$cgi_root/mainpage.pl", 'load main page' );
138
139     $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
140     $agent->follow_link_ok( { text => 'Stage MARC records for import' },
141         'go to stage MARC' );
142
143     $agent->post(
144         "$cgi_root/tools/upload-file.pl?temp=1",
145         [ 'fileToUpload' => [$file], ],
146         'Content_Type' => 'form-data',
147     );
148     ok( $agent->success, 'uploaded file' );
149
150     $jsonresponse = decode_json $agent->content();
151     is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
152     my $fileid = $jsonresponse->{'fileid'};
153
154     $agent->get_ok( "$cgi_root/tools/stage-marc-import.pl",
155         'reopen stage MARC page' );
156     $agent->submit_form_ok(
157         {
158             form_number => 5,
159             fields      => {
160                 'uploadedfileid'  => $fileid,
161                 'nomatch_action'  => 'create_new',
162                 'overlay_action'  => 'replace',
163                 'item_action'     => 'always_add',
164                 'matcher'         => '',
165                 'comments'        => '',
166                 'encoding'        => 'utf8',
167                 'parse_items'     => '1',
168                 'runinbackground' => '1',
169                 'record_type'     => 'biblio'
170             }
171         },
172         'stage MARC'
173     );
174
175     $jsonresponse = decode_json $agent->content();
176     my $jobID = $jsonresponse->{'jobID'};
177     ok( $jobID, 'have job ID' );
178
179     my $completed = 0;
180
181     # if we haven't completed the batch in two minutes, it's not happening
182     for my $counter ( 1 .. 24 ) {
183         $agent->get(
184             "$cgi_root/tools/background-job-progress.pl?jobID=$jobID"
185         ); # get job progress
186         $jsonresponse = decode_json $agent->content();
187         if ( $jsonresponse->{'job_status'} eq 'completed' ) {
188             $completed = 1;
189             last;
190         }
191         warn(
192             (
193                 $jsonresponse->{'job_size'}
194                 ? floor(
195                     100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
196                   )
197                 : '100'
198             )
199             . "% completed"
200         );
201         sleep 5;
202     }
203     is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
204
205     $agent->get_ok(
206         "$cgi_root/tools/stage-marc-import.pl",
207         'reopen stage MARC page at end of upload'
208     );
209     $agent->submit_form_ok(
210         {
211             form_number => 5,
212             fields      => {
213                 'uploadedfileid'  => $fileid,
214                 'nomatch_action'  => 'create_new',
215                 'overlay_action'  => 'replace',
216                 'item_action'     => 'always_add',
217                 'matcher'         => '1',
218                 'comments'        => '',
219                 'encoding'        => 'utf8',
220                 'parse_items'     => '1',
221                 'runinbackground' => '1',
222                 'completedJobID'  => $jobID,
223                 'record_type'     => 'biblio'
224             }
225         },
226         'stage MARC'
227     );
228
229     $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
230
231
232     $agent->form_number(6);
233     $agent->field( 'framework', '' );
234     $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
235     my $webpage = $agent->{content};
236
237     $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
238     my $batch_id = $2;
239
240     # wait enough time for the indexer
241     sleep 10;
242
243     return $batch_id;
244
245 }
246
247 sub clean_records {
248     my ( $self, $batch_id ) = @_;
249
250     my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
251     my $cgi_root = $self->{intranet} . '/cgi-bin/koha';
252
253     my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
254                     { order_by => 'import_record_id', order_by_direction => 'DESC' });
255     my $biblionumber = $data->[0]->{'matched_biblionumber'};
256
257     $agent->get_ok( "$cgi_root/tools/manage-marc-import.pl", 'view and clean batch' );
258     $agent->form_name('clean_batch_'.$batch_id);
259     $agent->click();
260     $agent->get_ok( "$cgi_root/catalogue/detail.pl?biblionumber=$biblionumber", 'biblio on intranet' );
261     $agent->get_ok( "$cgi_root/cataloguing/addbiblio.pl?op=delete&biblionumber=$biblionumber", 'biblio deleted' );
262
263 }
264
265 sub cleanup {
266     my ( $self ) = @_;
267     kill 9, $self->{zebra_pid}   if defined $self->{zebra_pid};
268     kill 9, $self->{indexer_pid} if defined $self->{indexer_pid};
269     # Clean up the Zebra files since the child process was just shot
270     rmtree $self->{datadir};
271 }
272
273 1;