1 package t::lib::Mocks::Zebra;
3 # This file is part of Koha.
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.
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.
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>.
20 use File::Basename qw(dirname );
21 use File::Temp qw( tempdir );
22 use File::Path qw( rmtree );
23 use JSON qw( decode_json );
28 t::lib::Mocks::Zebra - Trying to mock zebra index
30 IMPORTANT NOTE: This module is not working as you may think it could work.
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).
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.
42 my ( $class, $params ) = @_;
44 my $marcflavour = $params->{marcflavour} ? lc($params->{marcflavour}) : 'marc21';
45 my $koha_conf = $params->{koha_conf};
47 my $datadir = tempdir();
49 unless ( $koha_conf ) {
50 system(dirname(__FILE__) . "/../../db_dependent/zebra_config.pl $datadir $marcflavour");
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/";
58 $koha_conf = $ENV{KOHA_CONF};
59 $zebra_db_dir = dirname($koha_conf);
64 koha_conf => $koha_conf,
65 zebra_db_dir => $zebra_db_dir,
66 intranet => $params->{intranet},
67 opac => $params->{opac}
70 return bless $self, $class;
73 # function that launches the zebra daemon
77 my $datadir = $self->{datadir};
78 my $koha_conf = $self->{koha_conf};
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");
87 $self->{zebra_pid} = $zebra_pid;
92 my $rootdir = dirname(__FILE__) . '/../../../';
93 my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
95 my $indexer_pid = fork();
97 if ( $indexer_pid == 0 ) {
98 exec("$rebuild_zebra -daemon -sleep 5");
102 $self->{indexer_pid} = $indexer_pid;
106 my ( $self, $marc_dir, $marc_format, $record_type, $init ) = @_;
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' );
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";
119 for my $cmd ( @cmds ) {
124 sub load_records_ui {
125 my ( $self, $file ) = @_;
127 my $cgi_root = $self->{intranet} . '/cgi-bin/koha';
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' );
137 $agent->get_ok( "$cgi_root/mainpage.pl", 'load main page' );
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' );
144 "$cgi_root/tools/upload-file.pl?temp=1",
145 [ 'fileToUpload' => [$file], ],
146 'Content_Type' => 'form-data',
148 ok( $agent->success, 'uploaded file' );
150 $jsonresponse = decode_json $agent->content();
151 is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
152 my $fileid = $jsonresponse->{'fileid'};
154 $agent->get_ok( "$cgi_root/tools/stage-marc-import.pl",
155 'reopen stage MARC page' );
156 $agent->submit_form_ok(
160 'uploadedfileid' => $fileid,
161 'nomatch_action' => 'create_new',
162 'overlay_action' => 'replace',
163 'item_action' => 'always_add',
166 'encoding' => 'utf8',
167 'parse_items' => '1',
168 'runinbackground' => '1',
169 'record_type' => 'biblio'
175 $jsonresponse = decode_json $agent->content();
176 my $jobID = $jsonresponse->{'jobID'};
177 ok( $jobID, 'have job ID' );
181 # if we haven't completed the batch in two minutes, it's not happening
182 for my $counter ( 1 .. 24 ) {
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' ) {
193 $jsonresponse->{'job_size'}
195 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
203 is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
206 "$cgi_root/tools/stage-marc-import.pl",
207 'reopen stage MARC page at end of upload'
209 $agent->submit_form_ok(
213 'uploadedfileid' => $fileid,
214 'nomatch_action' => 'create_new',
215 'overlay_action' => 'replace',
216 'item_action' => 'always_add',
219 'encoding' => 'utf8',
220 'parse_items' => '1',
221 'runinbackground' => '1',
222 'completedJobID' => $jobID,
223 'record_type' => 'biblio'
229 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
232 $agent->form_number(6);
233 $agent->field( 'framework', '' );
234 $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
235 my $webpage = $agent->{content};
237 $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
240 # wait enough time for the indexer
248 my ( $self, $batch_id ) = @_;
250 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
251 my $cgi_root = $self->{intranet} . '/cgi-bin/koha';
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'};
257 $agent->get_ok( "$cgi_root/tools/manage-marc-import.pl", 'view and clean batch' );
258 $agent->form_name('clean_batch_'.$batch_id);
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' );
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};