import rt 3.8.7
[freeside.git] / rt / t / api / cf.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings FATAL => 'all';
5
6 use RT::Test tests => 139;
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 $cfvs = $ticket->CustomFieldValues( -1 );
73 is( $cfvs->Count, 0 );
74 is( $ticket->FirstCustomFieldValue( -1 ), undef );
75
76 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
77 is( $cfvs->Count, 0 );
78 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
79
80 for (@custom_fields) {
81         $cfvs = $ticket->CustomFieldValues( $_->id );
82         is( $cfvs->Count, 0 );
83
84         $cfvs = $ticket->CustomFieldValues( $_->Name );
85         is( $cfvs->Count, 0 );
86         is( $ticket->FirstCustomFieldValue( $_->id ), undef );
87         is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
88 }
89 # }}}
90
91 # try to add field value with fields that do not exist {{{
92 my ($status, $msg) = $ticket->AddCustomFieldValue( Field => -1 , Value => 'foo' );
93 ok(!$status, "shouldn't add value" );
94 ($status, $msg) = $ticket->AddCustomFieldValue( Field => 'SomeUnexpedCustomFieldName' , Value => 'foo' );
95 ok(!$status, "shouldn't add value" );
96 # }}}
97
98 # {{{
99 SKIP: {
100
101         skip "TODO: We want fields that are not allowed to set unexpected values", 10;
102         for (@custom_fields) {
103                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'SomeUnexpectedCFValue' );
104                 ok( !$status, 'value doesn\'t exist');
105         
106                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
107                 ok( !$status, 'value doesn\'t exist');
108         
109                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
110                 ok( !$status, 'value doesn\'t exist');
111         }
112         
113         # Let check that we did not add value to be sure
114         # using only FirstCustomFieldValue sub because
115         # we checked other variants allready
116         for (@custom_fields) {
117                 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
118         }
119         
120 }
121 # Add some values to our custom fields
122 for (@custom_fields) {
123         # this should be tested elsewhere
124         $_->AddValue( Name => 'Foo' );
125         $_->AddValue( Name => 'Bar' );
126 }
127
128 my $test_add_delete_cycle = sub {
129         my $cb = shift;
130         for (@custom_fields) {
131                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
132                 ok( $status, "message: $msg");
133         }
134         
135         # does it exist?
136         $cfvs = $ticket->CustomFieldValues;
137         is( $cfvs->Count, 3, "We found all three custom fields on our ticket" );
138         for (@custom_fields) {
139                 $cfvs = $ticket->CustomFieldValues( $_->id );
140                 is( $cfvs->Count, 1 , "we found one custom field when searching by id");
141         
142                 $cfvs = $ticket->CustomFieldValues( $_->Name );
143                 is( $cfvs->Count, 1 , " We found one custom field when searching by name for " . $_->Name);
144                 is( $ticket->FirstCustomFieldValue( $_->id ), 'Foo' , "first value by id is foo");
145                 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Foo' , "first value by name is foo");
146         }
147         # because our CFs are SingleValue then new value addition should override
148         for (@custom_fields) {
149                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'Bar' );
150                 ok( $status, "message: $msg");
151         }
152         $cfvs = $ticket->CustomFieldValues;
153         is( $cfvs->Count, 3 );
154         for (@custom_fields) {
155                 $cfvs = $ticket->CustomFieldValues( $_->id );
156                 is( $cfvs->Count, 1 );
157         
158                 $cfvs = $ticket->CustomFieldValues( $_->Name );
159                 is( $cfvs->Count, 1 );
160                 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
161                 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
162         }
163         # delete it
164         for (@custom_fields ) { 
165                 ($status, $msg) = $ticket->DeleteCustomFieldValue( Field => $_ , Value => 'Bar' );
166                 ok( $status, "Deleted a custom field value 'Bar' for field ".$_->id.": $msg");
167         }
168         $cfvs = $ticket->CustomFieldValues;
169         is( $cfvs->Count, 0, "The ticket (".$ticket->id.") no longer has any custom field values"  );
170         for (@custom_fields) {
171                 $cfvs = $ticket->CustomFieldValues( $_->id );
172                 is( $cfvs->Count, 0,  $ticket->id." has no values for cf  ".$_->id );
173         
174                 $cfvs = $ticket->CustomFieldValues( $_->Name );
175                 is( $cfvs->Count, 0 , $ticket->id." has no values for cf  '".$_->Name. "'" );
176                 is( $ticket->FirstCustomFieldValue( $_->id ), undef , "There is no first custom field value when loading by id" );
177                 is( $ticket->FirstCustomFieldValue( $_->Name ), undef, "There is no first custom field value when loading by Name" );
178         }
179 };
180
181 # lets test cycle via CF id
182 $test_add_delete_cycle->( sub { return $_[0]->id } );
183 # lets test cycle via CF object reference
184 $test_add_delete_cycle->( sub { return $_[0] } );
185
186 $ticket->AddCustomFieldValue( Field => $local_cf2->id , Value => 'Baz' );
187 $ticket->AddCustomFieldValue( Field => $global_cf3->id , Value => 'Baz' );
188 # now if we ask for cf values on RecordCustomFields4 we should not get any
189 $cfvs = $ticket->CustomFieldValues( 'RecordCustomFields4' );
190 is( $cfvs->Count, 0, "No custom field values for non-Queue cf" );
191 is( $ticket->FirstCustomFieldValue( 'RecordCustomFields4' ), undef, "No first custom field value for non-Queue cf" );
192
193 {
194     my $cfname = $global_cf3->Name;
195     ($status, $msg) = $global_cf3->SetDisabled(1);
196     ok($status, "Disabled CF named $cfname");
197
198     my $load = RT::CustomField->new( $RT::SystemUser );
199     $load->LoadByName( Name => $cfname);
200     ok($load->Id, "Loaded CF named $cfname");
201     is($load->Id, $global_cf3->Id, "Can load disabled CFs");
202
203     my $dup = RT::CustomField->new( $RT::SystemUser );
204     $dup->Create( Name => $cfname, Type => 'SelectSingle', Queue => 0 );
205     ok($dup->Id, "Created CF with duplicate name");
206
207     $load->LoadByName( Name => $cfname);
208     is($load->Id, $dup->Id, "Loading by name gets non-disabled first");
209
210     $dup->SetDisabled(1);
211     $global_cf3->SetDisabled(0);
212
213     $load->LoadByName( Name => $cfname);
214     is($load->Id, $global_cf3->Id, "Loading by name gets non-disabled first, even with order swapped");
215 }
216
217 #SKIP: {
218 #       skip "TODO: should we add CF values to objects via CF Name?", 48;
219 # names are not unique
220         # lets test cycle via CF Name
221 #       $test_add_delete_cycle->( sub { return $_[0]->Name } );
222 #}
223
224