4 use Test
::More tests
=> 40;
6 use C4
::Bookseller
qw( AddBookseller );
8 use Koha
::AdditionalField
;
10 my $dbh = C4
::Context
->dbh;
11 $dbh->{AutoCommit
} = 0;
12 $dbh->{RaiseError
} = 1;
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 $booksellerid = C4
::Bookseller
::AddBookseller
(
114 address1
=> "bookseller's address",
120 my ($biblionumber, $biblioitemnumber) = AddBiblio
(MARC
::Record
->new, '');
122 my $bpid = AddBudgetPeriod
({
123 budget_period_startdate
=> '01-01-2015',
124 budget_period_enddate
=> '01-01-2016',
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
135 my $frequency_id = AddSubscriptionFrequency
({ description
=> "Test frequency 1" });
136 my $pattern_id = AddSubscriptionNumberpattern
({
137 label
=> 'Test numberpattern 1',
138 numberingmethod
=> '{X}'
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
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
161 $subscriptionid1 => "value_for_af1_$subscriptionid1",
162 $subscriptionid2 => "value_for_af1_$subscriptionid2",
164 $af1->{values} = $af1_values;
168 $subscriptionid1 => "old_value_for_af2_$subscriptionid1",
169 $subscriptionid2 => "old_value_for_af2_$subscriptionid2",
171 $af2->{values} = $af2_values;
173 my $new_af2_values = {
174 $subscriptionid1 => "value_for_af2_$subscriptionid1",
175 $subscriptionid2 => "value_for_af2_$subscriptionid2",
177 $af2->{values} = $new_af2_values;
178 $af2->insert_values; # Insert should replace old values
180 my $common_values = {
181 $subscriptionid1 => 'common_value',
182 $subscriptionid2 => 'common_value',
185 $af_common->{values} = $common_values;
186 $af_common->insert_values;
189 $af1 = Koha
::AdditionalField
->new({ id
=> $af1->id })->fetch;
190 $af2 = Koha
::AdditionalField
->new({ id
=> $af2->id })->fetch;
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" );
201 $af1->fetch_all_values;
203 like
( $@
, qr
|^BAD CALL
|, 'fetch_all_values: fail if called with a blessed object' );
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
|,
212 $subscriptionid2 => {
213 $af1_name => qq|value_for_af1_
$subscriptionid2|,
214 $af2_name => qq|value_for_af2_
$subscriptionid2|,
215 'common' => q
|common_value
|,
218 is_deeply
( $fetched_values, $expected_values, "fetch_all_values: values for table subscription" );
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
|,
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" );
230 # get_matching_record_ids
232 $af1->get_matching_record_ids;
234 like
( $@
, qr
|^BAD CALL
|, 'get_matching_record_ids: fail if called with a blessed object' );
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" );
244 value
=> qq|value_for_af1_
$subscriptionid1|
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" );
253 value
=> qq|value_for_af1_
$subscriptionid1|
257 value
=> qq|value_for_af2_
$subscriptionid1|,
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" );
266 value
=> q
|common_value
|,
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" );
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" );
292 $af1 = Koha
::AdditionalField
->new({ id
=> $af1->id })->fetch;
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});
298 is_deeply
( $af1->values, {$subscriptionid2 => qq|value_for_af1_
$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
301 is_deeply
( $af1->values, {}, "fetch_values: no values" );