Bug 9988: Add get_usage_count and linked_biblionumbers to Koha::Authority

When replacing the Zebra code in sub merge, we actually need CountUsage
as well as the results it gets from SearchEngine.

In order to get count and/or results, we now create:

[1] instance methods get_usage_count and linked_biblionumbers in
    Koha::Authority,
[2] class methods of the same name in Koha::Authorities.

The instance method calls the class method. The class method can be used
for deleted authority records, and in 'legacy calls' before refactoring.

Note: On BZ 18149 we will replace all CountUsage calls.

Test plan:
Run t/db_dependent/Koha/Authorities.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Marc Véron <veron@veron.ch>
Signed-off-by: Jacek Ablewicz <abl@biblos.pk.edu.pl>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Marcel de Rooy 2017-02-15 15:48:17 +01:00 committed by Kyle M Hall
parent 25c87a7542
commit 2bb870b3f0
3 changed files with 192 additions and 3 deletions

View File

@ -35,8 +35,65 @@ Koha::Authorities - Koha Authority object set class
=head2 Class Methods
=head3 get_usage_count
$count = Koha::Authorities->get_usage_count({ authid => $i });
Returns the number of linked biblio records.
Note: Code originates from C4::AuthoritiesMarc::CountUsage.
This is a class method, since the authid may refer to a deleted record.
=cut
sub get_usage_count {
my ( $class, $params ) = @_;
my $authid = $params->{authid} || return;
my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
my ( $err, $result, $count ) = $searcher->simple_search_compat( 'an:' . $authid, 0, 0 );
if( $err ) {
warn "Error: $err from search for " . $authid;
return;
}
return $count;
}
=head3 linked_biblionumbers
my @biblios = Koha::Authorities->linked_biblionumbers({
authid => $id, [ max_results => $max ], [ offset => $offset ],
});
Returns array of biblionumbers, as extracted from the result records of
the search engine.
This is a class method, since the authid may refer to a deleted record.
=cut
sub linked_biblionumbers {
my ( $self, $params ) = @_;
my $authid = $params->{authid} || return;
my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
# if max_results is undefined, we will get all results
my ( $err, $result, $count ) = $searcher->simple_search_compat( 'an:' . $authid, $params->{offset} // 0, $params->{max_results} );
if( $err ) {
warn "Error: $err from search for " . $authid;
return;
}
my @biblionumbers;
foreach my $res ( @$result ) {
my $bibno = $searcher->extract_biblionumber( $res );
push @biblionumbers, $bibno if $bibno;
}
return @biblionumbers;
}
=head3 type
=cut

View File

@ -20,6 +20,7 @@ package Koha::Authority;
use Modern::Perl;
use base qw(Koha::Object);
use Koha::SearchEngine::Search;
=head1 NAME
@ -27,10 +28,39 @@ Koha::Authority - Koha Authority Object class
=head1 API
=head2 Class Methods
=head2 Instance Methods
=head3 get_usage_count
$count = $self->get_usage_count;
Returns the number of linked biblio records.
=cut
sub get_usage_count {
my ( $self ) = @_;
return Koha::Authorities->get_usage_count({ authid => $self->authid });
}
=head3 linked_biblionumbers
my @biblios = $self->linked_biblionumbers({
[ max_results => $max ], [ offset => $offset ],
});
Returns an array of biblionumbers.
=cut
sub linked_biblionumbers {
my ( $self, $params ) = @_;
$params->{authid} = $self->authid;
return Koha::Authorities->linked_biblionumbers( $params );
}
=head2 Class Methods
=head3 type
=cut

View File

@ -19,12 +19,16 @@
use Modern::Perl;
use Test::More tests => 5;
use Test::More tests => 6;
use MARC::Field;
use MARC::File::XML;
use MARC::Record;
use Test::Deep;
use Test::MockModule;
use Test::MockObject;
use Test::Warn;
use C4::Context;
use Koha::Authority;
use Koha::Authorities;
use Koha::Authority::MergeRequest;
@ -33,12 +37,22 @@ use Koha::Authority::Type;
use Koha::Authority::Types;
use Koha::Database;
use t::lib::Mocks;
use t::lib::TestBuilder;
BEGIN {
#TODO Helpful as long as we have issues here
my $mock = Test::MockObject->new();
$mock->fake_module( 'Catmandu::Store::ElasticSearch' );
}
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
my $builder = t::lib::TestBuilder->new;
# Globals
our $search_compat_pars;
our $builder = t::lib::TestBuilder->new;
my $nb_of_authorities = Koha::Authorities->search->count;
my $nb_of_authority_types = Koha::Authority::Types->search->count;
my $new_authority_type_1 = Koha::Authority::Type->new(
@ -110,4 +124,92 @@ subtest 'Testing reporting_tag_xml in MergeRequests' => sub {
);
};
subtest 'Trivial tests for get_count_usage and linked_biblionumbers' => sub {
plan tests => 5;
# NOTE: We are not testing $searcher->simple_search_compat here. Suppose
# that should be done in t/db../Koha/SearchEngine?
# So we're just testing the 'wrapper' here.
my ( $mods, $koha_fields );
t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
$mods->{zebra} = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' );
$mods->{elastic} = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch::Search' );
$mods->{biblio} = Test::MockModule->new( 'C4::Biblio' );
$mods->{zebra}->mock( 'simple_search_compat', \&simple_search_compat );
$mods->{elastic}->mock( 'simple_search_compat', \&simple_search_compat );
$mods->{biblio}->mock( 'GetMarcFromKohaField', sub { return @$koha_fields; });
my $auth1 = $builder->build({ source => 'AuthHeader' });
$auth1 = Koha::Authorities->find( $auth1->{authid} );
# Test error condition
my $count;
$search_compat_pars = [ 0, 'some_error' ];
warning_like { $count = $auth1->get_usage_count }
qr/some_error/, 'Catch warn of simple_search_compat';
is( $count, undef, 'Undef returned when error encountered' );
# Simple test with some results; one result discarded in the 2nd test
$search_compat_pars = [ 1 ];
$koha_fields = [ '001', '' ];
is( $auth1->get_usage_count, 3, 'Three results expected (Zebra)' );
cmp_deeply( [ $auth1->linked_biblionumbers ], [ 1001, 3003 ],
'linked_biblionumbers should ignore record without biblionumber' );
# And a simple test with Elastic
t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
cmp_deeply( [ $auth1->linked_biblionumbers ], [ 2001 ],
'linked_biblionumbers with Elasticsearch' );
};
sub simple_search_compat {
if( $search_compat_pars->[0] == 0 ) {
return ( $search_compat_pars->[1], [], 0 );
} elsif( $search_compat_pars->[0] == 1 ) {
my $records = C4::Context->preference('SearchEngine') eq 'Zebra'
? few_marcxml_records()
: few_marc_records();
return ( undef, $records, scalar @$records );
}
}
sub few_marcxml_records {
return [
q|<?xml version="1.0" encoding="UTF-8"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<controlfield tag="001">1001</controlfield>
<datafield tag="110" ind1=" " ind2=" ">
<subfield code="9">102</subfield>
<subfield code="a">My Corporation</subfield>
</datafield>
</record>|,
q|<?xml version="1.0" encoding="UTF-8"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<!-- No biblionumber here -->
<datafield tag="610" ind1=" " ind2=" ">
<subfield code="9">112</subfield>
<subfield code="a">Another Corporation</subfield>
</datafield>
</record>|,
q|<?xml version="1.0" encoding="UTF-8"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.loc.gov/MARC21/slim" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd">
<controlfield tag="001">3003</controlfield>
<datafield tag="110" ind1=" " ind2=" ">
<subfield code="9">102</subfield>
<subfield code="a">My Corporation</subfield>
</datafield>
</record>|
];
}
sub few_marc_records {
my $marc = MARC::Record->new;
$marc->append_fields(
MARC::Field->new( '001', '2001' ),
MARC::Field->new( '245', '', '', a => 'Title' ),
);
return [ $marc ];
}
$schema->storage->txn_rollback;