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

(-)a/Koha/AuthorisedValues.pm (-2 / +18 lines)
Lines 87-93 sub search_by_koha_field { Link Here
87
    my $frameworkcode    = $params->{frameworkcode} || '';
87
    my $frameworkcode    = $params->{frameworkcode} || '';
88
    my $kohafield        = $params->{kohafield};
88
    my $kohafield        = $params->{kohafield};
89
    my $category         = $params->{category};
89
    my $category         = $params->{category};
90
    #my $authorised_value = $params->{authorised_value};
91
90
92
    return unless $kohafield;
91
    return unless $kohafield;
93
92
Lines 95-101 sub search_by_koha_field { Link Here
95
        {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
94
        {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
96
            'marc_subfield_structures.kohafield'     => $kohafield,
95
            'marc_subfield_structures.kohafield'     => $kohafield,
97
            ( defined $category ? ( category_name    => $category )         : () ),
96
            ( defined $category ? ( category_name    => $category )         : () ),
98
            ( exists $params->{authorised_value} ? ( 'me.authorised_value' => $params->{authorised_value} ) : () ),
99
        },
97
        },
100
        {   join     => { category => 'marc_subfield_structures' },
98
        {   join     => { category => 'marc_subfield_structures' },
101
            distinct => 1,
99
            distinct => 1,
Lines 103-108 sub search_by_koha_field { Link Here
103
    );
101
    );
104
}
102
}
105
103
104
sub find_by_koha_field {
105
    my ( $self, $params ) = @_;
106
    my $frameworkcode    = $params->{frameworkcode} || '';
107
    my $kohafield        = $params->{kohafield};
108
    my $authorised_value = $params->{authorised_value};
109
110
    my $av = $self->SUPER::search(
111
        {   'marc_subfield_structures.frameworkcode' => $frameworkcode,
112
            'marc_subfield_structures.kohafield'     => $kohafield,
113
            'me.authorised_value'                    => $authorised_value,
114
        },
115
        {   join     => { category => 'marc_subfield_structures' },
116
            distinct => 1,
117
        }
118
    );
119
    return $av->count ? $av->next : undef;
120
}
121
106
sub categories {
122
sub categories {
107
    my ( $self ) = @_;
123
    my ( $self ) = @_;
108
    my $rs = $self->_resultset->search(
124
    my $rs = $self->_resultset->search(
(-)a/t/db_dependent/AuthorisedValues.t (-13 / +13 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' => sub {
121
subtest 'search_by_*_field + find_by_koha_field' => sub {
122
    plan tests => 2;
122
    plan tests => 3;
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 151-157 subtest 'search_by_*_field' => sub { Link Here
151
        is( $avs->next->authorised_value, 'location_1', );
151
        is( $avs->next->authorised_value, 'location_1', );
152
    };
152
    };
153
    subtest 'search_by_koha_field' => sub {
153
    subtest 'search_by_koha_field' => sub {
154
        plan tests => 8;
154
        plan tests => 3;
155
        my $avs;
155
        my $avs;
156
        $avs = Koha::AuthorisedValues->search_by_koha_field();
156
        $avs = Koha::AuthorisedValues->search_by_koha_field();
157
        is ( $avs, undef );
157
        is ( $avs, undef );
Lines 159-175 subtest 'search_by_*_field' => sub { Link Here
159
        is( $avs->count,                  3, );
159
        is( $avs->count,                  3, );
160
        is( $avs->next->authorised_value, 'location_1', );
160
        is( $avs->next->authorised_value, 'location_1', );
161
161
162
    };
163
    subtest 'find_by_koha_field' => sub {
164
        plan tests => 3;
162
        # Test authorised_value = 0
165
        # Test authorised_value = 0
163
        $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } );
166
        my $av;
164
        is( $avs->count,                  1, );
167
        $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } );
165
        is( $avs->next->lib, $av_0->lib, );
168
        is( $av->lib, $av_0->lib, );
166
        # Test authorised_value = ""
169
        # Test authorised_value = ""
167
        $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } );
170
        $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } );
168
        is( $avs->count,                  1, );
171
        is( $av->lib, $av_empty_string->lib, );
169
        is( $avs->next->lib, $av_empty_string->lib, );
170
        # Test authorised_value = undef => we do not want to retrieve anything
172
        # Test authorised_value = undef => we do not want to retrieve anything
171
        $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
173
        $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
172
        is( $avs->count,                  0, );
174
        is( $av, undef, );
173
174
    };
175
    };
175
};
176
};
176
- 

Return to bug 17642