Bug 18292: Remove return 1 statements in tests
[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 t::lib::TestBuilder;
7
8 use Koha::Database;
9 use C4::Context;
10 use Koha::AuthorisedValue;
11 use Koha::AuthorisedValues;
12 use Koha::AuthorisedValueCategories;
13 use Koha::MarcSubfieldStructures;
14
15 my $schema  = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $builder = t::lib::TestBuilder->new;
18
19 Koha::AuthorisedValues->delete;
20 Koha::AuthorisedValueCategories->delete;
21
22 # insert
23 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->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 is( $av3->opac_description, 'opac display value 3', 'Got correction opac description if lib_opac is set' );
88 $av3->lib_opac('');
89 is( $av3->opac_description, 'display value 3', 'Got correction opac description if lib_opac is *not* set' );
90
91 my @authorised_values =
92   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
93 is( @authorised_values, 3, "Get correct number of values" );
94
95 my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
96 my $branchcode2 = $builder->build({ source => 'Branch' })->{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 => 'c', frameworkcode => 'ACQ' } );
128     $mss->delete if $mss;
129     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } );
130     $mss->delete if $mss;
131     $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => '5', frameworkcode => '' } );
132     $mss->delete if $mss;
133     Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store;
134     Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store;
135     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
136     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store;
137     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store;
138     Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => '5', frameworkcode => '', authorised_value => 'restricted_for_testing', kohafield => 'items.restricted' } )->store;
139     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store;
140     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store;
141     Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store;
142     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store;
143     Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store;
144     subtest 'search_by_marc_field' => sub {
145         plan tests => 4;
146         my $avs;
147         $avs = Koha::AuthorisedValues->search_by_marc_field();
148         is ( $avs, undef );
149         $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' });
150         is ( $avs, undef );
151         $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'});
152         is( $avs->count, 3, 'default fk');
153         is( $avs->next->authorised_value, 'location_1', );
154     };
155     subtest 'search_by_koha_field' => sub {
156         plan tests => 3;
157         my $avs;
158         $avs = Koha::AuthorisedValues->search_by_koha_field();
159         is ( $avs, undef );
160         $avs = Koha::AuthorisedValues->search_by_koha_field( { kohafield => 'items.location', tagfield => 952, tagsubfield => 'c' } );
161         is( $avs->count,                  3, );
162         is( $avs->next->authorised_value, 'location_1', );
163
164     };
165     subtest 'find_by_koha_field' => sub {
166         plan tests => 3;
167         # Test authorised_value = 0
168         my $av;
169         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => 0 } );
170         is( $av->lib, $av_0->lib, );
171         # Test authorised_value = ""
172         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => '' } );
173         is( $av->lib, $av_empty_string->lib, );
174         # Test authorised_value = undef => we do not want to retrieve anything
175         $av = Koha::AuthorisedValues->find_by_koha_field( { kohafield => 'items.restricted', authorised_value => undef } );
176         is( $av, undef, );
177     };
178     subtest 'get_description_by_koha_field' => sub {
179         plan tests => 4;
180         my $descriptions;
181
182         # Test authorised_value = 0
183         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
184             { kohafield => 'items.restricted', authorised_value => 0 } );
185         is_deeply( $descriptions,
186             { lib => $av_0->lib, opac_description => $av_0->lib_opac },
187         );
188
189         # Test authorised_value = ""
190         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
191             { kohafield => 'items.restricted', authorised_value => '' } );
192         is_deeply(
193             $descriptions,
194             {
195                 lib              => $av_empty_string->lib,
196                 opac_description => $av_empty_string->lib_opac
197             },
198         );
199
200         # Test authorised_value = undef => we do not want to retrieve anything
201         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
202             { kohafield => 'items.restricted', authorised_value => undef } );
203         is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
204
205         # No authorised_value
206         $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
207             { kohafield => 'items.restricted', authorised_value => "does not exist" } );
208         is_deeply( $descriptions, {}, ) ;    # This could be arguable, we could return undef instead
209     };
210     subtest 'get_descriptions_by_koha_field' => sub {
211         plan tests => 1;
212         my @descriptions = Koha::AuthorisedValues->get_descriptions_by_koha_field( { kohafield => 'items.restricted' } );
213         is_deeply(
214             \@descriptions,
215             [
216                 {
217                     authorised_value => '',
218                     lib              => $av_empty_string->lib,
219                     opac_description => $av_empty_string->lib_opac
220                 },
221                 {
222                     authorised_value => $av_0->authorised_value,
223                     lib              => $av_0->lib,
224                     opac_description => $av_0->lib_opac
225                 }
226             ],
227         );
228     };
229 };
230
231 $schema->storage->txn_rollback;