Bug 15769: Remove outdated C4::Koha::slashifyDate subroutine
[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::Bookseller qw( AddBookseller );
7 use C4::Context;
8 use Koha::AdditionalField;
9
10 my $dbh = C4::Context->dbh;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
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 $booksellerid = C4::Bookseller::AddBookseller(
112     {
113         name => "my vendor",
114         address1 => "bookseller's address",
115         phone => "0123456",
116         active => 1
117     }
118 );
119
120 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
121 my $budgetid;
122 my $bpid = AddBudgetPeriod({
123     budget_period_startdate => '01-01-2015',
124     budget_period_enddate   => '01-01-2016',
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_period_id   => $bpid
133 });
134
135 my $frequency_id = AddSubscriptionFrequency({ description => "Test frequency 1" });
136 my $pattern_id = AddSubscriptionNumberpattern({
137     label => 'Test numberpattern 1',
138     numberingmethod => '{X}'
139 });
140
141 my $subscriptionid1 = NewSubscription(
142     undef,      "",     undef, undef, $budget_id, $biblionumber,
143     '2013-01-01', $frequency_id, undef, undef,  undef,
144     undef,      undef,  undef, undef, undef, undef,
145     1,          "notes",undef, '2013-01-01', undef, $pattern_id,
146     undef,       undef,  0,    "intnotes",  0,
147     undef, undef, 0,          undef,         '2013-01-01', 0
148 );
149
150 my $subscriptionid2 = NewSubscription(
151     undef,      "",     undef, undef, $budget_id, $biblionumber,
152     '2013-01-01', $frequency_id, undef, undef,  undef,
153     undef,      undef,  undef, undef, undef, undef,
154     1,          "notes",undef, '2013-01-01', undef, $pattern_id,
155     undef,       undef,  0,    "intnotes",  0,
156     undef, undef, 0,          undef,         '2013-01-01', 0
157 );
158
159 # insert
160 my $af1_values = {
161     $subscriptionid1 => "value_for_af1_$subscriptionid1",
162     $subscriptionid2 => "value_for_af1_$subscriptionid2",
163 };
164 $af1->{values} = $af1_values;
165 $af1->insert_values;
166
167 my $af2_values = {
168     $subscriptionid1 => "old_value_for_af2_$subscriptionid1",
169     $subscriptionid2 => "old_value_for_af2_$subscriptionid2",
170 };
171 $af2->{values} = $af2_values;
172 $af2->insert_values;
173 my $new_af2_values = {
174     $subscriptionid1 => "value_for_af2_$subscriptionid1",
175     $subscriptionid2 => "value_for_af2_$subscriptionid2",
176 };
177 $af2->{values} = $new_af2_values;
178 $af2->insert_values; # Insert should replace old values
179
180 my $common_values = {
181     $subscriptionid1 => 'common_value',
182     $subscriptionid2 => 'common_value',
183 };
184
185 $af_common->{values} = $common_values;
186 $af_common->insert_values;
187
188 # fetch_values
189 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
190 $af2 = Koha::AdditionalField->new({ id => $af2->id })->fetch;
191
192 $af1->fetch_values;
193 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_$subscriptionid2| }, "fetch_values: without argument, returns 2 records" );
194 $af1->fetch_values({ record_id => $subscriptionid1 });
195 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|}, "fetch_values: values for af1 and subscription1" );
196 $af2->fetch_values({ record_id => $subscriptionid2 });
197 is_deeply ( $af2->values, {$subscriptionid2 => qq|value_for_af2_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
198
199 # fetch_all_values
200 eval{
201     $af1->fetch_all_values;
202 };
203 like ( $@, qr|^BAD CALL|, 'fetch_all_values: fail if called with a blessed object' );
204
205 my $fetched_values = Koha::AdditionalField->fetch_all_values({ tablename => 'subscription' });
206 my $expected_values = {
207     $subscriptionid1 => {
208         $af1_name => qq|value_for_af1_$subscriptionid1|,
209         $af2_name => qq|value_for_af2_$subscriptionid1|,
210         'common' => q|common_value|,
211     },
212     $subscriptionid2 => {
213         $af1_name => qq|value_for_af1_$subscriptionid2|,
214         $af2_name => qq|value_for_af2_$subscriptionid2|,
215         'common' => q|common_value|,
216     }
217 };
218 is_deeply ( $fetched_values, $expected_values, "fetch_all_values: values for table subscription" );
219
220 my $expected_values_1 = {
221     $subscriptionid1 => {
222         $af1_name => qq|value_for_af1_$subscriptionid1|,
223         $af2_name => qq|value_for_af2_$subscriptionid1|,
224         common => q|common_value|,
225     }
226 };
227 my $fetched_values_1 = Koha::AdditionalField->fetch_all_values({ tablename => 'subscription', record_id => $subscriptionid1 });
228 is_deeply ( $fetched_values_1, $expected_values_1, "fetch_all_values: values for subscription1" );
229
230 # get_matching_record_ids
231 eval{
232     $af1->get_matching_record_ids;
233 };
234 like ( $@, qr|^BAD CALL|, 'get_matching_record_ids: fail if called with a blessed object' );
235
236 my $matching_record_ids = Koha::AdditionalField->get_matching_record_ids;
237 is_deeply ( $matching_record_ids, [], "get_matching_record_ids: return [] if no argument given" );
238 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription' });
239 is_deeply ( $matching_record_ids, [], "get_matching_record_ids: return [] if no field given" );
240
241 my $fields = [
242     {
243         name => $af1_name,
244         value => qq|value_for_af1_$subscriptionid1|
245     }
246 ];
247 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
248 is_deeply ( $matching_record_ids, [ $subscriptionid1 ], "get_matching_record_ids: field $af1_name: value_for_af1_$subscriptionid1 matches subscription1" );
249
250 $fields = [
251     {
252         name => $af1_name,
253         value => qq|value_for_af1_$subscriptionid1|
254     },
255     {
256         name => $af2_name,
257         value => qq|value_for_af2_$subscriptionid1|,
258     }
259 ];
260 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
261 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" );
262
263 $fields = [
264     {
265         name => 'common',
266         value => q|common_value|,
267     }
268 ];
269 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
270 my $exists = grep /$subscriptionid1/, @$matching_record_ids;
271 is ( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription1" );
272 $exists = grep /$subscriptionid2/, @$matching_record_ids;
273 is ( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription2 too" );
274 $exists = grep /not_existent_id/, @$matching_record_ids;
275 is ( $exists, 0, "get_matching_record_ids: field common: common_value does not inexistent id" );
276
277 $fields = [
278     {
279         name => 'common',
280         value => q|common|,
281     }
282 ];
283 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields, exact_match => 0 });
284 $exists = grep /$subscriptionid1/, @$matching_record_ids;
285 is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription1" );
286 $exists = grep /$subscriptionid2/, @$matching_record_ids;
287 is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription2 too" );
288 $exists = grep /not_existent_id/, @$matching_record_ids;
289 is ( $exists, 0, "get_matching_record_ids: field common: common% does not inexistent id" );
290
291 # delete_values
292 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
293
294 $af1->fetch_values;
295 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_$subscriptionid2| }, "fetch_values: without argument, returns 2 records" );
296 $af1->delete_values({record_id => $subscriptionid1});
297 $af1->fetch_values;
298 is_deeply ( $af1->values, {$subscriptionid2 => qq|value_for_af1_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
299 $af1->delete_values;
300 $af1->fetch_values;
301 is_deeply ( $af1->values, {}, "fetch_values: no values" );
302
303
304 $dbh->rollback;