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

(-)a/Koha/AuthorisedValues.pm (+20 lines)
Lines 25-30 use Koha::Database; Link Here
25
25
26
use Koha::AuthorisedValue;
26
use Koha::AuthorisedValue;
27
use Koha::MarcSubfieldStructures;
27
use Koha::MarcSubfieldStructures;
28
use Koha::Cache::Memory::Lite;
28
29
29
use base qw(Koha::Objects);
30
use base qw(Koha::Objects);
30
31
Lines 119-124 sub find_by_koha_field { Link Here
119
    return $av->count ? $av->next : undef;
120
    return $av->count ? $av->next : undef;
120
}
121
}
121
122
123
sub get_description_by_koha_field {
124
    my ( $self, $params ) = @_;
125
    my $frameworkcode    = $params->{frameworkcode} || '';
126
    my $kohafield        = $params->{kohafield};
127
    my $authorised_value = $params->{authorised_value};
128
129
    return {} unless defined $authorised_value;
130
131
    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
132
    my $cache_key    = "Av_descriptions:$frameworkcode:$kohafield:$authorised_value";
133
    my $cached       = $memory_cache->get_from_cache($cache_key);
134
    return $cached if $cached;
135
136
    my $av = $self->find_by_koha_field($params);
137
    my $descriptions = { lib => $av->lib, opac_description => $av->opac_description };
138
    $memory_cache->set_in_cache( $cache_key, $descriptions );
139
    return $descriptions;
140
}
141
122
sub categories {
142
sub categories {
123
    my ( $self ) = @_;
143
    my ( $self ) = @_;
124
    my $rs = $self->_resultset->search(
144
    my $rs = $self->_resultset->search(
(-)a/t/db_dependent/AuthorisedValues.t (-3 / +29 lines)
Lines 118-125 is( @categories, 3, 'There should have 2 categories inserted' ); Link Here
118
is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
118
is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
119
is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
119
is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
120
120
121
subtest 'search_by_*_field + find_by_koha_field' => sub {
121
subtest 'search_by_*_field + find_by_koha_field + search_for_descriptions' => sub {
122
    plan tests => 3;
122
    plan tests => 4;
123
    my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
123
    my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
124
    $loc_cat->delete if $loc_cat;
124
    $loc_cat->delete if $loc_cat;
125
    my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
125
    my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
Lines 173-176 subtest 'search_by_*_field + find_by_koha_field' => sub { Link Here
173
        $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
173
        $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
174
        is( $av, undef, );
174
        is( $av, undef, );
175
    };
175
    };
176
    subtest 'get_description_by_koha_field' => sub {
177
        plan tests => 3;
178
        my $descriptions;
179
180
        # Test authorised_value = 0
181
        $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
182
            { kohafield => 'items.restricted', authorised_value => 0 } );
183
        is_deeply( $descriptions,
184
            { lib => $av_0->lib, opac_description => $av_0->lib_opac },
185
        );
186
187
        # Test authorised_value = ""
188
        $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
189
            { kohafield => 'items.restricted', authorised_value => '' } );
190
        is_deeply(
191
            $descriptions,
192
            {
193
                lib              => $av_empty_string->lib,
194
                opac_description => $av_empty_string->lib_opac
195
            },
196
        );
197
198
        # Test authorised_value = undef => we do not want to retrieve anything
199
        $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
200
            { kohafield => 'items.restricted', authorised_value => undef } );
201
        is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
202
    };
176
};
203
};
177
- 

Return to bug 17642