4 use Test::More tests => 40;
7 use Koha::AdditionalField;
10 my $schema = Koha::Database->new->schema;
11 $schema->storage->txn_begin;
12 my $dbh = C4::Context->dbh;
14 $dbh->do( q|DELETE FROM additional_fields| );
15 $dbh->do( q|DELETE FROM additional_field_values| );
17 my $afs = Koha::AdditionalField->all;
18 is( scalar( @$afs ), 0, "all: there is no additional field" );
20 my $af1_name = q|af1|;
21 my $af1 = Koha::AdditionalField->new({
22 tablename => 'subscription',
24 authorised_values_category => '',
28 is ( $af1->name, $af1_name, "new: name value is kept" );
31 like ( $af1->id, qr|^\d+$|, "new: populate id value" );
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,
41 authorised_value_category => $af2_avc,
42 marcfield => $af2_marcfield,
43 searchable => $af2_searchable,
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" );
56 my $af3 = Koha::AdditionalField->new({
57 tablename => 'a_table',
59 authorised_value_category => '',
65 my $af_common = Koha::AdditionalField->new({
66 tablename => 'subscription',
68 authorised_value_category => '',
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" );
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" );
97 $afs = Koha::AdditionalField->all;
98 is( scalar( @$afs ), 3, "all: got 3 additional fields after deleting one" );
101 # Testing additional field values
103 ## Creating 2 subscriptions
108 use C4::Serials::Frequency;
109 use C4::Serials::Numberpattern;
111 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
113 my $bpid = AddBudgetPeriod({
114 budget_period_startdate => '2015-01-01',
115 budget_period_enddate => '2016-01-01',
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
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}'
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
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
153 $subscriptionid1 => "value_for_af1_$subscriptionid1",
154 $subscriptionid2 => "value_for_af1_$subscriptionid2",
156 $af1->{values} = $af1_values;
160 $subscriptionid1 => "old_value_for_af2_$subscriptionid1",
161 $subscriptionid2 => "old_value_for_af2_$subscriptionid2",
163 $af2->{values} = $af2_values;
165 my $new_af2_values = {
166 $subscriptionid1 => "value_for_af2_$subscriptionid1",
167 $subscriptionid2 => "value_for_af2_$subscriptionid2",
169 $af2->{values} = $new_af2_values;
170 $af2->insert_values; # Insert should replace old values
172 my $common_values = {
173 $subscriptionid1 => 'common_value',
174 $subscriptionid2 => 'common_value',
177 $af_common->{values} = $common_values;
178 $af_common->insert_values;
181 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
182 $af2 = Koha::AdditionalField->new({ id => $af2->id })->fetch;
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" );
193 $af1->fetch_all_values;
195 like ( $@, qr|^BAD CALL|, 'fetch_all_values: fail if called with a blessed object' );
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|,
204 $subscriptionid2 => {
205 $af1_name => qq|value_for_af1_$subscriptionid2|,
206 $af2_name => qq|value_for_af2_$subscriptionid2|,
207 'common' => q|common_value|,
210 is_deeply ( $fetched_values, $expected_values, "fetch_all_values: values for table subscription" );
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|,
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" );
222 # get_matching_record_ids
224 $af1->get_matching_record_ids;
226 like ( $@, qr|^BAD CALL|, 'get_matching_record_ids: fail if called with a blessed object' );
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" );
236 value => qq|value_for_af1_$subscriptionid1|
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" );
245 value => qq|value_for_af1_$subscriptionid1|
249 value => qq|value_for_af2_$subscriptionid1|,
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" );
258 value => q|common_value|,
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" );
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" );
284 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
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});
290 is_deeply ( $af1->values, {$subscriptionid2 => qq|value_for_af1_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
293 is_deeply ( $af1->values, {}, "fetch_values: no values" );