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