From 7ab7cc526cc14d1d6390187f1d157c17b0149aa8 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Wed, 10 Apr 2024 08:24:56 +0000 Subject: [PATCH] Bug 35659: (QA follow-up) --- Koha/OAI/Client/Harvester.pm | 18 ++++++++---------- misc/cronjobs/harvest_oai.pl | 2 +- t/db_dependent/Koha/OAIHarvester.t | 25 +++++++++++++++++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Koha/OAI/Client/Harvester.pm b/Koha/OAI/Client/Harvester.pm index f800fb1dc1..433232d8ab 100644 --- a/Koha/OAI/Client/Harvester.pm +++ b/Koha/OAI/Client/Harvester.pm @@ -30,9 +30,9 @@ Koha::OAI::Client::Harvester contains OAI-PMH harvester main functions use utf8; use open qw( :std :utf8 ); -use C4::Biblio qw( AddBiblio GetFrameworkCode ModBiblio DelBiblio ); +use C4::Biblio qw( AddBiblio GetFrameworkCode ModBiblio DelBiblio ); use C4::AuthoritiesMarc qw (AddAuthority GuessAuthTypeCode ModAuthority DelAuthority ); -use C4::Log qw( cronlogaction ); +use C4::Log qw( cronlogaction ); use HTTP::OAI; use HTTP::OAI::Metadata::OAI_DC; use Koha::DateUtils qw( dt_from_string ); @@ -47,7 +47,7 @@ use MARC::Record; use Modern::Perl; use DateTime; use DateTime::Format::Strptime; -use Try::Tiny qw( catch try ); +use Try::Tiny qw( catch try ); use Date::Calc qw( Add_Delta_Days Today @@ -88,7 +88,7 @@ sub new { $verbose = $args->{'verbose'}; $days = $args->{'days'}; $force = $args->{'force'}; - return $self; + return bless $args, $self; } =head2 init @@ -143,7 +143,6 @@ sub init { if ($days) { # Change this to yyyy-mm-dd - my $dt_today = dt_from_string(); my ( $nowyear, $nowmonth, $nowday ) = Today(); my @date = Add_Delta_Days( $nowyear, $nowmonth, $nowday, -$days ); my $dt_start = DateTime->new( year => $date[0], month => $date[1], day => $date[2] ); @@ -296,10 +295,9 @@ sub processRecord { $status = 'deleted'; } } else { - my $existing_dt = $strp->parse_datetime( $imported_record->datestamp ); - my $incoming_dt = $strp->parse_datetime( $oai_record->datestamp ); - - if ( $force || !$incoming_dt || !$existing_dt || $incoming_dt > $existing_dt ) { + my $existing_dt = dt_from_string( $imported_record->datestamp, 'iso' ); + my $incoming_dt = $oai_record->datestamp ? dt_from_string( $oai_record->datestamp, 'iso' ) : dt_from_string; + if ( $force || $incoming_dt > $existing_dt ) { if ( $server->recordtype eq "biblio" ) { my $biblionumber = $imported_record->biblionumber; my $result = ModBiblio( $marcrecord, $biblionumber, GetFrameworkCode($biblionumber) ); @@ -311,7 +309,7 @@ sub processRecord { } $imported_record->update( { - datestamp => $imported_record->datestamp, + datestamp => $incoming_dt, } ); $status = 'updated'; diff --git a/misc/cronjobs/harvest_oai.pl b/misc/cronjobs/harvest_oai.pl index 2cc212652d..5471cbe164 100755 --- a/misc/cronjobs/harvest_oai.pl +++ b/misc/cronjobs/harvest_oai.pl @@ -25,7 +25,7 @@ use Getopt::Long qw( GetOptions ); use Koha::Script -cron; use Koha::OAI::Client::Harvester; use Koha::OaiServers; -use C4::Log qw( cronlogaction ); +use C4::Log qw( cronlogaction ); use Try::Tiny qw( catch try ); my $command_line_options = join( " ", @ARGV ); diff --git a/t/db_dependent/Koha/OAIHarvester.t b/t/db_dependent/Koha/OAIHarvester.t index e42b9a08f4..db973bd268 100755 --- a/t/db_dependent/Koha/OAIHarvester.t +++ b/t/db_dependent/Koha/OAIHarvester.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 9; use Test::Exception; use t::lib::TestBuilder; @@ -53,13 +53,23 @@ my $record = '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->header->datestamp('2017-05-10T09:18:13Z'); $r->metadata( HTTP::OAI::Metadata->new( dom => $record ) ); my $status = $harvester->processRecord($r); is( $status, 'skipped', 'Record with no identifier is skipped' ); $r->header->identifier('oai:myarchive.org:oid-233'); +$status = $harvester->processRecord($r); +is( $status, 'added', 'Record with no date is added' ); + +$status = $harvester->processRecord($r); +is( $status, 'updated', 'Record with no date is updated' ); + +$status = $harvester->processRecord($r); +is( $status, 'updated', 'Record with no date is updated even without force' ); + +$r->header->identifier('oai:myarchive.org:oid-234'); +$r->header->datestamp('2017-05-10T09:18:13Z'); $status = $harvester->processRecord($r); is( $status, 'added', 'Record is added' ); @@ -75,4 +85,15 @@ $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::Oaipmh::Biblios->find( { identifier => 'oai:myarchive.org:oid-234' } ); +my $added_datestamp = $imported_record->datestamp; +$r->header->datestamp(undef); +$status = $harvester->processRecord($r); +$imported_record = Koha::Import::Oaipmh::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' +); + $schema->storage->txn_rollback; -- 2.30.2