Bug 17355: Override delete methods
[koha.git] / t / db_dependent / AuthorisedValues.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 17;
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 Koha::AuthorisedValues->delete;
21 Koha::AuthorisedValueCategories->delete;
22
23 # insert
24 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing', is_system => 1 })->store;
25 Koha::AuthorisedValueCategory->new({ category_name => 'aaav_for_testing' })->store;
26 Koha::AuthorisedValueCategory->new({ category_name => 'restricted_for_testing' })->store;
27 my $av1 = Koha::AuthorisedValue->new(
28     {
29         category         => 'av_for_testing',
30         authorised_value => 'value 1',
31         lib              => 'display value 1',
32         lib_opac         => 'opac display value 1',
33         imageurl         => 'image1.png',
34     }
35 )->store();
36
37 my $av2 = Koha::AuthorisedValue->new(
38     {
39         category         => 'av_for_testing',
40         authorised_value => 'value 2',
41         lib              => 'display value 2',
42         lib_opac         => 'opac display value 2',
43         imageurl         => 'image2.png',
44     }
45 )->store();
46
47 my $av3 = Koha::AuthorisedValue->new(
48     {
49         category         => 'av_for_testing',
50         authorised_value => 'value 3',
51         lib              => 'display value 3',
52         lib_opac         => 'opac display value 3',
53         imageurl         => 'image2.png',
54     }
55 )->store();
56
57 my $av4 = Koha::AuthorisedValue->new(
58     {
59         category         => 'aaav_for_testing',
60         authorised_value => 'value 4',
61         lib              => 'display value 4',
62         lib_opac         => 'opac display value 4',
63         imageurl         => 'image4.png',
64     }
65 )->store();
66 my $av_empty_string = Koha::AuthorisedValue->new(
67     {
68         category         => 'restricted_for_testing',
69         authorised_value => undef, # Should have been defaulted to ""
70         lib              => 'display value undef',
71         lib_opac         => 'opac display value undef',
72     }
73 )->store();
74 my $av_0 = Koha::AuthorisedValue->new(
75     {
76         category         => 'restricted_for_testing',
77         authorised_value => 0,
78         lib              => 'display value 0',
79         lib_opac         => 'opac display value 0',
80     }
81 )->store();
82
83 ok( $av1->id(), 'AV 1 is inserted' );
84 ok( $av2->id(), 'AV 2 is inserted' );
85 ok( $av3->id(), 'AV 3 is inserted' );
86 ok( $av4->id(), 'AV 4 is inserted' );
87
88 { # delete is_system AV categories
89     try {
90         Koha::AuthorisedValueCategories->find('av_for_testing')->delete
91     }
92     catch {
93         ok(
94             $_->isa('Koha::Exceptions::CannotDeleteDefault'),
95             'A system AV category cannot be deleted'
96         );
97     };
98
99     try {
100         Koha::AuthorisedValueCategories->search->delete
101     }
102     catch {
103         ok(
104             $_->isa('Koha::Exceptions::CannotDeleteDefault'),
105             'system AV categories cannot be deleted'
106         );
107     };
108 }
109
110 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
111 $av3->lib_opac('');
112 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
113
114 my @authorised_values =
115   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
116 is( @authorised_values, 3, "Get correct number of values" );
117
118 my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
119 my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
120
121 $av1->add_library_limit( $branchcode1 );
122
123 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } );
124 is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" );
125
126 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
127 is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" );
128
129 $av1->del_library_limit( $branchcode1 );
130 @authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } );
131 is( @authorised_values, 3, "Branch limitation deleted successfully" );
132
133 $av1->add_library_limit( $branchcode1 );
134 $av1->library_limits( [ $branchcode1, $branchcode2 ] );
135
136 my $limits = $av1->library_limits->as_list;
137 is( @$limits, 2, 'library_limits functions correctly both as setter and getter' );
138
139 my @categories = Koha::AuthorisedValues->new->categories;
140 is( @categories, 3, 'There should have 2 categories inserted' );
141 is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' );
142 is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' );
143
144 subtest 'search_by_*_field + find_by_koha_field + get_description' => sub {
145     plan tests => 5;
146     my $loc_cat = Koha::AuthorisedValueCategories->find('LOC');
147     $loc_cat->delete if $loc_cat;
148     my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } );
149     $mss->delete if $mss;
150     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ' } );
151     $mss->delete if $mss;
152     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
153     $mss->delete if $mss;
154     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => '5', frameworkcode => '' } );
155     $mss->delete if $mss;
156     Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store;
157     Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
158     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
159     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
160     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
161     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => '5', frameworkcode => '', authorised_value => 'restricted_for_testing', kohafield => 'items.restricted' } )->store;
162     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store;
163     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store;
164     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store;
165     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
166     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
167     subtest 'search_by_marc_field' => sub {
168         plan tests => 4;
169         my $avs;
170         $avs = Koha::AuthorisedValues->search_by_marc_field();
171         is ( $avs, undef );
172         $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
173         is ( $avs, undef );
174         $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'});
175         is( $avs->count, 3, 'default fk');
176         is( $avs->next->authorised_value, 'location_1', );
177     };
178     subtest 'search_by_koha_field' => sub {
179         plan tests => 3;
180         my $avs;
181         $avs = Koha::AuthorisedValues->search_by_koha_field();
182         is ( $avs, undef );
183         $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location', tagfield => 952, tagsubfield => 'c' } );
184         is( $avs->count,                  3, );
185         is( $avs->next->authorised_value, 'location_1', );
186
187     };
188     subtest 'find_by_koha_field' => sub {
189         plan tests => 3;
190         # Test authorised_value = 0
191         my $av;
192         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } );
193         is( $av->lib, $av_0->lib, );
194         # Test authorised_value = ""
195         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } );
196         is( $av->lib, $av_empty_string->lib, );
197         # Test authorised_value = undef => we do not want to retrieve anything
198         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
199         is( $av, undef, );
200     };
201     subtest 'get_description_by_koha_field' => sub {
202         plan tests => 4;
203         my $descriptions;
204
205         # Test authorised_value = 0
206         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
207             { kohafield => 'items.restricted', authorised_value => 0 } );
208         is_deeply( $descriptions,
209             { lib => $av_0->lib, opac_description => $av_0->lib_opac },
210         );
211
212         # Test authorised_value = ""
213         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
214             { kohafield => 'items.restricted', authorised_value => '' } );
215         is_deeply(
216             $descriptions,
217             {
218                 lib              => $av_empty_string->lib,
219                 opac_description => $av_empty_string->lib_opac
220             },
221         );
222
223         # Test authorised_value = undef => we do not want to retrieve anything
224         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
225             { kohafield => 'items.restricted', authorised_value => undef } );
226         is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
227
228         # No authorised_value
229         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
230             { kohafield => 'items.restricted', authorised_value => "does not exist" } );
231         is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
232     };
233     subtest 'get_descriptions_by_koha_field' => sub {
234         plan tests => 1;
235         my @descriptions = Koha::AuthorisedValues->get_descriptions_by_koha_field( { kohafield => 'items.restricted' } );
236         is_deeply(
237             \@descriptions,
238             [
239                 {
240                     authorised_value => '',
241                     lib              => $av_empty_string->lib,
242                     opac_description => $av_empty_string->lib_opac
243                 },
244                 {
245                     authorised_value => $av_0->authorised_value,
246                     lib              => $av_0->lib,
247                     opac_description => $av_0->lib_opac
248                 }
249             ],
250         );
251     };
252 };
253
254 $schema->storage->txn_rollback;