Bug 10855: Squash several fixes
[koha-ffzg.git] / t / db_dependent / AdditionalField.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use Test::More tests => 37;
5
6 use C4::Context;
7 use Koha::AdditionalField;
8
9 my $dbh = C4::Context->dbh;
10 $dbh->{AutoCommit} = 0;
11 $dbh->{RaiseError} = 1;
12
13 $dbh->do( q|DELETE FROM additional_fields| );
14 $dbh->do( q|DELETE FROM additional_field_values| );
15
16 my $afs = Koha::AdditionalField->all;
17 is( scalar( @$afs ), 0, "all: there is no additional field" );
18
19 my $af1_name = q|af1|;
20 my $af1 = Koha::AdditionalField->new({
21     tablename => 'subscription',
22     name => $af1_name,
23     authorised_values_category => '',
24     marcfield => '',
25     searchable => 1,
26 });
27 is ( $af1->name, $af1_name, "new: name value is kept" );
28
29 $af1->insert;
30 like ( $af1->id, qr|^\d+$|, "new: populate id value" );
31
32 my $af2_name = q|af2|;
33 my $af2_marcfield = q|200$a|;
34 my $af2_searchable = 1;
35 my $af2_tablename = q|subscription|;
36 my $af2_avc = q|LOST|;
37 my $af2 = Koha::AdditionalField->new({
38     tablename => $af2_tablename,
39     name => $af2_name,
40     authorised_value_category => $af2_avc,
41     marcfield => $af2_marcfield,
42     searchable => $af2_searchable,
43 });
44 $af2->insert;
45 my $af2_id = $af2->id;
46 $af2 = Koha::AdditionalField->new({ id => $af2_id })->fetch;
47 is( ref($af2) , q|Koha::AdditionalField|, "fetch: return an object" );
48 is( $af2->id, $af2_id, "fetch: id for af2" );
49 is( $af2->tablename, $af2_tablename, "fetch: tablename for af2" );
50 is( $af2->name, $af2_name, "fetch: name for af2" );
51 is( $af2->authorised_value_category, $af2_avc, "fetch: authorised_value_category for af2" );
52 is( $af2->marcfield, $af2_marcfield, "fetch: marcfield for af2" );
53 is( $af2->searchable, $af2_searchable, "fetch: searchable for af2" );
54
55 my $af3 = Koha::AdditionalField->new({
56     tablename => 'a_table',
57     name => q|af3|,
58     authorised_value_category => '',
59     marcfield => '',
60     searchable => 1,
61 });
62 $af3->insert;
63
64 my $af_common = Koha::AdditionalField->new({
65     tablename => 'subscription',
66     name => q|common|,
67     authorised_value_category => '',
68     marcfield => '',
69     searchable => 1,
70 });
71 $af_common->insert;
72
73 # update
74 $af3->{tablename} = q|another_table|;
75 $af3->{name} = q|af3_mod|;
76 $af3->{authorised_value_category} = q|LOST|;
77 $af3->{marcfield} = q|200$a|;
78 $af3->{searchable} = 0;
79 my $updated = $af3->update;
80 $af3 = Koha::AdditionalField->new({ id => $af3->id })->fetch;
81 is( $updated, 1, "update: return number of affected rows" );
82 is( $af3->tablename, q|a_table|, "update: tablename is *not* updated, there is no sense to copy a field to another table" );
83 is( $af3->name, q|af3_mod|, "update: name" );
84 is( $af3->authorised_value_category, q|LOST|, "update: authorised_value_category" );
85 is( $af3->marcfield, q|200$a|, "update: marcfield" );
86 is( $af3->searchable, q|0|, "update: searchable" );
87
88 # fetch all
89 $afs = Koha::AdditionalField->all;
90 is( scalar( @$afs ), 4, "all: got 4 additional fields" );
91 $afs = Koha::AdditionalField->all({tablename => 'subscription'});
92 is( scalar( @$afs ), 3, "all: got 3 additional fields for the subscription table" );
93 $afs = Koha::AdditionalField->all({searchable => 1});
94 is( scalar( @$afs ), 3, "all: got 3 searchable additional fields" );
95 $af3->delete;
96 $afs = Koha::AdditionalField->all;
97 is( scalar( @$afs ), 3, "all: got 3 additional fields after deleting one" );
98
99
100 # Testing additional field values
101
102 ## Creating 2 subscriptions
103 use C4::Acquisition;
104 use C4::Biblio;
105 use C4::Budgets;
106 use C4::Serials;
107 use C4::Serials::Frequency;
108 use C4::Serials::Numberpattern;
109
110 my $booksellerid = C4::Bookseller::AddBookseller(
111     {
112         name => "my vendor",
113         address1 => "bookseller's address",
114         phone => "0123456",
115         active => 1
116     }
117 );
118
119 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
120 my $budgetid;
121 my $bpid = AddBudgetPeriod({
122     budget_period_startdate => '01-01-2015',
123     budget_period_enddate   => '01-01-2016',
124     budget_description      => "budget desc"
125 });
126
127 my $budget_id = AddBudget({
128     budget_code        => "ABCD",
129     budget_amount      => "123.132",
130     budget_name        => "Périodiques",
131     budget_notes       => "This is a note",
132     budget_description => "Serials",
133     budget_active      => 1,
134     budget_period_id   => $bpid
135 });
136
137 my $frequency_id = AddSubscriptionFrequency({ description => "Test frequency 1" });
138 my $pattern_id = AddSubscriptionNumberpattern({
139     label => 'Test numberpattern 1',
140     numberingmethod => '{X}'
141 });
142
143 my $subscriptionid1 = NewSubscription(
144     undef,      "",     undef, undef, $budget_id, $biblionumber,
145     '2013-01-01', $frequency_id, undef, undef,  undef,
146     undef,      undef,  undef, undef, undef, undef,
147     1,          "notes",undef, '2013-01-01', undef, $pattern_id,
148     undef,       undef,  0,    "intnotes",  0,
149     undef, undef, 0,          undef,         '2013-01-01', 0
150 );
151
152 my $subscriptionid2 = NewSubscription(
153     undef,      "",     undef, undef, $budget_id, $biblionumber,
154     '2013-01-01', $frequency_id, undef, undef,  undef,
155     undef,      undef,  undef, undef, undef, undef,
156     1,          "notes",undef, '2013-01-01', undef, $pattern_id,
157     undef,       undef,  0,    "intnotes",  0,
158     undef, undef, 0,          undef,         '2013-01-01', 0
159 );
160
161 # insert
162 my $af1_values = {
163     $subscriptionid1 => "value_for_af1_$subscriptionid1",
164     $subscriptionid2 => "value_for_af1_$subscriptionid2",
165 };
166 $af1->{values} = $af1_values;
167 $af1->insert_values;
168
169 my $af2_values = {
170     $subscriptionid1 => "old_value_for_af2_$subscriptionid1",
171     $subscriptionid2 => "old_value_for_af2_$subscriptionid2",
172 };
173 $af2->{values} = $af2_values;
174 $af2->insert_values;
175 my $new_af2_values = {
176     $subscriptionid1 => "value_for_af2_$subscriptionid1",
177     $subscriptionid2 => "value_for_af2_$subscriptionid2",
178 };
179 $af2->{values} = $new_af2_values;
180 $af2->insert_values; # Insert should replace old values
181
182 my $common_values = {
183     $subscriptionid1 => 'common_value',
184     $subscriptionid2 => 'common_value',
185 };
186
187 $af_common->{values} = $common_values;
188 $af_common->insert_values;
189
190 # fetch_values
191 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
192 $af2 = Koha::AdditionalField->new({ id => $af2->id })->fetch;
193
194 $af1->fetch_values;
195 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_$subscriptionid2| }, "fetch_values: without argument, returns 2 records" );
196 $af1->fetch_values({ record_id => $subscriptionid1 });
197 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|}, "fetch_values: values for af1 and subscription1" );
198 $af2->fetch_values({ record_id => $subscriptionid2 });
199 is_deeply ( $af2->values, {$subscriptionid2 => qq|value_for_af2_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
200
201 # fetch_all_values
202 eval{
203     $af1->fetch_all_values;
204 };
205 like ( $@, qr|^BAD CALL|, 'fetch_all_values: fail if called with a blessed object' );
206
207 my $fetched_values = Koha::AdditionalField->fetch_all_values({ tablename => 'subscription' });
208 my $expected_values = {
209     $subscriptionid1 => {
210         $af1_name => qq|value_for_af1_$subscriptionid1|,
211         $af2_name => qq|value_for_af2_$subscriptionid1|,
212         'common' => q|common_value|,
213     },
214     $subscriptionid2 => {
215         $af1_name => qq|value_for_af1_$subscriptionid2|,
216         $af2_name => qq|value_for_af2_$subscriptionid2|,
217         'common' => q|common_value|,
218     }
219 };
220 is_deeply ( $fetched_values, $expected_values, "fetch_all_values: values for table subscription" );
221
222 my $expected_values_1 = {
223     $subscriptionid1 => {
224         $af1_name => qq|value_for_af1_$subscriptionid1|,
225         $af2_name => qq|value_for_af2_$subscriptionid1|,
226         common => q|common_value|,
227     }
228 };
229 my $fetched_values_1 = Koha::AdditionalField->fetch_all_values({ tablename => 'subscription', record_id => $subscriptionid1 });
230 is_deeply ( $fetched_values_1, $expected_values_1, "fetch_all_values: values for subscription1" );
231
232 # get_matching_record_ids
233 eval{
234     $af1->get_matching_record_ids;
235 };
236 like ( $@, qr|^BAD CALL|, 'get_matching_record_ids: fail if called with a blessed object' );
237
238 my $matching_record_ids = Koha::AdditionalField->get_matching_record_ids;
239 is_deeply ( $matching_record_ids, [], "get_matching_record_ids: return [] if no argument given" );
240 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription' });
241 is_deeply ( $matching_record_ids, [], "get_matching_record_ids: return [] if no field given" );
242
243 my $fields = [
244     {
245         name => $af1_name,
246         value => qq|value_for_af1_$subscriptionid1|
247     }
248 ];
249 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
250 is_deeply ( $matching_record_ids, [ $subscriptionid1 ], "get_matching_record_ids: field $af1_name: value_for_af1_$subscriptionid1 matches subscription1" );
251
252 $fields = [
253     {
254         name => $af1_name,
255         value => qq|value_for_af1_$subscriptionid1|
256     },
257     {
258         name => $af2_name,
259         value => qq|value_for_af2_$subscriptionid1|,
260     }
261 ];
262 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
263 is_deeply ( $matching_record_ids, [ $subscriptionid1 ], "get_matching_record_ids: fields $af1_name:value_for_af1_$subscriptionid1 and $af2_name:value_for_af2_$subscriptionid1 match subscription1" );
264
265 $fields = [
266     {
267         name => 'common',
268         value => q|common_value|,
269     }
270 ];
271 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
272 my $exists = grep /$subscriptionid1/, @$matching_record_ids;
273 is ( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription1" );
274 $exists = grep /$subscriptionid2/, @$matching_record_ids;
275 is ( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription2 too" );
276 $exists = grep /not_existent_id/, @$matching_record_ids;
277 is ( $exists, 0, "get_matching_record_ids: field common: common_value does not inexistent id" );
278
279 $fields = [
280     {
281         name => 'common',
282         value => q|common|,
283     }
284 ];
285 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields, exact_match => 0 });
286 $exists = grep /$subscriptionid1/, @$matching_record_ids;
287 is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription1" );
288 $exists = grep /$subscriptionid2/, @$matching_record_ids;
289 is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription2 too" );
290 $exists = grep /not_existent_id/, @$matching_record_ids;
291 is ( $exists, 0, "get_matching_record_ids: field common: common% does not inexistent id" );
292
293
294 $dbh->rollback;