4 use Test
::More tests
=> 34;
7 use Koha
::AdditionalField
;
9 my $dbh = C4
::Context
->dbh;
10 $dbh->{AutoCommit
} = 0;
11 $dbh->{RaiseError
} = 1;
13 $dbh->do( q
|DELETE FROM additional_fields
| );
14 $dbh->do( q
|DELETE FROM additional_field_values
| );
16 my $afs = Koha
::AdditionalField
->all;
17 is
( scalar( @
$afs ), 0, "all: there is no additional field" );
19 my $af1_name = q
|af1
|;
20 my $af1 = Koha
::AdditionalField
->new({
21 tablename
=> 'subscription',
23 authorised_values_category
=> '',
27 is
( $af1->name, $af1_name, "new: name value is kept" );
30 like
( $af1->id, qr
|^\d
+$|, "new: populate id value" );
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,
40 authorised_value_category
=> $af2_avc,
41 marcfield
=> $af2_marcfield,
42 searchable
=> $af2_searchable,
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" );
55 my $af3 = Koha
::AdditionalField
->new({
56 tablename
=> 'a_table',
58 authorised_value_category
=> '',
64 my $af_common = Koha
::AdditionalField
->new({
65 tablename
=> 'subscription',
67 authorised_value_category
=> '',
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" );
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" );
96 $afs = Koha
::AdditionalField
->all;
97 is
( scalar( @
$afs ), 3, "all: got 3 additional fields after deleting one" );
100 # Testing additional field values
102 ## Creating 2 subscriptions
108 my $booksellerid = C4
::Bookseller
::AddBookseller
(
111 address1
=> "bookseller's address",
117 my ($biblionumber, $biblioitemnumber) = AddBiblio
(MARC
::Record
->new, '');
119 my $bpid = AddBudgetPeriod
({
120 budget_period_startdate
=> '01-01-2015',
121 budget_period_enddate
=> '12-31-2015',
122 budget_description
=> "budget desc"
125 my $budget_id = AddBudget
({
126 budget_code
=> "ABCD",
127 budget_amount
=> "123.132",
128 budget_name
=> "Périodiques",
129 budget_notes
=> "This is a note",
130 budget_description
=> "Serials",
132 budget_period_id
=> $bpid
135 my $subscriptionid1 = NewSubscription
(
136 undef, "", undef, undef, $budget_id, $biblionumber, '01-01-2013',undef,
137 undef, undef, undef, undef, undef, undef, undef, undef,
138 undef, undef, undef, undef, undef, undef, undef, undef,
139 undef, undef, undef, undef, undef, undef, undef, 1,
140 "notes", undef, undef, undef, undef, undef, undef, 0,
141 "intnotes", 0, undef, undef, 0, undef, '31-12-2013',
144 my $subscriptionid2 = NewSubscription
(
145 undef, "", undef, undef, $budget_id, $biblionumber, '01-01-2013',undef,
146 undef, undef, undef, undef, undef, undef, undef, undef,
147 undef, undef, undef, undef, undef, undef, undef, undef,
148 undef, undef, undef, undef, undef, undef, undef, 1,
149 "notes", undef, undef, undef, undef, undef, undef, 0,
150 "intnotes", 0, undef, undef, 0, undef, '31-12-2013',
155 $subscriptionid1 => "value_for_af1_$subscriptionid1",
156 $subscriptionid2 => "value_for_af1_$subscriptionid2",
158 $af1->{values} = $af1_values;
162 $subscriptionid1 => "old_value_for_af2_$subscriptionid1",
163 $subscriptionid2 => "old_value_for_af2_$subscriptionid2",
165 $af2->{values} = $af2_values;
167 my $new_af2_values = {
168 $subscriptionid1 => "value_for_af2_$subscriptionid1",
169 $subscriptionid2 => "value_for_af2_$subscriptionid2",
171 $af2->{values} = $new_af2_values;
172 $af2->insert_values; # Insert should replace old values
174 my $common_values = {
175 $subscriptionid1 => 'common_value',
176 $subscriptionid2 => 'common_value',
179 $af_common->{values} = $common_values;
180 $af_common->insert_values;
183 $af1 = Koha
::AdditionalField
->new({ id
=> $af1->id })->fetch;
184 $af2 = Koha
::AdditionalField
->new({ id
=> $af2->id })->fetch;
187 is_deeply
( $af1->values, {$subscriptionid1 => qq|value_for_af1_
$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_
$subscriptionid2| }, "fetch_values: without argument, returns 2 records" );
188 $af1->fetch_values({ record_id
=> $subscriptionid1 });
189 is_deeply
( $af1->values, {$subscriptionid1 => qq|value_for_af1_
$subscriptionid1|}, "fetch_values: values for af1 and subscription1" );
190 $af2->fetch_values({ record_id
=> $subscriptionid2 });
191 is_deeply
( $af2->values, {$subscriptionid2 => qq|value_for_af2_
$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
195 $af1->fetch_all_values;
197 like
( $@
, qr
|^BAD CALL
|, 'fetch_all_values: fail if called with a blessed object' );
199 my $fetched_values = Koha
::AdditionalField
->fetch_all_values({ tablename
=> 'subscription' });
200 my $expected_values = {
201 $subscriptionid1 => {
202 $af1_name => qq|value_for_af1_
$subscriptionid1|,
203 $af2_name => qq|value_for_af2_
$subscriptionid1|,
204 'common' => q
|common_value
|,
206 $subscriptionid2 => {
207 $af1_name => qq|value_for_af1_
$subscriptionid2|,
208 $af2_name => qq|value_for_af2_
$subscriptionid2|,
209 'common' => q
|common_value
|,
212 is_deeply
( $fetched_values, $expected_values, "fetch_all_values: values for table subscription" );
214 my $expected_values_1 = {
215 $subscriptionid1 => {
216 $af1_name => qq|value_for_af1_
$subscriptionid1|,
217 $af2_name => qq|value_for_af2_
$subscriptionid1|,
218 common
=> q
|common_value
|,
221 my $fetched_values_1 = Koha
::AdditionalField
->fetch_all_values({ tablename
=> 'subscription', record_id
=> $subscriptionid1 });
222 is_deeply
( $fetched_values_1, $expected_values_1, "fetch_all_values: values for subscription1" );
224 # get_matching_record_ids
226 $af1->get_matching_record_ids;
228 like
( $@
, qr
|^BAD CALL
|, 'get_matching_record_ids: fail if called with a blessed object' );
230 my $matching_record_ids = Koha
::AdditionalField
->get_matching_record_ids;
231 is_deeply
( $matching_record_ids, [], "get_matching_record_ids: return [] if no argument given" );
232 $matching_record_ids = Koha
::AdditionalField
->get_matching_record_ids({ tablename
=> 'subscription' });
233 is_deeply
( $matching_record_ids, [], "get_matching_record_ids: return [] if no field given" );
238 value
=> qq|value_for_af1_
$subscriptionid1|
241 $matching_record_ids = Koha
::AdditionalField
->get_matching_record_ids({ tablename
=> 'subscription', fields
=> $fields });
242 is_deeply
( $matching_record_ids, [ $subscriptionid1 ], "get_matching_record_ids: field $af1_name: value_for_af1_$subscriptionid1 matches subscription1" );
247 value
=> qq|value_for_af1_
$subscriptionid1|
251 value
=> qq|value_for_af2_
$subscriptionid1|,
254 $matching_record_ids = Koha
::AdditionalField
->get_matching_record_ids({ tablename
=> 'subscription', fields
=> $fields });
255 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" );
260 value
=> q
|common_value
|,
263 $matching_record_ids = Koha
::AdditionalField
->get_matching_record_ids({ tablename
=> 'subscription', fields
=> $fields });
264 my $exists = grep /$subscriptionid1/, @
$matching_record_ids;
265 is
( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription1" );
266 $exists = grep /$subscriptionid2/, @
$matching_record_ids;
267 is
( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription2 too" );
268 $exists = grep /not_existent_id/, @
$matching_record_ids;
269 is
( $exists, 0, "get_matching_record_ids: field common: common_value does not inexistent id" );