From 69f8d138b2de3691bddb4fba554b85150fc7d14e Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 26 Oct 2022 17:34:10 +0200 Subject: [PATCH] Bug 31992: Fix t::lib::Mocks::Zebra --- t/db_dependent/www/batch.t | 127 +++++-------------------------- t/db_dependent/www/search_utf8.t | 2 +- t/lib/Mocks/Zebra.pm | 71 ++++------------- 3 files changed, 32 insertions(+), 168 deletions(-) diff --git a/t/db_dependent/www/batch.t b/t/db_dependent/www/batch.t index f883e2f978d..e4a4e94bf8a 100755 --- a/t/db_dependent/www/batch.t +++ b/t/db_dependent/www/batch.t @@ -26,6 +26,7 @@ use JSON; use File::Basename; use File::Spec; use POSIX; +use t::lib::Mocks::Zebra; my $testdir = File::Spec->rel2abs( dirname(__FILE__) ); @@ -52,114 +53,19 @@ if (not defined $intranet) { "your username and password"; } else { - plan tests => 26; + plan tests => 13; } $intranet =~ s#/$##; -my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); -my $jsonresponse; - -$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/cataloging-home/i }, 'open cataloging module' ); -$agent->follow_link_ok( { text => 'Stage 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( +my $mock_zebra = t::lib::Mocks::Zebra->new( { - form_number => 5, - fields => { - 'uploadedfileid' => $fileid, - 'nomatch_action' => 'create_new', - 'overlay_action' => 'replace', - 'item_action' => 'always_add', - 'matcher' => '', - 'comments' => '', - 'encoding' => 'UTF-8', - '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; + intranet => $intranet, + koha_conf => $ENV{KOHA_CONF}, } - 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' => 'UTF-8', - 'parse_items' => '1', - 'runinbackground' => '1', - 'completedJobID' => $jobID, - 'record_type' => 'biblio' - } - }, - 'stage MARC' ); -$agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' ); +my $import_batch_id = $mock_zebra->load_records_ui($file); my $bookdescription; if ( $marcflavour eq 'UNIMARC' ) { @@ -169,22 +75,23 @@ else { $bookdescription = 'Data structures'; } -# Save the staged records URI for later use -my $staged_records_uri = $agent->uri; +my $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 $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1]; # Get datatable for the batch id -$agent->get_ok( - "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id", - 'get the datatable for the new batch id' -); -$jsonresponse = decode_json $agent->content; +$agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id"); +my $jsonresponse = decode_json $agent->content; like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' ); is( $jsonresponse->{ aaData }[0]->{ status }, 'staged', 'record marked as staged' ); is( $jsonresponse->{ aaData }[0]->{ overlay_status }, 'no_match', 'record has no matches' ); # Back to the manage staged records page -$agent->get($staged_records_uri); +$agent->get("$intranet/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=$import_batch_id"); $agent->form_number(6); $agent->field( 'framework', '' ); $agent->click_ok( 'mainformsubmit', "imported records into catalog" ); @@ -201,7 +108,7 @@ $agent->get_ok( $agent->content_contains( 'Details for ' . $bookdescription, 'bib is imported' ); -$agent->get($staged_records_uri); +$agent->get("$intranet/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=$import_batch_id"); $agent->form_number(5); $agent->click_ok( 'mainformsubmit', "revert import" ); $agent->get_ok( diff --git a/t/db_dependent/www/search_utf8.t b/t/db_dependent/www/search_utf8.t index 70d4e1464c5..df72115359b 100755 --- a/t/db_dependent/www/search_utf8.t +++ b/t/db_dependent/www/search_utf8.t @@ -63,7 +63,7 @@ elsif ( not $opac ) { plan skip_all => "Tests skip. You must set env. variable KOHA_OPAC_URL to do tests\n"; } else { - plan tests => 89; + plan tests => 80; } $intranet =~ s#/$##; diff --git a/t/lib/Mocks/Zebra.pm b/t/lib/Mocks/Zebra.pm index cc45a5c958d..9231e1f4235 100644 --- a/t/lib/Mocks/Zebra.pm +++ b/t/lib/Mocks/Zebra.pm @@ -22,6 +22,7 @@ use File::Temp qw( tempdir ); use File::Path qw( rmtree ); use JSON qw( decode_json ); use C4::ImportBatch; +use Koha::BackgroundJobs; =head1 NAME @@ -172,75 +173,31 @@ sub load_records_ui { '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; + sleep(1); + # FIXME - This if fragile and can fail if there is a race condition + my $job = Koha::BackgroundJobs->search({ type => 'stage_marc_for_import' })->last; + my $i; + while ( $job->status ne 'finished' ) { + sleep(1); + last if ++$i > 10; } - is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' ); + is ( $job->status, 'finished', 'job is finished' ); + + $job->discard_changes; + my $import_batch_id = $job->report->{import_batch_id}; $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' + "$cgi_root/tools/manage-marc-import.pl?import_batch_id=$import_batch_id", ); - $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 $batch_id = $2; # wait enough time for the indexer sleep 10; - return $batch_id; + return $import_batch_id; } -- 2.25.1