Bug 31992: Fix t::lib::Mocks::Zebra

This will fix t/db_dependent/www/batch.t and t/db_dependent/www/search_utf8.t

QA - improvement ideas welcome! It's definitely not the best we can do.

Test plan:
  prove t/db_dependent/www/search_utf8.t t/db_dependent/www/batch.t
must return green

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2022-10-26 17:34:10 +02:00 committed by Tomas Cohen Arazi
parent dfaf5834d1
commit cccae533fe
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
3 changed files with 45 additions and 179 deletions

View file

@ -26,6 +26,8 @@ use JSON;
use File::Basename;
use File::Spec;
use POSIX;
use t::lib::Mocks::Zebra;
use Koha::BackgroundJobs;
my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
@ -52,114 +54,19 @@ if (not defined $intranet) {
"your username and password";
}
else {
plan tests => 26;
plan tests => 24;
}
$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,29 +76,20 @@ 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;
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->form_number(6);
$agent->field( 'framework', '' );
$agent->click_ok( 'mainformsubmit', "imported records into catalog" );
$agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
$jsonresponse = decode_json $agent->content;
is( $jsonresponse->{ aaData }[0]->{ status }, 'imported', 'record marked as imported' );
my $jsonresponse = decode_json $agent->content;
like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' );
is( $jsonresponse->{ aaData }[0]->{ status }, 'imported', 'record marked as staged' );
is( $jsonresponse->{ aaData }[0]->{ overlay_status }, 'no_match', 'record has no matches' );
my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
@ -201,9 +99,20 @@ $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" );
sleep(1);
# FIXME - This if fragile and can fail if there is a race condition
my $job = Koha::BackgroundJobs->search({ type => 'marc_import_revert_batch' })->last;
my $i;
while ( $job->discard_changes->status ne 'finished' ) {
sleep(1);
last if ++$i > 10;
}
is ( $job->status, 'finished', 'job is finished' );
$agent->get_ok(
"$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
'getting reverted bib' );

View file

@ -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#/$##;

View file

@ -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->discard_changes->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'
"$cgi_root/tools/manage-marc-import.pl?import_batch_id=$import_batch_id",
);
$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;
return $import_batch_id;
}