From 4e52110f447ba54d2f7c02ed88bc12718a9bf4fd Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 13 Nov 2020 16:33:40 +0000 Subject: [PATCH] Bug 26903: [20.05.x] Pass record ids and records through to update_index when passed to index_records When both a record and record_id are passed to index_records the data should be passed through to update_index. We missed copying over the record ids to the variable we use as a check. To test: 1 - Set searchEngine system preference to Elasticsearch 2 - Reindex your db 3 - Search authorities 4 - Edit a record and add 'testwaffle' to the main heading 5 - Search authorities for 'testwaffle' - no results 6 - Apply patch 7 - Edit the record again, change 'testwaffle' to 'testpancake' 8 - Search authorities for 'testpancake' - result! 9 - Confirm imported authorities and authorities added via Z39 are correctly indexed Signed-off-by: David Nind Signed-off-by: Martin Renvoize Bug 26903: Unit tests Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- Koha/SearchEngine/Elasticsearch/Indexer.pm | 5 ++-- .../Koha/SearchEngine/Elasticsearch/Indexer.t | 34 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/Indexer.pm b/Koha/SearchEngine/Elasticsearch/Indexer.pm index 38946d2843..e5f6474fb3 100644 --- a/Koha/SearchEngine/Elasticsearch/Indexer.pm +++ b/Koha/SearchEngine/Elasticsearch/Indexer.pm @@ -101,7 +101,6 @@ sub update_index { my $documents = $self->marc_records_to_documents($records); my @body; - for (my $i = 0; $i < scalar @$biblionums; $i++) { my $id = $biblionums->[$i]; my $document = $documents->[$i]; @@ -302,7 +301,9 @@ sub index_records { $records = [$records] if ref $records ne 'ARRAY' && defined $records; if ( $op eq 'specialUpdate' ) { my $index_record_numbers; - unless ($records) { + if ($records){ + $index_record_numbers = $record_numbers; + } else { foreach my $record_number ( @$record_numbers ){ my $record = _get_record( $record_number, $server ); if( $record ){ diff --git a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Indexer.t b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Indexer.t index 6fc4123954..42e9266c9b 100644 --- a/t/db_dependent/Koha/SearchEngine/Elasticsearch/Indexer.t +++ b/t/db_dependent/Koha/SearchEngine/Elasticsearch/Indexer.t @@ -19,9 +19,11 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Test::MockModule; +use Test::Warn; use t::lib::Mocks; +use t::lib::TestBuilder; use MARC::Record; @@ -35,9 +37,12 @@ SKIP: { eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; }; - skip 'Elasticsearch configuration not available', 1 + skip 'Elasticsearch configuration not available', 2 if $@; +my $builder = t::lib::TestBuilder->new; +my $biblio = $builder->build_sample_biblio; # create biblio before we start mocking to avoid trouble indexing on creation + subtest 'create_index() tests' => sub { plan tests => 6; my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' ); @@ -80,4 +85,29 @@ subtest 'create_index() tests' => sub { 'Dropping the index' ); }; + +subtest 'index_records() tests' => sub { + plan tests => 2; + my $mock_index = Test::MockModule->new("Koha::SearchEngine::Elasticsearch::Indexer"); + $mock_index->mock( update_index => sub { + my ($self, $record_ids, $records) = @_; + warn $record_ids->[0] . $records->[0]->as_usmarc; + }); + + my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'authorities' }); + + my $marc_record = MARC::Record->new(); + $marc_record->append_fields( + MARC::Field->new('001', '1234567'), + MARC::Field->new('100', '', '', 'a' => 'Rosenstock, Jeff'), + ); + warning_is{ $indexer->index_records([42],'specialUpdate','authorityserver',[$marc_record]); } "42".$marc_record->as_usmarc, + "When passing record and ids to index_records they are correctly passed through to update_index"; + + $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' }); + $marc_record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber, embed_items => 1 }); + warning_is{ $indexer->index_records([$biblio->biblionumber],'specialUpdate','biblioserver'); } $biblio->biblionumber.$marc_record->as_usmarc, + "When passing id only to index_records the marc record is fetched and passed through to update_index"; +}; + } -- 2.11.0