rt 4.2.15
[freeside.git] / rt / t / customfields / api.t
1
2 use strict;
3 use warnings FATAL => 'all';
4
5 use RT::Test nodata => 1, tests => undef;
6 use Test::Warn;
7
8 # Before we get going, ditch all object_cfs; this will remove
9 # all custom fields systemwide;
10 my $object_cfs = RT::ObjectCustomFields->new(RT->SystemUser);
11 $object_cfs->UnLimit();
12 while (my $ocf = $object_cfs->Next) {
13     $ocf->Delete();
14 }
15
16
17 my $queue = RT::Queue->new( RT->SystemUser );
18 $queue->Create( Name => 'RecordCustomFields-'.$$ );
19 ok ($queue->id, "Created the queue");
20
21 my $queue2 = RT::Queue->new( RT->SystemUser );
22 $queue2->Create( Name => 'RecordCustomFields2' );
23
24 my $ticket = RT::Ticket->new( RT->SystemUser );
25 $ticket->Create(
26     Queue => $queue->Id,
27     Requestor => 'root@localhost',
28     Subject => 'RecordCustomFields1',
29 );
30
31 my $cfs = $ticket->CustomFields;
32 is( $cfs->Count, 0 );
33
34 # Check that record has no any CF values yet {{{
35 my $cfvs = $ticket->CustomFieldValues;
36 is( $cfvs->Count, 0 );
37 is( $ticket->FirstCustomFieldValue, undef );
38
39 my $local_cf1 = RT::CustomField->new( RT->SystemUser );
40 $local_cf1->Create( Name => 'RecordCustomFields1-'.$$, Type => 'SelectSingle', Queue => $queue->id );
41 $local_cf1->AddValue( Name => 'RecordCustomFieldValues11' );
42 $local_cf1->AddValue( Name => 'RecordCustomFieldValues12' );
43
44 my $local_cf2 = RT::CustomField->new( RT->SystemUser );
45 $local_cf2->Create( Name => 'RecordCustomFields2-'.$$, Type => 'SelectSingle', Queue => $queue->id );
46 $local_cf2->AddValue( Name => 'RecordCustomFieldValues21' );
47 $local_cf2->AddValue( Name => 'RecordCustomFieldValues22' );
48
49 my $global_cf3 = RT::CustomField->new( RT->SystemUser );
50 $global_cf3->Create( Name => 'RecordCustomFields3-'.$$, Type => 'SelectSingle', Queue => 0 );
51 $global_cf3->AddValue( Name => 'RecordCustomFieldValues31' );
52 $global_cf3->AddValue( Name => 'RecordCustomFieldValues32' );
53
54 my $local_cf4 = RT::CustomField->new( RT->SystemUser );
55 $local_cf4->Create( Name => 'RecordCustomFields4', Type => 'SelectSingle', Queue => $queue2->id );
56 $local_cf4->AddValue( Name => 'RecordCustomFieldValues41' );
57 $local_cf4->AddValue( Name => 'RecordCustomFieldValues42' );
58
59
60 my @custom_fields = ($local_cf1, $local_cf2, $global_cf3);
61
62
63 $cfs = $ticket->CustomFields;
64 is( $cfs->Count, 3 );
65
66 # Check that record has no any CF values yet {{{
67 $cfvs = $ticket->CustomFieldValues;
68 is( $cfvs->Count, 0 );
69 is( $ticket->FirstCustomFieldValue, undef );
70
71 # CF with ID -1 shouldnt exist at all
72 warning_like {
73     $cfvs = $ticket->CustomFieldValues( -1 );
74 } qr{Couldn't load custom field};
75 is( $cfvs->Count, 0 );
76 warning_like {
77     is( $ticket->FirstCustomFieldValue( -1 ), undef );
78 } qr{Couldn't load custom field};
79
80 warning_like {
81     $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
82 } qr{Couldn't load custom field};
83 is( $cfvs->Count, 0 );
84 warning_like {
85     is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
86 } qr{Couldn't load custom field};
87
88 for (@custom_fields) {
89     $cfvs = $ticket->CustomFieldValues( $_->id );
90     is( $cfvs->Count, 0 );
91
92     $cfvs = $ticket->CustomFieldValues( $_->Name );
93     is( $cfvs->Count, 0 );
94     is( $ticket->FirstCustomFieldValue( $_->id ), undef );
95     is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
96 }
97
98 # try to add field value with fields that do not exist {{{
99 my ($status, $msg) = $ticket->AddCustomFieldValue( Field => -1 , Value => 'foo' );
100 ok(!$status, "shouldn't add value" );
101 ($status, $msg) = $ticket->AddCustomFieldValue( Field => 'SomeUnexpedCustomFieldName' , Value => 'foo' );
102 ok(!$status, "shouldn't add value" );
103
104 SKIP: {
105
106     skip "TODO: We want fields that are not allowed to set unexpected values", 10;
107     for (@custom_fields) {
108         ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'SomeUnexpectedCFValue' );
109         ok( !$status, 'value doesn\'t exist');
110
111         ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
112         ok( !$status, 'value doesn\'t exist');
113
114         ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
115         ok( !$status, 'value doesn\'t exist');
116     }
117
118     # Let check that we did not add value to be sure
119     # using only FirstCustomFieldValue sub because
120     # we checked other variants allready
121     for (@custom_fields) {
122         is( $ticket->FirstCustomFieldValue( $_->id ), undef );
123     }
124
125 }
126 # Add some values to our custom fields
127 for (@custom_fields) {
128     # this should be tested elsewhere
129     $_->AddValue( Name => 'Foo' );
130     $_->AddValue( Name => 'Bar' );
131 }
132
133 my $test_add_delete_cycle = sub {
134     my $cb = shift;
135     for (@custom_fields) {
136         ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
137         ok( $status, "message: $msg");
138     }
139
140     # does it exist?
141     $cfvs = $ticket->CustomFieldValues;
142     is( $cfvs->Count, 3, "We found all three custom fields on our ticket" );
143     for (@custom_fields) {
144         $cfvs = $ticket->CustomFieldValues( $_->id );
145         is( $cfvs->Count, 1 , "we found one custom field when searching by id");
146
147         $cfvs = $ticket->CustomFieldValues( $_->Name );
148         is( $cfvs->Count, 1 , " We found one custom field when searching by name for " . $_->Name);
149         is( $ticket->FirstCustomFieldValue( $_->id ), 'Foo' , "first value by id is foo");
150         is( $ticket->FirstCustomFieldValue( $_->Name ), 'Foo' , "first value by name is foo");
151     }
152     # because our CFs are SingleValue then new value addition should override
153     for (@custom_fields) {
154         ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'Bar' );
155         ok( $status, "message: $msg");
156     }
157     $cfvs = $ticket->CustomFieldValues;
158     is( $cfvs->Count, 3 );
159     for (@custom_fields) {
160         $cfvs = $ticket->CustomFieldValues( $_->id );
161         is( $cfvs->Count, 1 );
162
163         $cfvs = $ticket->CustomFieldValues( $_->Name );
164         is( $cfvs->Count, 1 );
165         is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
166         is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
167     }
168     # delete it
169     for (@custom_fields ) {
170         ($status, $msg) = $ticket->DeleteCustomFieldValue( Field => $_ , Value => 'Bar' );
171         ok( $status, "Deleted a custom field value 'Bar' for field ".$_->id.": $msg");
172     }
173     $cfvs = $ticket->CustomFieldValues;
174     is( $cfvs->Count, 0, "The ticket (".$ticket->id.") no longer has any custom field values"  );
175     for (@custom_fields) {
176         $cfvs = $ticket->CustomFieldValues( $_->id );
177         is( $cfvs->Count, 0,  $ticket->id." has no values for cf  ".$_->id );
178
179         $cfvs = $ticket->CustomFieldValues( $_->Name );
180         is( $cfvs->Count, 0 , $ticket->id." has no values for cf  '".$_->Name. "'" );
181         is( $ticket->FirstCustomFieldValue( $_->id ), undef , "There is no first custom field value when loading by id" );
182         is( $ticket->FirstCustomFieldValue( $_->Name ), undef, "There is no first custom field value when loading by Name" );
183     }
184 };
185
186 # lets test cycle via CF id
187 $test_add_delete_cycle->( sub { return $_[0]->id } );
188 # lets test cycle via CF object reference
189 $test_add_delete_cycle->( sub { return $_[0] } );
190
191 $ticket->AddCustomFieldValue( Field => $local_cf2->id , Value => 'Baz' );
192 $ticket->AddCustomFieldValue( Field => $global_cf3->id , Value => 'Baz' );
193 # now if we ask for cf values on RecordCustomFields4 we should not get any
194 warning_like {
195     $cfvs = $ticket->CustomFieldValues( 'RecordCustomFields4' );
196 } qr{Couldn't load custom field};
197 is( $cfvs->Count, 0, "No custom field values for non-Queue cf" );
198 warning_like {
199     is( $ticket->FirstCustomFieldValue( 'RecordCustomFields4' ), undef, "No first custom field value for non-Queue cf" );
200 } qr{Couldn't load custom field};
201
202 {
203     my $cfname = $global_cf3->Name;
204     ($status, $msg) = $global_cf3->SetDisabled(1);
205     ok($status, "Disabled CF named $cfname");
206
207     my $load = RT::CustomField->new( RT->SystemUser );
208     $load->LoadByName( Name => $cfname);
209     ok($load->Id, "Loaded CF named $cfname");
210     is($load->Id, $global_cf3->Id, "Can load disabled CFs");
211
212     my $dup = RT::CustomField->new( RT->SystemUser );
213     $dup->Create( Name => $cfname, Type => 'SelectSingle', Queue => 0 );
214     ok($dup->Id, "Created CF with duplicate name");
215
216     $load->LoadByName( Name => $cfname);
217     is($load->Id, $dup->Id, "Loading by name gets non-disabled first");
218
219     $dup->SetDisabled(1);
220     $global_cf3->SetDisabled(0);
221
222     $load->LoadByName( Name => $cfname);
223     is($load->Id, $global_cf3->Id, "Loading by name gets non-disabled first, even with order swapped");
224 }
225
226 {
227     my $cf = RT::Test->load_or_create_custom_field(
228         Name  => 'HasEntry cache',
229         Type  => 'FreeformSingle',
230         Queue => 0,
231     );
232
233     my ( $ret, $msg ) = $ticket->AddCustomFieldValue( Field => $cf, Value => 'foo' );
234     ok( $ret, $msg );
235     is( $ticket->FirstCustomFieldValue( $cf ), 'foo', 'value is foo' );
236     my $ocfvs = $ticket->CustomFieldValues( $cf );
237     ok( $ocfvs->HasEntry( 'foo' ), 'foo is cached in HasEntry' );
238
239     ( $ret, $msg ) = $ticket->AddCustomFieldValue( Field => $cf, Value => 'bar' );
240     ok( $ret, $msg );
241     is( $ticket->FirstCustomFieldValue( $cf ), 'bar', 'value is bar' );
242     ok( !$ocfvs->HasEntry( 'foo' ), 'foo is not cached in HasEntry' );
243     ok( $ocfvs->HasEntry( 'bar' ),  'bar is cached in HasEntry' );
244
245     ( $ret, $msg ) = $ticket->AddCustomFieldValue( Field => $cf, Value => 'foo' );
246     ok( $ret, $msg );
247     is( $ticket->FirstCustomFieldValue( $cf ), 'foo', 'value is foo' );
248     ok( $ocfvs->HasEntry( 'foo' ),  'foo is cached in HasEntry' );
249     ok( !$ocfvs->HasEntry( 'bar' ), 'bar is not cached in HasEntry' );
250 }
251
252 #SKIP: {
253 #       skip "TODO: should we add CF values to objects via CF Name?", 48;
254 # names are not unique
255         # lets test cycle via CF Name
256 #       $test_add_delete_cycle->( sub { return $_[0]->Name } );
257 #}
258
259 done_testing;