From 54a1c5433bb89f714cba5061ae33eb94851a0260 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 26 Oct 2021 12:20:54 +0200 Subject: [PATCH] Bug 19185: Fix regressions.t The following test is failing: | # Failed test 'OPAC - Remove from cart' | # at t/db_dependent/selenium/regressions.t line 132. | Can't call method "get_value" on an undefined value at t/db_dependent/selenium/regressions.t line 110. | # Looks like your test exited with 2 just after 3. | [12:14:08] t/db_dependent/selenium/regressions.t We are dependind on the search engine and the records in the DB but the installer is not inserting any records. This patch is suggesting to reuse the code from search_utf8 (and so make it reusable first) for remove_from_cart test. This code is mocking the Zebra index with some MARC data and so the search will return results. We will finally be able to click on the add to/remove from cart links. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Jonathan Druart --- t/db_dependent/selenium/regressions.t | 42 +--- t/db_dependent/selenium/remove_from_cart.t | 117 +++++++++ t/db_dependent/www/search_utf8.t | 262 ++++----------------- t/lib/Mocks/Zebra.pm | 206 ++++++++++++++++ 4 files changed, 374 insertions(+), 253 deletions(-) create mode 100755 t/db_dependent/selenium/remove_from_cart.t create mode 100644 t/lib/Mocks/Zebra.pm diff --git a/t/db_dependent/selenium/regressions.t b/t/db_dependent/selenium/regressions.t index 220345fb82..871af73e2e 100755 --- a/t/db_dependent/selenium/regressions.t +++ b/t/db_dependent/selenium/regressions.t @@ -20,7 +20,7 @@ use utf8; use C4::Context; -use Test::More tests => 7; +use Test::More tests => 6; use Test::MockModule; use C4::Context; @@ -89,46 +89,6 @@ subtest 'OPAC - Bibliographic record detail page must contain the data-biblionum $biblionumber, "#catalogue_detail_biblio contains data-biblionumber" ); push @cleanup, $biblio; - }; - -subtest 'OPAC - Remove from cart' => sub { - plan tests => 4; - - # We need to prevent scrolling to prevent the floating toolbar from overlapping buttons we are testing - my $window_size = $driver->get_window_size(); - $driver->set_window_size(1920,10800); - - $driver->get( $opac_base_url . "opac-search.pl?q=d" ); - - # A better way to do that would be to modify the way we display the basket count - # We should show/hide the count instead or recreate the node - my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span'); - is( scalar(@basket_count_elts), 0, 'Basket should be empty'); - - # This will fail if nothing is indexed, but at this point we should have everything setup correctly - my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]'); - my $biblionumber1 = $checkboxes[0]->get_value(); - my $biblionumber3 = $checkboxes[2]->get_value(); - my $biblionumber5 = $checkboxes[4]->get_value(); - - $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber1.'"]')->click; - my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span'); - is( $basket_count_elt->get_text(), - 1, 'One element should have been added to the cart' ); - - $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber3.'"]')->click; - $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber5.'"]')->click; - $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span'); - is( $basket_count_elt->get_text(), - 3, '3 elements should have been added to the cart' ); - - $driver->find_element('//a[@class="btn btn-link btn-sm remove cartRemove cartR'.$biblionumber3.'"]')->click; - $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span'); - is( $basket_count_elt->get_text(), - 2, '1 element should have been removed from the cart' ); - - # Reset window size - $driver->set_window_size($window_size->{'height'}, $window_size->{'width'}); }; subtest 'Play sound on the circulation page' => sub { diff --git a/t/db_dependent/selenium/remove_from_cart.t b/t/db_dependent/selenium/remove_from_cart.t new file mode 100755 index 0000000000..02ce186b39 --- /dev/null +++ b/t/db_dependent/selenium/remove_from_cart.t @@ -0,0 +1,117 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use File::Basename qw(dirname ); + +use Test::WWW::Mechanize; +use Test::More tests => 1; +use Test::MockModule; + +use C4::Context; +use t::lib::Mocks; +use t::lib::Selenium; +use t::lib::TestBuilder; +use t::lib::Mocks; +use t::lib::Mocks::Zebra; + +eval { require Selenium::Remote::Driver; }; +skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@; + +my $s = t::lib::Selenium->new; + +my $driver = $s->driver; +my $opac_base_url = $s->opac_base_url; +my $base_url = $s->base_url; +my $builder = t::lib::TestBuilder->new; + +my $SearchEngine_value = C4::Context->preference('SearchEngine'); +C4::Context->set_preference('SearchEngine', 'Zebra'); + +my $mock_zebra = t::lib::Mocks::Zebra->new( + { + koha_conf => $ENV{KOHA_CONF}, + user => $ENV{KOHA_USER}, + password => $ENV{KOHA_PASS}, + intranet => $ENV{KOHA_INTRANET_URL}, + opac => $ENV{KOHA_OPAC_URL}, + } +); + +subtest 'OPAC - Remove from cart' => sub { + plan tests => 22; + + #-------------------------------- Test with greek and corean chars; + # launch the zebra saerch process + $mock_zebra->launch_zebra; + # launch the zebra index process + $mock_zebra->launch_indexer; + + my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21'; + + my $testdir = File::Spec->rel2abs( dirname(__FILE__) . '/../www/'); + my $file = + $marcflavour eq 'UNIMARC' + ? "$testdir/data/unimarcutf8record.mrc" + : "$testdir/data/marc21utf8record.mrc"; + + my $batch_id = $mock_zebra->load_records($file); + + # We need to prevent scrolling to prevent the floating toolbar from overlapping buttons we are testing + my $window_size = $driver->get_window_size(); + $driver->set_window_size(1920,10800); + + $driver->get( $opac_base_url . "opac-search.pl?q=d" ); + + # A better way to do that would be to modify the way we display the basket count + # We should show/hide the count instead or recreate the node + my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span'); + is( scalar(@basket_count_elts), 0, 'Basket should be empty'); + + # This will fail if nothing is indexed, but at this point we should have everything setup correctly + my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]'); + my $biblionumber1 = $checkboxes[0]->get_value(); + my $biblionumber3 = $checkboxes[2]->get_value(); + my $biblionumber5 = $checkboxes[4]->get_value(); + + $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber1.'"]')->click; + my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span'); + is( $basket_count_elt->get_text(), + 1, 'One element should have been added to the cart' ); + + $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber3.'"]')->click; + $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber5.'"]')->click; + $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span'); + is( $basket_count_elt->get_text(), + 3, '3 elements should have been added to the cart' ); + + $driver->find_element('//a[@class="btn btn-link btn-sm remove cartRemove cartR'.$biblionumber3.'"]')->click; + $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span'); + is( $basket_count_elt->get_text(), + 2, '1 element should have been removed from the cart' ); + + # Reset window size + $driver->set_window_size($window_size->{'height'}, $window_size->{'width'}); + + $mock_zebra->clean_records($batch_id); + $mock_zebra->cleanup; +}; + +END { + C4::Context->set_preference('SearchEngine', $SearchEngine_value); + $mock_zebra->cleanup; +}; diff --git a/t/db_dependent/www/search_utf8.t b/t/db_dependent/www/search_utf8.t index b79b49cba5..4bbfbec888 100755 --- a/t/db_dependent/www/search_utf8.t +++ b/t/db_dependent/www/search_utf8.t @@ -22,22 +22,17 @@ use Test::More; #See plan tests => \d+ below use Test::WWW::Mechanize; use Data::Dumper; use XML::Simple; -use JSON; -use File::Basename; -use File::Path; -use File::Spec; -use File::Temp qw/ tempdir /; +use File::Basename qw(dirname ); use POSIX; use Encode; use URI::Escape; use C4::Context; +use t::lib::Mocks::Zebra; + my $testdir = File::Spec->rel2abs( dirname(__FILE__) ); # global variables that will be used when forking -our $zebra_pid; -our $indexer_pid; -our $datadir = tempdir();; my $koha_conf = $ENV{KOHA_CONF}; my $xml = XMLin($koha_conf); @@ -61,9 +56,8 @@ my $file3 = my $user = $ENV{KOHA_USER} || $xml->{config}->{user}; my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass}; -my $intranet = $ENV{KOHA_INTRANET_URL}; -my $opac = $ENV{KOHA_OPAC_URL}; - +our $intranet = $ENV{KOHA_INTRANET_URL}; +our $opac = $ENV{KOHA_OPAC_URL}; # test KOHA_INTRANET_URL is set if ( not $intranet ) { @@ -74,181 +68,83 @@ elsif ( not $opac ) { plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n"; } else { - plan tests => 87; + plan tests => 89; } $intranet =~ s#/$##; $opac =~ s#/$##; +my $mock_zebra = t::lib::Mocks::Zebra->new( + { + koha_conf => $koha_conf, + user => $user, + password => $password, + intranet => $intranet, + opac => $opac + } +); + #-------------------------------- Test with greek and corean chars; # launch the zebra saerch process -launch_zebra( $datadir, $koha_conf ); -if ( not defined $zebra_pid ) { +$mock_zebra->launch_zebra; +if ( not defined $mock_zebra->{zebra_pid} ) { plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n"; } # launch the zebra index process -launch_indexer( ); -if ( not defined $indexer_pid ) { +$mock_zebra->launch_indexer; +if ( not defined $mock_zebra->{indexer_pid} ) { plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n"; } -my $utf8_reg1 = qr/学協会. μμ/; -test_search($file1,'Αθήνα', 'deuteros', $utf8_reg1); +our $agent = Test::WWW::Mechanize->new( autocheck => 1 ); +$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' ); +$agent->form_name('loginform'); +$agent->field( 'password', $password ); +$agent->field( 'userid', $user ); +$agent->field( 'branch', '' ); +$agent->click_ok( '', 'login to staff interface' ); +my $batch_id = $mock_zebra->load_records($file1); +my $utf8_reg1 = qr/学協会. μμ/; +test_search('Αθήνα', 'deuteros', $utf8_reg1); +$mock_zebra->clean_records($batch_id); +$mock_zebra->cleanup; #--------------------------------- Test with only utf-8 chars in the latin-1 range; -launch_zebra( $datadir, $koha_conf ); -if ( not defined $zebra_pid ) { +$mock_zebra->launch_zebra; +if ( not defined $mock_zebra->{zebra_pid} ) { plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n"; } -launch_indexer( ); -if ( not defined $indexer_pid ) { +$mock_zebra->launch_indexer; +if ( not defined $mock_zebra->{indexer_pid} ) { plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n"; } +$batch_id = $mock_zebra->load_records($file2); my $utf8_reg2 = qr/Tòmas/; -test_search($file2,'Ramòn', 'Tòmas',$utf8_reg2); +test_search('Ramòn', 'Tòmas',$utf8_reg2); +$mock_zebra->clean_records($batch_id); +$mock_zebra->cleanup; #--------------------------------- Test with supplementary utf-8 chars; -launch_zebra( $datadir, $koha_conf ); -if ( not defined $zebra_pid ) { +$mock_zebra->launch_zebra; +if ( not defined $mock_zebra->{zebra_pid} ) { plan skip_all => "Tests skip. Error starting Zebra Server to do those tests\n"; } -launch_indexer( ); -if ( not defined $indexer_pid ) { +$mock_zebra->launch_indexer; +if ( not defined $mock_zebra->{indexer_pid} ) { plan skip_all => "Tests skip. Error starting the indexer daemon to do those tests\n"; } +$batch_id = $mock_zebra->load_records($file3); my $utf8_reg3 = qr/😀/; -test_search($file3, "𠻺tomasito𠻺", 'A tiny record', $utf8_reg3); +test_search("𠻺tomasito𠻺", 'A tiny record', $utf8_reg3); +$mock_zebra->clean_records($batch_id); +$mock_zebra->cleanup; sub test_search{ - #Params - my $file = $_[0]; - my $publisher = $_[1]; - my $search_key = $_[2]; - my $utf8_reg = $_[3]; - - my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); - my $jsonresponse; - - # -------------------------------------------------- LOAD RECORD - - $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' ); - $agent->form_name('loginform'); - $agent->field( 'password', $password ); - $agent->field( 'userid', $user ); - $agent->field( 'branch', '' ); - $agent->click_ok( '', 'login to staff interface' ); - - $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' ); - - $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' ); - $agent->follow_link_ok( { text => 'Stage MARC records for import' }, - 'go to stage MARC' ); - - $agent->post( - "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1", - [ 'fileToUpload' => [$file], ], - 'Content_Type' => 'form-data', - ); - ok( $agent->success, 'uploaded file' ); - - $jsonresponse = decode_json $agent->content(); - is( $jsonresponse->{'status'}, 'done', 'upload succeeded' ); - my $fileid = $jsonresponse->{'fileid'}; - - $agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl", - 'reopen stage MARC page' ); - $agent->submit_form_ok( - { - form_number => 5, - fields => { - 'uploadedfileid' => $fileid, - 'nomatch_action' => 'create_new', - 'overlay_action' => 'replace', - 'item_action' => 'always_add', - 'matcher' => '', - 'comments' => '', - 'encoding' => 'utf8', - 'parse_items' => '1', - 'runinbackground' => '1', - 'record_type' => 'biblio' - } - }, - 'stage MARC' - ); - - $jsonresponse = decode_json $agent->content(); - my $jobID = $jsonresponse->{'jobID'}; - ok( $jobID, 'have job ID' ); - - my $completed = 0; - - # if we haven't completed the batch in two minutes, it's not happening - for my $counter ( 1 .. 24 ) { - $agent->get( - "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID" - ); # get job progress - $jsonresponse = decode_json $agent->content(); - if ( $jsonresponse->{'job_status'} eq 'completed' ) { - $completed = 1; - last; - } - warn( - ( - $jsonresponse->{'job_size'} - ? floor( - 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'} - ) - : '100' - ) - . "% completed" - ); - sleep 5; - } - is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' ); - - $agent->get_ok( - "$intranet/cgi-bin/koha/tools/stage-marc-import.pl", - 'reopen stage MARC page at end of upload' - ); - $agent->submit_form_ok( - { - form_number => 5, - fields => { - 'uploadedfileid' => $fileid, - 'nomatch_action' => 'create_new', - 'overlay_action' => 'replace', - 'item_action' => 'always_add', - 'matcher' => '1', - 'comments' => '', - 'encoding' => 'utf8', - 'parse_items' => '1', - 'runinbackground' => '1', - 'completedJobID' => $jobID, - 'record_type' => 'biblio' - } - }, - 'stage MARC' - ); - - $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' ); - - - $agent->form_number(6); - $agent->field( 'framework', '' ); - $agent->click_ok( 'mainformsubmit', "imported records into catalog" ); - my $webpage = $agent->{content}; - - $webpage =~ /(.*.*?)(\d{1,})(.*<\/title>)/sx; - my $id_batch = $2; - my $id_bib_number = GetBiblionumberFromImport($id_batch); - - # wait enough time for the indexer - sleep 10; + my ( $publisher, $search_key, $utf8_reg ) = @_; # --------------------------------- TEST INTRANET SEARCH - $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/search.pl" , "got search on intranet"); $agent->form_number(5); $agent->field('idx', 'kw'); @@ -290,70 +186,12 @@ sub test_search{ # in the previous regex $agent->base_unlike( qr|idx=\w+|, 'Base does not contain an idx' ); - ok ( ( length(Encode::encode('UTF-8', $opac_text)) != length($opac_text) ) , 'UTF-8 are multi-byte. Good') ; ok ($opac_text =~ $utf8_reg, 'UTF-8 chars are correctly present. Good'); - #-------------------------------------------------- REVERT - - $agent->get_ok( "$intranet/cgi-bin/koha/tools/manage-marc-import.pl", 'view and clean batch' ); - $agent->form_name('clean_batch_'.$id_batch); - $agent->click(); - $agent->get_ok( "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$id_bib_number", 'biblio on intranet' ); - $agent->get_ok( "$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?op=delete&biblionumber=$id_bib_number", 'biblio deleted' ); - - # clean - cleanup(); -} - - -# function that launches the zebra daemon -sub launch_zebra { - - my ( $datadir, $koha_conf ) = @_; - - $zebra_pid = fork(); - if ( $zebra_pid == 0 ) { - exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log"); - exit; - } - sleep( 1 ); -} - -sub launch_indexer { - - my $rootdir = dirname(__FILE__) . '/../../../'; - my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl"; - - $indexer_pid = fork(); - - if ( $indexer_pid == 0 ) { - exec("$rebuild_zebra -daemon -sleep 5"); - exit; - } - sleep( 1 ); -} - -sub cleanup { - - kill 9, $zebra_pid if defined $zebra_pid; - kill 9, $indexer_pid if defined $indexer_pid; - # Clean up the Zebra files since the child process was just shot - rmtree $datadir; - -} - -sub GetBiblionumberFromImport{ - my ( $batch_id) = @_; - use C4::ImportBatch; - my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef, - { order_by => 'import_record_id', order_by_direction => 'DESC' }); - my $biblionumber = $data->[0]->{'matched_biblionumber'}; - - return $biblionumber; } END { - cleanup(); + $mock_zebra->cleanup; }; diff --git a/t/lib/Mocks/Zebra.pm b/t/lib/Mocks/Zebra.pm new file mode 100644 index 0000000000..a20cd8dd9d --- /dev/null +++ b/t/lib/Mocks/Zebra.pm @@ -0,0 +1,206 @@ +package t::lib::Mocks::Zebra; + +use Modern::Perl; +use Test::More; +use File::Basename qw(dirname ); +use File::Temp qw( tempdir ); +use File::Path qw( rmtree ); +use JSON qw( decode_json ); +use C4::ImportBatch; + +sub new { + my ( $class, $params ) = @_; + + my $datadir = tempdir();; + my $self = { + datadir => $datadir, + koha_conf => $params->{koha_conf}, + user => $params->{user}, + password => $params->{password}, + intranet => $params->{intranet}, + opac => $params->{opac} + }; + return bless $self, $class; +} + +# function that launches the zebra daemon +sub launch_zebra { + my ( $self ) = @_; + + my $datadir = $self->{datadir}; + my $koha_conf = $self->{koha_conf}; + + my $zebra_pid = fork(); + if ( $zebra_pid == 0 ) { + exec("zebrasrv -f $koha_conf -v none,request -l $datadir/zebra.log"); + exit; + } + sleep( 1 ); + $self->{zebra_pid} = $zebra_pid; +} + +sub launch_indexer { + my ($self) = @_; + my $rootdir = dirname(__FILE__) . '/../../../'; + my $rebuild_zebra = "$rootdir/misc/migration_tools/rebuild_zebra.pl"; + + my $indexer_pid = fork(); + + if ( $indexer_pid == 0 ) { + exec("$rebuild_zebra -daemon -sleep 5"); + exit; + } + sleep( 1 ); + $self->{indexer_pid} = $indexer_pid; +} + +sub load_records { + my ( $self, $file ) = @_; + my $jsonresponse; + my $cgi_root = $self->{intranet} . '/cgi-bin/koha'; + + our $agent = Test::WWW::Mechanize->new( autocheck => 1 ); + $agent->get_ok( "$cgi_root/mainpage.pl", 'connect to intranet' ); + $agent->form_name('loginform'); + $agent->field( 'password', $self->{password} ); + $agent->field( 'userid', $self->{user} ); + $agent->field( 'branch', '' ); + $agent->click_ok( '', 'login to staff interface' ); + + $agent->get_ok( "$cgi_root/mainpage.pl", 'load main page' ); + + $agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' ); + $agent->follow_link_ok( { text => 'Stage MARC records for import' }, + 'go to stage MARC' ); + + $agent->post( + "$cgi_root/tools/upload-file.pl?temp=1", + [ 'fileToUpload' => [$file], ], + 'Content_Type' => 'form-data', + ); + ok( $agent->success, 'uploaded file' ); + + $jsonresponse = decode_json $agent->content(); + is( $jsonresponse->{'status'}, 'done', 'upload succeeded' ); + my $fileid = $jsonresponse->{'fileid'}; + + $agent->get_ok( "$cgi_root/tools/stage-marc-import.pl", + 'reopen stage MARC page' ); + $agent->submit_form_ok( + { + form_number => 5, + fields => { + 'uploadedfileid' => $fileid, + 'nomatch_action' => 'create_new', + 'overlay_action' => 'replace', + 'item_action' => 'always_add', + 'matcher' => '', + 'comments' => '', + 'encoding' => 'utf8', + 'parse_items' => '1', + 'runinbackground' => '1', + 'record_type' => 'biblio' + } + }, + 'stage MARC' + ); + + $jsonresponse = decode_json $agent->content(); + my $jobID = $jsonresponse->{'jobID'}; + ok( $jobID, 'have job ID' ); + + my $completed = 0; + + # if we haven't completed the batch in two minutes, it's not happening + for my $counter ( 1 .. 24 ) { + $agent->get( + "$cgi_root/tools/background-job-progress.pl?jobID=$jobID" + ); # get job progress + $jsonresponse = decode_json $agent->content(); + if ( $jsonresponse->{'job_status'} eq 'completed' ) { + $completed = 1; + last; + } + warn( + ( + $jsonresponse->{'job_size'} + ? floor( + 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'} + ) + : '100' + ) + . "% completed" + ); + sleep 5; + } + is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' ); + + $agent->get_ok( + "$cgi_root/tools/stage-marc-import.pl", + 'reopen stage MARC page at end of upload' + ); + $agent->submit_form_ok( + { + form_number => 5, + fields => { + 'uploadedfileid' => $fileid, + 'nomatch_action' => 'create_new', + 'overlay_action' => 'replace', + 'item_action' => 'always_add', + 'matcher' => '1', + 'comments' => '', + 'encoding' => 'utf8', + 'parse_items' => '1', + 'runinbackground' => '1', + 'completedJobID' => $jobID, + 'record_type' => 'biblio' + } + }, + 'stage MARC' + ); + + $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' ); + + + $agent->form_number(6); + $agent->field( 'framework', '' ); + $agent->click_ok( 'mainformsubmit', "imported records into catalog" ); + my $webpage = $agent->{content}; + + $webpage =~ /(.*<title>.*?)(\d{1,})(.*<\/title>)/sx; + my $batch_id = $2; + + # wait enough time for the indexer + sleep 10; + + return $batch_id; + +} + +sub clean_records { + my ( $self, $batch_id ) = @_; + + my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); + my $cgi_root = $self->{intranet} . '/cgi-bin/koha'; + + my $data = C4::ImportBatch::GetImportRecordsRange($batch_id, '', '', undef, + { order_by => 'import_record_id', order_by_direction => 'DESC' }); + my $biblionumber = $data->[0]->{'matched_biblionumber'}; + + $agent->get_ok( "$cgi_root/tools/manage-marc-import.pl", 'view and clean batch' ); + $agent->form_name('clean_batch_'.$batch_id); + $agent->click(); + $agent->get_ok( "$cgi_root/catalogue/detail.pl?biblionumber=$biblionumber", 'biblio on intranet' ); + $agent->get_ok( "$cgi_root/cataloguing/addbiblio.pl?op=delete&biblionumber=$biblionumber", 'biblio deleted' ); + +} + +sub cleanup { + my ( $self ) = @_; + kill 9, $self->{zebra_pid} if defined $self->{zebra_pid}; + kill 9, $self->{indexer_pid} if defined $self->{indexer_pid}; + # Clean up the Zebra files since the child process was just shot + rmtree $self->{datadir}; +} + +1; -- 2.39.5