Bug 17642: Add and use get_descriptions_by_koha_field
[koha.git] / t / db_dependent / AuthorisedValues.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 15;
5
6 use C4::Context;
7 use Koha::AuthorisedValue;
8 use Koha::AuthorisedValues;
9 use Koha::AuthorisedValueCategories;
10 use Koha::MarcSubfieldStructures;
11
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
15
16 $dbh->do("DELETE FROM authorised_values");
17 $dbh->do("DELETE FROM authorised_value_categories");
18
19 # insert
20 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store;
21 Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store;
22 Koha::AuthorisedValueCategory->new({ category_name => 'restricted_for_testing' })->store;
23 my $av1 = Koha::AuthorisedValue->new(
24     {
25         category         => 'av_for_testing',
26         authorised_value => 'value 1',
27         lib              => 'display value 1',
28         lib_opac         => 'opac display value 1',
29         imageurl         => 'image1.png',
30     }
31 )->store();
32
33 my $av2 = Koha::AuthorisedValue->new(
34     {
35         category         => 'av_for_testing',
36         authorised_value => 'value 2',
37         lib              => 'display value 2',
38         lib_opac         => 'opac display value 2',
39         imageurl         => 'image2.png',
40     }
41 )->store();
42
43 my $av3 = Koha::AuthorisedValue->new(
44     {
45         category         => 'av_for_testing',
46         authorised_value => 'value 3',
47         lib              => 'display value 3',
48         lib_opac         => 'opac display value 3',
49         imageurl         => 'image2.png',
50     }
51 )->store();
52
53 my $av4 = Koha::AuthorisedValue->new(
54     {
55         category         => 'aaav_for_testing',
56         authorised_value => 'value 4',
57         lib              => 'display value 4',
58         lib_opac         => 'opac display value 4',
59         imageurl         => 'image4.png',
60     }
61 )->store();
62 my $av_empty_string = Koha::AuthorisedValue->new(
63     {
64         category         => 'restricted_for_testing',
65         authorised_value => undef, # Should have been defaulted to ""
66         lib              => 'display value undef',
67         lib_opac         => 'opac display value undef',
68     }
69 )->store();
70 my $av_0 = Koha::AuthorisedValue->new(
71     {
72         category         => 'restricted_for_testing',
73         authorised_value => 0,
74         lib              => 'display value 0',
75         lib_opac         => 'opac display value 0',
76     }
77 )->store();
78
79 ok( $av1->id(), 'AV 1 is inserted' );
80 ok( $av2->id(), 'AV 2 is inserted' );
81 ok( $av3->id(), 'AV 3 is inserted' );
82 ok( $av4->id(), 'AV 4 is inserted' );
83
84 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
85 $av3->lib_opac('');
86 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
87
88 my @authorised_values =
89   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
90 is( @authorised_values, 3, "Get correct number of values" );
91
92 my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
93 my $branch1 = $branches_rs->next();
94 my $branchcode1 = $branch1->branchcode();
95 my $branch2 = $branches_rs->next();
96 my $branchcode2 = $branch2->branchcode();
97
98 $av1->add_branch_limitation( $branchcode1 );
99
100 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
101 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
102
103 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
104 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
105
106 $av1->del_branch_limitation( $branchcode1 );
107 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
108 is( @authorised_values, 3, "Branch limitation deleted successfully" );
109
110 $av1->add_branch_limitation( $branchcode1 );
111 $av1->branch_limitations( [ $branchcode1, $branchcode2 ] );
112
113 my $limits = $av1->branch_limitations;
114 is( @$limits, 2, 'branch_limitations functions correctly both as setter and getter' );
115
116 my @categories = Koha::AuthorisedValues->new->categories;
117 is( @categories, 3, 'There should have 2 categories inserted' );
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)' );
120
121 subtest 'search_by_*_field + find_by_koha_field + get_description' => sub {
122     plan tests => 5;
123     my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
124     $loc_cat->delete if $loc_cat;
125     my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
126     $mss->delete if $mss;
127     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
128     $mss->delete if $mss;
129     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => '5', frameworkcode => '' } );
130     $mss->delete if $mss;
131     Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store;
132     Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
133     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
134     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
135     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
136     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => '5', frameworkcode => '', authorised_value => 'restricted_for_testing', kohafield => 'items.restricted' } )->store;
137     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store;
138     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store;
139     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store;
140     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
141     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
142     subtest 'search_by_marc_field' => sub {
143         plan tests => 4;
144         my $avs;
145         $avs = Koha::AuthorisedValues->search_by_marc_field();
146         is ( $avs, undef );
147         $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
148         is ( $avs, undef );
149         $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'});
150         is( $avs->count, 3, 'default fk');
151         is( $avs->next->authorised_value, 'location_1', );
152     };
153     subtest 'search_by_koha_field' => sub {
154         plan tests => 3;
155         my $avs;
156         $avs = Koha::AuthorisedValues->search_by_koha_field();
157         is ( $avs, undef );
158         $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location', tagfield => 952, tagsubfield => 'c' } );
159         is( $avs->count,                  3, );
160         is( $avs->next->authorised_value, 'location_1', );
161
162     };
163     subtest 'find_by_koha_field' => sub {
164         plan tests => 3;
165         # Test authorised_value = 0
166         my $av;
167         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } );
168         is( $av->lib, $av_0->lib, );
169         # Test authorised_value = ""
170         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } );
171         is( $av->lib, $av_empty_string->lib, );
172         # Test authorised_value = undef => we do not want to retrieve anything
173         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
174         is( $av, undef, );
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     };
203     subtest 'get_descriptions_by_koha_field' => sub {
204         plan tests => 1;
205         my @descriptions = Koha::AuthorisedValues->get_descriptions_by_koha_field( { kohafield => 'items.restricted' } );
206         is_deeply(
207             \@descriptions,
208             [
209                 {
210                     authorised_value => '',
211                     lib              => $av_empty_string->lib,
212                     opac_description => $av_empty_string->lib_opac
213                 },
214                 {
215                     authorised_value => $av_0->authorised_value,
216                     lib              => $av_0->lib,
217                     opac_description => $av_0->lib_opac
218                 }
219             ],
220         );
221     };
222 };