Bug 35070: Tidy
[koha.git] / t / db_dependent / AuthorisedValues.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 16;
5 use Try::Tiny;
6
7 use t::lib::TestBuilder;
8
9 use Koha::Database;
10 use C4::Context;
11 use Koha::AuthorisedValue;
12 use Koha::AuthorisedValues;
13 use Koha::AuthorisedValueCategories;
14 use Koha::MarcSubfieldStructures;
15
16 my $schema  = Koha::Database->new->schema;
17 $schema->storage->txn_begin;
18 my $builder = t::lib::TestBuilder->new;
19
20 my @existing_categories = Koha::AuthorisedValues->new->categories;
21
22 # insert
23 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing', is_system => 1 })->store;
24 Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store;
25 Koha::AuthorisedValueCategory->new({ category_name => 'restricted_for_testing' })->store;
26 my $av1 = Koha::AuthorisedValue->new(
27     {
28         category         => 'av_for_testing',
29         authorised_value => 'value 1',
30         lib              => 'display value 1',
31         lib_opac         => 'opac display value 1',
32         imageurl         => 'image1.png',
33     }
34 )->store();
35
36 my $av2 = Koha::AuthorisedValue->new(
37     {
38         category         => 'av_for_testing',
39         authorised_value => 'value 2',
40         lib              => 'display value 2',
41         lib_opac         => 'opac display value 2',
42         imageurl         => 'image2.png',
43     }
44 )->store();
45
46 my $av3 = Koha::AuthorisedValue->new(
47     {
48         category         => 'av_for_testing',
49         authorised_value => 'value 3',
50         lib              => 'display value 3',
51         lib_opac         => 'opac display value 3',
52         imageurl         => 'image2.png',
53     }
54 )->store();
55
56 my $av4 = Koha::AuthorisedValue->new(
57     {
58         category         => 'aaav_for_testing',
59         authorised_value => 'value 4',
60         lib              => 'display value 4',
61         lib_opac         => 'opac display value 4',
62         imageurl         => 'image4.png',
63     }
64 )->store();
65 my $av_empty_string = Koha::AuthorisedValue->new(
66     {
67         category         => 'restricted_for_testing',
68         authorised_value => undef, # Should have been defaulted to ""
69         lib              => 'display value undef',
70         lib_opac         => 'opac display value undef',
71     }
72 )->store();
73 my $av_0 = Koha::AuthorisedValue->new(
74     {
75         category         => 'restricted_for_testing',
76         authorised_value => 0,
77         lib              => 'display value 0',
78         lib_opac         => 'opac display value 0',
79     }
80 )->store();
81
82 ok( $av1->id(), 'AV 1 is inserted' );
83 ok( $av2->id(), 'AV 2 is inserted' );
84 ok( $av3->id(), 'AV 3 is inserted' );
85 ok( $av4->id(), 'AV 4 is inserted' );
86
87 { # delete is_system AV categories
88     try {
89         Koha::AuthorisedValueCategories->find('av_for_testing')->delete
90     }
91     catch {
92         ok(
93             $_->isa('Koha::Exceptions::CannotDeleteDefault'),
94             'A system AV category cannot be deleted'
95         );
96     };
97
98     try {
99         Koha::AuthorisedValueCategories->search->delete
100     }
101     catch {
102         ok(
103             $_->isa('Koha::Exceptions::CannotDeleteDefault'),
104             'system AV categories cannot be deleted'
105         );
106     };
107 }
108
109 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
110 $av3->lib_opac('');
111 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
112
113 my @authorised_values =
114   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } )->as_list;
115 is( @authorised_values, 3, "Get correct number of values" );
116
117 my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
118 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
119
120 $av1->add_library_limit( $branchcode1 );
121
122 @authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode1 )->as_list;
123 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
124
125 @authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode2 )->as_list;
126 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
127
128 $av1->del_library_limit( $branchcode1 );
129 @authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode2 )->as_list;
130 is( @authorised_values, 3, "Branch limitation deleted successfully" );
131
132 $av1->add_library_limit( $branchcode1 );
133 $av1->library_limits( [ $branchcode1, $branchcode2 ] );
134
135 my $limits = $av1->library_limits->as_list;
136 is( @$limits, 2, 'library_limits functions correctly both as setter and getter' );
137
138 my @categories = Koha::AuthorisedValues->new->categories;
139 is( @categories, @existing_categories+3, 'There should have 3 categories inserted' );
140 is_deeply(
141     \@categories,
142     [ sort { uc $a cmp uc $b } @categories ],
143     'categories must be ordered by category names'
144 );
145
146 subtest 'search_by_*_field + find_by_koha_field + get_description + authorised_values' => sub {
147     plan tests => 7;
148
149     my $test_cat = Koha::AuthorisedValueCategories->find('TEST');
150     $test_cat->delete if $test_cat;
151     my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
152     $mss->delete if $mss;
153     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ' } );
154     $mss->delete if $mss;
155     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
156     $mss->delete if $mss;
157     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => '5', frameworkcode => '' } );
158     $mss->delete if $mss;
159     $mss = Koha::MarcSubfieldStructures->search( { tagfield => '003', frameworkcode => '' } );
160     $mss->delete if $mss;
161     Koha::AuthorisedValueCategory->new( { category_name => 'TEST' } )->store;
162     Koha::AuthorisedValueCategory->new( { category_name => 'CONTROL_TEST' } )->store;
163     Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
164     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'TEST', kohafield => 'items.location' } )->store;
165     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'TEST', kohafield => 'items.location' } )->store;
166     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
167     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => '5', frameworkcode => '', authorised_value => 'restricted_for_testing', kohafield => 'items.restricted' } )->store;
168     Koha::MarcSubfieldStructure->new( { tagfield => '003', frameworkcode => '', authorised_value => 'CONTROL_TEST', } )
169         ->store;
170     Koha::AuthorisedValue->new( { category => 'TEST', authorised_value => 'location_1', lib => 'location_1' } )->store;
171     Koha::AuthorisedValue->new( { category => 'TEST', authorised_value => 'location_2', lib => 'location_2' } )->store;
172     Koha::AuthorisedValue->new( { category => 'TEST', authorised_value => 'location_3', lib => 'location_3' } )->store;
173     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
174     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
175
176     Koha::AuthorisedValue->new( { category => 'CONTROL_TEST', authorised_value => 'lib1', lib => 'lib1' } )->store;
177     Koha::AuthorisedValue->new( { category => 'CONTROL_TEST', authorised_value => 'lib2', lib => 'lib2' } )->store;
178
179     subtest 'search_by_marc_field' => sub {
180         plan tests => 4;
181         my $avs;
182         $avs = Koha::AuthorisedValues->search_by_marc_field();
183         is ( $avs, undef );
184         $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
185         is ( $avs, undef );
186         $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'});
187         is( $avs->count, 3, 'default fk');
188         is( $avs->next->authorised_value, 'location_1', );
189     };
190     subtest 'search_by_koha_field' => sub {
191         plan tests => 3;
192         my $avs;
193         $avs = Koha::AuthorisedValues->search_by_koha_field();
194         is ( $avs, undef );
195         $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location' } );
196         is( $avs->count,                  3, );
197         is( $avs->next->authorised_value, 'location_1', );
198
199     };
200     subtest 'find_by_koha_field' => sub {
201         plan tests => 3;
202         # Test authorised_value = 0
203         my $av;
204         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } );
205         is( $av->lib, $av_0->lib, );
206         # Test authorised_value = ""
207         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } );
208         is( $av->lib, $av_empty_string->lib, );
209         # Test authorised_value = undef => we do not want to retrieve anything
210         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
211         is( $av, undef, );
212     };
213     subtest 'get_description_by_koha_field' => sub {
214         plan tests => 4;
215         my $descriptions;
216
217         # Test authorised_value = 0
218         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
219             { kohafield => 'items.restricted', authorised_value => 0 } );
220         is_deeply( $descriptions,
221             { lib => $av_0->lib, opac_description => $av_0->lib_opac },
222         );
223
224         # Test authorised_value = ""
225         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
226             { kohafield => 'items.restricted', authorised_value => '' } );
227         is_deeply(
228             $descriptions,
229             {
230                 lib              => $av_empty_string->lib,
231                 opac_description => $av_empty_string->lib_opac
232             },
233         );
234
235         # Test authorised_value = undef => we do not want to retrieve anything
236         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
237             { kohafield => 'items.restricted', authorised_value => undef } );
238         is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
239
240         # No authorised_value
241         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
242             { kohafield => 'items.restricted', authorised_value => "does not exist" } );
243         is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
244     };
245     subtest 'get_descriptions_by_koha_field' => sub {
246         plan tests => 1;
247         my @descriptions = Koha::AuthorisedValues->get_descriptions_by_koha_field( { kohafield => 'items.restricted' } );
248         is_deeply(
249             \@descriptions,
250             [
251                 {
252                     authorised_value => $av_0->authorised_value,
253                     lib              => $av_0->lib,
254                     opac_description => $av_0->lib_opac
255                 },
256                 {
257                     authorised_value => '',
258                     lib              => $av_empty_string->lib,
259                     opac_description => $av_empty_string->lib_opac
260                 }
261             ],
262         );
263     };
264
265     subtest 'get_descriptions_by_marc_field' => sub {
266         plan tests => 4;
267
268         my $control_descriptions =
269             Koha::AuthorisedValues->get_descriptions_by_marc_field( { frameworkcode => '', tagfield => '003', } );
270         is_deeply(
271             $control_descriptions,
272             {
273                 'lib1' => 'lib1',
274                 'lib2' => 'lib2',
275             },
276         );
277
278         my $control_descriptions_cached =
279             Koha::AuthorisedValues->get_descriptions_by_marc_field( { frameworkcode => '', tagfield => '003', } );
280
281         is(
282             "$control_descriptions", "$control_descriptions_cached",
283             "Same memory address used proves cached control desc data"
284         );
285
286         my $descriptions = Koha::AuthorisedValues->get_descriptions_by_marc_field(
287             { frameworkcode => '', tagfield => '952', tagsubfield => 'c' } );
288         is_deeply(
289             $descriptions,
290             {
291                 'location_1' => 'location_1',
292                 'location_2' => 'location_2',
293                 'location_3' => 'location_3',
294             },
295         );
296
297         my $descriptions_cached = Koha::AuthorisedValues->get_descriptions_by_marc_field(
298             { frameworkcode => '', tagfield => '952', tagsubfield => 'c' } );
299         is( "$descriptions", "$descriptions_cached", "Same memory address used proves cached desc data" );
300     };
301
302     subtest 'authorised_values' => sub {
303
304         plan tests => 2;
305
306         $schema->storage->txn_begin;
307
308         my $authorised_value_category =
309         $builder->build_object(
310             {
311                 class => 'Koha::AuthorisedValueCategories',
312                 value => {
313                     category_name => 'test_avs'
314                 }
315             }
316         );
317
318         is( $authorised_value_category->authorised_values->count, 0, "no authorised values yet" );
319
320         my $av1 = Koha::AuthorisedValue->new(
321             {
322                 category         => 'test_avs',
323                 authorised_value => 'value 1',
324                 lib              => 'display value 1',
325                 lib_opac         => 'opac display value 1',
326                 imageurl         => 'image1.png',
327             }
328         )->store();
329         is( $authorised_value_category->authorised_values->count, 1, "one authorised value" );
330
331         $schema->storage->txn_rollback;
332     };
333 };
334
335 $schema->storage->txn_rollback;