View | Details | Raw Unified | Return to bug 9988
Collapse All | Expand All

(-)a/Koha/Authorities.pm (+57 lines)
Lines 35-42 Koha::Authorities - Koha Authority object set class Link Here
35
35
36
=head2 Class Methods
36
=head2 Class Methods
37
37
38
=head3 get_usage_count
39
40
    $count = Koha::Authorities->get_usage_count({ authid => $i });
41
42
    Returns the number of linked biblio records.
43
44
    Note: Code originates from C4::AuthoritiesMarc::CountUsage.
45
46
    This is a class method, since the authid may refer to a deleted record.
47
38
=cut
48
=cut
39
49
50
sub get_usage_count {
51
    my ( $class, $params ) = @_;
52
    my $authid = $params->{authid} || return;
53
54
    my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
55
    my ( $err, $result, $count ) = $searcher->simple_search_compat( 'an:' . $authid, 0, 0 );
56
    if( $err ) {
57
        warn "Error: $err from search for " . $authid;
58
        return;
59
    }
60
    return $count;
61
}
62
63
=head3 linked_biblionumbers
64
65
    my @biblios = Koha::Authorities->linked_biblionumbers({
66
        authid => $id, [ max_results => $max ], [ offset => $offset ],
67
    });
68
69
    Returns array of biblionumbers, as extracted from the result records of
70
    the search engine.
71
72
    This is a class method, since the authid may refer to a deleted record.
73
74
=cut
75
76
sub linked_biblionumbers {
77
    my ( $self, $params ) = @_;
78
    my $authid = $params->{authid} || return;
79
80
    my $searcher = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
81
    # if max_results is undefined, we will get all results
82
    my ( $err, $result, $count ) = $searcher->simple_search_compat( 'an:' . $authid, $params->{offset} // 0, $params->{max_results} );
83
84
    if( $err ) {
85
        warn "Error: $err from search for " . $authid;
86
        return;
87
    }
88
89
    my @biblionumbers;
90
    foreach my $res ( @$result ) {
91
        my $bibno = $searcher->extract_biblionumber( $res );
92
        push @biblionumbers, $bibno if $bibno;
93
    }
94
    return @biblionumbers;
95
}
96
40
=head3 type
97
=head3 type
41
98
42
=cut
99
=cut
(-)a/Koha/Authority.pm (-1 / +31 lines)
Lines 20-25 package Koha::Authority; Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use base qw(Koha::Object);
22
use base qw(Koha::Object);
23
use Koha::SearchEngine::Search;
23
24
24
=head1 NAME
25
=head1 NAME
25
26
Lines 27-36 Koha::Authority - Koha Authority Object class Link Here
27
28
28
=head1 API
29
=head1 API
29
30
30
=head2 Class Methods
31
=head2 Instance Methods
32
33
=head3 get_usage_count
34
35
    $count = $self->get_usage_count;
36
37
    Returns the number of linked biblio records.
31
38
32
=cut
39
=cut
33
40
41
sub get_usage_count {
42
    my ( $self ) = @_;
43
    return Koha::Authorities->get_usage_count({ authid => $self->authid });
44
}
45
46
=head3 linked_biblionumbers
47
48
    my @biblios = $self->linked_biblionumbers({
49
        [ max_results => $max ], [ offset => $offset ],
50
    });
51
52
    Returns an array of biblionumbers.
53
54
=cut
55
56
sub linked_biblionumbers {
57
    my ( $self, $params ) = @_;
58
    $params->{authid} = $self->authid;
59
    return Koha::Authorities->linked_biblionumbers( $params );
60
}
61
62
=head2 Class Methods
63
34
=head3 type
64
=head3 type
35
65
36
=cut
66
=cut
(-)a/t/db_dependent/Koha/Authorities.t (-3 / +104 lines)
Lines 19-30 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 5;
22
use Test::More tests => 6;
23
use MARC::Field;
23
use MARC::Field;
24
use MARC::File::XML;
24
use MARC::File::XML;
25
use MARC::Record;
25
use MARC::Record;
26
use Test::Deep;
26
use Test::Deep;
27
use Test::MockModule;
28
use Test::MockObject;
29
use Test::Warn;
27
30
31
use C4::Context;
28
use Koha::Authority;
32
use Koha::Authority;
29
use Koha::Authorities;
33
use Koha::Authorities;
30
use Koha::Authority::MergeRequest;
34
use Koha::Authority::MergeRequest;
Lines 33-44 use Koha::Authority::Type; Link Here
33
use Koha::Authority::Types;
37
use Koha::Authority::Types;
34
use Koha::Database;
38
use Koha::Database;
35
39
40
use t::lib::Mocks;
36
use t::lib::TestBuilder;
41
use t::lib::TestBuilder;
37
42
43
BEGIN {
44
    #TODO Helpful as long as we have issues here
45
    my $mock = Test::MockObject->new();
46
    $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
47
}
48
38
my $schema = Koha::Database->new->schema;
49
my $schema = Koha::Database->new->schema;
39
$schema->storage->txn_begin;
50
$schema->storage->txn_begin;
40
51
41
my $builder               = t::lib::TestBuilder->new;
52
# Globals
53
our $search_compat_pars;
54
our $builder              = t::lib::TestBuilder->new;
55
42
my $nb_of_authorities     = Koha::Authorities->search->count;
56
my $nb_of_authorities     = Koha::Authorities->search->count;
43
my $nb_of_authority_types = Koha::Authority::Types->search->count;
57
my $nb_of_authority_types = Koha::Authority::Types->search->count;
44
my $new_authority_type_1  = Koha::Authority::Type->new(
58
my $new_authority_type_1  = Koha::Authority::Type->new(
Lines 110-113 subtest 'Testing reporting_tag_xml in MergeRequests' => sub { Link Here
110
    );
124
    );
111
};
125
};
112
126
127
subtest 'Trivial tests for get_count_usage and linked_biblionumbers' => sub {
128
    plan tests => 5;
129
130
    # NOTE: We are not testing $searcher->simple_search_compat here. Suppose
131
    # that should be done in t/db../Koha/SearchEngine?
132
    # So we're just testing the 'wrapper' here.
133
134
    my ( $mods, $koha_fields );
135
    t::lib::Mocks::mock_preference('SearchEngine', 'Zebra');
136
    $mods->{zebra} = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' );
137
    $mods->{elastic} = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch::Search' );
138
    $mods->{biblio} = Test::MockModule->new( 'C4::Biblio' );
139
    $mods->{zebra}->mock( 'simple_search_compat', \&simple_search_compat );
140
    $mods->{elastic}->mock( 'simple_search_compat', \&simple_search_compat );
141
    $mods->{biblio}->mock( 'GetMarcFromKohaField', sub { return @$koha_fields; });
142
143
    my $auth1 = $builder->build({ source => 'AuthHeader' });
144
    $auth1 = Koha::Authorities->find( $auth1->{authid} );
145
146
    # Test error condition
147
    my $count;
148
    $search_compat_pars = [ 0, 'some_error' ];
149
    warning_like { $count = $auth1->get_usage_count }
150
        qr/some_error/, 'Catch warn of simple_search_compat';
151
    is( $count, undef, 'Undef returned when error encountered' );
152
153
    # Simple test with some results; one result discarded in the 2nd test
154
    $search_compat_pars = [ 1 ];
155
    $koha_fields = [ '001', '' ];
156
    is(  $auth1->get_usage_count, 3, 'Three results expected (Zebra)' );
157
    cmp_deeply( [ $auth1->linked_biblionumbers ], [ 1001, 3003 ],
158
        'linked_biblionumbers should ignore record without biblionumber' );
159
160
    # And a simple test with Elastic
161
    t::lib::Mocks::mock_preference('SearchEngine', 'Elasticsearch');
162
    cmp_deeply( [ $auth1->linked_biblionumbers ], [ 2001 ],
163
        'linked_biblionumbers with Elasticsearch' );
164
};
165
166
sub simple_search_compat {
167
    if( $search_compat_pars->[0] == 0 ) {
168
        return ( $search_compat_pars->[1], [], 0 );
169
    } elsif( $search_compat_pars->[0] == 1 ) {
170
        my $records = C4::Context->preference('SearchEngine') eq 'Zebra'
171
            ? few_marcxml_records()
172
            : few_marc_records();
173
        return ( undef, $records, scalar @$records );
174
    }
175
}
176
177
sub few_marcxml_records {
178
    return [
179
q|<?xml version="1.0" encoding="UTF-8"?>
180
<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">
181
    <controlfield tag="001">1001</controlfield>
182
    <datafield tag="110" ind1=" " ind2=" ">
183
        <subfield code="9">102</subfield>
184
        <subfield code="a">My Corporation</subfield>
185
    </datafield>
186
</record>|,
187
q|<?xml version="1.0" encoding="UTF-8"?>
188
<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">
189
    <!-- No biblionumber here -->
190
    <datafield tag="610" ind1=" " ind2=" ">
191
        <subfield code="9">112</subfield>
192
        <subfield code="a">Another Corporation</subfield>
193
    </datafield>
194
</record>|,
195
q|<?xml version="1.0" encoding="UTF-8"?>
196
<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">
197
    <controlfield tag="001">3003</controlfield>
198
    <datafield tag="110" ind1=" " ind2=" ">
199
        <subfield code="9">102</subfield>
200
        <subfield code="a">My Corporation</subfield>
201
    </datafield>
202
</record>|
203
    ];
204
}
205
206
sub few_marc_records {
207
    my $marc = MARC::Record->new;
208
    $marc->append_fields(
209
        MARC::Field->new( '001', '2001' ),
210
        MARC::Field->new( '245', '', '', a => 'Title' ),
211
    );
212
    return [ $marc ];
213
}
214
113
$schema->storage->txn_rollback;
215
$schema->storage->txn_rollback;
114
- 

Return to bug 9988