View | Details | Raw Unified | Return to bug 19185
Collapse All | Expand All

(-)a/t/db_dependent/selenium/regressions.t (-41 / +1 lines)
Lines 20-26 use utf8; Link Here
20
20
21
use C4::Context;
21
use C4::Context;
22
22
23
use Test::More tests => 7;
23
use Test::More tests => 6;
24
use Test::MockModule;
24
use Test::MockModule;
25
25
26
use C4::Context;
26
use C4::Context;
Lines 89-134 subtest 'OPAC - Bibliographic record detail page must contain the data-biblionum Link Here
89
        $biblionumber, "#catalogue_detail_biblio contains data-biblionumber" );
89
        $biblionumber, "#catalogue_detail_biblio contains data-biblionumber" );
90
90
91
    push @cleanup, $biblio;
91
    push @cleanup, $biblio;
92
  };
93
94
subtest 'OPAC - Remove from cart' => sub {
95
    plan tests => 4;
96
97
    # We need to prevent scrolling to prevent the floating toolbar from overlapping buttons we are testing
98
    my $window_size = $driver->get_window_size();
99
    $driver->set_window_size(1920,10800);
100
101
    $driver->get( $opac_base_url . "opac-search.pl?q=d" );
102
103
    # A better way to do that would be to modify the way we display the basket count
104
    # We should show/hide the count instead or recreate the node
105
    my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
106
    is( scalar(@basket_count_elts), 0, 'Basket should be empty');
107
108
    # This will fail if nothing is indexed, but at this point we should have everything setup correctly
109
    my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
110
    my $biblionumber1 = $checkboxes[0]->get_value();
111
    my $biblionumber3 = $checkboxes[2]->get_value();
112
    my $biblionumber5 = $checkboxes[4]->get_value();
113
114
    $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber1.'"]')->click;
115
    my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
116
    is( $basket_count_elt->get_text(),
117
        1, 'One element should have been added to the cart' );
118
119
    $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber3.'"]')->click;
120
    $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber5.'"]')->click;
121
    $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
122
    is( $basket_count_elt->get_text(),
123
        3, '3 elements should have been added to the cart' );
124
125
    $driver->find_element('//a[@class="btn btn-link btn-sm remove cartRemove cartR'.$biblionumber3.'"]')->click;
126
    $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
127
    is( $basket_count_elt->get_text(),
128
        2, '1 element should have been removed from the cart' );
129
130
    # Reset window size
131
    $driver->set_window_size($window_size->{'height'}, $window_size->{'width'});
132
};
92
};
133
93
134
subtest 'Play sound on the circulation page' => sub {
94
subtest 'Play sound on the circulation page' => sub {
(-)a/t/db_dependent/selenium/remove_from_cart.t (+117 lines)
Line 0 Link Here
1
#!/usr/bin/perl
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 File::Basename qw(dirname );
20
21
use Test::WWW::Mechanize;
22
use Test::More tests => 1;
23
use Test::MockModule;
24
25
use C4::Context;
26
use t::lib::Mocks;
27
use t::lib::Selenium;
28
use t::lib::TestBuilder;
29
use t::lib::Mocks;
30
use t::lib::Mocks::Zebra;
31
32
eval { require Selenium::Remote::Driver; };
33
skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
34
35
my $s = t::lib::Selenium->new;
36
37
my $driver = $s->driver;
38
my $opac_base_url = $s->opac_base_url;
39
my $base_url = $s->base_url;
40
my $builder = t::lib::TestBuilder->new;
41
42
my $SearchEngine_value = C4::Context->preference('SearchEngine');
43
C4::Context->set_preference('SearchEngine', 'Zebra');
44
45
my $mock_zebra = t::lib::Mocks::Zebra->new(
46
    {
47
        koha_conf => $ENV{KOHA_CONF},
48
        user      => $ENV{KOHA_USER},
49
        password  => $ENV{KOHA_PASS},
50
        intranet  => $ENV{KOHA_INTRANET_URL},
51
        opac      => $ENV{KOHA_OPAC_URL},
52
    }
53
);
54
55
subtest 'OPAC - Remove from cart' => sub {
56
    plan tests => 22;
57
58
    #-------------------------------- Test with greek and corean chars;
59
    # launch the zebra saerch process
60
    $mock_zebra->launch_zebra;
61
    # launch the zebra index process
62
    $mock_zebra->launch_indexer;
63
64
    my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
65
66
    my $testdir = File::Spec->rel2abs( dirname(__FILE__) . '/../www/');
67
    my $file =
68
      $marcflavour eq 'UNIMARC'
69
      ? "$testdir/data/unimarcutf8record.mrc"
70
      : "$testdir/data/marc21utf8record.mrc";
71
72
    my $batch_id = $mock_zebra->load_records($file);
73
74
    # We need to prevent scrolling to prevent the floating toolbar from overlapping buttons we are testing
75
    my $window_size = $driver->get_window_size();
76
    $driver->set_window_size(1920,10800);
77
78
    $driver->get( $opac_base_url . "opac-search.pl?q=d" );
79
80
    # A better way to do that would be to modify the way we display the basket count
81
    # We should show/hide the count instead or recreate the node
82
    my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
83
    is( scalar(@basket_count_elts), 0, 'Basket should be empty');
84
85
    # This will fail if nothing is indexed, but at this point we should have everything setup correctly
86
    my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
87
    my $biblionumber1 = $checkboxes[0]->get_value();
88
    my $biblionumber3 = $checkboxes[2]->get_value();
89
    my $biblionumber5 = $checkboxes[4]->get_value();
90
91
    $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber1.'"]')->click;
92
    my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
93
    is( $basket_count_elt->get_text(),
94
        1, 'One element should have been added to the cart' );
95
96
    $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber3.'"]')->click;
97
    $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber5.'"]')->click;
98
    $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
99
    is( $basket_count_elt->get_text(),
100
        3, '3 elements should have been added to the cart' );
101
102
    $driver->find_element('//a[@class="btn btn-link btn-sm remove cartRemove cartR'.$biblionumber3.'"]')->click;
103
    $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
104
    is( $basket_count_elt->get_text(),
105
        2, '1 element should have been removed from the cart' );
106
107
    # Reset window size
108
    $driver->set_window_size($window_size->{'height'}, $window_size->{'width'});
109
110
    $mock_zebra->clean_records($batch_id);
111
    $mock_zebra->cleanup;
112
};
113
114
END {
115
    C4::Context->set_preference('SearchEngine', $SearchEngine_value);
116
    $mock_zebra->cleanup;
117
};
(-)a/t/db_dependent/www/search_utf8.t (-212 / +50 lines)
Lines 22-43 use Test::More; #See plan tests => \d+ below Link Here
22
use Test::WWW::Mechanize;
22
use Test::WWW::Mechanize;
23
use Data::Dumper;
23
use Data::Dumper;
24
use XML::Simple;
24
use XML::Simple;
25
use JSON;
25
use File::Basename qw(dirname );
26
use File::Basename;
27
use File::Path;
28
use File::Spec;
29
use File::Temp qw/ tempdir /;
30
use POSIX;
26
use POSIX;
31
use Encode;
27
use Encode;
32
use URI::Escape;
28
use URI::Escape;
33
29
34
use C4::Context;
30
use C4::Context;
35
31
32
use t::lib::Mocks::Zebra;
33
36
my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
34
my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
37
# global variables that will be used when forking
35
# global variables that will be used when forking
38
our $zebra_pid;
39
our $indexer_pid;
40
our $datadir = tempdir();;
41
36
42
my $koha_conf = $ENV{KOHA_CONF};
37
my $koha_conf = $ENV{KOHA_CONF};
43
my $xml       = XMLin($koha_conf);
38
my $xml       = XMLin($koha_conf);
Lines 61-69 my $file3 = Link Here
61
56
62
my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
57
my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
63
my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
58
my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
64
my $intranet = $ENV{KOHA_INTRANET_URL};
59
our $intranet = $ENV{KOHA_INTRANET_URL};
65
my $opac     = $ENV{KOHA_OPAC_URL};
60
our $opac     = $ENV{KOHA_OPAC_URL};
66
67
61
68
# test KOHA_INTRANET_URL is set
62
# test KOHA_INTRANET_URL is set
69
if ( not $intranet ) {
63
if ( not $intranet ) {
Lines 74-254 elsif ( not $opac ) { Link Here
74
   plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
68
   plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n";
75
}
69
}
76
else {
70
else {
77
    plan tests => 87;
71
    plan tests => 89;
78
}
72
}
79
73
80
$intranet =~ s#/$##;
74
$intranet =~ s#/$##;
81
$opac     =~ s#/$##;
75
$opac     =~ s#/$##;
82
76
77
my $mock_zebra = t::lib::Mocks::Zebra->new(
78
    {
79
        koha_conf => $koha_conf,
80
        user      => $user,
81
        password  => $password,
82
        intranet  => $intranet,
83
        opac      => $opac
84
    }
85
);
86
83
#-------------------------------- Test with greek and corean chars;
87
#-------------------------------- Test with greek and corean chars;
84
# launch the zebra saerch process
88
# launch the zebra saerch process
85
launch_zebra( $datadir, $koha_conf );
89
$mock_zebra->launch_zebra;
86
if ( not defined $zebra_pid ) {
90
if ( not defined $mock_zebra->{zebra_pid} ) {
87
    plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
91
    plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
88
}
92
}
89
# launch the zebra index process
93
# launch the zebra index process
90
launch_indexer( );
94
$mock_zebra->launch_indexer;
91
if ( not defined $indexer_pid ) {
95
if ( not defined $mock_zebra->{indexer_pid} ) {
92
    plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
96
    plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
93
}
97
}
94
98
95
my $utf8_reg1 = qr/学協会. μμ/;
99
our $agent = Test::WWW::Mechanize->new( autocheck => 1 );
96
test_search($file1,'Αθήνα', 'deuteros', $utf8_reg1);
100
$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
101
$agent->form_name('loginform');
102
$agent->field( 'password', $password );
103
$agent->field( 'userid',   $user );
104
$agent->field( 'branch',   '' );
105
$agent->click_ok( '', 'login to staff interface' );
97
106
107
my $batch_id = $mock_zebra->load_records($file1);
108
my $utf8_reg1 = qr/学協会. μμ/;
109
test_search('Αθήνα', 'deuteros', $utf8_reg1);
110
$mock_zebra->clean_records($batch_id);
111
$mock_zebra->cleanup;
98
112
99
#--------------------------------- Test with only utf-8 chars in the latin-1 range;
113
#--------------------------------- Test with only utf-8 chars in the latin-1 range;
100
launch_zebra( $datadir, $koha_conf );
114
$mock_zebra->launch_zebra;
101
if ( not defined $zebra_pid ) {
115
if ( not defined $mock_zebra->{zebra_pid} ) {
102
    plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
116
    plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
103
}
117
}
104
launch_indexer( );
118
$mock_zebra->launch_indexer;
105
if ( not defined $indexer_pid ) {
119
if ( not defined $mock_zebra->{indexer_pid} ) {
106
    plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
120
    plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
107
}
121
}
122
$batch_id = $mock_zebra->load_records($file2);
108
my $utf8_reg2 = qr/Tòmas/;
123
my $utf8_reg2 = qr/Tòmas/;
109
test_search($file2,'Ramòn', 'Tòmas',$utf8_reg2);
124
test_search('Ramòn', 'Tòmas',$utf8_reg2);
125
$mock_zebra->clean_records($batch_id);
126
$mock_zebra->cleanup;
110
127
111
#--------------------------------- Test with supplementary utf-8 chars;
128
#--------------------------------- Test with supplementary utf-8 chars;
112
launch_zebra( $datadir, $koha_conf );
129
$mock_zebra->launch_zebra;
113
if ( not defined $zebra_pid ) {
130
if ( not defined $mock_zebra->{zebra_pid} ) {
114
    plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
131
    plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n";
115
}
132
}
116
launch_indexer( );
133
$mock_zebra->launch_indexer;
117
if ( not defined $indexer_pid ) {
134
if ( not defined $mock_zebra->{indexer_pid} ) {
118
    plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
135
    plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n";
119
}
136
}
137
$batch_id = $mock_zebra->load_records($file3);
120
my $utf8_reg3 = qr/😀/;
138
my $utf8_reg3 = qr/😀/;
121
test_search($file3, "𠻺tomasito𠻺", 'A tiny record', $utf8_reg3);
139
test_search("𠻺tomasito𠻺", 'A tiny record', $utf8_reg3);
140
$mock_zebra->clean_records($batch_id);
141
$mock_zebra->cleanup;
122
142
123
sub test_search{
143
sub test_search{
124
    #Params
144
    my ( $publisher, $search_key, $utf8_reg ) = @_;
125
    my $file = $_[0];
126
    my $publisher = $_[1];
127
    my $search_key = $_[2];
128
    my $utf8_reg = $_[3];
129
130
    my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
131
    my $jsonresponse;
132
133
    # -------------------------------------------------- LOAD RECORD
134
135
    $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
136
    $agent->form_name('loginform');
137
    $agent->field( 'password', $password );
138
    $agent->field( 'userid',   $user );
139
    $agent->field( 'branch',   '' );
140
    $agent->click_ok( '', 'login to staff interface' );
141
142
    $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' );
143
144
    $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
145
    $agent->follow_link_ok( { text => 'Stage MARC records for import' },
146
        'go to stage MARC' );
147
148
    $agent->post(
149
        "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
150
        [ 'fileToUpload' => [$file], ],
151
        'Content_Type' => 'form-data',
152
    );
153
    ok( $agent->success, 'uploaded file' );
154
155
    $jsonresponse = decode_json $agent->content();
156
    is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
157
    my $fileid = $jsonresponse->{'fileid'};
158
159
    $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
160
        'reopen stage MARC page' );
161
    $agent->submit_form_ok(
162
        {
163
            form_number => 5,
164
            fields      => {
165
                'uploadedfileid'  => $fileid,
166
                'nomatch_action'  => 'create_new',
167
                'overlay_action'  => 'replace',
168
                'item_action'     => 'always_add',
169
                'matcher'         => '',
170
                'comments'        => '',
171
                'encoding'        => 'utf8',
172
                'parse_items'     => '1',
173
                'runinbackground' => '1',
174
                'record_type'     => 'biblio'
175
            }
176
        },
177
        'stage MARC'
178
    );
179
180
    $jsonresponse = decode_json $agent->content();
181
    my $jobID = $jsonresponse->{'jobID'};
182
    ok( $jobID, 'have job ID' );
183
184
    my $completed = 0;
185
186
    # if we haven't completed the batch in two minutes, it's not happening
187
    for my $counter ( 1 .. 24 ) {
188
        $agent->get(
189
            "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID"
190
        ); # get job progress
191
        $jsonresponse = decode_json $agent->content();
192
        if ( $jsonresponse->{'job_status'} eq 'completed' ) {
193
            $completed = 1;
194
            last;
195
        }
196
        warn(
197
            (
198
                $jsonresponse->{'job_size'}
199
                ? floor(
200
                    100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
201
                  )
202
                : '100'
203
            )
204
            . "% completed"
205
        );
206
        sleep 5;
207
    }
208
    is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
209
210
    $agent->get_ok(
211
        "$intranet/cgi-bin/koha/tools/stage-marc-import.pl",
212
        'reopen stage MARC page at end of upload'
213
    );
214
    $agent->submit_form_ok(
215
        {
216
            form_number => 5,
217
            fields      => {
218
                'uploadedfileid'  => $fileid,
219
                'nomatch_action'  => 'create_new',
220
                'overlay_action'  => 'replace',
221
                'item_action'     => 'always_add',
222
                'matcher'         => '1',
223
                'comments'        => '',
224
                'encoding'        => 'utf8',
225
                'parse_items'     => '1',
226
                'runinbackground' => '1',
227
                'completedJobID'  => $jobID,
228
                'record_type'     => 'biblio'
229
            }
230
        },
231
        'stage MARC'
232
    );
233
234
    $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
235
236
237
    $agent->form_number(6);
238
    $agent->field( 'framework', '' );
239
    $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
240
    my $webpage = $agent->{content};
241
242
    $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
243
    my $id_batch = $2;
244
    my $id_bib_number = GetBiblionumberFromImport($id_batch);
245
246
    # wait enough time for the indexer
247
    sleep 10;
248
145
249
    # --------------------------------- TEST INTRANET SEARCH
146
    # --------------------------------- TEST INTRANET SEARCH
250
147
251
252
    $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
148
    $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet");
253
    $agent->form_number(5);
149
    $agent->form_number(5);
254
    $agent->field('idx', 'kw');
150
    $agent->field('idx', 'kw');
Lines 290-359 sub test_search{ Link Here
290
    # in the previous regex
186
    # in the previous regex
291
    $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
187
    $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' );
292
188
293
294
    ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
189
    ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ;
295
    ok ($opac_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
190
    ok ($opac_text =~  $utf8_reg, 'UTF-8 chars are correctly present. Good');
296
191
297
    #-------------------------------------------------- REVERT
298
299
    $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' );
300
    $agent->form_name('clean_batch_'.$id_batch);
301
    $agent->click();
302
    $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' );
303
    $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' );
304
305
    # clean
306
    cleanup();
307
}
308
309
310
# function that launches the zebra daemon
311
sub launch_zebra {
312
313
    my ( $datadir, $koha_conf ) = @_;
314
315
    $zebra_pid = fork();
316
    if ( $zebra_pid == 0 ) {
317
        exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
318
        exit;
319
    }
320
    sleep( 1 );
321
}
322
323
sub launch_indexer {
324
325
    my $rootdir       = dirname(__FILE__) . '/../../../';
326
    my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
327
328
    $indexer_pid = fork();
329
330
    if ( $indexer_pid == 0 ) {
331
        exec("$rebuild_zebra -daemon -sleep 5");
332
        exit;
333
    }
334
    sleep( 1 );
335
}
336
337
sub cleanup {
338
339
    kill 9, $zebra_pid   if defined $zebra_pid;
340
    kill 9, $indexer_pid if defined $indexer_pid;
341
    # Clean up the Zebra files since the child process was just shot
342
    rmtree $datadir;
343
344
}
345
346
sub GetBiblionumberFromImport{
347
    my ( $batch_id) = @_;
348
    use C4::ImportBatch;
349
    my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
350
                    { order_by => 'import_record_id', order_by_direction => 'DESC' });
351
    my $biblionumber = $data->[0]->{'matched_biblionumber'};
352
353
    return $biblionumber;
354
}
192
}
355
193
356
END {
194
END {
357
    cleanup();
195
    $mock_zebra->cleanup;
358
};
196
};
359
197
(-)a/t/lib/Mocks/Zebra.pm (-1 / +206 lines)
Line 0 Link Here
0
- 
1
package t::lib::Mocks::Zebra;
2
3
use Modern::Perl;
4
use Test::More;
5
use File::Basename qw(dirname );
6
use File::Temp qw( tempdir );
7
use File::Path qw( rmtree );
8
use JSON qw( decode_json );
9
use C4::ImportBatch;
10
11
sub new {
12
    my ( $class, $params ) = @_;
13
14
    my $datadir = tempdir();;
15
    my $self = {
16
        datadir   => $datadir,
17
        koha_conf => $params->{koha_conf},
18
        user      => $params->{user},
19
        password  => $params->{password},
20
        intranet  => $params->{intranet},
21
        opac      => $params->{opac}
22
    };
23
    return bless $self, $class;
24
}
25
26
# function that launches the zebra daemon
27
sub launch_zebra {
28
    my ( $self ) = @_;
29
30
    my $datadir = $self->{datadir};
31
    my $koha_conf = $self->{koha_conf};
32
33
    my $zebra_pid = fork();
34
    if ( $zebra_pid == 0 ) {
35
        exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log");
36
        exit;
37
    }
38
    sleep( 1 );
39
    $self->{zebra_pid} = $zebra_pid;
40
}
41
42
sub launch_indexer {
43
    my ($self) = @_;
44
    my $rootdir       = dirname(__FILE__) . '/../../../';
45
    my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl";
46
47
    my $indexer_pid = fork();
48
49
    if ( $indexer_pid == 0 ) {
50
        exec("$rebuild_zebra -daemon -sleep 5");
51
        exit;
52
    }
53
    sleep( 1 );
54
    $self->{indexer_pid} = $indexer_pid;
55
}
56
57
sub load_records {
58
    my ( $self, $file ) = @_;
59
    my $jsonresponse;
60
    my $cgi_root = $self->{intranet} . '/cgi-bin/koha';
61
62
    our $agent = Test::WWW::Mechanize->new( autocheck => 1 );
63
    $agent->get_ok( "$cgi_root/mainpage.pl", 'connect to intranet' );
64
    $agent->form_name('loginform');
65
    $agent->field( 'password', $self->{password} );
66
    $agent->field( 'userid',   $self->{user} );
67
    $agent->field( 'branch',   '' );
68
    $agent->click_ok( '', 'login to staff interface' );
69
70
    $agent->get_ok( "$cgi_root/mainpage.pl", 'load main page' );
71
72
    $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' );
73
    $agent->follow_link_ok( { text => 'Stage MARC records for import' },
74
        'go to stage MARC' );
75
76
    $agent->post(
77
        "$cgi_root/tools/upload-file.pl?temp=1",
78
        [ 'fileToUpload' => [$file], ],
79
        'Content_Type' => 'form-data',
80
    );
81
    ok( $agent->success, 'uploaded file' );
82
83
    $jsonresponse = decode_json $agent->content();
84
    is( $jsonresponse->{'status'}, 'done', 'upload succeeded' );
85
    my $fileid = $jsonresponse->{'fileid'};
86
87
    $agent->get_ok( "$cgi_root/tools/stage-marc-import.pl",
88
        'reopen stage MARC page' );
89
    $agent->submit_form_ok(
90
        {
91
            form_number => 5,
92
            fields      => {
93
                'uploadedfileid'  => $fileid,
94
                'nomatch_action'  => 'create_new',
95
                'overlay_action'  => 'replace',
96
                'item_action'     => 'always_add',
97
                'matcher'         => '',
98
                'comments'        => '',
99
                'encoding'        => 'utf8',
100
                'parse_items'     => '1',
101
                'runinbackground' => '1',
102
                'record_type'     => 'biblio'
103
            }
104
        },
105
        'stage MARC'
106
    );
107
108
    $jsonresponse = decode_json $agent->content();
109
    my $jobID = $jsonresponse->{'jobID'};
110
    ok( $jobID, 'have job ID' );
111
112
    my $completed = 0;
113
114
    # if we haven't completed the batch in two minutes, it's not happening
115
    for my $counter ( 1 .. 24 ) {
116
        $agent->get(
117
            "$cgi_root/tools/background-job-progress.pl?jobID=$jobID"
118
        ); # get job progress
119
        $jsonresponse = decode_json $agent->content();
120
        if ( $jsonresponse->{'job_status'} eq 'completed' ) {
121
            $completed = 1;
122
            last;
123
        }
124
        warn(
125
            (
126
                $jsonresponse->{'job_size'}
127
                ? floor(
128
                    100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'}
129
                  )
130
                : '100'
131
            )
132
            . "% completed"
133
        );
134
        sleep 5;
135
    }
136
    is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' );
137
138
    $agent->get_ok(
139
        "$cgi_root/tools/stage-marc-import.pl",
140
        'reopen stage MARC page at end of upload'
141
    );
142
    $agent->submit_form_ok(
143
        {
144
            form_number => 5,
145
            fields      => {
146
                'uploadedfileid'  => $fileid,
147
                'nomatch_action'  => 'create_new',
148
                'overlay_action'  => 'replace',
149
                'item_action'     => 'always_add',
150
                'matcher'         => '1',
151
                'comments'        => '',
152
                'encoding'        => 'utf8',
153
                'parse_items'     => '1',
154
                'runinbackground' => '1',
155
                'completedJobID'  => $jobID,
156
                'record_type'     => 'biblio'
157
            }
158
        },
159
        'stage MARC'
160
    );
161
162
    $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
163
164
165
    $agent->form_number(6);
166
    $agent->field( 'framework', '' );
167
    $agent->click_ok( 'mainformsubmit', "imported records into catalog" );
168
    my $webpage = $agent->{content};
169
170
    $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx;
171
    my $batch_id = $2;
172
173
    # wait enough time for the indexer
174
    sleep 10;
175
176
    return $batch_id;
177
178
}
179
180
sub clean_records {
181
    my ( $self, $batch_id ) = @_;
182
183
    my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
184
    my $cgi_root = $self->{intranet} . '/cgi-bin/koha';
185
186
    my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef,
187
                    { order_by => 'import_record_id', order_by_direction => 'DESC' });
188
    my $biblionumber = $data->[0]->{'matched_biblionumber'};
189
190
    $agent->get_ok( "$cgi_root/tools/manage-marc-import.pl", 'view and clean batch' );
191
    $agent->form_name('clean_batch_'.$batch_id);
192
    $agent->click();
193
    $agent->get_ok( "$cgi_root/catalogue/detail.pl?biblionumber=$biblionumber", 'biblio on intranet' );
194
    $agent->get_ok( "$cgi_root/cataloguing/addbiblio.pl?op=delete&biblionumber=$biblionumber", 'biblio deleted' );
195
196
}
197
198
sub cleanup {
199
    my ( $self ) = @_;
200
    kill 9, $self->{zebra_pid}   if defined $self->{zebra_pid};
201
    kill 9, $self->{indexer_pid} if defined $self->{indexer_pid};
202
    # Clean up the Zebra files since the child process was just shot
203
    rmtree $self->{datadir};
204
}
205
206
1;

Return to bug 19185