Bug 18374: (QA follow-up) Tidy up code
[koha.git] / t / db_dependent / AdditionalField.t
blob0a56ce0f4313e3cb76754bfcbf0be25a514fcaca
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 40;
6 use C4::Context;
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',
22 name => $af1_name,
23 authorised_values_category => '',
24 marcfield => '',
25 searchable => 1,
26 });
27 is ( $af1->name, $af1_name, "new: name value is kept" );
29 $af1->insert;
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,
39 name => $af2_name,
40 authorised_value_category => $af2_avc,
41 marcfield => $af2_marcfield,
42 searchable => $af2_searchable,
43 });
44 $af2->insert;
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',
57 name => q|af3|,
58 authorised_value_category => '',
59 marcfield => '',
60 searchable => 1,
61 });
62 $af3->insert;
64 my $af_common = Koha::AdditionalField->new({
65 tablename => 'subscription',
66 name => q|common|,
67 authorised_value_category => '',
68 marcfield => '',
69 searchable => 1,
70 });
71 $af_common->insert;
73 # update
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" );
88 # fetch all
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" );
95 $af3->delete;
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
103 use C4::Acquisition;
104 use C4::Biblio;
105 use C4::Budgets;
106 use C4::Serials;
107 use C4::Serials::Frequency;
108 use C4::Serials::Numberpattern;
110 my ($biblionumber, $biblioitemnumber) = AddBiblio(MARC::Record->new, '');
111 my $budgetid;
112 my $bpid = AddBudgetPeriod({
113 budget_period_startdate => '01-01-2015',
114 budget_period_enddate => '01-01-2016',
117 my $budget_id = AddBudget({
118 budget_code => "ABCD",
119 budget_amount => "123.132",
120 budget_name => "Périodiques",
121 budget_notes => "This is a note",
122 budget_period_id => $bpid
125 my $frequency_id = AddSubscriptionFrequency({ description => "Test frequency 1" });
126 my $pattern_id = AddSubscriptionNumberpattern({
127 label => 'Test numberpattern 1',
128 numberingmethod => '{X}'
131 my $subscriptionid1 = NewSubscription(
132 undef, "", undef, undef, $budget_id, $biblionumber,
133 '2013-01-01', $frequency_id, undef, undef, undef,
134 undef, undef, undef, undef, undef, undef,
135 1, "notes",undef, '2013-01-01', undef, $pattern_id,
136 undef, undef, 0, "intnotes", 0,
137 undef, undef, 0, undef, '2013-01-01', 0
140 my $subscriptionid2 = NewSubscription(
141 undef, "", undef, undef, $budget_id, $biblionumber,
142 '2013-01-01', $frequency_id, undef, undef, undef,
143 undef, undef, undef, undef, undef, undef,
144 1, "notes",undef, '2013-01-01', undef, $pattern_id,
145 undef, undef, 0, "intnotes", 0,
146 undef, undef, 0, undef, '2013-01-01', 0
149 # insert
150 my $af1_values = {
151 $subscriptionid1 => "value_for_af1_$subscriptionid1",
152 $subscriptionid2 => "value_for_af1_$subscriptionid2",
154 $af1->{values} = $af1_values;
155 $af1->insert_values;
157 my $af2_values = {
158 $subscriptionid1 => "old_value_for_af2_$subscriptionid1",
159 $subscriptionid2 => "old_value_for_af2_$subscriptionid2",
161 $af2->{values} = $af2_values;
162 $af2->insert_values;
163 my $new_af2_values = {
164 $subscriptionid1 => "value_for_af2_$subscriptionid1",
165 $subscriptionid2 => "value_for_af2_$subscriptionid2",
167 $af2->{values} = $new_af2_values;
168 $af2->insert_values; # Insert should replace old values
170 my $common_values = {
171 $subscriptionid1 => 'common_value',
172 $subscriptionid2 => 'common_value',
175 $af_common->{values} = $common_values;
176 $af_common->insert_values;
178 # fetch_values
179 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
180 $af2 = Koha::AdditionalField->new({ id => $af2->id })->fetch;
182 $af1->fetch_values;
183 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_$subscriptionid2| }, "fetch_values: without argument, returns 2 records" );
184 $af1->fetch_values({ record_id => $subscriptionid1 });
185 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|}, "fetch_values: values for af1 and subscription1" );
186 $af2->fetch_values({ record_id => $subscriptionid2 });
187 is_deeply ( $af2->values, {$subscriptionid2 => qq|value_for_af2_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
189 # fetch_all_values
190 eval{
191 $af1->fetch_all_values;
193 like ( $@, qr|^BAD CALL|, 'fetch_all_values: fail if called with a blessed object' );
195 my $fetched_values = Koha::AdditionalField->fetch_all_values({ tablename => 'subscription' });
196 my $expected_values = {
197 $subscriptionid1 => {
198 $af1_name => qq|value_for_af1_$subscriptionid1|,
199 $af2_name => qq|value_for_af2_$subscriptionid1|,
200 'common' => q|common_value|,
202 $subscriptionid2 => {
203 $af1_name => qq|value_for_af1_$subscriptionid2|,
204 $af2_name => qq|value_for_af2_$subscriptionid2|,
205 'common' => q|common_value|,
208 is_deeply ( $fetched_values, $expected_values, "fetch_all_values: values for table subscription" );
210 my $expected_values_1 = {
211 $subscriptionid1 => {
212 $af1_name => qq|value_for_af1_$subscriptionid1|,
213 $af2_name => qq|value_for_af2_$subscriptionid1|,
214 common => q|common_value|,
217 my $fetched_values_1 = Koha::AdditionalField->fetch_all_values({ tablename => 'subscription', record_id => $subscriptionid1 });
218 is_deeply ( $fetched_values_1, $expected_values_1, "fetch_all_values: values for subscription1" );
220 # get_matching_record_ids
221 eval{
222 $af1->get_matching_record_ids;
224 like ( $@, qr|^BAD CALL|, 'get_matching_record_ids: fail if called with a blessed object' );
226 my $matching_record_ids = Koha::AdditionalField->get_matching_record_ids;
227 is_deeply ( $matching_record_ids, [], "get_matching_record_ids: return [] if no argument given" );
228 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription' });
229 is_deeply ( $matching_record_ids, [], "get_matching_record_ids: return [] if no field given" );
231 my $fields = [
233 name => $af1_name,
234 value => qq|value_for_af1_$subscriptionid1|
237 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
238 is_deeply ( $matching_record_ids, [ $subscriptionid1 ], "get_matching_record_ids: field $af1_name: value_for_af1_$subscriptionid1 matches subscription1" );
240 $fields = [
242 name => $af1_name,
243 value => qq|value_for_af1_$subscriptionid1|
246 name => $af2_name,
247 value => qq|value_for_af2_$subscriptionid1|,
250 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
251 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" );
253 $fields = [
255 name => 'common',
256 value => q|common_value|,
259 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields });
260 my $exists = grep /$subscriptionid1/, @$matching_record_ids;
261 is ( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription1" );
262 $exists = grep /$subscriptionid2/, @$matching_record_ids;
263 is ( $exists, 1, "get_matching_record_ids: field common: common_value matches subscription2 too" );
264 $exists = grep /not_existent_id/, @$matching_record_ids;
265 is ( $exists, 0, "get_matching_record_ids: field common: common_value does not inexistent id" );
267 $fields = [
269 name => 'common',
270 value => q|common|,
273 $matching_record_ids = Koha::AdditionalField->get_matching_record_ids({ tablename => 'subscription', fields => $fields, exact_match => 0 });
274 $exists = grep /$subscriptionid1/, @$matching_record_ids;
275 is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription1" );
276 $exists = grep /$subscriptionid2/, @$matching_record_ids;
277 is ( $exists, 1, "get_matching_record_ids: field common: common% matches subscription2 too" );
278 $exists = grep /not_existent_id/, @$matching_record_ids;
279 is ( $exists, 0, "get_matching_record_ids: field common: common% does not inexistent id" );
281 # delete_values
282 $af1 = Koha::AdditionalField->new({ id => $af1->id })->fetch;
284 $af1->fetch_values;
285 is_deeply ( $af1->values, {$subscriptionid1 => qq|value_for_af1_$subscriptionid1|, $subscriptionid2 => qq|value_for_af1_$subscriptionid2| }, "fetch_values: without argument, returns 2 records" );
286 $af1->delete_values({record_id => $subscriptionid1});
287 $af1->fetch_values;
288 is_deeply ( $af1->values, {$subscriptionid2 => qq|value_for_af1_$subscriptionid2|}, "fetch_values: values for af2 and subscription2" );
289 $af1->delete_values;
290 $af1->fetch_values;
291 is_deeply ( $af1->values, {}, "fetch_values: no values" );
294 $dbh->rollback;