From e5fd04f1d184cc3672bd735826c21be0061ae116 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Tue, 10 Sep 2024 15:34:07 +0000 Subject: [PATCH] Bug 35659: (QA follow-up): Add init function tests by using Koha's OAI server --- Koha/OAI/Client/Harvester.pm | 13 +++-- t/db_dependent/Koha/OAIHarvester.t | 92 ++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/Koha/OAI/Client/Harvester.pm b/Koha/OAI/Client/Harvester.pm index 779632824d8..9e60214be16 100644 --- a/Koha/OAI/Client/Harvester.pm +++ b/Koha/OAI/Client/Harvester.pm @@ -87,10 +87,10 @@ Starts harvesting sub init { my ( $self, $args ) = @_; - $self->printlog("Starting OAI Harvest from repository"); my $server = $self->{server}; my $days = $self->{days}; + my $init_results; my $h = HTTP::OAI::Harvester->new( repository => HTTP::OAI::Identify->new( @@ -117,6 +117,7 @@ sub init { $lmdflog .= $_->metadataPrefix . " "; } $self->printlog("Available metadata formats for this repository: $lmdflog"); + $init_results->{metadata_formats} = $lmdflog; } catch { $self->printlog("ListMetadataFormats failed"); }; @@ -148,7 +149,8 @@ sub init { ); if ( $response->is_error ) { $self->printlog( "Error requesting ListRecords: " . $response->code . " " . $response->message ); - exit; + $init_results->{is_error} = $response->message; + return $init_results; } $self->printlog( "Request URL: " . $response->requestURL ); @@ -164,6 +166,7 @@ sub init { my $status = $self->processRecord($oai_record); $stats{$status}++; } + $init_results->{total} = $stats{'total'}; my $results = ''; foreach my $status (@statuses) { @@ -193,20 +196,22 @@ sub init { ) ) or $self->printlog("OAI_HARVEST_REPORT letter not found"); - my $success = C4::Letters::EnqueueLetter( + my $message_id = C4::Letters::EnqueueLetter( { letter => $letter, to_address => C4::Context->preference("OAI-PMH:HarvestEmailReport"), message_transport_type => 'email' } ); - if ($success) { + if ($message_id) { $self->printlog("Email report enqueued"); + $init_results->{letter_message_id} = $message_id; } else { $self->printlog("Unable to enqueue report email"); } } $self->printlog("Ending OAI Harvest from repository"); + return $init_results; } =head2 processRecord diff --git a/t/db_dependent/Koha/OAIHarvester.t b/t/db_dependent/Koha/OAIHarvester.t index c8ecd701b4b..84b91ad03aa 100755 --- a/t/db_dependent/Koha/OAIHarvester.t +++ b/t/db_dependent/Koha/OAIHarvester.t @@ -18,11 +18,13 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 24; +use Test::More tests => 28; use Test::Exception; use File::Temp qw/tempfile/; use t::lib::TestBuilder; +use t::lib::Mocks; + use HTTP::OAI; use HTTP::OAI::Metadata::OAI_DC; use HTTP::OAI::Record; @@ -39,8 +41,9 @@ $schema->storage->txn_begin; my $new_oai_biblio = Koha::OAIServer->new( { - endpoint => 'my_host1.org', - oai_set => 'set1', + endpoint => C4::Context->preference('OPACBaseURL') + . '/cgi-bin/koha/oai.pl', + oai_set => '', servername => 'my_test_1', dataformat => 'oai_dc', recordtype => 'biblio', @@ -48,10 +51,26 @@ my $new_oai_biblio = Koha::OAIServer->new( } )->store; -my $harvester = Koha::OAI::Client::Harvester->new( { server => $new_oai_biblio, verbose => 1, days => 1, force => 1 } ); +C4::Context->set_preference( 'OAI-PMH', 1 ); +t::lib::Mocks::mock_preference( 'OAI-PMH:HarvestEmailReport', + C4::Context->preference('KohaAdminEmailAddress') ); + +my $harvester = Koha::OAI::Client::Harvester->new( + { server => $new_oai_biblio, verbose => 1, days => 1, force => 1 } ); + +my $init_results = $harvester->init(); + +like( + $init_results->{metadata_formats}, + qr/oai_dc marc21 marcxml/, + 'Got list of supported metadata formats' +); +is( $init_results->{is_error}, undef, 'ListRecords request worked' ); +cmp_ok( $init_results->{total}, '>', 0, 'Records have been processed' ); +isnt( $init_results->{letter_message_id}, undef, 'Report has been enqueued' ); my $record = - 'Pièces diverses ARCH/0320 [cote]FR-920509801 [RCR établissement]Central obrera boliviana [Fonds ou collection]1 carton1971/2000Archives et manuscrits'; +'Pièces diverses ARCH/0320 [cote]FR-920509801 [RCR établissement]Central obrera boliviana [Fonds ou collection]1 carton1971/2000Archives et manuscrits'; my $r = HTTP::OAI::Record->new(); $r->metadata( HTTP::OAI::Metadata->new( dom => $record ) ); @@ -78,15 +97,18 @@ is( $status, 'added', 'Record is added' ); $status = $harvester->processRecord($r); is( $status, 'updated', 'When force is used, record is updated' ); -$harvester = Koha::OAI::Client::Harvester->new( { server => $new_oai_biblio, verbose => 1, days => 1, force => 0 } ); -$status = $harvester->processRecord($r); -is( $status, 'skipped', 'When force is not used, record is skipped (already up to date)' ); +$harvester = Koha::OAI::Client::Harvester->new( + { server => $new_oai_biblio, verbose => 1, days => 1, force => 0 } ); +$status = $harvester->processRecord($r); +is( $status, 'skipped', + 'When force is not used, record is skipped (already up to date)' ); $r->header->datestamp('2018-05-10T09:18:13Z'); $status = $harvester->processRecord($r); is( $status, 'updated', 'When force is not used, record is updated if newer' ); -my $imported_record = Koha::Import::OAI::Biblios->find( { identifier => 'oai:myarchive.org:oid-234' } ); +my $imported_record = Koha::Import::OAI::Biblios->find( + { identifier => 'oai:myarchive.org:oid-234' } ); my $added_datestamp = '2017-05-10T09:18:13Z'; $imported_record->update( { @@ -96,18 +118,20 @@ $imported_record->update( $r->header->datestamp(undef); $status = $harvester->processRecord($r); -$imported_record = Koha::Import::OAI::Biblios->find( { identifier => 'oai:myarchive.org:oid-234' } ); +$imported_record = Koha::Import::OAI::Biblios->find( + { identifier => 'oai:myarchive.org:oid-234' } ); my $updated_datestamp = $imported_record->datestamp; -isnt( - $added_datestamp, $updated_datestamp, - 'local datestamp is updated even if there is no datestamp in incoming record' +isnt( $added_datestamp, $updated_datestamp, +'local datestamp is updated even if there is no datestamp in incoming record' ); $r->header->status('deleted'); $status = $harvester->processRecord($r); -is( $status, 'deleted', 'When a record is marked to be deleted, status is deleted' ); +is( $status, 'deleted', + 'When a record is marked to be deleted, status is deleted' ); -$imported_record = Koha::Import::OAI::Biblios->find( { identifier => 'oai:myarchive.org:oid-234' } ); +$imported_record = Koha::Import::OAI::Biblios->find( + { identifier => 'oai:myarchive.org:oid-234' } ); is( $imported_record, undef, 'Record has been deleted' ); $status = $harvester->processRecord($r); @@ -126,10 +150,11 @@ my $new_oai_auth = Koha::OAIServer->new( } )->store; -$harvester = Koha::OAI::Client::Harvester->new( { server => $new_oai_auth, verbose => 1, days => 1, force => 1 } ); +$harvester = Koha::OAI::Client::Harvester->new( + { server => $new_oai_auth, verbose => 1, days => 1, force => 1 } ); my $auth = - 'EmersonEverett H.'; +'EmersonEverett H.'; $r = HTTP::OAI::Record->new(); $r->metadata( HTTP::OAI::Metadata->new( dom => $auth ) ); @@ -145,7 +170,8 @@ $status = $harvester->processRecord($r); is( $status, 'updated', 'Authority with no date is updated' ); $status = $harvester->processRecord($r); -is( $status, 'updated', 'Authority with no date is updated even without force' ); +is( $status, 'updated', + 'Authority with no date is updated even without force' ); $r->header->identifier('oai:myarchive.org:oid-162'); $r->header->datestamp('2017-05-10T09:18:13Z'); @@ -156,15 +182,19 @@ is( $status, 'added', 'Authority is added' ); $status = $harvester->processRecord($r); is( $status, 'updated', 'When force is used, authority is updated' ); -$harvester = Koha::OAI::Client::Harvester->new( { server => $new_oai_auth, verbose => 1, days => 1, force => 0 } ); -$status = $harvester->processRecord($r); -is( $status, 'skipped', 'When force is not used, authority is skipped (already up to date)' ); +$harvester = Koha::OAI::Client::Harvester->new( + { server => $new_oai_auth, verbose => 1, days => 1, force => 0 } ); +$status = $harvester->processRecord($r); +is( $status, 'skipped', + 'When force is not used, authority is skipped (already up to date)' ); $r->header->datestamp('2018-05-10T09:18:13Z'); $status = $harvester->processRecord($r); -is( $status, 'updated', 'When force is not used, authority is updated if newer' ); +is( $status, 'updated', + 'When force is not used, authority is updated if newer' ); -my $imported_authority = Koha::Import::OAI::Authorities->find( { identifier => 'oai:myarchive.org:oid-162' } ); +my $imported_authority = Koha::Import::OAI::Authorities->find( + { identifier => 'oai:myarchive.org:oid-162' } ); $imported_authority->update( { datestamp => $added_datestamp, @@ -174,18 +204,20 @@ $imported_authority->update( $r->header->datestamp(undef); $status = $harvester->processRecord($r); -$imported_authority = Koha::Import::OAI::Authorities->find( { identifier => 'oai:myarchive.org:oid-162' } ); -$updated_datestamp = $imported_authority->datestamp; -isnt( - $added_datestamp, $updated_datestamp, - 'local datestamp is updated even if there is no datestamp in incoming authority' +$imported_authority = Koha::Import::OAI::Authorities->find( + { identifier => 'oai:myarchive.org:oid-162' } ); +$updated_datestamp = $imported_authority->datestamp; +isnt( $added_datestamp, $updated_datestamp, +'local datestamp is updated even if there is no datestamp in incoming authority' ); $r->header->status('deleted'); $status = $harvester->processRecord($r); -is( $status, 'deleted', 'When an authority is marked to be deleted, status is deleted' ); +is( $status, 'deleted', + 'When an authority is marked to be deleted, status is deleted' ); -$imported_record = Koha::Import::OAI::Biblios->find( { identifier => 'oai:myarchive.org:oid-162' } ); +$imported_record = Koha::Import::OAI::Biblios->find( + { identifier => 'oai:myarchive.org:oid-162' } ); is( $imported_record, undef, 'Authority has been deleted' ); $status = $harvester->processRecord($r); -- 2.39.2